102982024-03-30 10:54:18111Pletykálkodáscpp17Wrong answer 84/10059ms7084 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;
		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(k!=1&&v[k]>0){
					c.emplace_back(i,j,v[k],k);
					continue;
				}
				if(v[k]<0){
					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
1Accepted3ms1828 KiB
2Accepted3ms2020 KiB
3Accepted4ms2812 KiB
subtask29/9
4Accepted3ms2512 KiB
5Accepted2ms2588 KiB
6Accepted2ms2592 KiB
subtask313/13
7Accepted17ms4516 KiB
8Accepted24ms4520 KiB
9Accepted23ms4636 KiB
subtask40/16
10Wrong answer18ms3492 KiB
11Wrong answer18ms3488 KiB
12Wrong answer17ms3780 KiB
subtask525/25
13Accepted3ms2976 KiB
14Accepted3ms2984 KiB
15Accepted3ms2980 KiB
16Accepted3ms2976 KiB
17Accepted3ms2972 KiB
18Accepted3ms2964 KiB
19Accepted3ms3064 KiB
subtask613/13
20Accepted4ms3484 KiB
21Accepted8ms3736 KiB
22Accepted4ms3708 KiB
23Accepted8ms4248 KiB
24Accepted6ms3924 KiB
25Accepted6ms3992 KiB
26Accepted6ms3872 KiB
subtask724/24
27Accepted26ms6728 KiB
28Accepted35ms7084 KiB
29Accepted26ms6968 KiB
30Accepted35ms6408 KiB
31Accepted39ms6184 KiB
32Accepted45ms5968 KiB
33Accepted52ms5744 KiB
34Accepted59ms5592 KiB
35Accepted41ms6136 KiB
36Accepted45ms5828 KiB
37Accepted52ms5788 KiB