이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define maxN 500002
using namespace std;
int n,x,y,i,j,l,t,deg[maxN],br[maxN],par[maxN],a[maxN];
bool visited[maxN];
vector <int> adj[maxN];
void dfs1(int n){
int i;
if(deg[n]==1) br[n]=1;
else br[n]=0;
visited[n]=true;
for(i=0;i<adj[n].size();i++){
if(!visited[adj[n][i]]){
par[adj[n][i]]=n;
dfs1(adj[n][i]);
br[n]+=br[adj[n][i]];
}
}
}
void dfs2(int n){
int i;
visited[n]=true;
if(deg[n]==1) {a[t]=n; t++;}
for(i=0;i<adj[n].size();i++){
if(!visited[adj[n][i]]){
dfs2(adj[n][i]);
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
l=0;
cin>>n;
for(i=1;i<=n;i++){
deg[i]=0;
}
for(i=1;i<n;i++){
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
deg[x]++;
deg[y]++;
}
for(i=1;i<=n;i++){
if(deg[i]==1) l++;
visited[i]=false;
}
par[1]=1;
dfs1(1);
for(i=1;i<=n;i++){
bool b=true;
if(l-br[i]>l-l/2) continue;
for(j=0;j<adj[i].size();j++){
if(adj[i][j]!=par[i]) b=b && (br[adj[i][j]]<=l-l/2);
}
if(b) {x=i; break;}
}
t=0;
for(i=1;i<=n;i++){
visited[i]=false;
}
dfs2(x);
cout<<l-l/2<<endl;
for(i=0;i<l-l/2;i++){
cout<<a[i]<<" "<<a[i+l/2]<<endl;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'void dfs1(int)':
net.cpp:15:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<adj[n].size();i++){
^
net.cpp: In function 'void dfs2(int)':
net.cpp:27:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<adj[n].size();i++){
^
net.cpp: In function 'int main()':
net.cpp:58:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<adj[i].size();j++){
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |