102822024-03-29 22:02:18111Pletykálkodáscpp17Runtime error 13/1008ms5792 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;
	if(M!=N-1){
		return 1;
	}
	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);
	}
	auto dfs1=[&](auto self,int i,int p)->void{
		g[i].erase(remove(g[i].begin(),g[i].end(),p),g[i].end());
		for(int j:g[i]){
			self(self,j,i);
		}
	};
	dfs1(dfs1,1,0);
	vector<pair<int,int>>ans;
	int l=0;
	auto dfs2=[&](auto self,int i)->void{
		for(int j:g[i]){
			self(self,j);
			ans.emplace_back(i,j);
			l=j;
		}
	};
	dfs2(dfs2,1);
	auto dfs3=[&](auto self,int i)->void{
		for(int j:g[i]){
			ans.emplace_back(i,j);
			if(i==1&&j==l){
				ans.pop_back();
			}
			self(self,j);
		}
	};
	dfs3(dfs3,1);
	cout<<ans.size()<<'\n';
	for(auto[a,b]:ans){
		cout<<a<<' '<<b<<'\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1832 KiB
2Runtime error3ms2024 KiB
3Runtime error3ms2240 KiB
subtask20/9
4Runtime error3ms2472 KiB
5Runtime error3ms2704 KiB
6Runtime error3ms2920 KiB
subtask313/13
7Accepted8ms4836 KiB
8Accepted8ms5068 KiB
9Accepted8ms5404 KiB
subtask40/16
10Runtime error3ms3932 KiB
11Runtime error3ms3968 KiB
12Runtime error3ms3988 KiB
subtask50/25
13Runtime error3ms4080 KiB
14Runtime error3ms4112 KiB
15Runtime error3ms4108 KiB
16Runtime error3ms4204 KiB
17Runtime error3ms4332 KiB
18Runtime error3ms4424 KiB
19Runtime error3ms4656 KiB
subtask60/13
20Runtime error3ms4676 KiB
21Runtime error3ms4956 KiB
22Runtime error3ms5048 KiB
23Runtime error3ms5296 KiB
24Runtime error3ms5272 KiB
25Runtime error3ms5044 KiB
26Runtime error3ms5336 KiB
subtask70/24
27Runtime error3ms5432 KiB
28Runtime error3ms5456 KiB
29Runtime error3ms5476 KiB
30Runtime error3ms5364 KiB
31Runtime error3ms5456 KiB
32Runtime error3ms5468 KiB
33Runtime error3ms5492 KiB
34Runtime error3ms5520 KiB
35Runtime error3ms5468 KiB
36Runtime error3ms5716 KiB
37Runtime error3ms5792 KiB