107112024-04-10 09:45:50szilVasútépítéscpp17Accepted 100/10052ms22656 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAXN = 1001;
const int MAXM = 200'001;
const int INF = 1e8;

vector<pair<int, int>> g[MAXN];
vector<int> g2[MAXN];
int vis[MAXN], vis2[MAXM], vis3[MAXN];
int nodes, edges, fok[MAXN], ans[MAXN][MAXN], timer = 2, timer2 = INF, st;

void dfs(int u, int p = -1) {
    vis[u] = true;
    nodes++;
    for (auto [v, idx] : g[u]) {
        if (v == p) continue;
        if (vis2[idx] == 0) {
            vis2[idx] = true;
            edges++;
        }
        if (!vis[v]) {
            vis2[idx] = 3;
            dfs(v, u);
        } else {
            st = u;
            ans[u][v] = ans[v][u] = 1;
        }
    }
}

vector<int> cyc;
vector<int> res;

void dfs2(int u, int p = -1) {
    vis3[u] = true;
    cyc.push_back(u);
    for (auto [v, idx] : g[u]) {
        if (v == p) continue;
        if (v == st) {
            res = cyc;
        }
        if (!res.empty()) return;
        if (!vis3[v]) {
            dfs2(v, u);
        }
        if (!res.empty()) return;
    }
    cyc.pop_back();
}

void dfs3(int u)  {
    vis3[u] = true;
    for (auto [v, idx] : g[u]) {
        if (!vis3[v]) {
            ans[u][v] = ans[v][u] = timer++;
        }
    }
    for (auto [v, idx] : g[u]) {
        if (!vis3[v]) {
            dfs3(v);
        }
    }
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, m; cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            ans[i][j] = timer2--;
        }
    }
    for (int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;
        g[u].emplace_back(v, i);
        g[v].emplace_back(u, i);
    }
    for (int i = 1; i <= n; i++) {
        if (g[i].empty()) {
            cout << "-1\n";
            return 0;
        }
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            nodes = edges = 0;
            st = i;
            dfs(i);
            if (edges > nodes) {
                cout << "-1\n";
                return 0;
            }
            fill(vis3, vis3+MAXN, false);
            cyc.clear(); res.clear();
            dfs2(st);
            if (edges == nodes && res.empty()) {
                throw 1;
            }
            if (edges == nodes - 1 && !res.empty()) throw 1;
            if (res.empty()) {
                fill(vis3, vis3+MAXN, false);
                dfs3(st);
            } else {
                for (int i = 1; i < res.size(); i++) {
                    ans[res[i-1]][res[i]] = ans[res[i]][res[i-1]] = timer++;
                }
                ans[res[0]][res.back()] = ans[res.back()][res[0]] = timer++;
                fill(vis3, vis3+MAXN, false);
                for (int u : res) vis3[u] = true;
                for (int u : res) {
                    dfs3(u);
                }
            }
        }
    }
    
    for (int i = 1; i <= n; i++) {
        for (int j = i+1; j <= n; j++) {
            cout << ans[i][j] << " ";
        }
        cout << "\n";
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2044 KiB
2Accepted3ms2296 KiB
3Accepted3ms3140 KiB
subtask240/40
4Accepted50ms10300 KiB
5Accepted50ms10296 KiB
6Accepted52ms10644 KiB
7Accepted52ms11132 KiB
8Accepted50ms11224 KiB
9Accepted52ms11048 KiB
10Accepted52ms11244 KiB
11Accepted3ms3124 KiB
subtask360/60
12Accepted50ms10300 KiB
13Accepted50ms10296 KiB
14Accepted52ms10644 KiB
15Accepted52ms11132 KiB
16Accepted50ms11224 KiB
17Accepted52ms11048 KiB
18Accepted52ms11244 KiB
19Accepted3ms3124 KiB
20Accepted50ms11532 KiB
21Accepted50ms11612 KiB
22Accepted50ms11592 KiB
23Accepted50ms11496 KiB
24Accepted50ms11508 KiB
25Accepted52ms11784 KiB
26Accepted50ms11796 KiB
27Accepted50ms12176 KiB
28Accepted52ms22656 KiB
29Accepted6ms12208 KiB
30Accepted7ms12296 KiB
31Accepted6ms12188 KiB