107852024-04-12 16:00:44111Vasútépítéscpp17Accepted 100/10071ms29196 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++){
		vector<int>s=ans[i];
		sort(s.rbegin(),s.rend());
		while(!s.empty()&&s.back()==-1){
			s.pop_back();
		}
		int ok=0;
		for(int j:g[i]){
			if(s.size()>=1&&ans[i][j]==s.back()||s.size()>=2&&ans[i][j]==s.end()[-2])ok=1;
		}
		if(!ok){
			cout<<-1<<'\n';
			exit(0);
		}
	}
	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
1Accepted3ms1700 KiB
2Accepted3ms1868 KiB
3Accepted3ms2312 KiB
subtask240/40
4Accepted71ms18372 KiB
5Accepted70ms18592 KiB
6Accepted71ms18888 KiB
7Accepted71ms19060 KiB
8Accepted71ms19120 KiB
9Accepted70ms19352 KiB
10Accepted68ms19680 KiB
11Accepted3ms3752 KiB
subtask360/60
12Accepted71ms18372 KiB
13Accepted70ms18592 KiB
14Accepted71ms18888 KiB
15Accepted71ms19060 KiB
16Accepted71ms19120 KiB
17Accepted70ms19352 KiB
18Accepted68ms19680 KiB
19Accepted3ms3752 KiB
20Accepted71ms20084 KiB
21Accepted71ms19616 KiB
22Accepted71ms19796 KiB
23Accepted68ms19812 KiB
24Accepted68ms20016 KiB
25Accepted71ms20372 KiB
26Accepted71ms20252 KiB
27Accepted71ms20300 KiB
28Accepted48ms29196 KiB
29Accepted13ms20368 KiB
30Accepted8ms20672 KiB
31Accepted8ms20356 KiB