168852025-05-15 13:29:44AblablablaVasútépítéscpp17Wrong answer 40/100104ms9396 KiB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

vector<vector<int>> csucsok, ans, egyutt, kor;
vector<bool> bejart;

int keres(int akt, int elozo){
    bejart[akt] = 1;
    egyutt.back().push_back(akt);
    int vissza = -1;

    for(int x : csucsok[akt]){
        if(x == elozo) continue;
        if(bejart[x]){
            vissza = x;
            continue;
        }

        int a = keres(x, akt);

        if(a != -1){
            vissza = a;
        }
    }

    if(vissza != -1){
        kor.back().push_back(akt);
    }

    if(vissza == akt){
        vissza = -1;
    }

    return vissza;
}

int lent = 0, fent = 1e9;

void berak(int i, int j){
    assert(ans[i][j] == -1 && ans[j][i] == -1);
    assert(i != j);

    ans[i][j] = lent;
    ans[j][i] = lent;
    lent++;
}

void megold(int akt){
    bejart[akt] = 1;

    for(int x : csucsok[akt]){
        if(bejart[x]) continue;

        berak(akt, x);
        megold(x);
    }
}

int main()
{
    int n, m;
    cin >> n >> m;


    csucsok.assign(n, vector<int>());
    ans.assign(n, vector<int>(n, -1));

    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        a--; b--;

        csucsok[a].push_back(b);
        csucsok[b].push_back(a);
    }

    if(n < m){
        cout << "-1\n";
        return 0;
    }
    for(auto x : csucsok){
        if(x.size() == 0){
            cout << "-1\n";
            return 0;
        }
    }

    bejart.assign(n, 0);

    for(int i = 0; i < n; i++){
        if(!bejart[i]){
            egyutt.push_back(vector<int>());
            kor.push_back(vector<int>());

            keres(i, -1);
        }
    }

    int k = egyutt.size();

    for(auto x : egyutt){
        int akt = 0;
        for(int y : x){
            akt += csucsok[y].size();
        }

        akt /= 2;

        if(akt != x.size() - 1 && akt != x.size()){
            cout << "-1\n";
            return 0;
        }
    }

    bejart.assign(n, 0);

    for(int i = 0; i < k; i++){
        if(kor[i].size() == 0){
            // fa

            megold(egyutt[i][0]);
        } else{
            // fak vezetnek a korbe

            // korben eloszor

            for(int j = 0; j < kor[i].size(); j++){
                int elozo = (j - 1 + kor[i].size()) % kor[i].size();

                berak(kor[i][j], kor[i][elozo]);
                bejart[kor[i][j]] = 1;
            }

            // aztan fakban

            for(int x : kor[i]){
                megold(x);
            }
        }
    }

    for(int i = 0; i + 1 < n; i++){
        for(int j = i + 1; j < n; j++){
            if(ans[i][j] == -1){
                ans[i][j] = fent;
                fent--;
            }
        }
    }

    for(int i = 0; i + 1 < n; i++){
        for(int j = i + 1; j < n; j++){
            cout << ans[i][j] << " ";
        }
        cout << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
2Accepted1ms316 KiB
3Accepted2ms316 KiB
subtask240/40
4Accepted72ms9396 KiB
5Accepted74ms9376 KiB
6Accepted72ms9236 KiB
7Accepted74ms9372 KiB
8Accepted72ms9268 KiB
9Accepted71ms9268 KiB
10Accepted74ms9268 KiB
11Accepted1ms316 KiB
subtask30/60
12Accepted72ms9396 KiB
13Accepted74ms9376 KiB
14Accepted72ms9236 KiB
15Accepted74ms9372 KiB
16Accepted72ms9268 KiB
17Accepted71ms9268 KiB
18Accepted74ms9268 KiB
19Accepted1ms316 KiB
20Wrong answer74ms9268 KiB
21Wrong answer72ms9276 KiB
22Wrong answer72ms9280 KiB
23Wrong answer75ms9272 KiB
24Wrong answer72ms9268 KiB
25Wrong answer78ms9264 KiB
26Wrong answer72ms9236 KiB
27Accepted74ms9268 KiB
28Accepted104ms6452 KiB
29Accepted4ms4416 KiB
30Accepted4ms4404 KiB
31Accepted4ms4404 KiB