109402024-04-20 17:26:07k_balintVasútépítéscpp17Wrong answer 40/10054ms12504 KiB
#include <bits/stdc++.h>
using namespace std;
const int c=1005;

int n,m,tt;
vector<int> adj[c];
int vis[c];
int cnt,deg;
vector<int> kor;
int ans[c][c];
int par[c];
bool korben[c];
int st,ed;

void dfs(int v){
    ++cnt;
    vis[v]=1;
    for(int x:adj[v]){
        if(v<x) ++deg;
        if(!vis[x]) dfs(x);
    }
}


void dfs2(int v, int p){
    par[v]=p;
    vis[v]=2;
    for(int x:adj[v]){
        if(x==p) continue;
        if(vis[x]==2){
            if(st==-1){
                st=x,ed=v;
            }
        }
        else dfs2(x,v);
    }
}

void dfs3(int v){
    vis[v]=1;
    for(int x:adj[v]){
        if(!korben[x] && !vis[x]){
            ans[v][x]=ans[x][v]=++tt;
            dfs3(x);
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int a,b; cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    for(int i=1;i<=n;i++){
        if(!vis[i]){
            deg=0; cnt=0;
            dfs(i);
            if(deg>cnt){
                cout << -1 << endl;
                return 0;
            }
            st=-1,ed=-1;
            dfs2(i,0);
            if(st==-1) continue;
            ans[st][ed]=ans[ed][st]=++tt;

            korben[st]=1;
            int cur=ed;
            while(cur != st){
                korben[cur]=1;
                ans[cur][par[cur]]=ans[par[cur]][cur]=++tt;
                cur=par[cur];
            }
        }
    }

    for(int i=1;i<=n;i++) vis[i]=0;

    for(int i=1;i<=n;i++){
        if(!vis[i] && korben[i]) dfs3(i);
    }

    for(int i=1;i<=n;i++){
        if(!vis[i]) dfs3(i);
    }

    for(int i=1;i<n;i++){
        for(int j=i+1;j<=n;j++){
            if(!ans[i][j]) ans[i][j]=++tt;
            cout << ans[i][j] << ' ';
        }
        cout << '\n';
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2076 KiB
2Accepted3ms2352 KiB
3Accepted4ms3448 KiB
subtask240/40
4Accepted54ms10604 KiB
5Accepted52ms10852 KiB
6Accepted54ms11136 KiB
7Accepted52ms11352 KiB
8Accepted54ms11444 KiB
9Accepted52ms11480 KiB
10Accepted52ms11656 KiB
11Accepted3ms3876 KiB
subtask30/60
12Accepted54ms10604 KiB
13Accepted52ms10852 KiB
14Accepted54ms11136 KiB
15Accepted52ms11352 KiB
16Accepted54ms11444 KiB
17Accepted52ms11480 KiB
18Accepted52ms11656 KiB
19Accepted3ms3876 KiB
20Accepted52ms11868 KiB
21Accepted54ms11992 KiB
22Accepted52ms12244 KiB
23Accepted52ms12420 KiB
24Accepted50ms12268 KiB
25Accepted52ms12288 KiB
26Accepted52ms12472 KiB
27Accepted52ms12504 KiB
28Accepted43ms9020 KiB
29Wrong answer52ms12484 KiB
30Accepted3ms4688 KiB
31Accepted3ms5376 KiB