Submission #286734

# Submission time Handle Problem Language Result Execution time Memory
286734 2020-08-30T20:18:45 Z Marlov Network (BOI15_net) C++14
100 / 100
641 ms 77512 KB
/*
Code by @marlov       
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <utility>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <iterator>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;

#define maxN 500005

int N;
int tl=0;
int leaves[maxN];
int root=0;
vector<int> adj[maxN];
vector< pair<int,int> > edges;
vector< vector<int> > connect;
priority_queue< pair<int,int> > pq;

void findRoot(int n,int p){
	if(adj[n].size()==1){
		leaves[n]++;
	}
	bool wks=true;
	for(int i:adj[n]) if(i!=p){
		findRoot(i,n);
		if(leaves[i]>floor(tl/2)) wks=false; 
		leaves[n]+=leaves[i];
	}
	if(tl-leaves[n]>floor(tl/2)) wks=false;
	if(wks&&adj[n].size()>1) root=n;
}

void addL(int n,int p){
	if(adj[n].size()==1){
		connect.back().push_back(n);
		return;
	}
	for(int i:adj[n]) if(i!=p){
		addL(i,n);
	}

}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin>>N;
	int u,v;
	for(int i=0;i<N-1;i++){
		cin>>u>>v;
		u--;v--;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for(int i=0;i<N;i++){
		if(adj[i].size()==1) tl++;
	}
	findRoot(0,-1);
	for(int i:adj[root]){
		int ci=connect.size();
		connect.push_back(vector<int>(0));
		addL(i,root);
		pq.push(make_pair(connect[ci].size()-1,ci));
	}
	while(pq.size()>1){
		int fi=pq.top().second; 
		int fl=pq.top().first; pq.pop();
		int sl=pq.top().first;
		int si=pq.top().second;pq.pop();
		edges.push_back(make_pair(connect[fi][fl],connect[si][sl]));
		if(fl>0) pq.push(make_pair(fl-1,fi));
		if(sl>0) pq.push(make_pair(sl-1,si));
	}
	if(pq.size()==1){
		edges.push_back(make_pair(connect[pq.top().second].front(),root));
	}
		cout<<edges.size()<<'\n';
	for(int i=0;i<(int)edges.size();i++){
		cout<<edges[i].first+1<<" "<<edges[i].second+1<<'\n';
	}
	return 0;
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1,n=0?)
	* do smth instead of nothing and stay organized
*/
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12032 KB Output is correct
2 Correct 7 ms 12032 KB Output is correct
3 Correct 8 ms 12032 KB Output is correct
4 Correct 8 ms 12160 KB Output is correct
5 Correct 7 ms 12032 KB Output is correct
6 Correct 7 ms 12032 KB Output is correct
7 Correct 7 ms 12032 KB Output is correct
8 Correct 7 ms 12032 KB Output is correct
9 Correct 7 ms 12032 KB Output is correct
10 Correct 7 ms 12032 KB Output is correct
11 Correct 8 ms 12032 KB Output is correct
12 Correct 7 ms 12032 KB Output is correct
13 Correct 8 ms 12032 KB Output is correct
14 Correct 7 ms 12032 KB Output is correct
15 Correct 8 ms 12032 KB Output is correct
16 Correct 8 ms 12032 KB Output is correct
17 Correct 7 ms 12032 KB Output is correct
18 Correct 8 ms 12032 KB Output is correct
19 Correct 7 ms 12032 KB Output is correct
20 Correct 8 ms 12032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12032 KB Output is correct
2 Correct 7 ms 12032 KB Output is correct
3 Correct 8 ms 12032 KB Output is correct
4 Correct 8 ms 12160 KB Output is correct
5 Correct 7 ms 12032 KB Output is correct
6 Correct 7 ms 12032 KB Output is correct
7 Correct 7 ms 12032 KB Output is correct
8 Correct 7 ms 12032 KB Output is correct
9 Correct 7 ms 12032 KB Output is correct
10 Correct 7 ms 12032 KB Output is correct
11 Correct 8 ms 12032 KB Output is correct
12 Correct 7 ms 12032 KB Output is correct
13 Correct 8 ms 12032 KB Output is correct
14 Correct 7 ms 12032 KB Output is correct
15 Correct 8 ms 12032 KB Output is correct
16 Correct 8 ms 12032 KB Output is correct
17 Correct 7 ms 12032 KB Output is correct
18 Correct 8 ms 12032 KB Output is correct
19 Correct 7 ms 12032 KB Output is correct
20 Correct 8 ms 12032 KB Output is correct
21 Correct 7 ms 12032 KB Output is correct
22 Correct 8 ms 12288 KB Output is correct
23 Correct 9 ms 12288 KB Output is correct
24 Correct 9 ms 12288 KB Output is correct
25 Correct 9 ms 12288 KB Output is correct
26 Correct 8 ms 12160 KB Output is correct
27 Correct 8 ms 12160 KB Output is correct
28 Correct 9 ms 12160 KB Output is correct
29 Correct 8 ms 12160 KB Output is correct
30 Correct 8 ms 12032 KB Output is correct
31 Correct 8 ms 12160 KB Output is correct
32 Correct 8 ms 12032 KB Output is correct
33 Correct 7 ms 12032 KB Output is correct
34 Correct 7 ms 12032 KB Output is correct
35 Correct 7 ms 12032 KB Output is correct
36 Correct 8 ms 12032 KB Output is correct
37 Correct 8 ms 12160 KB Output is correct
38 Correct 8 ms 12160 KB Output is correct
39 Correct 7 ms 12032 KB Output is correct
40 Correct 8 ms 12032 KB Output is correct
41 Correct 7 ms 12032 KB Output is correct
42 Correct 8 ms 12160 KB Output is correct
43 Correct 8 ms 12160 KB Output is correct
44 Correct 8 ms 12032 KB Output is correct
45 Correct 8 ms 12160 KB Output is correct
46 Correct 9 ms 12160 KB Output is correct
47 Correct 8 ms 12032 KB Output is correct
48 Correct 8 ms 12160 KB Output is correct
49 Correct 8 ms 12160 KB Output is correct
50 Correct 8 ms 12032 KB Output is correct
51 Correct 7 ms 12032 KB Output is correct
52 Correct 7 ms 12160 KB Output is correct
53 Correct 8 ms 12160 KB Output is correct
54 Correct 7 ms 12032 KB Output is correct
55 Correct 7 ms 12160 KB Output is correct
56 Correct 7 ms 12032 KB Output is correct
57 Correct 8 ms 12160 KB Output is correct
58 Correct 8 ms 12160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12032 KB Output is correct
2 Correct 7 ms 12032 KB Output is correct
3 Correct 8 ms 12032 KB Output is correct
4 Correct 8 ms 12160 KB Output is correct
5 Correct 7 ms 12032 KB Output is correct
6 Correct 7 ms 12032 KB Output is correct
7 Correct 7 ms 12032 KB Output is correct
8 Correct 7 ms 12032 KB Output is correct
9 Correct 7 ms 12032 KB Output is correct
10 Correct 7 ms 12032 KB Output is correct
11 Correct 8 ms 12032 KB Output is correct
12 Correct 7 ms 12032 KB Output is correct
13 Correct 8 ms 12032 KB Output is correct
14 Correct 7 ms 12032 KB Output is correct
15 Correct 8 ms 12032 KB Output is correct
16 Correct 8 ms 12032 KB Output is correct
17 Correct 7 ms 12032 KB Output is correct
18 Correct 8 ms 12032 KB Output is correct
19 Correct 7 ms 12032 KB Output is correct
20 Correct 8 ms 12032 KB Output is correct
21 Correct 7 ms 12032 KB Output is correct
22 Correct 8 ms 12288 KB Output is correct
23 Correct 9 ms 12288 KB Output is correct
24 Correct 9 ms 12288 KB Output is correct
25 Correct 9 ms 12288 KB Output is correct
26 Correct 8 ms 12160 KB Output is correct
27 Correct 8 ms 12160 KB Output is correct
28 Correct 9 ms 12160 KB Output is correct
29 Correct 8 ms 12160 KB Output is correct
30 Correct 8 ms 12032 KB Output is correct
31 Correct 8 ms 12160 KB Output is correct
32 Correct 8 ms 12032 KB Output is correct
33 Correct 7 ms 12032 KB Output is correct
34 Correct 7 ms 12032 KB Output is correct
35 Correct 7 ms 12032 KB Output is correct
36 Correct 8 ms 12032 KB Output is correct
37 Correct 8 ms 12160 KB Output is correct
38 Correct 8 ms 12160 KB Output is correct
39 Correct 7 ms 12032 KB Output is correct
40 Correct 8 ms 12032 KB Output is correct
41 Correct 7 ms 12032 KB Output is correct
42 Correct 8 ms 12160 KB Output is correct
43 Correct 8 ms 12160 KB Output is correct
44 Correct 8 ms 12032 KB Output is correct
45 Correct 8 ms 12160 KB Output is correct
46 Correct 9 ms 12160 KB Output is correct
47 Correct 8 ms 12032 KB Output is correct
48 Correct 8 ms 12160 KB Output is correct
49 Correct 8 ms 12160 KB Output is correct
50 Correct 8 ms 12032 KB Output is correct
51 Correct 7 ms 12032 KB Output is correct
52 Correct 7 ms 12160 KB Output is correct
53 Correct 8 ms 12160 KB Output is correct
54 Correct 7 ms 12032 KB Output is correct
55 Correct 7 ms 12160 KB Output is correct
56 Correct 7 ms 12032 KB Output is correct
57 Correct 8 ms 12160 KB Output is correct
58 Correct 8 ms 12160 KB Output is correct
59 Correct 498 ms 49448 KB Output is correct
60 Correct 538 ms 42736 KB Output is correct
61 Correct 7 ms 12032 KB Output is correct
62 Correct 7 ms 12032 KB Output is correct
63 Correct 438 ms 43364 KB Output is correct
64 Correct 10 ms 12800 KB Output is correct
65 Correct 20 ms 14976 KB Output is correct
66 Correct 192 ms 36628 KB Output is correct
67 Correct 641 ms 61560 KB Output is correct
68 Correct 618 ms 62728 KB Output is correct
69 Correct 18 ms 14012 KB Output is correct
70 Correct 135 ms 31876 KB Output is correct
71 Correct 493 ms 77512 KB Output is correct
72 Correct 502 ms 72648 KB Output is correct
73 Correct 106 ms 19952 KB Output is correct
74 Correct 537 ms 40744 KB Output is correct
75 Correct 41 ms 17456 KB Output is correct
76 Correct 465 ms 61520 KB Output is correct
77 Correct 451 ms 62280 KB Output is correct
78 Correct 37 ms 16512 KB Output is correct
79 Correct 619 ms 58104 KB Output is correct
80 Correct 10 ms 12504 KB Output is correct
81 Correct 149 ms 27876 KB Output is correct
82 Correct 532 ms 59616 KB Output is correct
83 Correct 526 ms 40740 KB Output is correct
84 Correct 535 ms 41196 KB Output is correct
85 Correct 528 ms 41212 KB Output is correct
86 Correct 521 ms 43176 KB Output is correct
87 Correct 513 ms 42996 KB Output is correct