Submission #263384

# Submission time Handle Problem Language Result Execution time Memory
263384 2020-08-13T16:24:46 Z mjkocijan Mergers (JOI19_mergers) C++14
100 / 100
1923 ms 194492 KB
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> ii;
#define MAXN 500500
#define LOG 20

int n, k;
vector<int> g[MAXN];
int s[MAXN], rt[MAXN], sz[MAXN], dep[MAXN], st1[MAXN], st2[MAXN];
vector<int> sr[MAXN];
int anc[LOG][MAXN];

int dfs(int cv, int rod, int dub = 0)
{
	dep[cv] = dub;
	anc[0][cv] = rod;
	st1[cv] = 1;
	for (int i: g[cv]) {
		if (rod != i) {
			st1[cv] += dfs(i, cv, dub + 1);
		}
	}
	return st1[cv];
}

int lca(int x, int y)
{
	if (dep[x] > dep[y]) swap(x, y);
	
	for (int i = LOG - 1; i >= 0; i--) {
		int novi = anc[i][y];
		if (dep[novi] >= dep[x])
			y = novi;
	}
	
	if (x == y) return x;
	
	for (int i = LOG - 1; i >= 0; i--) {
		if (anc[i][x] != anc[i][y]) {
			x = anc[i][x];
			y = anc[i][y];
		}
	}
	
	return anc[0][x];
}

int fn(int x)
{
	if (x == rt[x]) return x;
	
	return rt[x] = fn(rt[x]);
}

void merg(int x, int y)
{
	x = fn(x);
	y = fn(y);
	
	if (x == y) return;
	
	if (dep[x] < dep[y]) return;

	//if (sz[x] > sz[y]) swap(x, y);
	
	rt[x] = y;
	//sz[y] += sz[x] == sz[y];
	st2[y] += st2[x];
}

//set<int> s;

set<int> sus[MAXN];

