109372024-04-20 17:14:20k_balintVasútépítéscpp17Wrong answer 40/10050ms11896 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; deg+=adj[v].size();
    vis[v]=1;
    for(int x:adj[v]){
        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>2*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 && cur != 0){
                cout << cur << endl;
                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
1Wrong answer3ms2076 KiB
2Accepted3ms2352 KiB
3Wrong answer3ms3352 KiB
subtask240/40
4Accepted48ms10672 KiB
5Accepted46ms10692 KiB
6Accepted46ms10696 KiB
7Accepted46ms10916 KiB
8Accepted48ms10836 KiB
9Accepted46ms10832 KiB
10Accepted48ms11228 KiB
11Accepted3ms3456 KiB
subtask30/60
12Accepted48ms10672 KiB
13Accepted46ms10692 KiB
14Accepted46ms10696 KiB
15Accepted46ms10916 KiB
16Accepted48ms10836 KiB
17Accepted46ms10832 KiB
18Accepted48ms11228 KiB
19Accepted3ms3456 KiB
20Wrong answer48ms11324 KiB
21Wrong answer48ms11276 KiB
22Wrong answer50ms11544 KiB
23Wrong answer48ms11744 KiB
24Wrong answer48ms11848 KiB
25Wrong answer48ms11896 KiB
26Wrong answer48ms11864 KiB
27Wrong answer48ms11836 KiB
28Accepted41ms8244 KiB
29Wrong answer48ms11788 KiB
30Accepted3ms4028 KiB
31Wrong answer4ms4968 KiB