Submission #376812

# Submission time Handle Problem Language Result Execution time Memory
376812 2021-03-12T04:42:14 Z 8e7 Mergers (JOI19_mergers) C++14
100 / 100
1128 ms 119492 KB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#define ll long long
#define maxn 500005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
vector<int> adj[maxn], tree[maxn];
int tot;
int col[2 * maxn], prv[maxn], cnt[maxn], par[maxn], ind[maxn];
//cnt: number of nodes colored i, ind: dsu parent
int num[maxn], ord[2 * maxn], arr[2 * maxn];
//ord: euler tour array, num: ans to query, arr:previous item
bool poss[maxn];

struct BIT {
	int bit[2 * maxn];
	void modify(int ind, int val) {
		if (ind == 0) return;
		for (;ind < 2 * maxn;ind += ind & (-ind)) bit[ind] += val;
	}
	int query(int ind) {
		int ret = 0;
		for (;ind > 0;ind -= ind & (-ind)) ret += bit[ind];
		return ret;
	}
} bb;

struct query{
	int l, r, id; //[l, r]
	query() {
		l = 0, r = 0, id = 0;
	}
	query(int a, int b, int c) {
		l = a, r = b, id = c;
	}
};
vector<query> que;
inline bool cmp(query a, query b) {
	return a.r < b.r;
}

int cur, ti = 1;
void dfs(int n, int pa) {
	ord[ti] = ord[ti + tot] = n;
	int lef = ti++;

	par[n] = pa;
	for (int v:adj[n]) {
		if (v != pa) {
			dfs(v, n);
		}
	}
	int rig = ti;
	//cout << n << " " << lef << " " << rig << endl;
	que.push_back(query(lef, rig - 1, n));
	que.push_back(query(rig, lef + tot - 1, n));

}

int find(int a) {
	return a == ind[a] ? a : ind[a] = find(ind[a]);
}
void Union(int a, int b) {
	ind[find(a)] = find(b);
}

int ans = 0;
void dfs2(int n, int par) {
	int chi = 0;
	for (int v:tree[n]) {
		if (v != par) {
			dfs2(v, n);
			chi++;
		}
	}
	if (chi == 0 || (n == 1 && chi == 1)) ans++;
}