int main()
{
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n - 1; i++) {
		int q, w;
		scanf("%d%d", &q, &w);
		q--; w--;
		g[q].pb(w);
		g[w].pb(q);
	}
	for (int i = 0; i < n; i++) {
		rt[i] = i;
		st2[i] = 1;
		scanf("%d", &s[i]);
		s[i]--;
		sr[s[i]].pb(i);
	}
	dfs(0, 0);
	for (int i = 1; i < LOG; i++) {
		for (int j = 0; j < n; j++) {
			anc[i][j] = anc[i - 1][anc[i - 1][j]];
		}
	}
	for (int i = 0; i < k; i++) {
		int lcaa = sr[i][0];
		for (int j = 1; j < sr[i].size(); j++) {
			lcaa = lca(lcaa, sr[i][j]);
		}
		for (int j: sr[i]) {
			j = fn(j);
			while (dep[j] > dep[lcaa]) {
				merg(j, anc[0][j]);
				j = fn(j);
			}
		}
	}
	int reza = 0;
	for (int i = 0; i < n; i++) {
		for (int j: g[i]) {
			if (fn(i) != fn(j))
				sus[fn(i)].insert(fn(j));
		}
		//cout << i+1<< ' '<<fn(i)+1<<endl;
		//cout << i+1<<' '<<fn(i)+1<<' '<<st1[i]<<' '<<st2[i]<<endl;
		//if (i == fn(i) && st1[i] == st2[i]) reza++;
	}
	for (int i = 0; i < n; i++) {
		if (sus[i].size() == 1)
			reza++;
		//cout << i+1<<' '<<
	}
	/*if (reza == 1)
		printf("0\n");
	else*/
		printf("%d\n", (reza + 1) / 2);
  return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:104:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |   for (int j = 1; j < sr[i].size(); j++) {
      |                   ~~^~~~~~~~~~~~~~
mergers.cpp:81:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   81 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:84:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   84 |   scanf("%d%d", &q, &w);
      |   ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:92:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   92 |   scanf("%d", &s[i]);
      |   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47480 KB Output is correct
2 Correct 32 ms 47484 KB Output is correct
3 Correct 36 ms 47480 KB Output is correct
4 Correct 30 ms 47488 KB Output is correct
5 Correct 30 ms 47480 KB Output is correct
6 Correct 30 ms 47488 KB Output is correct
7 Correct 30 ms 47480 KB Output is correct
8 Correct 29 ms 47488 KB Output is correct
9 Correct 35 ms 47484 KB Output is correct
10 Correct 32 ms 47588 KB Output is correct
11 Correct 31 ms 47480 KB Output is correct
12 Correct 32 ms 47488 KB Output is correct
13 Correct 35 ms 47488 KB Output is correct
14 Correct 32 ms 47540 KB Output is correct
15 Correct 32 ms 47608 KB Output is correct
16 Correct 32 ms 47480 KB Output is correct
17 Correct 32 ms 47480 KB Output is correct
18 Correct 33 ms 47480 KB Output is correct
19 Correct 35 ms 47480 KB Output is correct
20 Correct 32 ms 47480 KB Output is correct
21 Correct 32 ms 47488 KB Output is correct
22 Correct 33 ms 47608 KB Output is correct
23 Correct 32 ms 47488 KB Output is correct
24 Correct 31 ms 47480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47480 KB Output is correct
2 Correct 32 ms 47484 KB Output is correct
3 Correct 36 ms 47480 KB Output is correct
4 Correct 30 ms 47488 KB Output is correct
5 Correct 30 ms 47480 KB Output is correct
6 Correct 30 ms 47488 KB Output is correct
7 Correct 30 ms 47480 KB Output is correct
8 Correct 29 ms 47488 KB Output is correct
9 Correct 35 ms 47484 KB Output is correct
10 Correct 32 ms 47588 KB Output is correct
11 Correct 31 ms 47480 KB Output is correct
12 Correct 32 ms 47488 KB Output is correct
13 Correct 35 ms 47488 KB Output is correct
14 Correct 32 ms 47540 KB Output is correct
15 Correct 32 ms 47608 KB Output is correct
16 Correct 32 ms 47480 KB Output is correct
17 Correct 32 ms 47480 KB Output is correct
18 Correct 33 ms 47480 KB Output is correct
19 Correct 35 ms 47480 KB Output is correct
20 Correct 32 ms 47480 KB Output is correct
21 Correct 32 ms 47488 KB Output is correct
22 Correct 33 ms 47608 KB Output is correct
23 Correct 32 ms 47488 KB Output is correct
24 Correct 31 ms 47480 KB Output is correct
25 Correct 31 ms 47480 KB Output is correct
26 Correct 35 ms 48128 KB Output is correct
27 Correct 34 ms 47992 KB Output is correct
28 Correct 39 ms 48256 KB Output is correct
29 Correct 35 ms 48376 KB Output is correct
30 Correct 34 ms 47992 KB Output is correct
31 Correct 35 ms 47480 KB Output is correct
32 Correct 34 ms 48256 KB Output is correct
33 Correct 31 ms 47488 KB Output is correct
34 Correct 34 ms 48000 KB Output is correct
35 Correct 35 ms 48384 KB Output is correct
36 Correct 35 ms 47992 KB Output is correct
37 Correct 35 ms 47992 KB Output is correct
38 Correct 32 ms 47480 KB Output is correct
39 Correct 35 ms 47992 KB Output is correct
40 Correct 35 ms 48000 KB Output is correct
41 Correct 34 ms 47992 KB Output is correct
42 Correct 34 ms 48120 KB Output is correct
43 Correct 34 ms 48176 KB Output is correct
44 Correct 32 ms 47616 KB Output is correct
45 Correct 35 ms 48124 KB Output is correct
46 Correct 40 ms 48128 KB Output is correct
47 Correct 32 ms 47480 KB Output is correct
48 Correct 35 ms 48128 KB Output is correct
49 Correct 36 ms 48376 KB Output is correct
50 Correct 35 ms 48376 KB Output is correct
51 Correct 35 ms 47992 KB Output is correct
52 Correct 40 ms 47992 KB Output is correct
53 Correct 39 ms 48120 KB Output is correct
54 Correct 37 ms 47996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47480 KB Output is correct
2 Correct 32 ms 47484 KB Output is correct
3 Correct 36 ms 47480 KB Output is correct
4 Correct 30 ms 47488 KB Output is correct
5 Correct 30 ms 47480 KB Output is correct
6 Correct 30 ms 47488 KB Output is correct
7 Correct 30 ms 47480 KB Output is correct
8 Correct 29 ms 47488 KB Output is correct
9 Correct 35 ms 47484 KB Output is correct
10 Correct 32 ms 47588 KB Output is correct
11 Correct 31 ms 47480 KB Output is correct
12 Correct 32 ms 47488 KB Output is correct
13 Correct 35 ms 47488 KB Output is correct
14 Correct 32 ms 47540 KB Output is correct
15 Correct 32 ms 47608 KB Output is correct
16 Correct 32 ms 47480 KB Output is correct
17 Correct 32 ms 47480 KB Output is correct
18 Correct 33 ms 47480 KB Output is correct
19 Correct 35 ms 47480 KB Output is correct
20 Correct 32 ms 47480 KB Output is correct
21 Correct 32 ms 47488 KB Output is correct
22 Correct 33 ms 47608 KB Output is correct
23 Correct 32 ms 47488 KB Output is correct
24 Correct 31 ms 47480 KB Output is correct
25 Correct 31 ms 47480 KB Output is correct
26 Correct 154 ms 62832 KB Output is correct
27 Correct 178 ms 62456 KB Output is correct
28 Correct 35 ms 47992 KB Output is correct
29 Correct 33 ms 47488 KB Output is correct
30 Correct 36 ms 47488 KB Output is correct
31 Correct 170 ms 62328 KB Output is correct
32 Correct 35 ms 48000 KB Output is correct
33 Correct 149 ms 68216 KB Output is correct
34 Correct 178 ms 62584 KB Output is correct
35 Correct 34 ms 48000 KB Output is correct
36 Correct 189 ms 62908 KB Output is correct
37 Correct 34 ms 47992 KB Output is correct
38 Correct 35 ms 48000 KB Output is correct
39 Correct 171 ms 62704 KB Output is correct
40 Correct 34 ms 48120 KB Output is correct
41 Correct 148 ms 62420 KB Output is correct
42 Correct 179 ms 64248 KB Output is correct
43 Correct 33 ms 47488 KB Output is correct
44 Correct 149 ms 68340 KB Output is correct
45 Correct 153 ms 65532 KB Output is correct
46 Correct 38 ms 47992 KB Output is correct
47 Correct 37 ms 47992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 61676 KB Output is correct
2 Correct 164 ms 73712 KB Output is correct
3 Correct 35 ms 48128 KB Output is correct
4 Correct 34 ms 48000 KB Output is correct
5 Correct 32 ms 47480 KB Output is correct
6 Correct 31 ms 47480 KB Output is correct
7 Correct 34 ms 47992 KB Output is correct
8 Correct 235 ms 64780 KB Output is correct
9 Correct 34 ms 48000 KB Output is correct
10 Correct 218 ms 62712 KB Output is correct
11 Correct 32 ms 47480 KB Output is correct
12 Correct 241 ms 62620 KB Output is correct
13 Correct 231 ms 65656 KB Output is correct
14 Correct 160 ms 74852 KB Output is correct
15 Correct 158 ms 62704 KB Output is correct
16 Correct 35 ms 48120 KB Output is correct
17 Correct 32 ms 47608 KB Output is correct
18 Correct 179 ms 72944 KB Output is correct
19 Correct 170 ms 77944 KB Output is correct
20 Correct 35 ms 47964 KB Output is correct
21 Correct 33 ms 47488 KB Output is correct
22 Correct 192 ms 65776 KB Output is correct
23 Correct 34 ms 47992 KB Output is correct
24 Correct 245 ms 63864 KB Output is correct
25 Correct 167 ms 76792 KB Output is correct
26 Correct 37 ms 48512 KB Output is correct
27 Correct 34 ms 48384 KB Output is correct
28 Correct 35 ms 48000 KB Output is correct
29 Correct 34 ms 47992 KB Output is correct
30 Correct 37 ms 48120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47480 KB Output is correct
2 Correct 32 ms 47484 KB Output is correct
3 Correct 36 ms 47480 KB Output is correct
4 Correct 30 ms 47488 KB Output is correct
5 Correct 30 ms 47480 KB Output is correct
6 Correct 30 ms 47488 KB Output is correct
7 Correct 30 ms 47480 KB Output is correct
8 Correct 29 ms 47488 KB Output is correct
9 Correct 35 ms 47484 KB Output is correct
10 Correct 32 ms 47588 KB Output is correct
11 Correct 31 ms 47480 KB Output is correct
12 Correct 32 ms 47488 KB Output is correct
13 Correct 35 ms 47488 KB Output is correct
14 Correct 32 ms 47540 KB Output is correct
15 Correct 32 ms 47608 KB Output is correct
16 Correct 32 ms 47480 KB Output is correct
17 Correct 32 ms 47480 KB Output is correct
18 Correct 33 ms 47480 KB Output is correct
19 Correct 35 ms 47480 KB Output is correct
20 Correct 32 ms 47480 KB Output is correct
21 Correct 32 ms 47488 KB Output is correct
22 Correct 33 ms 47608 KB Output is correct
23 Correct 32 ms 47488 KB Output is correct
24 Correct 31 ms 47480 KB Output is correct
25 Correct 31 ms 47480 KB Output is correct
26 Correct 35 ms 48128 KB Output is correct
27 Correct 34 ms 47992 KB Output is correct
28 Correct 39 ms 48256 KB Output is correct
29 Correct 35 ms 48376 KB Output is correct
30 Correct 34 ms 47992 KB Output is correct
31 Correct 35 ms 47480 KB Output is correct
32 Correct 34 ms 48256 KB Output is correct
33 Correct 31 ms 47488 KB Output is correct
34 Correct 34 ms 48000 KB Output is correct
35 Correct 35 ms 48384 KB Output is correct
36 Correct 35 ms 47992 KB Output is correct
37 Correct 35 ms 47992 KB Output is correct
38 Correct 32 ms 47480 KB Output is correct
39 Correct 35 ms 47992 KB Output is correct
40 Correct 35 ms 48000 KB Output is correct
41 Correct 34 ms 47992 KB Output is correct
42 Correct 34 ms 48120 KB Output is correct
43 Correct 34 ms 48176 KB Output is correct
44 Correct 32 ms 47616 KB Output is correct
45 Correct 35 ms 48124 KB Output is correct
46 Correct 40 ms 48128 KB Output is correct
47 Correct 32 ms 47480 KB Output is correct
48 Correct 35 ms 48128 KB Output is correct
49 Correct 36 ms 48376 KB Output is correct
50 Correct 35 ms 48376 KB Output is correct
51 Correct 35 ms 47992 KB Output is correct
52 Correct 40 ms 47992 KB Output is correct
53 Correct 39 ms 48120 KB Output is correct
54 Correct 37 ms 47996 KB Output is correct
55 Correct 31 ms 47480 KB Output is correct
56 Correct 154 ms 62832 KB Output is correct
57 Correct 178 ms 62456 KB Output is correct
58 Correct 35 ms 47992 KB Output is correct
59 Correct 33 ms 47488 KB Output is correct
60 Correct 36 ms 47488 KB Output is correct
61 Correct 170 ms 62328 KB Output is correct
62 Correct 35 ms 48000 KB Output is correct
63 Correct 149 ms 68216 KB Output is correct
64 Correct 178 ms 62584 KB Output is correct
65 Correct 34 ms 48000 KB Output is correct
66 Correct 189 ms 62908 KB Output is correct
67 Correct 34 ms 47992 KB Output is correct
68 Correct 35 ms 48000 KB Output is correct
69 Correct 171 ms 62704 KB Output is correct
70 Correct 34 ms 48120 KB Output is correct
71 Correct 148 ms 62420 KB Output is correct
72 Correct 179 ms 64248 KB Output is correct
73 Correct 33 ms 47488 KB Output is correct
74 Correct 149 ms 68340 KB Output is correct
75 Correct 153 ms 65532 KB Output is correct
76 Correct 38 ms 47992 KB Output is correct
77 Correct 37 ms 47992 KB Output is correct
78 Correct 180 ms 61676 KB Output is correct
79 Correct 164 ms 73712 KB Output is correct
80 Correct 35 ms 48128 KB Output is correct
81 Correct 34 ms 48000 KB Output is correct
82 Correct 32 ms 47480 KB Output is correct
83 Correct 31 ms 47480 KB Output is correct
84 Correct 34 ms 47992 KB Output is correct
85 Correct 235 ms 64780 KB Output is correct
86 Correct 34 ms 48000 KB Output is correct
87 Correct 218 ms 62712 KB Output is correct
88 Correct 32 ms 47480 KB Output is correct
89 Correct 241 ms 62620 KB Output is correct
90 Correct 231 ms 65656 KB Output is correct
91 Correct 160 ms 74852 KB Output is correct
92 Correct 158 ms 62704 KB Output is correct
93 Correct 35 ms 48120 KB Output is correct
94 Correct 32 ms 47608 KB Output is correct
95 Correct 179 ms 72944 KB Output is correct
96 Correct 170 ms 77944 KB Output is correct
97 Correct 35 ms 47964 KB Output is correct
98 Correct 33 ms 47488 KB Output is correct
99 Correct 192 ms 65776 KB Output is correct
100 Correct 34 ms 47992 KB Output is correct
101 Correct 245 ms 63864 KB Output is correct
102 Correct 167 ms 76792 KB Output is correct
103 Correct 37 ms 48512 KB Output is correct
104 Correct 34 ms 48384 KB Output is correct
105 Correct 35 ms 48000 KB Output is correct
106 Correct 34 ms 47992 KB Output is correct
107 Correct 37 ms 48120 KB Output is correct
108 Correct 1424 ms 129304 KB Output is correct
109 Correct 1128 ms 132920 KB Output is correct
110 Correct 864 ms 141912 KB Output is correct
111 Correct 1085 ms 194492 KB Output is correct
112 Correct 1245 ms 168804 KB Output is correct
113 Correct 1208 ms 170844 KB Output is correct
114 Correct 794 ms 119396 KB Output is correct
115 Correct 821 ms 119652 KB Output is correct
116 Correct 1923 ms 122488 KB Output is correct
117 Correct 1010 ms 180164 KB Output is correct
118 Correct 1453 ms 119380 KB Output is correct
119 Correct 994 ms 180216 KB Output is correct
120 Correct 1008 ms 191096 KB Output is correct
121 Correct 991 ms 180192 KB Output is correct
122 Correct 1761 ms 135344 KB Output is correct
123 Correct 1313 ms 181820 KB Output is correct
124 Correct 1067 ms 162344 KB Output is correct