109342024-04-20 16:25:06k_balintVasútépítéscpp17Wrong answer 0/10048ms12548 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;
bool van;
int ans[c][c];
int par[c];

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){
    if(van) return;
    vis[v]=2;
    par[v]=p;

    for(int x:adj[v]){
        if(van) break;
        if(x==p) continue;
        if(vis[x]==1) dfs2(x,v);
        else if(!van){
            int k=v;
            while(k != par[x]){
                kor.push_back(k);
                k=par[k];
            }
            van=1;
        }
    }
}

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;
            }
            kor.clear();
            van=0;
            dfs2(i,0);
            for(int j=0;j<kor.size();j++){
                int x=kor[(j+1)%kor.size()];
                int y=kor[j];
                ans[x][y]=ans[y][x]=++tt;
            }
        }
    }

    for(int i=1;i<=n;i++){
        for(int x:adj[i]){
            if(!ans[x][i]){
                ans[x][i]=ans[i][x]=++tt;
            }
        }
    }

    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
2Accepted3ms2244 KiB
3Wrong answer3ms3240 KiB
subtask20/40
4Wrong answer48ms10652 KiB
5Wrong answer48ms10676 KiB
6Wrong answer48ms10612 KiB
7Wrong answer48ms10892 KiB
8Wrong answer46ms11048 KiB
9Wrong answer48ms11224 KiB
10Wrong answer46ms11444 KiB
11Accepted3ms3720 KiB
subtask30/60
12Wrong answer48ms10652 KiB
13Wrong answer48ms10676 KiB
14Wrong answer48ms10612 KiB
15Wrong answer48ms10892 KiB
16Wrong answer46ms11048 KiB
17Wrong answer48ms11224 KiB
18Wrong answer46ms11444 KiB
19Accepted3ms3720 KiB
20Wrong answer46ms11808 KiB
21Wrong answer46ms11988 KiB
22Wrong answer46ms11844 KiB
23Wrong answer48ms11908 KiB
24Wrong answer48ms12140 KiB
25Wrong answer48ms11880 KiB
26Wrong answer48ms12168 KiB
27Wrong answer48ms12516 KiB
28Accepted41ms8964 KiB
29Wrong answer48ms12548 KiB
30Accepted3ms4732 KiB
31Accepted3ms5324 KiB