Submission #105567

# Submission time Handle Problem Language Result Execution time Memory
105567 2019-04-13T13:48:13 Z Pro_ktmr Mergers (JOI19_mergers) C++14
100 / 100
1993 ms 102648 KB
#include"bits/stdc++.h"
using namespace std;
#define LL long long
#define MP make_pair
#define PB push_back

struct UnionFind{
private:
	int N;
	vector<int> par;
public:
	void init(int n){
		N = n;
		par.clear();
		for(int i=0; i<N; i++) par.PB(i);
	}
	int root(int i){
		if(par[i] == i) return i;
		return par[i] = root(par[i]);
	}
	void unite(int i, int j){
		i = root(i);
		j = root(j);
		if(i == j) return;
		par[j] = i;
	}
	bool same(int i, int j){
		return (root(i) == root(j));
	}
};

int N,K;
pair<int,int> E[500000];
vector<int> edge[500000];
vector<int> node[500000];

int par[500000];
bool visit[500000] = {};
void dfs(int now){
	visit[now] = true;
	for(int i=0; i<edge[now].size(); i++){
		if(visit[edge[now][i]]) continue;
		par[edge[now][i]] = now;
		dfs(edge[now][i]);
	}
}
int d[500000] = {};
int depth(int i){
	if(i == 0) return 0;
	if(d[i] != 0) return d[i];
	return d[i] = depth(par[i])+1;
}

UnionFind uf;
vector<int> newEdge[500000];
int main(){
	cin >> N >> K;
	for(int i=0; i<N-1; i++){
		int A,B;
		cin >> A >> B;
		A--; B--;
		edge[A].PB(B);
		edge[B].PB(A);
		E[i] = MP(A,B);
	}
	par[0] = 0;
	dfs(0);
	for(int i=0; i<N; i++){
		int S;
		cin >> S;
		S--;
		node[S].PB(i);
	}
	
	//
	
	uf.init(N);
	for(int i=0; i<K; i++){
		if(node[i].size() == 0) continue;
		int now = uf.root(node[i][0]);
		for(int j=1; j<node[i].size(); j++){
			int tmp = uf.root(node[i][j]);
			while(now != tmp){
				if(depth(now) > depth(tmp)){
					uf.unite(par[now],now);
					now = uf.root(now);
				}
				else{
					uf.unite(par[tmp],tmp);
					tmp = uf.root(tmp);
				}
			}
		}
	}
	
	//
	
	for(int i=0; i<N-1; i++){
		if(uf.same(E[i].first, E[i].second)) continue;
		newEdge[uf.root(E[i].first)].PB(uf.root(E[i].second));
		newEdge[uf.root(E[i].second)].PB(uf.root(E[i].first));
	}
	int ans = 0;
	for(int i=0; i<N; i++){
		if(newEdge[i].size() == 1) ans++;
	}
	cout << (ans+1)/2 << endl;
	
	return 0;
}

Compilation message

