107112024-04-10 09:45:50szilVasútépítéscpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva3ms2044 KiB
2Elfogadva3ms2296 KiB
3Elfogadva3ms3140 KiB
subtask240/40
4Elfogadva50ms10300 KiB
5Elfogadva50ms10296 KiB
6Elfogadva52ms10644 KiB
7Elfogadva52ms11132 KiB
8Elfogadva50ms11224 KiB
9Elfogadva52ms11048 KiB
10Elfogadva52ms11244 KiB
11Elfogadva3ms3124 KiB
subtask360/60
12Elfogadva50ms10300 KiB
13Elfogadva50ms10296 KiB
14Elfogadva52ms10644 KiB
15Elfogadva52ms11132 KiB
16Elfogadva50ms11224 KiB
17Elfogadva52ms11048 KiB
18Elfogadva52ms11244 KiB
19Elfogadva3ms3124 KiB
20Elfogadva50ms11532 KiB
21Elfogadva50ms11612 KiB
22Elfogadva50ms11592 KiB
23Elfogadva50ms11496 KiB
24Elfogadva50ms11508 KiB
25Elfogadva52ms11784 KiB
26Elfogadva50ms11796 KiB
27Elfogadva50ms12176 KiB
28Elfogadva52ms22656 KiB
29Elfogadva6ms12208 KiB
30Elfogadva7ms12296 KiB
31Elfogadva6ms12188 KiB