168822025-05-15 12:56:20AblablablaVasútépítéscpp17Wrong answer 40/100103ms9392 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 = 1, fent = 1e9;

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

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

        ans[akt][x] = lent;
        ans[x][akt] = lent;
        lent++;
        megold(x);
    }
}

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


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

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

    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();

    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();

                ans[kor[i][elozo]][kor[i][j]] = lent;
                ans[kor[i][j]][kor[i][elozo]] = lent;
                lent++;
                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++){
            cout << ans[i][j] << " ";
        }
        cout << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
2Accepted1ms316 KiB
3Accepted2ms316 KiB
subtask240/40
4Accepted74ms9324 KiB
5Accepted74ms9344 KiB
6Accepted76ms9288 KiB
7Accepted75ms9268 KiB
8Accepted76ms9392 KiB
9Accepted76ms9248 KiB
10Accepted74ms9240 KiB
11Accepted1ms316 KiB
subtask30/60
12Accepted74ms9324 KiB
13Accepted74ms9344 KiB
14Accepted76ms9288 KiB
15Accepted75ms9268 KiB
16Accepted76ms9392 KiB
17Accepted76ms9248 KiB
18Accepted74ms9240 KiB
19Accepted1ms316 KiB
20Wrong answer87ms9368 KiB
21Wrong answer75ms9248 KiB
22Wrong answer75ms9268 KiB
23Wrong answer76ms9252 KiB
24Wrong answer72ms9272 KiB
25Wrong answer75ms9252 KiB
26Wrong answer72ms9264 KiB
27Accepted74ms9268 KiB
28Accepted103ms7988 KiB
29Accepted8ms4384 KiB
30Accepted7ms4404 KiB
31Wrong answer75ms9204 KiB