# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
40617 |
2018-02-05T17:07:03 Z |
Hassoony |
Network (BOI15_net) |
C++14 |
|
1034 ms |
175436 KB |
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const ll inf=(1ll<<60);
const ll mod=1e9+7;
const int MX=6e5+9;
int n,subtree[MX],par[MX],leaf[MX],ans;
vector<int>v[MX],v1[MX];
void dfs_size(int x,int p){
subtree[x]=leaf[x];
par[x]=p;
for(auto pp:v[x]){
if(pp==p)continue;
dfs_size(pp,x);
subtree[x]+=subtree[pp];
}
}
int centroid(int x){
dfs_size(x,-1);
int S=subtree[x],idx=1;
queue<int>q;
q.push(x);
while(!q.empty()){
int nxt=q.front();q.pop();
int s=subtree[x]-subtree[nxt];
for(auto pp:v[nxt]){
if(pp==par[nxt])continue;
s=max(s,subtree[pp]);
q.push(pp);
}
if(S>s){
S=s;
idx=nxt;
}
}
return idx;
}
int x,y,cen=1,timer;
void dfs(int x,int p){
if(leaf[x]){
v1[timer].push_back(x);
return;
}
for(auto pp:v[x]){
if(pp==p)continue;
dfs(pp,x);
if(x==cen)timer++;
}
}
int leaves;
int cel(int x,int y){
if(x%y==0)return x/y;
return x/y+1;
}
int main(){
scanf("%d",&n);
for(int i=0;i<n-1;i++){
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=1;i<=n;i++){
if(v[i].size()==1){
leaf[i]=1;
leaves++;
}
}
int idx=1;
for(int i=1;i<=n;i++)if(!leaf[i])idx=i;
ans=cel(leaves,2);
cen=centroid(idx);
dfs(cen,-1);
priority_queue<pair<int,int> >pq;
for(int i=0;i<timer;i++){
pq.push({v1[i].size(),i});
}
printf("%d\n",ans);
while(pq.size()>1){
int com1=pq.top().second,sz1=pq.top().first;pq.pop();
int com2=pq.top().second,sz2=pq.top().first;pq.pop();
if(sz1<=0||sz2<=0)continue;
printf("%d %d\n",v1[com1].back(),v1[com2].back());
v1[com1].pop_back();sz1--;
v1[com2].pop_back();sz2--;
if(sz1>0)pq.push({sz1,com1});
if(sz2>0)pq.push({sz2,com2});
}
if(pq.size()==1){
printf("%d %d",v1[pq.top().second][0],cen);
}
}
Compilation message
net.cpp: In function 'int main()':
net.cpp:57:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
^
net.cpp:59:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&x,&y);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
28536 KB |
Output is correct |
2 |
Correct |
25 ms |
28640 KB |
Output is correct |
3 |
Correct |
25 ms |
28640 KB |
Output is correct |
4 |
Correct |
29 ms |
28640 KB |
Output is correct |
5 |
Correct |
24 ms |
28720 KB |
Output is correct |
6 |
Correct |
25 ms |
28720 KB |
Output is correct |
7 |
Correct |
24 ms |
28720 KB |
Output is correct |
8 |
Correct |
25 ms |
28720 KB |
Output is correct |
9 |
Correct |
24 ms |
28720 KB |
Output is correct |
10 |
Correct |
25 ms |
28760 KB |
Output is correct |
11 |
Correct |
25 ms |
28764 KB |
Output is correct |
12 |
Correct |
25 ms |
28764 KB |
Output is correct |
13 |
Correct |
25 ms |
28764 KB |
Output is correct |
14 |
Correct |
27 ms |
28764 KB |
Output is correct |
15 |
Correct |
25 ms |
28892 KB |
Output is correct |
16 |
Correct |
25 ms |
28892 KB |
Output is correct |
17 |
Correct |
25 ms |
28892 KB |
Output is correct |
18 |
Correct |
25 ms |
28892 KB |
Output is correct |
19 |
Correct |
25 ms |
28892 KB |
Output is correct |
20 |
Correct |
24 ms |
28892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
28536 KB |
Output is correct |
2 |
Correct |
25 ms |
28640 KB |
Output is correct |
3 |
Correct |
25 ms |
28640 KB |
Output is correct |
4 |
Correct |
29 ms |
28640 KB |
Output is correct |
5 |
Correct |
24 ms |
28720 KB |
Output is correct |
6 |
Correct |
25 ms |
28720 KB |
Output is correct |
7 |
Correct |
24 ms |
28720 KB |
Output is correct |
8 |
Correct |
25 ms |
28720 KB |
Output is correct |
9 |
Correct |
24 ms |
28720 KB |
Output is correct |
10 |
Correct |
25 ms |
28760 KB |
Output is correct |
11 |
Correct |
25 ms |
28764 KB |
Output is correct |
12 |
Correct |
25 ms |
28764 KB |
Output is correct |
13 |
Correct |
25 ms |
28764 KB |
Output is correct |
14 |
Correct |
27 ms |
28764 KB |
Output is correct |
15 |
Correct |
25 ms |
28892 KB |
Output is correct |
16 |
Correct |
25 ms |
28892 KB |
Output is correct |
17 |
Correct |
25 ms |
28892 KB |
Output is correct |
18 |
Correct |
25 ms |
28892 KB |
Output is correct |
19 |
Correct |
25 ms |
28892 KB |
Output is correct |
20 |
Correct |
24 ms |
28892 KB |
Output is correct |
21 |
Correct |
26 ms |
28892 KB |
Output is correct |
22 |
Correct |
28 ms |
28892 KB |
Output is correct |
23 |
Correct |
28 ms |
28892 KB |
Output is correct |
24 |
Correct |
26 ms |
28908 KB |
Output is correct |
25 |
Correct |
32 ms |
28924 KB |
Output is correct |
26 |
Correct |
26 ms |
28924 KB |
Output is correct |
27 |
Correct |
26 ms |
28924 KB |
Output is correct |
28 |
Correct |
25 ms |
28924 KB |
Output is correct |
29 |
Correct |
25 ms |
28924 KB |
Output is correct |
30 |
Correct |
25 ms |
28924 KB |
Output is correct |
31 |
Correct |
26 ms |
28940 KB |
Output is correct |
32 |
Correct |
24 ms |
28940 KB |
Output is correct |
33 |
Correct |
25 ms |
28940 KB |
Output is correct |
34 |
Correct |
25 ms |
28940 KB |
Output is correct |
35 |
Correct |
26 ms |
28940 KB |
Output is correct |
36 |
Correct |
28 ms |
28940 KB |
Output is correct |
37 |
Correct |
25 ms |
28940 KB |
Output is correct |
38 |
Correct |
25 ms |
28992 KB |
Output is correct |
39 |
Correct |
25 ms |
28992 KB |
Output is correct |
40 |
Correct |
25 ms |
28992 KB |
Output is correct |
41 |
Correct |
25 ms |
28992 KB |
Output is correct |
42 |
Correct |
25 ms |
28992 KB |
Output is correct |
43 |
Correct |
26 ms |
28992 KB |
Output is correct |
44 |
Correct |
25 ms |
28992 KB |
Output is correct |
45 |
Correct |
28 ms |
28992 KB |
Output is correct |
46 |
Correct |
27 ms |
28992 KB |
Output is correct |
47 |
Correct |
25 ms |
28992 KB |
Output is correct |
48 |
Correct |
25 ms |
28992 KB |
Output is correct |
49 |
Correct |
25 ms |
28992 KB |
Output is correct |
50 |
Correct |
25 ms |
28992 KB |
Output is correct |
51 |
Correct |
25 ms |
28992 KB |
Output is correct |
52 |
Correct |
25 ms |
28992 KB |
Output is correct |
53 |
Correct |
26 ms |
29012 KB |
Output is correct |
54 |
Correct |
24 ms |
29012 KB |
Output is correct |
55 |
Correct |
24 ms |
29012 KB |
Output is correct |
56 |
Correct |
26 ms |
29012 KB |
Output is correct |
57 |
Correct |
26 ms |
29012 KB |
Output is correct |
58 |
Correct |
27 ms |
29060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
28536 KB |
Output is correct |
2 |
Correct |
25 ms |
28640 KB |
Output is correct |
3 |
Correct |
25 ms |
28640 KB |
Output is correct |
4 |
Correct |
29 ms |
28640 KB |
Output is correct |
5 |
Correct |
24 ms |
28720 KB |
Output is correct |
6 |
Correct |
25 ms |
28720 KB |
Output is correct |
7 |
Correct |
24 ms |
28720 KB |
Output is correct |
8 |
Correct |
25 ms |
28720 KB |
Output is correct |
9 |
Correct |
24 ms |
28720 KB |
Output is correct |
10 |
Correct |
25 ms |
28760 KB |
Output is correct |
11 |
Correct |
25 ms |
28764 KB |
Output is correct |
12 |
Correct |
25 ms |
28764 KB |
Output is correct |
13 |
Correct |
25 ms |
28764 KB |
Output is correct |
14 |
Correct |
27 ms |
28764 KB |
Output is correct |
15 |
Correct |
25 ms |
28892 KB |
Output is correct |
16 |
Correct |
25 ms |
28892 KB |
Output is correct |
17 |
Correct |
25 ms |
28892 KB |
Output is correct |
18 |
Correct |
25 ms |
28892 KB |
Output is correct |
19 |
Correct |
25 ms |
28892 KB |
Output is correct |
20 |
Correct |
24 ms |
28892 KB |
Output is correct |
21 |
Correct |
26 ms |
28892 KB |
Output is correct |
22 |
Correct |
28 ms |
28892 KB |
Output is correct |
23 |
Correct |
28 ms |
28892 KB |
Output is correct |
24 |
Correct |
26 ms |
28908 KB |
Output is correct |
25 |
Correct |
32 ms |
28924 KB |
Output is correct |
26 |
Correct |
26 ms |
28924 KB |
Output is correct |
27 |
Correct |
26 ms |
28924 KB |
Output is correct |
28 |
Correct |
25 ms |
28924 KB |
Output is correct |
29 |
Correct |
25 ms |
28924 KB |
Output is correct |
30 |
Correct |
25 ms |
28924 KB |
Output is correct |
31 |
Correct |
26 ms |
28940 KB |
Output is correct |
32 |
Correct |
24 ms |
28940 KB |
Output is correct |
33 |
Correct |
25 ms |
28940 KB |
Output is correct |
34 |
Correct |
25 ms |
28940 KB |
Output is correct |
35 |
Correct |
26 ms |
28940 KB |
Output is correct |
36 |
Correct |
28 ms |
28940 KB |
Output is correct |
37 |
Correct |
25 ms |
28940 KB |
Output is correct |
38 |
Correct |
25 ms |
28992 KB |
Output is correct |
39 |
Correct |
25 ms |
28992 KB |
Output is correct |
40 |
Correct |
25 ms |
28992 KB |
Output is correct |
41 |
Correct |
25 ms |
28992 KB |
Output is correct |
42 |
Correct |
25 ms |
28992 KB |
Output is correct |
43 |
Correct |
26 ms |
28992 KB |
Output is correct |
44 |
Correct |
25 ms |
28992 KB |
Output is correct |
45 |
Correct |
28 ms |
28992 KB |
Output is correct |
46 |
Correct |
27 ms |
28992 KB |
Output is correct |
47 |
Correct |
25 ms |
28992 KB |
Output is correct |
48 |
Correct |
25 ms |
28992 KB |
Output is correct |
49 |
Correct |
25 ms |
28992 KB |
Output is correct |
50 |
Correct |
25 ms |
28992 KB |
Output is correct |
51 |
Correct |
25 ms |
28992 KB |
Output is correct |
52 |
Correct |
25 ms |
28992 KB |
Output is correct |
53 |
Correct |
26 ms |
29012 KB |
Output is correct |
54 |
Correct |
24 ms |
29012 KB |
Output is correct |
55 |
Correct |
24 ms |
29012 KB |
Output is correct |
56 |
Correct |
26 ms |
29012 KB |
Output is correct |
57 |
Correct |
26 ms |
29012 KB |
Output is correct |
58 |
Correct |
27 ms |
29060 KB |
Output is correct |
59 |
Correct |
688 ms |
62884 KB |
Output is correct |
60 |
Correct |
745 ms |
67700 KB |
Output is correct |
61 |
Correct |
25 ms |
67700 KB |
Output is correct |
62 |
Correct |
24 ms |
67700 KB |
Output is correct |
63 |
Correct |
556 ms |
70644 KB |
Output is correct |
64 |
Correct |
28 ms |
70644 KB |
Output is correct |
65 |
Correct |
41 ms |
70644 KB |
Output is correct |
66 |
Correct |
289 ms |
70644 KB |
Output is correct |
67 |
Correct |
1034 ms |
106888 KB |
Output is correct |
68 |
Correct |
982 ms |
106888 KB |
Output is correct |
69 |
Correct |
37 ms |
106888 KB |
Output is correct |
70 |
Correct |
199 ms |
106888 KB |
Output is correct |
71 |
Correct |
596 ms |
118120 KB |
Output is correct |
72 |
Correct |
643 ms |
121960 KB |
Output is correct |
73 |
Correct |
154 ms |
121960 KB |
Output is correct |
74 |
Correct |
767 ms |
121960 KB |
Output is correct |
75 |
Correct |
71 ms |
121960 KB |
Output is correct |
76 |
Correct |
589 ms |
130728 KB |
Output is correct |
77 |
Correct |
501 ms |
137932 KB |
Output is correct |
78 |
Correct |
66 ms |
137932 KB |
Output is correct |
79 |
Correct |
847 ms |
141504 KB |
Output is correct |
80 |
Correct |
26 ms |
141504 KB |
Output is correct |
81 |
Correct |
182 ms |
141504 KB |
Output is correct |
82 |
Correct |
640 ms |
151752 KB |
Output is correct |
83 |
Correct |
702 ms |
151752 KB |
Output is correct |
84 |
Correct |
721 ms |
154412 KB |
Output is correct |
85 |
Correct |
726 ms |
161264 KB |
Output is correct |
86 |
Correct |
701 ms |
168624 KB |
Output is correct |
87 |
Correct |
688 ms |
175436 KB |
Output is correct |