Submission #121457

# Submission time Handle Problem Language Result Execution time Memory
121457 2019-06-26T14:19:49 Z gs14004 Mergers (JOI19_mergers) C++17
100 / 100
2510 ms 128352 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500050;
using lint = long long;
using pi = pair<int, int>;

struct disj{
	int pa[MAXN];
	void init(int n){
		iota(pa, pa + n + 1, 0);
	}
	int find(int x){
		return pa[x] = (pa[x] == x ? x : find(pa[x]));
	}
	bool uni(int p, int q){
		p = find(p);
		q = find(q);
		if(p == q) return 0;
		pa[q] = p; return 1;
	}
}disj;

int n, k;
vector<int> gph[MAXN];
vector<int> grp[MAXN];
vector<int> dfn;
int par[19][MAXN], dep[MAXN], din[MAXN];
int cnt[MAXN], dx[MAXN], piv;

int lca(int x, int y){
	if(dep[x] > dep[y]) swap(x, y);
	int dx = dep[y] - dep[x];
	for(int i=0; i<19; i++){
		if((dx >> i) & 1) y = par[i][y];
	}
	for(int i=18; i>=0; i--){
		if(par[i][x] != par[i][y]){
			x = par[i][x];
			y = par[i][y];
		}
	}
	if(x != y) return par[0][x];
	return x;
}

void dfs(int x, int p){
	dfn.push_back(x);
	din[x] = ++piv;
	for(auto &i : gph[x]){
		if(i != p){
			par[0][i] = x;
			dep[i] = dep[x] + 1;
			dfs(i, x);
		}
	}
}

int main(){
	scanf("%d %d",&n,&k);
	for(int i=0; i<n-1; i++){
		int s, e; scanf("%d %d",&s,&e);
		gph[s].push_back(e);
		gph[e].push_back(s);
	}
	for(int i=1; i<=n; i++){
		int x; scanf("%d",&x);
		grp[x].push_back(i);
	}
	dfs(1, 0);
	for(int i=1; i<19; i++){
		for(int j=1; j<=n; j++){
			par[i][j] = par[i-1][par[i-1][j]];
		}
	}
	for(int i=1; i<=k; i++){
		sort(grp[i].begin(), grp[i].end(), [&](const int &a, const int &b){
			return din[a] < din[b];
		});
		for(int j=0; j+1<grp[i].size(); j++){
			int ps = grp[i][j];
			int pe = grp[i][j+1];
			dx[pe]++; dx[ps]++; dx[lca(ps, pe)] -= 2;
		}
	}
	disj.init(n);
	for(int i=dfn.size()-1; i>=0; i--){
		int x = dfn[i];
		for(auto &j : gph[x]){
			if(j != par[0][x]) dx[x] += dx[j];
		}
		if(dx[x] > 0) disj.uni(par[0][x], x);
	}
	for(int i=2; i<=n; i++){
		int l = disj.find(par[0][i]);
		int r = disj.find(i);
		if(l != r) cnt[l]++, cnt[r]++;
	}
	int sum = count(cnt + 1, cnt + n + 1, 1);
	printf("%d\n", (sum + 1) / 2);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:79:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=0; j+1<grp[i].size(); j++){
                ~~~^~~~~~~~~~~~~~
mergers.cpp:59:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&k);
  ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:61:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int s, e; scanf("%d %d",&s,&e);
             ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:66:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x; scanf("%d",&x);
          ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23936 KB Output is correct
