102992024-03-30 10:56:44111Pletykálkodáscpp17Accepted 100/10061ms7448 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	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;
		if(i+1==N*(N-1)/2){
			break;
		}
		g[a].push_back(b);
		g[b].push_back(a);
	}
	vector<tuple<int,int,int,int>>c;
	for(int i=1;i<=N;i++){
		vector<int>v(N+1);
		v[i]=-1;
		for(int j:g[i]){
			v[j]=-1;
		}
		for(int j:g[i]){
			for(int k:g[j]){
				if(v[k]<0){
					continue;
				}
				if(v[k]>0){
					c.emplace_back(i,j,v[k],k);
					continue;
				}
				v[k]=j;
			}
		}
	}
	for(auto[a,b,aa,bb]:c){
		vector<int>v(N+1,-1);
		vector<vector<pair<int,int>>>w(2);
		auto dfs=[&](auto self,int i)->void{
			for(int j:g[i]){
				if(v[j]!=-1){
					continue;
				}
				v[j]=v[i];
				self(self,j);
				w[v[i]].emplace_back(i,j);
			}
		};
		v[a]=0;
		v[b]=1;
		v[aa]=0;
		v[bb]=1;
		dfs(dfs,aa);
		dfs(dfs,bb);
		if(count(v.begin(),v.end(),-1)==1){
			vector<pair<int,int>>s;
			for(auto[a,b]:w[0]){
				s.emplace_back(a,b);
			}
			for(auto[a,b]:w[1]){
				s.emplace_back(a,b);
			}
			s.emplace_back(a,aa);
			s.emplace_back(b,bb);
			s.emplace_back(a,b);
			s.emplace_back(aa,bb);
			reverse(w[0].begin(),w[0].end());
			reverse(w[1].begin(),w[1].end());
			for(auto[a,b]:w[0]){
				s.emplace_back(a,b);
			}
			for(auto[a,b]:w[1]){
				s.emplace_back(a,b);
			}
			cout<<s.size()<<'\n';
			for(auto[a,b]:s){
				cout<<a<<' '<<b<<'\n';
			}
			return 0;
		}
	}
	int a=1,b=g[1][0];
	vector<int>v(N+1,-1);
	vector<vector<pair<int,int>>>w(2);
	auto dfs=[&](auto self,int i)->void{
		for(int j:g[i]){
			if(v[j]!=-1){
				continue;
			}
			v[j]=v[i];
			self(self,j);
			w[v[i]].emplace_back(i,j);
		}
	};
	v[a]=0;
	v[b]=1;
	dfs(dfs,a);
	dfs(dfs,b);
	vector<pair<int,int>>s;
	for(auto[a,b]:w[0]){
		s.emplace_back(a,b);
	}
	for(auto[a,b]:w[1]){
		s.emplace_back(a,b);
	}
	s.emplace_back(a,b);
	reverse(w[0].begin(),w[0].end());
	reverse(w[1].begin(),w[1].end());
	for(auto[a,b]:w[0]){
		s.emplace_back(a,b);
	}
	for(auto[a,b]:w[1]){
		s.emplace_back(a,b);
	}
	cout<<s.size()<<'\n';
	for(auto[a,b]:s){
		cout<<a<<' '<<b<<'\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1976 KiB
2Accepted3ms2232 KiB
3Accepted4ms2772 KiB
subtask29/9
4Accepted3ms2612 KiB
5Accepted3ms2592 KiB
6Accepted3ms2780 KiB
subtask313/13
7Accepted17ms4968 KiB
8Accepted24ms4856 KiB
9Accepted24ms4884 KiB
subtask416/16
10Accepted14ms4000 KiB
11Accepted16ms4348 KiB
12Accepted14ms4416 KiB
subtask525/25
13Accepted3ms3776 KiB
14Accepted3ms3740 KiB
15Accepted3ms3984 KiB
16Accepted3ms3940 KiB
17Accepted3ms3944 KiB
18Accepted3ms4064 KiB
19Accepted3ms4156 KiB
subtask613/13
20Accepted4ms4660 KiB
21Accepted8ms4592 KiB
22Accepted4ms4636 KiB
23Accepted8ms5108 KiB
24Accepted6ms4776 KiB
25Accepted6ms4592 KiB
26Accepted6ms4700 KiB
subtask724/24
27Accepted26ms7364 KiB
28Accepted35ms7448 KiB
29Accepted27ms7404 KiB
30Accepted37ms6848 KiB
31Accepted41ms6608 KiB
32Accepted46ms6580 KiB
33Accepted54ms6372 KiB
34Accepted61ms6336 KiB
35Accepted41ms6792 KiB
36Accepted46ms6480 KiB
37Accepted54ms6616 KiB