Submission #31738

# Submission time Handle Problem Language Result Execution time Memory
31738 2017-09-03T07:12:09 Z kdh9949 Network (BOI15_net) C++14
100 / 100
1906 ms 128032 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

struct E{ int x, i; };

const int MN = 500005, inf = 1e9;
int n, m, vis[MN], pre[MN], p[MN], s[MN], cn[MN], rep[MN], chk[MN], cnt, low[MN];
vector<int> te[MN], se[MN], lf[MN];
vector<E> e[MN];
priority_queue<pii> pq;

void f(int x, int pv){
	pre[x] = ++cnt;
	low[x] = inf;
	vis[x] = 1;
	for(auto &i : e[x]){
		if(!vis[i.x]){
			te[x].push_back(i.x);
			p[i.x] = x;
			f(i.x, i.i);
		}
		else if(i.i != pv){
			low[x] = min(low[x], pre[i.x]);
		}
	}
}

int g(int x){
	int ret = low[x];
	for(auto &i : te[x]){
		int t = g(i);
		if(t > pre[x]) chk[i] = 1;
		ret = min(ret, t);
	}
	return ret;
}

void h(int x){
	cn[x] = cnt;
	for(auto &i : te[x]) if(!chk[i]) h(i);
}

int sf(int x, int p){
	s[x] = 0;
	for(auto &i : se[x]){
		if(i != p) s[x] += sf(i, x);
	}
	s[x] = max(s[x], 1);
	return s[x];
}

int cf(int x, int p, int t){
	for(auto &i : se[x]){
		if(i != p && s[i] > t / 2) return cf(i, x, t);
	}
	return x;
}

void l(vector<int> &v, int x, int p){
	int fl = 1;
	for(auto &i : se[x]){
		if(i == p) continue;
		fl = 0; l(v, i, x);
	}
	if(fl) v.push_back(x);
}

int main(){
	scanf("%d", &n);
	m = n - 1;
	for(int i = 1, x, y; i <= m; i++){
		scanf("%d%d", &x, &y);
		e[x].push_back({y, i});
		e[y].push_back({x, i});
	}
	f(1, -1);
	g(1);
	//for(int i = 1; i <= n; i++) if(chk[i]) printf("%d - %d\n", i, p[i]);
	cnt = 1; h(1); rep[1] = 1;
	for(int i = 2; i <= n; i++){
		if(chk[i]){
			cnt++;
			h(i);
			rep[cnt] = i;
		}
	}
	for(int i = 2; i <= n; i++){
		if(chk[i]){
			se[cn[p[i]]].push_back(cn[i]);
			se[cn[i]].push_back(cn[p[i]]);
		}
	}
	//puts("--");
	//for(int i = 1; i <= n; i++) printf("%d : %d\n", i, cn[i]);
	if(cnt == 1){ puts("0"); return 0; }
	if(cnt == 2){ printf("1\n%d %d\n", rep[1], rep[2]); return 0; }
	int r;
	for(int i = 1; i <= cnt; i++){
		if(se[i].size() > 1){
			r = cf(i, 0, sf(i, 0));
			break;
		}
	}
	cnt = 0;
	//puts("--");
	for(int i = 0; i < se[r].size(); i++){
		l(lf[i], se[r][i], r);
		pq.push({lf[i].size(), i});
		//printf("%d : %d\n", i, lf[i].size());
		cnt += lf[i].size();
	}
	//puts("--");
	int fl = cnt % 2;
	printf("%d\n", (cnt + 1) / 2);
	while(pq.size() > 1){
		pii a = pq.top(); pq.pop();
		pii b = pq.top(); pq.pop();
		printf("%d %d\n", rep[lf[a.second].back()], rep[lf[b.second].back()]);
		lf[a.second].pop_back();
		if(!fl) lf[b.second].pop_back();
		else fl = 0;
		if(lf[a.second].size()) pq.push({lf[a.second].size(), a.second});
		if(lf[b.second].size()) pq.push({lf[b.second].size(), b.second});
	}
}

Compilation message

net.cpp: In function 'int main()':
net.cpp:107:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < se[r].size(); i++){
                   ^
net.cpp:70:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
net.cpp:73:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &x, &y);
                        ^
net.cpp:98:6: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
  int r;
      ^