int main() {
	io
	int n, k;
	cin >> n >> k;
	tot = n;
	for (int i = 0;i < n - 1;i++) {
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	for (int i = 1;i <= n;i++) {
		cin >> col[i];
		col[i + n] = col[i];
		cnt[col[i]]++;
		ind[i] = i;
	}


	dfs(1, 0);
	for (int i = 1;i <= 2 * n;i++) {
		arr[i] = prv[col[ord[i]]];
		//cout << arr[i] << " ";
		prv[col[ord[i]]] = i;
	}
	//cout << endl;
	sort(que.begin(), que.end(), cmp);
	int id = 0;
	for (int i = 1;i <= 2 * n;i++) {
		bb.modify(arr[i] + 1, 1);
		while (id < que.size() && que[id].r == i) {
			int val = que[id].r - que[id].l + 1 - (i - bb.query(que[id].l));
			//cout << que[id].l << " " << que[id].r << "  " << val << " " << (i - bb.query(que[id].l)) << endl;
			num[que[id].id] += val;
			id++;
		}
	}
	for (int i = 2;i <= n;i++) {
		//cout << num[i] << " ";
		if (num[i] > k) {
			poss[i] = true;
		}
	}
	//cout << endl;


	for (int i = 1;i <= n;i++) {
		if (poss[i]) Union(i, par[i]);
	}
	int done = 1;
	for (int i = 1;i <= n;i++) {
		if (par[i] != 0 && !poss[i]) {
			done = 0;
			int a = find(i), b = find(par[i]);
			tree[a].push_back(b);
			tree[b].push_back(a);
		}
	}
	if (done) cout << 0 << "\n";
	else {
		dfs2(find(1), 0);
		cout << (ans + 1) / 2 << "\n";
	}
}
/*
5 4
1 2
2 3
3 4
3 5
1
2
1
3
4

5 4
1 2
2 3
3 4
4 5
1
2
3
4
1

2 2
1 2
1
2

 */

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:115:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |   while (id < que.size() && que[id].r == i) {
      |          ~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
3 Correct 18 ms 24048 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 18 ms 23916 KB Output is correct
10 Correct 17 ms 23916 KB Output is correct
11 Correct 17 ms 23916 KB Output is correct
12 Correct 17 ms 23916 KB Output is correct
13 Correct 17 ms 23916 KB Output is correct
14 Correct 17 ms 23916 KB Output is correct
15 Correct 17 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 17 ms 23916 KB Output is correct
18 Correct 17 ms 23916 KB Output is correct
19 Correct 17 ms 23916 KB Output is correct
20 Correct 16 ms 23916 KB Output is correct
21 Correct 17 ms 23916 KB Output is correct
22 Correct 17 ms 23916 KB Output is correct
23 Correct 16 ms 23916 KB Output is correct
24 Correct 17 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
3 Correct 18 ms 24048 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 18 ms 23916 KB Output is correct
10 Correct 17 ms 23916 KB Output is correct
11 Correct 17 ms 23916 KB Output is correct
12 Correct 17 ms 23916 KB Output is correct
13 Correct 17 ms 23916 KB Output is correct
14 Correct 17 ms 23916 KB Output is correct
15 Correct 17 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 17 ms 23916 KB Output is correct
18 Correct 17 ms 23916 KB Output is correct
19 Correct 17 ms 23916 KB Output is correct
20 Correct 16 ms 23916 KB Output is correct
21 Correct 17 ms 23916 KB Output is correct
22 Correct 17 ms 23916 KB Output is correct
23 Correct 16 ms 23916 KB Output is correct
24 Correct 17 ms 23916 KB Output is correct
25 Correct 16 ms 23936 KB Output is correct
26 Correct 20 ms 24428 KB Output is correct
27 Correct 20 ms 24428 KB Output is correct
28 Correct 19 ms 24556 KB Output is correct
29 Correct 21 ms 24428 KB Output is correct
30 Correct 19 ms 24388 KB Output is correct
31 Correct 17 ms 23916 KB Output is correct
32 Correct 19 ms 24556 KB Output is correct
33 Correct 17 ms 23916 KB Output is correct
34 Correct 19 ms 24428 KB Output is correct
35 Correct 20 ms 24556 KB Output is correct
36 Correct 19 ms 24300 KB Output is correct
37 Correct 19 ms 24428 KB Output is correct
38 Correct 17 ms 23916 KB Output is correct
39 Correct 19 ms 24428 KB Output is correct
40 Correct 19 ms 24448 KB Output is correct
41 Correct 19 ms 24300 KB Output is correct
42 Correct 19 ms 24428 KB Output is correct
43 Correct 19 ms 24684 KB Output is correct
44 Correct 17 ms 23916 KB Output is correct
45 Correct 19 ms 24428 KB Output is correct
46 Correct 19 ms 24428 KB Output is correct
47 Correct 17 ms 23916 KB Output is correct
48 Correct 19 ms 24300 KB Output is correct
49 Correct 19 ms 24556 KB Output is correct
50 Correct 19 ms 24428 KB Output is correct
51 Correct 19 ms 24300 KB Output is correct
52 Correct 19 ms 24428 KB Output is correct
53 Correct 19 ms 24428 KB Output is correct
54 Correct 19 ms 24300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
3 Correct 18 ms 24048 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 18 ms 23916 KB Output is correct
10 Correct 17 ms 23916 KB Output is correct
11 Correct 17 ms 23916 KB Output is correct
12 Correct 17 ms 23916 KB Output is correct
13 Correct 17 ms 23916 KB Output is correct
14 Correct 17 ms 23916 KB Output is correct
15 Correct 17 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 17 ms 23916 KB Output is correct
18 Correct 17 ms 23916 KB Output is correct
19 Correct 17 ms 23916 KB Output is correct
20 Correct 16 ms 23916 KB Output is correct
21 Correct 17 ms 23916 KB Output is correct
22 Correct 17 ms 23916 KB Output is correct
23 Correct 16 ms 23916 KB Output is correct
24 Correct 17 ms 23916 KB Output is correct
25 Correct 17 ms 23916 KB Output is correct
26 Correct 95 ms 35160 KB Output is correct
27 Correct 127 ms 34780 KB Output is correct
28 Correct 19 ms 24428 KB Output is correct
29 Correct 17 ms 23916 KB Output is correct
30 Correct 17 ms 23916 KB Output is correct
31 Correct 122 ms 34784 KB Output is correct
32 Correct 19 ms 24428 KB Output is correct
33 Correct 127 ms 43360 KB Output is correct
34 Correct 134 ms 34800 KB Output is correct
35 Correct 19 ms 24300 KB Output is correct
36 Correct 122 ms 35680 KB Output is correct
37 Correct 19 ms 24300 KB Output is correct
38 Correct 19 ms 24300 KB Output is correct
39 Correct 95 ms 35028 KB Output is correct
40 Correct 19 ms 24684 KB Output is correct
41 Correct 125 ms 34652 KB Output is correct
42 Correct 125 ms 37472 KB Output is correct
43 Correct 17 ms 23916 KB Output is correct
44 Correct 126 ms 43616 KB Output is correct
45 Correct 130 ms 39264 KB Output is correct
46 Correct 19 ms 24428 KB Output is correct
47 Correct 19 ms 24300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 35032 KB Output is correct
2 Correct 109 ms 38488 KB Output is correct
3 Correct 19 ms 24428 KB Output is correct
4 Correct 19 ms 24428 KB Output is correct
5 Correct 17 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 20 ms 24428 KB Output is correct
8 Correct 132 ms 35164 KB Output is correct
9 Correct 19 ms 24300 KB Output is correct
10 Correct 125 ms 34400 KB Output is correct
11 Correct 16 ms 23916 KB Output is correct
12 Correct 119 ms 34400 KB Output is correct
13 Correct 128 ms 35296 KB Output is correct
14 Correct 149 ms 37980 KB Output is correct
15 Correct 101 ms 34772 KB Output is correct
16 Correct 19 ms 24428 KB Output is correct
17 Correct 17 ms 23916 KB Output is correct
18 Correct 109 ms 38100 KB Output is correct
19 Correct 162 ms 42464 KB Output is correct
20 Correct 19 ms 24428 KB Output is correct
21 Correct 17 ms 23916 KB Output is correct
22 Correct 100 ms 35672 KB Output is correct
23 Correct 19 ms 24300 KB Output is correct
24 Correct 128 ms 34652 KB Output is correct
25 Correct 163 ms 40928 KB Output is correct
26 Correct 20 ms 24428 KB Output is correct
27 Correct 20 ms 24428 KB Output is correct
28 Correct 23 ms 24300 KB Output is correct
29 Correct 19 ms 24428 KB Output is correct
30 Correct 18 ms 24428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
3 Correct 18 ms 24048 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 18 ms 23916 KB Output is correct
10 Correct 17 ms 23916 KB Output is correct
11 Correct 17 ms 23916 KB Output is correct
12 Correct 17 ms 23916 KB Output is correct
13 Correct 17 ms 23916 KB Output is correct
14 Correct 17 ms 23916 KB Output is correct
15 Correct 17 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 17 ms 23916 KB Output is correct
18 Correct 17 ms 23916 KB Output is correct
19 Correct 17 ms 23916 KB Output is correct
20 Correct 16 ms 23916 KB Output is correct
21 Correct 17 ms 23916 KB Output is correct
22 Correct 17 ms 23916 KB Output is correct
23 Correct 16 ms 23916 KB Output is correct
24 Correct 17 ms 23916 KB Output is correct
25 Correct 16 ms 23936 KB Output is correct
26 Correct 20 ms 24428 KB Output is correct
27 Correct 20 ms 24428 KB Output is correct
28 Correct 19 ms 24556 KB Output is correct
29 Correct 21 ms 24428 KB Output is correct
30 Correct 19 ms 24388 KB Output is correct
31 Correct 17 ms 23916 KB Output is correct
32 Correct 19 ms 24556 KB Output is correct
33 Correct 17 ms 23916 KB Output is correct
34 Correct 19 ms 24428 KB Output is correct
35 Correct 20 ms 24556 KB Output is correct
36 Correct 19 ms 24300 KB Output is correct
37 Correct 19 ms 24428 KB Output is correct
38 Correct 17 ms 23916 KB Output is correct
39 Correct 19 ms 24428 KB Output is correct
40 Correct 19 ms 24448 KB Output is correct
41 Correct 19 ms 24300 KB Output is correct
42 Correct 19 ms 24428 KB Output is correct
43 Correct 19 ms 24684 KB Output is correct
44 Correct 17 ms 23916 KB Output is correct
45 Correct 19 ms 24428 KB Output is correct
46 Correct 19 ms 24428 KB Output is correct
47 Correct 17 ms 23916 KB Output is correct
48 Correct 19 ms 24300 KB Output is correct
49 Correct 19 ms 24556 KB Output is correct
50 Correct 19 ms 24428 KB Output is correct
51 Correct 19 ms 24300 KB Output is correct
52 Correct 19 ms 24428 KB Output is correct
53 Correct 19 ms 24428 KB Output is correct
54 Correct 19 ms 24300 KB Output is correct
55 Correct 17 ms 23916 KB Output is correct
56 Correct 95 ms 35160 KB Output is correct
57 Correct 127 ms 34780 KB Output is correct
58 Correct 19 ms 24428 KB Output is correct
59 Correct 17 ms 23916 KB Output is correct
60 Correct 17 ms 23916 KB Output is correct
61 Correct 122 ms 34784 KB Output is correct
62 Correct 19 ms 24428 KB Output is correct
63 Correct 127 ms 43360 KB Output is correct
64 Correct 134 ms 34800 KB Output is correct
65 Correct 19 ms 24300 KB Output is correct
66 Correct 122 ms 35680 KB Output is correct
67 Correct 19 ms 24300 KB Output is correct
68 Correct 19 ms 24300 KB Output is correct
69 Correct 95 ms 35028 KB Output is correct
70 Correct 19 ms 24684 KB Output is correct
71 Correct 125 ms 34652 KB Output is correct
72 Correct 125 ms 37472 KB Output is correct
73 Correct 17 ms 23916 KB Output is correct
74 Correct 126 ms 43616 KB Output is correct
75 Correct 130 ms 39264 KB Output is correct
76 Correct 19 ms 24428 KB Output is correct
77 Correct 19 ms 24300 KB Output is correct
78 Correct 97 ms 35032 KB Output is correct
79 Correct 109 ms 38488 KB Output is correct
80 Correct 19 ms 24428 KB Output is correct
81 Correct 19 ms 24428 KB Output is correct
82 Correct 17 ms 23916 KB Output is correct
83 Correct 17 ms 23916 KB Output is correct
84 Correct 20 ms 24428 KB Output is correct
85 Correct 132 ms 35164 KB Output is correct
86 Correct 19 ms 24300 KB Output is correct
87 Correct 125 ms 34400 KB Output is correct
88 Correct 16 ms 23916 KB Output is correct
89 Correct 119 ms 34400 KB Output is correct
90 Correct 128 ms 35296 KB Output is correct
91 Correct 149 ms 37980 KB Output is correct
92 Correct 101 ms 34772 KB Output is correct
93 Correct 19 ms 24428 KB Output is correct
94 Correct 17 ms 23916 KB Output is correct
95 Correct 109 ms 38100 KB Output is correct
96 Correct 162 ms 42464 KB Output is correct
97 Correct 19 ms 24428 KB Output is correct
98 Correct 17 ms 23916 KB Output is correct
99 Correct 100 ms 35672 KB Output is correct
100 Correct 19 ms 24300 KB Output is correct
101 Correct 128 ms 34652 KB Output is correct
102 Correct 163 ms 40928 KB Output is correct
103 Correct 20 ms 24428 KB Output is correct
104 Correct 20 ms 24428 KB Output is correct
105 Correct 23 ms 24300 KB Output is correct
106 Correct 19 ms 24428 KB Output is correct
107 Correct 18 ms 24428 KB Output is correct
108 Correct 573 ms 79172 KB Output is correct
109 Correct 768 ms 97092 KB Output is correct
110 Correct 762 ms 110660 KB Output is correct
111 Correct 1103 ms 119492 KB Output is correct
112 Correct 880 ms 102596 KB Output is correct
113 Correct 613 ms 94512 KB Output is correct
114 Correct 723 ms 75584 KB Output is correct
115 Correct 724 ms 75332 KB Output is correct
116 Correct 739 ms 76096 KB Output is correct
117 Correct 1042 ms 92992 KB Output is correct
118 Correct 720 ms 74588 KB Output is correct
119 Correct 1024 ms 93124 KB Output is correct
120 Correct 1128 ms 109240 KB Output is correct
121 Correct 1020 ms 94340 KB Output is correct
122 Correct 817 ms 80704 KB Output is correct
123 Correct 627 ms 95684 KB Output is correct
124 Correct 948 ms 88184 KB Output is correct