171772025-05-28 11:33:00AblablablaVasútépítéscpp17Runtime error 40/10098ms7740 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 szam = 0;

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

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

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){
        assert(0 == 1);
        return 0;
    }
    for(auto x : csucsok){
        if(x.size() == 0){
            assert(0 == 1);
            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 || x.size() < akt){
            assert(0 == 1);
            return 0;
        }
    }

    /*cout << "egyutt:\n";
    for(auto x : egyutt){
        for(int y : x){
            cout << y  << " ";
        }
        cout << "\n";
    }
    cout << "\n";*/

    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] = szam;
                szam++;
            }
        }
    }

    assert(szam <= 1e9);

    for(int i = 0; i + 1 < n; i++){
        for(int j = i + 1; j < n; j++){
            cout << ans[i][j] << " ";
        }
        cout << "\n";
    }

    /*for(auto x : ans){
        for(int y : x){
            cout << y << "\t";
        }

        cout << "\n";
    }*/
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms500 KiB
2Runtime error1ms316 KiB
3Accepted2ms316 KiB
subtask240/40
4Accepted65ms7732 KiB
5Accepted65ms7740 KiB
6Accepted68ms7732 KiB
7Accepted71ms7728 KiB
8Accepted65ms7732 KiB
9Accepted65ms7728 KiB
10Accepted68ms7716 KiB
11Accepted1ms508 KiB
subtask30/60
12Accepted65ms7732 KiB
13Accepted65ms7740 KiB
14Accepted68ms7732 KiB
15Accepted71ms7728 KiB
16Accepted65ms7732 KiB
17Accepted65ms7728 KiB
18Accepted68ms7716 KiB
19Accepted1ms508 KiB
20Wrong answer68ms7732 KiB
21Wrong answer68ms7732 KiB
22Wrong answer65ms7740 KiB
23Wrong answer67ms7732 KiB
24Wrong answer67ms7732 KiB
25Wrong answer68ms7732 KiB
26Wrong answer65ms7740 KiB
27Accepted68ms7732 KiB
28Runtime error98ms6708 KiB
29Runtime error4ms4672 KiB
30Runtime error4ms4404 KiB
31Runtime error4ms4404 KiB