2 Correct 21 ms 24036 KB Output is correct
3 Correct 21 ms 23936 KB Output is correct
4 Correct 21 ms 23936 KB Output is correct
5 Correct 21 ms 23928 KB Output is correct
6 Correct 22 ms 24064 KB Output is correct
7 Correct 21 ms 23936 KB Output is correct
8 Correct 21 ms 23936 KB Output is correct
9 Correct 20 ms 24056 KB Output is correct
10 Correct 21 ms 23936 KB Output is correct
11 Correct 20 ms 23936 KB Output is correct
12 Correct 25 ms 23936 KB Output is correct
13 Correct 26 ms 23936 KB Output is correct
14 Correct 21 ms 24016 KB Output is correct
15 Correct 23 ms 23996 KB Output is correct
16 Correct 22 ms 23928 KB Output is correct
17 Correct 23 ms 24040 KB Output is correct
18 Correct 33 ms 24116 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 26 ms 23928 KB Output is correct
21 Correct 22 ms 23936 KB Output is correct
22 Correct 22 ms 23936 KB Output is correct
23 Correct 21 ms 23936 KB Output is correct
24 Correct 23 ms 24028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23936 KB Output is correct
2 Correct 21 ms 24036 KB Output is correct
3 Correct 21 ms 23936 KB Output is correct
4 Correct 21 ms 23936 KB Output is correct
5 Correct 21 ms 23928 KB Output is correct
6 Correct 22 ms 24064 KB Output is correct
7 Correct 21 ms 23936 KB Output is correct
8 Correct 21 ms 23936 KB Output is correct
9 Correct 20 ms 24056 KB Output is correct
10 Correct 21 ms 23936 KB Output is correct
11 Correct 20 ms 23936 KB Output is correct
12 Correct 25 ms 23936 KB Output is correct
13 Correct 26 ms 23936 KB Output is correct
14 Correct 21 ms 24016 KB Output is correct
15 Correct 23 ms 23996 KB Output is correct
16 Correct 22 ms 23928 KB Output is correct
17 Correct 23 ms 24040 KB Output is correct
18 Correct 33 ms 24116 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 26 ms 23928 KB Output is correct
21 Correct 22 ms 23936 KB Output is correct
22 Correct 22 ms 23936 KB Output is correct
23 Correct 21 ms 23936 KB Output is correct
24 Correct 23 ms 24028 KB Output is correct
25 Correct 24 ms 24036 KB Output is correct
26 Correct 25 ms 24568 KB Output is correct
27 Correct 25 ms 24396 KB Output is correct
28 Correct 31 ms 24568 KB Output is correct
29 Correct 30 ms 24536 KB Output is correct
30 Correct 28 ms 24480 KB Output is correct
31 Correct 23 ms 23936 KB Output is correct
32 Correct 25 ms 24704 KB Output is correct
33 Correct 23 ms 23964 KB Output is correct
34 Correct 25 ms 24448 KB Output is correct
35 Correct 26 ms 24568 KB Output is correct
36 Correct 31 ms 24448 KB Output is correct
37 Correct 29 ms 24568 KB Output is correct
38 Correct 23 ms 24064 KB Output is correct
39 Correct 26 ms 24448 KB Output is correct
40 Correct 24 ms 24448 KB Output is correct
41 Correct 26 ms 24452 KB Output is correct
42 Correct 24 ms 24548 KB Output is correct
43 Correct 24 ms 24576 KB Output is correct
44 Correct 29 ms 23928 KB Output is correct
45 Correct 42 ms 24572 KB Output is correct
46 Correct 25 ms 24576 KB Output is correct
47 Correct 23 ms 23936 KB Output is correct
48 Correct 25 ms 24448 KB Output is correct
49 Correct 24 ms 24576 KB Output is correct
50 Correct 24 ms 24568 KB Output is correct
51 Correct 24 ms 24448 KB Output is correct
52 Correct 32 ms 24440 KB Output is correct
53 Correct 23 ms 24460 KB Output is correct
54 Correct 26 ms 24484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23936 KB Output is correct
2 Correct 21 ms 24036 KB Output is correct
3 Correct 21 ms 23936 KB Output is correct
4 Correct 21 ms 23936 KB Output is correct
5 Correct 21 ms 23928 KB Output is correct
6 Correct 22 ms 24064 KB Output is correct
7 Correct 21 ms 23936 KB Output is correct
8 Correct 21 ms 23936 KB Output is correct
9 Correct 20 ms 24056 KB Output is correct
10 Correct 21 ms 23936 KB Output is correct
11 Correct 20 ms 23936 KB Output is correct
12 Correct 25 ms 23936 KB Output is correct
13 Correct 26 ms 23936 KB Output is correct
14 Correct 21 ms 24016 KB Output is correct
15 Correct 23 ms 23996 KB Output is correct
16 Correct 22 ms 23928 KB Output is correct
17 Correct 23 ms 24040 KB Output is correct
18 Correct 33 ms 24116 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 26 ms 23928 KB Output is correct
21 Correct 22 ms 23936 KB Output is correct
22 Correct 22 ms 23936 KB Output is correct
23 Correct 21 ms 23936 KB Output is correct
24 Correct 23 ms 24028 KB Output is correct
25 Correct 21 ms 24064 KB Output is correct
26 Correct 266 ms 39036 KB Output is correct
27 Correct 387 ms 38724 KB Output is correct
28 Correct 25 ms 24448 KB Output is correct
29 Correct 22 ms 24064 KB Output is correct
30 Correct 23 ms 24012 KB Output is correct
31 Correct 291 ms 38948 KB Output is correct
32 Correct 27 ms 24448 KB Output is correct
33 Correct 330 ms 42864 KB Output is correct
34 Correct 340 ms 38900 KB Output is correct
35 Correct 26 ms 24448 KB Output is correct
36 Correct 374 ms 39260 KB Output is correct
37 Correct 26 ms 24568 KB Output is correct
38 Correct 25 ms 24448 KB Output is correct
39 Correct 360 ms 38860 KB Output is correct
40 Correct 29 ms 24576 KB Output is correct
41 Correct 284 ms 38516 KB Output is correct
42 Correct 357 ms 40048 KB Output is correct
43 Correct 23 ms 23936 KB Output is correct
44 Correct 390 ms 42860 KB Output is correct
45 Correct 354 ms 40996 KB Output is correct
46 Correct 26 ms 24440 KB Output is correct
47 Correct 25 ms 24448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 263 ms 39072 KB Output is correct
2 Correct 117 ms 41964 KB Output is correct
3 Correct 26 ms 24448 KB Output is correct
4 Correct 26 ms 24448 KB Output is correct
5 Correct 22 ms 24064 KB Output is correct
6 Correct 24 ms 23936 KB Output is correct
7 Correct 26 ms 24576 KB Output is correct
8 Correct 287 ms 40532 KB Output is correct
9 Correct 26 ms 24448 KB Output is correct
10 Correct 292 ms 39416 KB Output is correct
11 Correct 25 ms 24064 KB Output is correct
12 Correct 335 ms 39168 KB Output is correct
13 Correct 279 ms 40356 KB Output is correct
14 Correct 159 ms 41968 KB Output is correct
15 Correct 303 ms 38908 KB Output is correct
16 Correct 24 ms 24576 KB Output is correct
17 Correct 21 ms 23936 KB Output is correct
18 Correct 136 ms 42136 KB Output is correct
19 Correct 173 ms 44320 KB Output is correct
20 Correct 28 ms 24448 KB Output is correct
21 Correct 22 ms 23936 KB Output is correct
22 Correct 213 ms 40656 KB Output is correct
23 Correct 29 ms 24576 KB Output is correct
24 Correct 311 ms 39624 KB Output is correct
25 Correct 150 ms 43504 KB Output is correct
26 Correct 32 ms 24580 KB Output is correct
27 Correct 30 ms 24548 KB Output is correct
28 Correct 26 ms 24448 KB Output is correct
29 Correct 26 ms 24420 KB Output is correct
30 Correct 26 ms 24440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23936 KB Output is correct
2 Correct 21 ms 24036 KB Output is correct
3 Correct 21 ms 23936 KB Output is correct
4 Correct 21 ms 23936 KB Output is correct
5 Correct 21 ms 23928 KB Output is correct
6 Correct 22 ms 24064 KB Output is correct
7 Correct 21 ms 23936 KB Output is correct
8 Correct 21 ms 23936 KB Output is correct
9 Correct 20 ms 24056 KB Output is correct
10 Correct 21 ms 23936 KB Output is correct
11 Correct 20 ms 23936 KB Output is correct
12 Correct 25 ms 23936 KB Output is correct
13 Correct 26 ms 23936 KB Output is correct
14 Correct 21 ms 24016 KB Output is correct
15 Correct 23 ms 23996 KB Output is correct
16 Correct 22 ms 23928 KB Output is correct
17 Correct 23 ms 24040 KB Output is correct
18 Correct 33 ms 24116 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 26 ms 23928 KB Output is correct
21 Correct 22 ms 23936 KB Output is correct
22 Correct 22 ms 23936 KB Output is correct
23 Correct 21 ms 23936 KB Output is correct
24 Correct 23 ms 24028 KB Output is correct
25 Correct 24 ms 24036 KB Output is correct
26 Correct 25 ms 24568 KB Output is correct
27 Correct 25 ms 24396 KB Output is correct
28 Correct 31 ms 24568 KB Output is correct
29 Correct 30 ms 24536 KB Output is correct
30 Correct 28 ms 24480 KB Output is correct
31 Correct 23 ms 23936 KB Output is correct
32 Correct 25 ms 24704 KB Output is correct
33 Correct 23 ms 23964 KB Output is correct
34 Correct 25 ms 24448 KB Output is correct
35 Correct 26 ms 24568 KB Output is correct
36 Correct 31 ms 24448 KB Output is correct
37 Correct 29 ms 24568 KB Output is correct
38 Correct 23 ms 24064 KB Output is correct
39 Correct 26 ms 24448 KB Output is correct
40 Correct 24 ms 24448 KB Output is correct
41 Correct 26 ms 24452 KB Output is correct
42 Correct 24 ms 24548 KB Output is correct
43 Correct 24 ms 24576 KB Output is correct
44 Correct 29 ms 23928 KB Output is correct
45 Correct 42 ms 24572 KB Output is correct
46 Correct 25 ms 24576 KB Output is correct
47 Correct 23 ms 23936 KB Output is correct
48 Correct 25 ms 24448 KB Output is correct
49 Correct 24 ms 24576 KB Output is correct
50 Correct 24 ms 24568 KB Output is correct
51 Correct 24 ms 24448 KB Output is correct
52 Correct 32 ms 24440 KB Output is correct
53 Correct 23 ms 24460 KB Output is correct
54 Correct 26 ms 24484 KB Output is correct
55 Correct 21 ms 24064 KB Output is correct
56 Correct 266 ms 39036 KB Output is correct
57 Correct 387 ms 38724 KB Output is correct
58 Correct 25 ms 24448 KB Output is correct
59 Correct 22 ms 24064 KB Output is correct
60 Correct 23 ms 24012 KB Output is correct
61 Correct 291 ms 38948 KB Output is correct
62 Correct 27 ms 24448 KB Output is correct
63 Correct 330 ms 42864 KB Output is correct
64 Correct 340 ms 38900 KB Output is correct
65 Correct 26 ms 24448 KB Output is correct
66 Correct 374 ms 39260 KB Output is correct
67 Correct 26 ms 24568 KB Output is correct
68 Correct 25 ms 24448 KB Output is correct
69 Correct 360 ms 38860 KB Output is correct
70 Correct 29 ms 24576 KB Output is correct
71 Correct 284 ms 38516 KB Output is correct
72 Correct 357 ms 40048 KB Output is correct
73 Correct 23 ms 23936 KB Output is correct
74 Correct 390 ms 42860 KB Output is correct
75 Correct 354 ms 40996 KB Output is correct
76 Correct 26 ms 24440 KB Output is correct
77 Correct 25 ms 24448 KB Output is correct
78 Correct 263 ms 39072 KB Output is correct
79 Correct 117 ms 41964 KB Output is correct
80 Correct 26 ms 24448 KB Output is correct
81 Correct 26 ms 24448 KB Output is correct
82 Correct 22 ms 24064 KB Output is correct
83 Correct 24 ms 23936 KB Output is correct
84 Correct 26 ms 24576 KB Output is correct
85 Correct 287 ms 40532 KB Output is correct
86 Correct 26 ms 24448 KB Output is correct
87 Correct 292 ms 39416 KB Output is correct
88 Correct 25 ms 24064 KB Output is correct
89 Correct 335 ms 39168 KB Output is correct
90 Correct 279 ms 40356 KB Output is correct
91 Correct 159 ms 41968 KB Output is correct
92 Correct 303 ms 38908 KB Output is correct
93 Correct 24 ms 24576 KB Output is correct
94 Correct 21 ms 23936 KB Output is correct
95 Correct 136 ms 42136 KB Output is correct
96 Correct 173 ms 44320 KB Output is correct
97 Correct 28 ms 24448 KB Output is correct
98 Correct 22 ms 23936 KB Output is correct
99 Correct 213 ms 40656 KB Output is correct
100 Correct 29 ms 24576 KB Output is correct
101 Correct 311 ms 39624 KB Output is correct
102 Correct 150 ms 43504 KB Output is correct
103 Correct 32 ms 24580 KB Output is correct
104 Correct 30 ms 24548 KB Output is correct
105 Correct 26 ms 24448 KB Output is correct
106 Correct 26 ms 24420 KB Output is correct
107 Correct 26 ms 24440 KB Output is correct
108 Correct 1795 ms 107036 KB Output is correct
109 Correct 2423 ms 107496 KB Output is correct
110 Correct 2439 ms 113932 KB Output is correct
111 Correct 1060 ms 128352 KB Output is correct
112 Correct 1049 ms 122564 KB Output is correct
113 Correct 921 ms 114520 KB Output is correct
114 Correct 2013 ms 96960 KB Output is correct
115 Correct 2036 ms 96780 KB Output is correct
116 Correct 2510 ms 103224 KB Output is correct
117 Correct 900 ms 114680 KB Output is correct
118 Correct 1940 ms 98788 KB Output is correct
119 Correct 903 ms 114728 KB Output is correct
120 Correct 957 ms 122788 KB Output is correct
121 Correct 900 ms 114692 KB Output is correct
122 Correct 1594 ms 108272 KB Output is correct
123 Correct 655 ms 114164 KB Output is correct
124 Correct 1014 ms 112616 KB Output is correct