이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FOR(i,a) for (int i=0;i<(a);++i)
#define FORD(i,a) for (int i=(a)-1;i>=0;i--)
#define FORT(i,a,b) for (int i=(a);i<=(b);++i)
#define FORTD(i,b,a) for (int i=(b);i>=(a);--i)
#define trav(i,v) for (auto i : v)
#define all(v) v.begin(),v.end()
#define ad push_back
#define fr first
#define sc second
#define mpr(a,b) make_pair(a,b)
#define pir pair<int,int>
#define all(v) v.begin(),v.end()
#define make_unique(v) v.erase(unique(all(v),v.end()))
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define dbg(x);
#define dbgv(v);
#define srng mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define y1 EsiHancagorcRepa
#ifdef ALEXPC
#define dbg(x); cout<<#x<<" = "<<x<<endl
#define dbgv(v); cout<<#v<<" = ["; trav(tv,v)cout<<"tv,";cout<<"]"<<endl
#endif
//const int N=100,M=N*N;
const ll MOD=1000*1000*1000+7;
const int N=2*1e6+6,M=6*N,MAXND=N;
const ll MX=1000000000ll*1000000000ll;
int n,d[N];
vector<int> g[N];
vector<int> ans;
void dfs(int v,int par=-1){
if (d[v]==1){
ans.push_back(v);
return;
}
trav(to,g[v])if (to!=par)dfs(to,v);
}
int main(){
fastio;
srng;
cin>>n;
FOR(i,n-1){
int a,b;
cin>>a>>b;
d[a]++;
d[b]++;
g[a].push_back(b);
g[b].push_back(a);
}
FORT(i,1,n){
if (d[i]!=1){
dfs(i);
break;
}
}
int k;
if (ans.size()%2)k=ans.size()/2+1;
else k=ans.size()/2;
cout<<k<<endl;
FOR(i,k){
if (i+k==ans.size()){
cout<<ans[i]<<" "<<ans.back()<<endl;
}
else cout<<ans[i]<<" "<<ans[i+k]<<endl;
}
return 0;
}
/*
3 4
1 2 5
2 3 2
3 1 4
2 3 8
5
2 1 5
2 1 2
2 2 5
2 3 4
2 3 2
*/
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main()':
net.cpp:71:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i+k==ans.size()){
~~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |