109412024-04-20 17:28:54k_balintVasútépítéscpp17Accepted 100/10048ms12496 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 || cnt==1){
                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
3Accepted3ms3348 KiB
subtask240/40
4Accepted46ms10712 KiB
5Accepted46ms10816 KiB
6Accepted46ms10952 KiB
7Accepted46ms10964 KiB
8Accepted48ms11124 KiB
9Accepted48ms11472 KiB
10Accepted48ms11440 KiB
11Accepted3ms3376 KiB
subtask360/60
12Accepted46ms10712 KiB
13Accepted46ms10816 KiB
14Accepted46ms10952 KiB
15Accepted46ms10964 KiB
16Accepted48ms11124 KiB
17Accepted48ms11472 KiB
18Accepted48ms11440 KiB
19Accepted3ms3376 KiB
20Accepted48ms11792 KiB
21Accepted48ms11868 KiB
22Accepted48ms12120 KiB
23Accepted48ms12280 KiB
24Accepted48ms12320 KiB
25Accepted46ms12496 KiB
26Accepted46ms12432 KiB
27Accepted46ms12432 KiB
28Accepted43ms8848 KiB
29Accepted4ms7328 KiB
30Accepted3ms4964 KiB
31Accepted3ms5612 KiB