이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
ll n,m,i,ta,tb,D[101010],daun,bes[101010],ROOT,j,P[101010],sudah[101010],sisa,kel[101010],L[101010],VS;
vector<ll> v[101010],C,w[101010],isi[101010];
vector<pair<pair<ll,ll> ,ll> > Z;
void dfs(ll aa,ll bb)
{
//cout<<aa<<" "<<bb<<" "<<cc<<"\n";
bes[aa]=0;
if(D[aa]==1)bes[aa]=1;
ll ii,ma=0;
for(ii=0;ii<v[aa].size();ii++)
{
if(v[aa][ii]!=bb)
{
P[v[aa][ii]]=aa;
dfs(v[aa][ii],aa);
bes[aa]+=bes[v[aa][ii]];
ma=max(ma,bes[v[aa][ii]]);
}
}
ma=max(ma,daun-bes[aa]);
//cout<<aa<<" "<<ma<<"\n";
if(ma*2<=daun)ROOT=aa;
}
void dfs2(ll aa,ll bb,ll cc,ll dd)
{
L[aa]=dd;
if(D[aa]==1)
{
kel[dd]++;
isi[dd].pb(aa);
Z.pb(mp(mp(cc,dd),aa));
}
ll ii;
for(ii=0;ii<v[aa].size();ii++)
if(v[aa][ii]!=bb)
dfs2(v[aa][ii],aa,cc,dd);
}
int main()
{
ios_base::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL);
cin>>n;
for(i=1;i<n;i++)
{
cin>>ta>>tb;
v[ta].pb(tb);
v[tb].pb(ta);
D[ta]++;
D[tb]++;
}
for(i=1;i<=n;i++)
if(D[i]>1)
C.pb(i);
else
daun++;
//cout<<daun<<"\n";
dfs(C[0],C[0]);
//cout<<ROOT<<"\n";
//return 0;
VS=v[ROOT].size();
for(i=0;i<v[ROOT].size();i++)
dfs2(v[ROOT][i],ROOT,bes[v[ROOT][i]],i);
cout<<(daun/2+daun%2)<<"\n";
sort(Z.begin(),Z.end());
if(daun%2==1)
{
ll OO=Z[daun-1].fi.se;
if(isi[OO].back()!=ROOT)cout<<isi[OO].back()<<" "<<ROOT<<"\n";
else
{
for(i=1;i<=n;i++)
if(L[i]!=L[isi[OO].back()])
{
cout<<isi[OO].back()<<" "<<i<<"\n";
break;
}
}
daun--;
kel[OO]--;
isi[OO].pop_back();
Z.pop_back();
}
priority_queue<pair<ll,ll> > pq;
sisa=daun/2;
for(i=0;i<VS;i++)pq.push(mp(kel[i],i));
for(i=0;i<(daun/2);i++)
{
while(kel[pq.top().se]!=pq.top().fi)pq.pop();
ll A=pq.top().se;
pq.pop();
ll B=pq.top().se;
pq.pop();
cout<<isi[A].back()<<" "<<isi[B].back()<<"\n";
kel[A]--;
kel[B]--;
isi[A].pop_back();
isi[B].pop_back();
pq.push(mp(kel[A],A));
pq.push(mp(kel[B],B));
}
//if(sisa)while(1);
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'void dfs(long long int, long long int)':
net.cpp:17:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ii=0;ii<v[aa].size();ii++)
~~^~~~~~~~~~~~~
net.cpp: In function 'void dfs2(long long int, long long int, long long int, long long int)':
net.cpp:41:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ii=0;ii<v[aa].size();ii++)
~~^~~~~~~~~~~~~
net.cpp: In function 'int main()':
net.cpp:67:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<v[ROOT].size();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... |