이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize ("O2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define task "task"
#define task "task"
#define prll pair<ll,ll>
#define pb push_back
#define ld long double
const ll MIN=-1e18,MAX=1e18,MOD=1e9+7;
vector<int> a[500005];
vector<int> leaves;
void dfs(int u,int par)
{
if(a[u].size()==1) leaves.push_back(u);
for(auto i:a[u])
{
if(i!=par)
{
dfs(i,u);
}
}
}
int main(){
//freopen (task".inp", "r", stdin);
//freopen (task".out", "w", stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin>>n;
leaves.push_back(0);
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1,0);
// for(auto i:leaves) cout<<i<<" ";
// cout<<endl;
if(leaves.size()%2==1)
{
cout<<leaves.size()/2<<endl;
for(int i=1;i<=leaves.size()/2;i++)
{
cout<<leaves[i]<<" "<<leaves[i+leaves.size()/2]<<endl;
}
// if(leaves.size()%2==01)cout<<leaves[leaves.size()/2]<<" "<<1<<endl;
}
else
{
cout<<leaves.size()/2<<endl;
for(int i=1;i<=leaves.size()/2-1;i++)
{
cout<<leaves[i]<<" "<<leaves[i+leaves.size()/2]<<endl;
}
cout<<leaves[leaves.size()/2]<<" "<<1<<endl;
}
}
/*
*/
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main()':
net.cpp:47:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i=1;i<=leaves.size()/2;i++)
| ~^~~~~~~~~~~~~~~~~
net.cpp:56:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i=1;i<=leaves.size()/2-1;i++)
| ~^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |