Submission #711678

# Submission time Handle Problem Language Result Execution time Memory
711678 2023-03-17T11:04:12 Z Pherokung Network (BOI15_net) C++14
100 / 100
472 ms 65396 KB
#include<bits/stdc++.h>
using namespace std;
#define MAXN 500005
#define pb push_back
#define F first
#define S second
typedef pair<int,int> pa;
int n,u,v,top;
vector<int> adj[MAXN];
vector<pa> ans;

pa merge(pa A, pa B){
	if(A == make_pair(-1,-1)) return B;
	if(B == make_pair(-1,-1)) return A;
	if(A.S == - 1 && B.S == -1) return {A.F,B.F};
	if(A.S != -1 && B.S != -1){
		ans.push_back({A.S,B.S});
		return {A.F,B.F};
	}
	if(A.S != -1){
		ans.push_back({A.S,B.F});
		return {A.F,-1};
	}
	if(B.S != -1){
		ans.push_back({B.S,A.F});
		return {B.F,-1};
	}
}

pa dfs(int u,int p){
	pa val = (adj[u][0] == p && adj[u].size() == 1) ? make_pair(u,-1) : make_pair(-1,-1);
	for(auto v : adj[u]){
		if(v == p) continue;
		val = merge(val,dfs(v,u));
	}
	//printf("!! %d : %d %d\n",u,val.F,val.S);
	return val;
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		scanf("%d%d",&u,&v);
		adj[u].pb(v), adj[v].pb(u);
	}
	
	for(int i=1;i<=n;i++) if(adj[i].size() > 1) top = i;
	pa val = dfs(top,top);
	if(val.F != -1 && val.S != -1) ans.push_back({val.F,val.S});
	else if(val.F != -1) ans.push_back({val.F,top});
	
	printf("%d\n",ans.size());
	for(auto x : ans) printf("%d %d\n",x.F,x.S);
}

Compilation message

net.cpp: In function 'int main()':
net.cpp:52:11: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wformat=]
   52 |  printf("%d\n",ans.size());
      |          ~^    ~~~~~~~~~~
      |           |            |
      |           int          std::vector<std::pair<int, int> >::size_type {aka long unsigned int}
      |          %ld
net.cpp: In function 'pa merge(pa, pa)':
net.cpp:28:1: warning: control reaches end of non-void function [-Wreturn-type]
   28 | }
      | ^
net.cpp: In function 'int main()':
net.cpp:41:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
net.cpp:43:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |   scanf("%d%d",&u,&v);
      |   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11988 KB Output is correct
2 Correct 7 ms 12044 KB Output is correct
3 Correct 7 ms 12044 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 11988 KB Output is correct
7 Correct 7 ms 11988 KB Output is correct
8 Correct 7 ms 12028 KB Output is correct
9 Correct 6 ms 12156 KB Output is correct
10 Correct 7 ms 12044 KB Output is correct
11 Correct 7 ms 11988 KB Output is correct
12 Correct 7 ms 11988 KB Output is correct
13 Correct 8 ms 12048 KB Output is correct
14 Correct 7 ms 11988 KB Output is correct
15 Correct 8 ms 11988 KB Output is correct
16 Correct 7 ms 12016 KB Output is correct
17 Correct 7 ms 11948 KB Output is correct
18 Correct 7 ms 11968 KB Output is correct
19 Correct 7 ms 12044 KB Output is correct
20 Correct 7 ms 12044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11988 KB Output is correct
2 Correct 7 ms 12044 KB Output is correct
3 Correct 7 ms 12044 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 11988 KB Output is correct
7 Correct 7 ms 11988 KB Output is correct
8 Correct 7 ms 12028 KB Output is correct
9 Correct 6 ms 12156 KB Output is correct
10 Correct 7 ms 12044 KB Output is correct
11 Correct 7 ms 11988 KB Output is correct
12 Correct 7 ms 11988 KB Output is correct
13 Correct 8 ms 12048 KB Output is correct
14 Correct 7 ms 11988 KB Output is correct
15 Correct 8 ms 11988 KB Output is correct
16 Correct 7 ms 12016 KB Output is correct
17 Correct 7 ms 11948 KB Output is correct
18 Correct 7 ms 11968 KB Output is correct
19 Correct 7 ms 12044 KB Output is correct
20 Correct 7 ms 12044 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 8 ms 12140 KB Output is correct
23 Correct 7 ms 12148 KB Output is correct
24 Correct 9 ms 12116 KB Output is correct
25 Correct 9 ms 12056 KB Output is correct
26 Correct 9 ms 12116 KB Output is correct
27 Correct 7 ms 12116 KB Output is correct
28 Correct 8 ms 12068 KB Output is correct
29 Correct 7 ms 12052 KB Output is correct
30 Correct 6 ms 12040 KB Output is correct
31 Correct 7 ms 12040 KB Output is correct
32 Correct 7 ms 11988 KB Output is correct
33 Correct 6 ms 11960 KB Output is correct
34 Correct 7 ms 11988 KB Output is correct
35 Correct 6 ms 12036 KB Output is correct
36 Correct 6 ms 11988 KB Output is correct
37 Correct 6 ms 12084 KB Output is correct
38 Correct 7 ms 12116 KB Output is correct
39 Correct 8 ms 12044 KB Output is correct
40 Correct 8 ms 11988 KB Output is correct
41 Correct 6 ms 12048 KB Output is correct
42 Correct 7 ms 12048 KB Output is correct
43 Correct 7 ms 11988 KB Output is correct
44 Correct 7 ms 11988 KB Output is correct
45 Correct 7 ms 11988 KB Output is correct
46 Correct 7 ms 12064 KB Output is correct
47 Correct 7 ms 11988 KB Output is correct
48 Correct 7 ms 12044 KB Output is correct
49 Correct 7 ms 12060 KB Output is correct
50 Correct 6 ms 11988 KB Output is correct
51 Correct 8 ms 11988 KB Output is correct
52 Correct 7 ms 11988 KB Output is correct
53 Correct 8 ms 11988 KB Output is correct
54 Correct 7 ms 11988 KB Output is correct
55 Correct 7 ms 11960 KB Output is correct
56 Correct 7 ms 12044 KB Output is correct
57 Correct 7 ms 11988 KB Output is correct
58 Correct 7 ms 12056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11988 KB Output is correct
2 Correct 7 ms 12044 KB Output is correct
3 Correct 7 ms 12044 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 11988 KB Output is correct
7 Correct 7 ms 11988 KB Output is correct
8 Correct 7 ms 12028 KB Output is correct
9 Correct 6 ms 12156 KB Output is correct
10 Correct 7 ms 12044 KB Output is correct
11 Correct 7 ms 11988 KB Output is correct
12 Correct 7 ms 11988 KB Output is correct
13 Correct 8 ms 12048 KB Output is correct
14 Correct 7 ms 11988 KB Output is correct
15 Correct 8 ms 11988 KB Output is correct
16 Correct 7 ms 12016 KB Output is correct
17 Correct 7 ms 11948 KB Output is correct
18 Correct 7 ms 11968 KB Output is correct
19 Correct 7 ms 12044 KB Output is correct
20 Correct 7 ms 12044 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 8 ms 12140 KB Output is correct
23 Correct 7 ms 12148 KB Output is correct
24 Correct 9 ms 12116 KB Output is correct
25 Correct 9 ms 12056 KB Output is correct
26 Correct 9 ms 12116 KB Output is correct
27 Correct 7 ms 12116 KB Output is correct
28 Correct 8 ms 12068 KB Output is correct
29 Correct 7 ms 12052 KB Output is correct
30 Correct 6 ms 12040 KB Output is correct
31 Correct 7 ms 12040 KB Output is correct
32 Correct 7 ms 11988 KB Output is correct
33 Correct 6 ms 11960 KB Output is correct
34 Correct 7 ms 11988 KB Output is correct
35 Correct 6 ms 12036 KB Output is correct
36 Correct 6 ms 11988 KB Output is correct
37 Correct 6 ms 12084 KB Output is correct
38 Correct 7 ms 12116 KB Output is correct
39 Correct 8 ms 12044 KB Output is correct
40 Correct 8 ms 11988 KB Output is correct
41 Correct 6 ms 12048 KB Output is correct
42 Correct 7 ms 12048 KB Output is correct
43 Correct 7 ms 11988 KB Output is correct
44 Correct 7 ms 11988 KB Output is correct
45 Correct 7 ms 11988 KB Output is correct
46 Correct 7 ms 12064 KB Output is correct
47 Correct 7 ms 11988 KB Output is correct
48 Correct 7 ms 12044 KB Output is correct
49 Correct 7 ms 12060 KB Output is correct
50 Correct 6 ms 11988 KB Output is correct
51 Correct 8 ms 11988 KB Output is correct
52 Correct 7 ms 11988 KB Output is correct
53 Correct 8 ms 11988 KB Output is correct
54 Correct 7 ms 11988 KB Output is correct
55 Correct 7 ms 11960 KB Output is correct
56 Correct 7 ms 12044 KB Output is correct
57 Correct 7 ms 11988 KB Output is correct
58 Correct 7 ms 12056 KB Output is correct
59 Correct 338 ms 35960 KB Output is correct
60 Correct 415 ms 38668 KB Output is correct
61 Correct 6 ms 12044 KB Output is correct
62 Correct 7 ms 12036 KB Output is correct
63 Correct 290 ms 33528 KB Output is correct
64 Correct 9 ms 12416 KB Output is correct
65 Correct 16 ms 13908 KB Output is correct
66 Correct 125 ms 26812 KB Output is correct
67 Correct 466 ms 65396 KB Output is correct
68 Correct 472 ms 56024 KB Output is correct
69 Correct 13 ms 12832 KB Output is correct
70 Correct 88 ms 21168 KB Output is correct
71 Correct 330 ms 41796 KB Output is correct
72 Correct 313 ms 40596 KB Output is correct
73 Correct 90 ms 19012 KB Output is correct
74 Correct 441 ms 37560 KB Output is correct
75 Correct 32 ms 15212 KB Output is correct
76 Correct 338 ms 41452 KB Output is correct
77 Correct 309 ms 41596 KB Output is correct
78 Correct 46 ms 15616 KB Output is correct
79 Correct 422 ms 48612 KB Output is correct
80 Correct 11 ms 12184 KB Output is correct
81 Correct 98 ms 21176 KB Output is correct
82 Correct 429 ms 39416 KB Output is correct
83 Correct 447 ms 37592 KB Output is correct
84 Correct 416 ms 37656 KB Output is correct
85 Correct 441 ms 37656 KB Output is correct
86 Correct 414 ms 38740 KB Output is correct
87 Correct 400 ms 38784 KB Output is correct