107842024-04-12 15:58:47111Vasútépítéscpp17Wrong answer 40/10057ms28496 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
#ifdef CB
	freopen("be3.txt","r",stdin);
//	freopen("ki.txt","w",stdout);
#endif
	int N,M;
	cin>>N>>M;
	vector<vector<int>>g(N+1);
	for(int i=0;i<M;i++){
		int a,b;
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	int x1=0,x2=1e7,x3=2e7;
	vector<vector<int>>ans(N+1,vector<int>(N+1,-1));
	auto add1=[&](int i,int j)->void{
		ans[i][j]=x1;
		ans[j][i]=x1;
		x1++;
	};
	auto add2=[&](int i,int j)->void{
		ans[i][j]=x2;
		ans[j][i]=x2;
		x2++;
	};
	vector<int>v(N+1),w(N+1),p(N+1);
	for(int i=1;i<=N;i++){
		if(v[i]){
			continue;
		}
		v[i]=1;
		vector<int>c;
		vector<int>s;
		vector<int>t;
		auto dfs=[&](auto self,int i)->void{
			s.push_back(i);
			t.push_back(i);
			for(int j:g[i]){
				if(j==p[i]){
					continue;
				}
				if(v[j]){
					if(v[j]<v[i]){
						if(!c.empty()){
							cout<<-1<<'\n';
							exit(0);
						}
						c.push_back(j);
						w[j]=c.size();
						for(int k=s.size()-1;s[k]!=j;k--){
							c.push_back(s[k]);
							w[s[k]]=c.size();
						}
					}
					if(v[j]>v[i]&&!w[i]){
						cout<<-1<<'\n';
						exit(0);
					}
					continue;
				}
				v[j]=v[i]+1;
				p[j]=i;
				self(self,j);
			}
			s.pop_back();
		};
		dfs(dfs,i);
		if(!c.empty()){
			for(int i:c){
				auto dfs2=[&](auto self,int i,int p)->void{
					for(int j:g[i]){
						if(j==p||w[j]){
							continue;
						}
						add2(i,j);
						self(self,j,i);
					}
				};
				dfs2(dfs2,i,0);
			}
			for(int i=0;i<c.size();i++){
				add1(c[i],c[(i+1)%c.size()]);
			}
		}
		else{
			for(int j:t){
				add2(j,p[j]);
			}
		}
	}
	for(int i=1;i<=N;i++){
		for(int j=i+1;j<=N;j++){
			if(ans[i][j]==-1){
				ans[i][j]=x3++;
			}
			cout<<ans[i][j]<<' ';
		}
		cout<<'\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2060 KiB
3Accepted3ms2496 KiB
subtask240/40
4Accepted54ms18444 KiB
5Accepted57ms18724 KiB
6Accepted57ms18968 KiB
7Accepted57ms18940 KiB
8Accepted57ms19080 KiB
9Accepted54ms18996 KiB
10Accepted54ms18912 KiB
11Accepted3ms2968 KiB
subtask30/60
12Accepted54ms18444 KiB
13Accepted57ms18724 KiB
14Accepted57ms18968 KiB
15Accepted57ms18940 KiB
16Accepted57ms19080 KiB
17Accepted54ms18996 KiB
18Accepted54ms18912 KiB
19Accepted3ms2968 KiB
20Accepted56ms19736 KiB
21Accepted57ms19252 KiB
22Accepted57ms19436 KiB
23Accepted57ms19376 KiB
24Accepted57ms19364 KiB
25Accepted57ms19480 KiB
26Accepted54ms19644 KiB
27Accepted54ms19752 KiB
28Accepted50ms28496 KiB
29Wrong answer57ms19936 KiB
30Accepted9ms20220 KiB
31Accepted8ms20048 KiB