mergers.cpp: In function 'void dfs(int)':
mergers.cpp:41:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<edge[now].size(); i++){
               ~^~~~~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:81:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=1; j<node[i].size(); j++){
                ~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 38 ms 35576 KB Output is correct
2 Correct 38 ms 35500 KB Output is correct
3 Correct 40 ms 35576 KB Output is correct
4 Correct 35 ms 35576 KB Output is correct
5 Correct 39 ms 35576 KB Output is correct
6 Correct 39 ms 35576 KB Output is correct
7 Correct 42 ms 35524 KB Output is correct
8 Correct 35 ms 35584 KB Output is correct
9 Correct 37 ms 35576 KB Output is correct
10 Correct 35 ms 35576 KB Output is correct
11 Correct 37 ms 35576 KB Output is correct
12 Correct 37 ms 35576 KB Output is correct
13 Correct 34 ms 35576 KB Output is correct
14 Correct 36 ms 35576 KB Output is correct
15 Correct 42 ms 35576 KB Output is correct
16 Correct 39 ms 35576 KB Output is correct
17 Correct 35 ms 35584 KB Output is correct
18 Correct 38 ms 35576 KB Output is correct
19 Correct 38 ms 35576 KB Output is correct
20 Correct 36 ms 35584 KB Output is correct
21 Correct 42 ms 35552 KB Output is correct
22 Correct 34 ms 35576 KB Output is correct
23 Correct 35 ms 35556 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 35576 KB Output is correct
2 Correct 38 ms 35500 KB Output is correct
3 Correct 40 ms 35576 KB Output is correct
4 Correct 35 ms 35576 KB Output is correct
5 Correct 39 ms 35576 KB Output is correct
6 Correct 39 ms 35576 KB Output is correct
7 Correct 42 ms 35524 KB Output is correct
8 Correct 35 ms 35584 KB Output is correct
9 Correct 37 ms 35576 KB Output is correct
10 Correct 35 ms 35576 KB Output is correct
11 Correct 37 ms 35576 KB Output is correct
12 Correct 37 ms 35576 KB Output is correct
13 Correct 34 ms 35576 KB Output is correct
14 Correct 36 ms 35576 KB Output is correct
15 Correct 42 ms 35576 KB Output is correct
16 Correct 39 ms 35576 KB Output is correct
17 Correct 35 ms 35584 KB Output is correct
18 Correct 38 ms 35576 KB Output is correct
19 Correct 38 ms 35576 KB Output is correct
20 Correct 36 ms 35584 KB Output is correct
21 Correct 42 ms 35552 KB Output is correct
22 Correct 34 ms 35576 KB Output is correct
23 Correct 35 ms 35556 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
25 Correct 41 ms 35548 KB Output is correct
26 Correct 41 ms 35892 KB Output is correct
27 Correct 43 ms 35836 KB Output is correct
28 Correct 39 ms 35964 KB Output is correct
29 Correct 40 ms 35960 KB Output is correct
30 Correct 40 ms 35812 KB Output is correct
31 Correct 36 ms 35576 KB Output is correct
32 Correct 38 ms 35968 KB Output is correct
33 Correct 37 ms 35584 KB Output is correct
34 Correct 40 ms 35708 KB Output is correct
35 Correct 42 ms 35960 KB Output is correct
36 Correct 48 ms 35704 KB Output is correct
37 Correct 40 ms 35804 KB Output is correct
38 Correct 34 ms 35584 KB Output is correct
39 Correct 40 ms 35832 KB Output is correct
40 Correct 40 ms 35712 KB Output is correct
41 Correct 43 ms 35848 KB Output is correct
42 Correct 41 ms 35836 KB Output is correct
43 Correct 50 ms 35832 KB Output is correct
44 Correct 35 ms 35576 KB Output is correct
45 Correct 125 ms 35832 KB Output is correct
46 Correct 46 ms 35960 KB Output is correct
47 Correct 37 ms 35576 KB Output is correct
48 Correct 43 ms 35808 KB Output is correct
49 Correct 39 ms 35960 KB Output is correct
50 Correct 43 ms 35960 KB Output is correct
51 Correct 43 ms 35832 KB Output is correct
52 Correct 41 ms 35832 KB Output is correct
53 Correct 42 ms 35868 KB Output is correct
54 Correct 47 ms 35812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 35576 KB Output is correct
2 Correct 38 ms 35500 KB Output is correct
3 Correct 40 ms 35576 KB Output is correct
4 Correct 35 ms 35576 KB Output is correct
5 Correct 39 ms 35576 KB Output is correct
6 Correct 39 ms 35576 KB Output is correct
7 Correct 42 ms 35524 KB Output is correct
8 Correct 35 ms 35584 KB Output is correct
9 Correct 37 ms 35576 KB Output is correct
10 Correct 35 ms 35576 KB Output is correct
11 Correct 37 ms 35576 KB Output is correct
12 Correct 37 ms 35576 KB Output is correct
13 Correct 34 ms 35576 KB Output is correct
14 Correct 36 ms 35576 KB Output is correct
15 Correct 42 ms 35576 KB Output is correct
16 Correct 39 ms 35576 KB Output is correct
17 Correct 35 ms 35584 KB Output is correct
18 Correct 38 ms 35576 KB Output is correct
19 Correct 38 ms 35576 KB Output is correct
20 Correct 36 ms 35584 KB Output is correct
21 Correct 42 ms 35552 KB Output is correct
22 Correct 34 ms 35576 KB Output is correct
23 Correct 35 ms 35556 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
25 Correct 36 ms 35524 KB Output is correct
26 Correct 164 ms 42648 KB Output is correct
27 Correct 225 ms 42268 KB Output is correct
28 Correct 40 ms 35752 KB Output is correct
29 Correct 41 ms 35576 KB Output is correct
30 Correct 38 ms 35576 KB Output is correct
31 Correct 216 ms 42152 KB Output is correct
32 Correct 94 ms 35832 KB Output is correct
33 Correct 249 ms 45040 KB Output is correct
34 Correct 201 ms 42520 KB Output is correct
35 Correct 42 ms 35704 KB Output is correct
36 Correct 229 ms 42648 KB Output is correct
37 Correct 40 ms 35832 KB Output is correct
38 Correct 40 ms 35704 KB Output is correct
39 Correct 173 ms 42620 KB Output is correct
40 Correct 48 ms 35832 KB Output is correct
41 Correct 205 ms 42356 KB Output is correct
42 Correct 214 ms 43248 KB Output is correct
43 Correct 36 ms 35584 KB Output is correct
44 Correct 205 ms 45300 KB Output is correct
45 Correct 213 ms 43780 KB Output is correct
46 Correct 49 ms 35832 KB Output is correct
47 Correct 42 ms 35704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 228 ms 42848 KB Output is correct
2 Correct 226 ms 48744 KB Output is correct
3 Correct 40 ms 35832 KB Output is correct
4 Correct 40 ms 35832 KB Output is correct
5 Correct 43 ms 35584 KB Output is correct
6 Correct 37 ms 35576 KB Output is correct
7 Correct 39 ms 35832 KB Output is correct
8 Correct 266 ms 43580 KB Output is correct
9 Correct 49 ms 35692 KB Output is correct
10 Correct 224 ms 42420 KB Output is correct
11 Correct 33 ms 35628 KB Output is correct
12 Correct 306 ms 42420 KB Output is correct
13 Correct 273 ms 43944 KB Output is correct
14 Correct 294 ms 47728 KB Output is correct
15 Correct 165 ms 42880 KB Output is correct
16 Correct 38 ms 35864 KB Output is correct
17 Correct 42 ms 35576 KB Output is correct
18 Correct 270 ms 47780 KB Output is correct
19 Correct 303 ms 49232 KB Output is correct
20 Correct 40 ms 35832 KB Output is correct
21 Correct 36 ms 35584 KB Output is correct
22 Correct 223 ms 44400 KB Output is correct
23 Correct 44 ms 35832 KB Output is correct
24 Correct 250 ms 42896 KB Output is correct
25 Correct 322 ms 48636 KB Output is correct
26 Correct 43 ms 35832 KB Output is correct
27 Correct 47 ms 35936 KB Output is correct
28 Correct 41 ms 35764 KB Output is correct
29 Correct 39 ms 35840 KB Output is correct
30 Correct 44 ms 35836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 35576 KB Output is correct
2 Correct 38 ms 35500 KB Output is correct
3 Correct 40 ms 35576 KB Output is correct
4 Correct 35 ms 35576 KB Output is correct
5 Correct 39 ms 35576 KB Output is correct
6 Correct 39 ms 35576 KB Output is correct
7 Correct 42 ms 35524 KB Output is correct
8 Correct 35 ms 35584 KB Output is correct
9 Correct 37 ms 35576 KB Output is correct
10 Correct 35 ms 35576 KB Output is correct
11 Correct 37 ms 35576 KB Output is correct
12 Correct 37 ms 35576 KB Output is correct
13 Correct 34 ms 35576 KB Output is correct
14 Correct 36 ms 35576 KB Output is correct
15 Correct 42 ms 35576 KB Output is correct
16 Correct 39 ms 35576 KB Output is correct
17 Correct 35 ms 35584 KB Output is correct
18 Correct 38 ms 35576 KB Output is correct
19 Correct 38 ms 35576 KB Output is correct
20 Correct 36 ms 35584 KB Output is correct
21 Correct 42 ms 35552 KB Output is correct
22 Correct 34 ms 35576 KB Output is correct
23 Correct 35 ms 35556 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
25 Correct 41 ms 35548 KB Output is correct
26 Correct 41 ms 35892 KB Output is correct
27 Correct 43 ms 35836 KB Output is correct
28 Correct 39 ms 35964 KB Output is correct
29 Correct 40 ms 35960 KB Output is correct
30 Correct 40 ms 35812 KB Output is correct
31 Correct 36 ms 35576 KB Output is correct
32 Correct 38 ms 35968 KB Output is correct
33 Correct 37 ms 35584 KB Output is correct
34 Correct 40 ms 35708 KB Output is correct
35 Correct 42 ms 35960 KB Output is correct
36 Correct 48 ms 35704 KB Output is correct
37 Correct 40 ms 35804 KB Output is correct
38 Correct 34 ms 35584 KB Output is correct
39 Correct 40 ms 35832 KB Output is correct
40 Correct 40 ms 35712 KB Output is correct
41 Correct 43 ms 35848 KB Output is correct
42 Correct 41 ms 35836 KB Output is correct
43 Correct 50 ms 35832 KB Output is correct
44 Correct 35 ms 35576 KB Output is correct
45 Correct 125 ms 35832 KB Output is correct
46 Correct 46 ms 35960 KB Output is correct
47 Correct 37 ms 35576 KB Output is correct
48 Correct 43 ms 35808 KB Output is correct
49 Correct 39 ms 35960 KB Output is correct
50 Correct 43 ms 35960 KB Output is correct
51 Correct 43 ms 35832 KB Output is correct
52 Correct 41 ms 35832 KB Output is correct
53 Correct 42 ms 35868 KB Output is correct
54 Correct 47 ms 35812 KB Output is correct
55 Correct 36 ms 35524 KB Output is correct
56 Correct 164 ms 42648 KB Output is correct
57 Correct 225 ms 42268 KB Output is correct
58 Correct 40 ms 35752 KB Output is correct
59 Correct 41 ms 35576 KB Output is correct
60 Correct 38 ms 35576 KB Output is correct
61 Correct 216 ms 42152 KB Output is correct
62 Correct 94 ms 35832 KB Output is correct
63 Correct 249 ms 45040 KB Output is correct
64 Correct 201 ms 42520 KB Output is correct
65 Correct 42 ms 35704 KB Output is correct
66 Correct 229 ms 42648 KB Output is correct
67 Correct 40 ms 35832 KB Output is correct
68 Correct 40 ms 35704 KB Output is correct
69 Correct 173 ms 42620 KB Output is correct
70 Correct 48 ms 35832 KB Output is correct
71 Correct 205 ms 42356 KB Output is correct
72 Correct 214 ms 43248 KB Output is correct
73 Correct 36 ms 35584 KB Output is correct
74 Correct 205 ms 45300 KB Output is correct
75 Correct 213 ms 43780 KB Output is correct
76 Correct 49 ms 35832 KB Output is correct
77 Correct 42 ms 35704 KB Output is correct
78 Correct 228 ms 42848 KB Output is correct
79 Correct 226 ms 48744 KB Output is correct
80 Correct 40 ms 35832 KB Output is correct
81 Correct 40 ms 35832 KB Output is correct
82 Correct 43 ms 35584 KB Output is correct
83 Correct 37 ms 35576 KB Output is correct
84 Correct 39 ms 35832 KB Output is correct
85 Correct 266 ms 43580 KB Output is correct
86 Correct 49 ms 35692 KB Output is correct
87 Correct 224 ms 42420 KB Output is correct
88 Correct 33 ms 35628 KB Output is correct
89 Correct 306 ms 42420 KB Output is correct
90 Correct 273 ms 43944 KB Output is correct
91 Correct 294 ms 47728 KB Output is correct
92 Correct 165 ms 42880 KB Output is correct
93 Correct 38 ms 35864 KB Output is correct
94 Correct 42 ms 35576 KB Output is correct
95 Correct 270 ms 47780 KB Output is correct
96 Correct 303 ms 49232 KB Output is correct
97 Correct 40 ms 35832 KB Output is correct
98 Correct 36 ms 35584 KB Output is correct
99 Correct 223 ms 44400 KB Output is correct
100 Correct 44 ms 35832 KB Output is correct
101 Correct 250 ms 42896 KB Output is correct
102 Correct 322 ms 48636 KB Output is correct
103 Correct 43 ms 35832 KB Output is correct
104 Correct 47 ms 35936 KB Output is correct
105 Correct 41 ms 35764 KB Output is correct
106 Correct 39 ms 35840 KB Output is correct
107 Correct 44 ms 35836 KB Output is correct
108 Correct 1363 ms 73080 KB Output is correct
109 Correct 1243 ms 72804 KB Output is correct
110 Correct 1394 ms 77204 KB Output is correct
111 Correct 1993 ms 102648 KB Output is correct
112 Correct 1750 ms 94480 KB Output is correct
113 Correct 1323 ms 94324 KB Output is correct
114 Correct 1150 ms 66908 KB Output is correct
115 Correct 1074 ms 66804 KB Output is correct
116 Correct 1440 ms 69680 KB Output is correct
117 Correct 1774 ms 94632 KB Output is correct
118 Correct 1128 ms 67432 KB Output is correct
119 Correct 1858 ms 94712 KB Output is correct
120 Correct 1908 ms 99228 KB Output is correct
121 Correct 1699 ms 96356 KB Output is correct
122 Correct 1592 ms 77116 KB Output is correct
123 Correct 1419 ms 97388 KB Output is correct
124 Correct 1751 ms 89008 KB Output is correct