# Verdict Execution time Memory Grader output
1 Correct 9 ms 64520 KB Output is correct
2 Correct 6 ms 64520 KB Output is correct
3 Correct 19 ms 64520 KB Output is correct
4 Correct 23 ms 64520 KB Output is correct
5 Correct 9 ms 64520 KB Output is correct
6 Correct 13 ms 64520 KB Output is correct
7 Correct 9 ms 64520 KB Output is correct
8 Correct 16 ms 64520 KB Output is correct
9 Correct 19 ms 64520 KB Output is correct
10 Correct 9 ms 64520 KB Output is correct
11 Correct 13 ms 64520 KB Output is correct
12 Correct 13 ms 64520 KB Output is correct
13 Correct 9 ms 64520 KB Output is correct
14 Correct 16 ms 64520 KB Output is correct
15 Correct 19 ms 64520 KB Output is correct
16 Correct 9 ms 64520 KB Output is correct
17 Correct 16 ms 64520 KB Output is correct
18 Correct 13 ms 64520 KB Output is correct
19 Correct 9 ms 64520 KB Output is correct
20 Correct 19 ms 64520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 64520 KB Output is correct
2 Correct 6 ms 64520 KB Output is correct
3 Correct 19 ms 64520 KB Output is correct
4 Correct 23 ms 64520 KB Output is correct
5 Correct 9 ms 64520 KB Output is correct
6 Correct 13 ms 64520 KB Output is correct
7 Correct 9 ms 64520 KB Output is correct
8 Correct 16 ms 64520 KB Output is correct
9 Correct 19 ms 64520 KB Output is correct
10 Correct 9 ms 64520 KB Output is correct
11 Correct 13 ms 64520 KB Output is correct
12 Correct 13 ms 64520 KB Output is correct
13 Correct 9 ms 64520 KB Output is correct
14 Correct 16 ms 64520 KB Output is correct
15 Correct 19 ms 64520 KB Output is correct
16 Correct 9 ms 64520 KB Output is correct
17 Correct 16 ms 64520 KB Output is correct
18 Correct 13 ms 64520 KB Output is correct
19 Correct 9 ms 64520 KB Output is correct
20 Correct 19 ms 64520 KB Output is correct
21 Correct 9 ms 64520 KB Output is correct
22 Correct 13 ms 64784 KB Output is correct
23 Correct 13 ms 64784 KB Output is correct
24 Correct 16 ms 64784 KB Output is correct
25 Correct 16 ms 64784 KB Output is correct
26 Correct 13 ms 64652 KB Output is correct
27 Correct 9 ms 64652 KB Output is correct
28 Correct 16 ms 64652 KB Output is correct
29 Correct 19 ms 64652 KB Output is correct
30 Correct 19 ms 64520 KB Output is correct
31 Correct 13 ms 64652 KB Output is correct
32 Correct 13 ms 64520 KB Output is correct
33 Correct 16 ms 64520 KB Output is correct
34 Correct 13 ms 64520 KB Output is correct
35 Correct 19 ms 64520 KB Output is correct
36 Correct 16 ms 64520 KB Output is correct
37 Correct 23 ms 64652 KB Output is correct
38 Correct 16 ms 64652 KB Output is correct
39 Correct 9 ms 64520 KB Output is correct
40 Correct 13 ms 64520 KB Output is correct
41 Correct 19 ms 64520 KB Output is correct
42 Correct 16 ms 64652 KB Output is correct
43 Correct 9 ms 64652 KB Output is correct
44 Correct 13 ms 64520 KB Output is correct
45 Correct 13 ms 64520 KB Output is correct
46 Correct 6 ms 64652 KB Output is correct
47 Correct 13 ms 64520 KB Output is correct
48 Correct 6 ms 64520 KB Output is correct
49 Correct 6 ms 64652 KB Output is correct
50 Correct 9 ms 64520 KB Output is correct
51 Correct 13 ms 64520 KB Output is correct
52 Correct 9 ms 64520 KB Output is correct
53 Correct 19 ms 64652 KB Output is correct
54 Correct 9 ms 64520 KB Output is correct
55 Correct 9 ms 64520 KB Output is correct
56 Correct 13 ms 64520 KB Output is correct
57 Correct 19 ms 64652 KB Output is correct
58 Correct 16 ms 64784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 64520 KB Output is correct
2 Correct 6 ms 64520 KB Output is correct
3 Correct 19 ms 64520 KB Output is correct
4 Correct 23 ms 64520 KB Output is correct
5 Correct 9 ms 64520 KB Output is correct
6 Correct 13 ms 64520 KB Output is correct
7 Correct 9 ms 64520 KB Output is correct
8 Correct 16 ms 64520 KB Output is correct
9 Correct 19 ms 64520 KB Output is correct
10 Correct 9 ms 64520 KB Output is correct
11 Correct 13 ms 64520 KB Output is correct
12 Correct 13 ms 64520 KB Output is correct
13 Correct 9 ms 64520 KB Output is correct
14 Correct 16 ms 64520 KB Output is correct
15 Correct 19 ms 64520 KB Output is correct
16 Correct 9 ms 64520 KB Output is correct
17 Correct 16 ms 64520 KB Output is correct
18 Correct 13 ms 64520 KB Output is correct
19 Correct 9 ms 64520 KB Output is correct
20 Correct 19 ms 64520 KB Output is correct
21 Correct 9 ms 64520 KB Output is correct
22 Correct 13 ms 64784 KB Output is correct
23 Correct 13 ms 64784 KB Output is correct
24 Correct 16 ms 64784 KB Output is correct
25 Correct 16 ms 64784 KB Output is correct
26 Correct 13 ms 64652 KB Output is correct
27 Correct 9 ms 64652 KB Output is correct
28 Correct 16 ms 64652 KB Output is correct
29 Correct 19 ms 64652 KB Output is correct
30 Correct 19 ms 64520 KB Output is correct
31 Correct 13 ms 64652 KB Output is correct
32 Correct 13 ms 64520 KB Output is correct
33 Correct 16 ms 64520 KB Output is correct
34 Correct 13 ms 64520 KB Output is correct
35 Correct 19 ms 64520 KB Output is correct
36 Correct 16 ms 64520 KB Output is correct
37 Correct 23 ms 64652 KB Output is correct
38 Correct 16 ms 64652 KB Output is correct
39 Correct 9 ms 64520 KB Output is correct
40 Correct 13 ms 64520 KB Output is correct
41 Correct 19 ms 64520 KB Output is correct
42 Correct 16 ms 64652 KB Output is correct
43 Correct 9 ms 64652 KB Output is correct
44 Correct 13 ms 64520 KB Output is correct
45 Correct 13 ms 64520 KB Output is correct
46 Correct 6 ms 64652 KB Output is correct
47 Correct 13 ms 64520 KB Output is correct
48 Correct 6 ms 64520 KB Output is correct
49 Correct 6 ms 64652 KB Output is correct
50 Correct 9 ms 64520 KB Output is correct
51 Correct 13 ms 64520 KB Output is correct
52 Correct 9 ms 64520 KB Output is correct
53 Correct 19 ms 64652 KB Output is correct
54 Correct 9 ms 64520 KB Output is correct
55 Correct 9 ms 64520 KB Output is correct
56 Correct 13 ms 64520 KB Output is correct
57 Correct 19 ms 64652 KB Output is correct
58 Correct 16 ms 64784 KB Output is correct
59 Correct 1336 ms 110948 KB Output is correct
60 Correct 1586 ms 107852 KB Output is correct
61 Correct 13 ms 64520 KB Output is correct
62 Correct 9 ms 64520 KB Output is correct
63 Correct 1209 ms 100476 KB Output is correct
64 Correct 23 ms 65232 KB Output is correct
65 Correct 33 ms 67772 KB Output is correct
66 Correct 503 ms 91912 KB Output is correct
67 Correct 1833 ms 128012 KB Output is correct
68 Correct 1903 ms 128032 KB Output is correct
69 Correct 26 ms 66312 KB Output is correct
70 Correct 253 ms 86376 KB Output is correct
71 Correct 826 ms 123760 KB Output is correct
72 Correct 976 ms 123628 KB Output is correct
73 Correct 253 ms 76876 KB Output is correct
74 Correct 1536 ms 109256 KB Output is correct
75 Correct 66 ms 70364 KB Output is correct
76 Correct 866 ms 115184 KB Output is correct
77 Correct 809 ms 115320 KB Output is correct
78 Correct 109 ms 70552 KB Output is correct
79 Correct 1906 ms 125688 KB Output is correct
80 Correct 16 ms 64916 KB Output is correct
81 Correct 356 ms 83164 KB Output is correct
82 Correct 1519 ms 116164 KB Output is correct
83 Correct 1696 ms 109436 KB Output is correct
84 Correct 1713 ms 109196 KB Output is correct
85 Correct 1666 ms 109612 KB Output is correct
86 Correct 1466 ms 109416 KB Output is correct
87 Correct 1456 ms 108416 KB Output is correct