108282024-04-16 08:25:23TomaSajtVasútépítéscpp17Accepted 100/10056ms28044 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

int big = 1e9;
int t = 1;

vector<vector<int>> p;
vector<vector<int>> g;
vector<vector<int>> comps;
vector<int> vis;
int vis_mark = 1;

void dfs1(int u) {
  vis[u] = vis_mark;
  comps.back().push_back(u);
  for (int v : g[u]) {
    if (vis[v] == vis_mark) continue;
    dfs1(v);
  }
}

stack<int> s;
bool find_cycle(int u, int par) {
  s.push(u);
  vis[u] = vis_mark;
  for (int v : g[u]) {
    if (v == par) continue;
    if (vis[v] == vis_mark) {
      s.push(v);
      return true;
    }
    if (find_cycle(v, u)) return true;
  }
  s.pop();
  return false;
}

void dfs2(int u) {
  vis[u] = vis_mark;
  for (int v : g[u]) {
    if (vis[v] == vis_mark) continue;
    p[u][v] = p[v][u] = t++;
    dfs2(v);
  }
}

signed main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n, m;
  cin >> n >> m;
  p.resize(n + 1, vector<int>(n + 1, -1));
  g.resize(n + 1);
  vis.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  for (int i = 1; i <= n; i++) {
    if (vis[i] == vis_mark) continue;
    comps.emplace_back();
    dfs1(i);
  }

  for (vector<int> comp : comps) {
    int edge_cnt = 0;
    for (int u : comp) {
      for (int v : g[u]) {
        if (u < v) edge_cnt++;
      }
    }

    if (comp.size() == 1 || edge_cnt > comp.size()) {
      cout << "-1";
      return 0;
    }
    if (edge_cnt == comp.size()) {
      s = stack<int>();
      vis_mark++;
      assert(find_cycle(comp[0], -1));
      vis_mark++;

      int start = s.top();
      s.pop();
      int curr = start;
      int cycle_mark = vis_mark;
      do {
        vis[curr] = vis_mark;
        p[curr][s.top()] = p[s.top()][curr] = t++;
        curr = s.top();
        s.pop();
      } while (curr != start);
      vis_mark++;
      for (int i : comp) {
        if (vis[i] == cycle_mark) dfs2(i);
      }
    }
    if (edge_cnt < comp.size()) {
      vis_mark++;
      dfs2(comp[0]);
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (p[i][j] == -1) {
        cout << big-- << ' ';
      }
      else {
        cout << p[i][j] << ' ';
      }
    }
    cout << '\n';
  }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2056 KiB
3Accepted3ms2364 KiB
subtask240/40
4Accepted54ms18272 KiB
5Accepted54ms18204 KiB
6Accepted54ms18328 KiB
7Accepted56ms18544 KiB
8Accepted54ms18640 KiB
9Accepted54ms18640 KiB
10Accepted54ms18796 KiB
11Accepted3ms2708 KiB
subtask360/60
12Accepted54ms18272 KiB
13Accepted54ms18204 KiB
14Accepted54ms18328 KiB
15Accepted56ms18544 KiB
16Accepted54ms18640 KiB
17Accepted54ms18640 KiB
18Accepted54ms18796 KiB
19Accepted3ms2708 KiB
20Accepted54ms18804 KiB
21Accepted54ms18700 KiB
22Accepted54ms18944 KiB
23Accepted54ms18912 KiB
24Accepted54ms19124 KiB
25Accepted56ms19228 KiB
26Accepted54ms19280 KiB
27Accepted56ms19276 KiB
28Accepted50ms28044 KiB
29Accepted8ms19148 KiB
30Accepted8ms19512 KiB
31Accepted8ms19364 KiB