Submission #497880

# Submission time Handle Problem Language Result Execution time Memory
497880 2021-12-24T03:40:27 Z maomao90 Mergers (JOI19_mergers) C++17
100 / 100
1313 ms 161476 KB
// She will give birth to a son, and you are to give him the name Jesus,
// because he will save his people from their sins.
// Matthew 1:21
#include <bits/stdc++.h> 
using namespace std;

template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

#ifdef DEBUG
#define debug(args...) printf(args)
#else
#define debug(args...)
#endif

#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 500005
#define MAXL 20

int n, k;
vi adj[MAXN], nadj[MAXN];
int s[MAXN];
vi col[MAXN];
bool visited[MAXN];
int ans;

int pre[MAXN], pst[MAXN], l[MAXN], p[MAXL][MAXN], ptr;
void dfs(int u, int cp) {
	pre[u] = ptr++;
	p[0][u] = cp;
	REP (k, 1, MAXL) {
		if (p[k - 1][u] == -1) {
			p[k][u] = -1;
		} else {
			p[k][u] = p[k - 1][p[k - 1][u]];
		}
	}
	for (int v : adj[u]) {
		if (v == cp) continue;
		l[v] = l[u] + 1;
		dfs(v, u);
	}
	pst[u] = ptr;
}
int lca(int a, int b) {
	if (l[a] < l[b]) swap(a, b);
	RREP (k, MAXL - 1, 0) {
		if (p[k][a] == -1) continue;
		if (l[p[k][a]] >= l[b]) {
			a = p[k][a];
		}
	}
	if (a == b) {
		return a;
	}
	RREP (k, MAXL - 1, 0) {
		if (p[k][a] != p[k][b]) {
			a = p[k][a];
			b = p[k][b];
		}
	}
	return p[0][a];
}

int dsup[MAXN], rnk[MAXN], compp[MAXN];
int findp(int a) {
	if (dsup[a] == a) return a;
	return dsup[a] = findp(dsup[a]);
}
bool join(int a, int b) {
	int pa = findp(a), pb = findp(b);
	int np = compp[pb];
	if (pa == pb) return 0;
	if (rnk[pa] < rnk[pb]) swap(pa, pb);
	if (rnk[pa] == rnk[pb]) rnk[pa]++;
	dsup[pb] = pa;
	compp[pa] = np;
	return 1;
}

int main() {
	int _t = 1;
   	//scanf("%d", &_t);
	while (_t--) {
		scanf("%d%d", &n, &k);
		REP (i, 0, n + 1) {
			dsup[i] = i;
		}
		REP (i, 1, n) {
			int a, b; scanf("%d%d", &a, &b);
			adj[a].pb(b);
			adj[b].pb(a);
		}
		REP (i, 1, n + 1) {
			scanf("%d", &s[i]);
			col[s[i]].pb(i);
		}
		dfs(1, -1);
		REP (i, 1, n + 1) {
			compp[i] = p[0][i];
		}
		REP (i, 1, k + 1) {
			if (col[i].size() == 1) continue;
			sort(ALL(col[i]), [&] (int l, int r) {
					return pre[l] < pre[r];
					});
			int lc = col[i][0];
			for (int u : col[i]) {
				lc = lca(lc, u);
			}
			for (int u : col[i]) {
				while (compp[findp(u)] != -1 && l[compp[findp(u)]] >= l[lc]) {
					join(u, compp[findp(u)]);
				}
			}
		}
		int sze = 0;
		REP (u, 1, n + 1) {
			debug("%d: %d\n", u, findp(u));
			for (int v : adj[u]) {
				if (findp(u) == findp(v)) {
					continue;
				}
				nadj[findp(u)].pb(findp(v));
			}
		}
		REP (u, 1, n + 1) {
			if (visited[findp(u)]) continue;
			visited[findp(u)] = 1;
			sze++;
		}
		REP (i, 1, n + 1) {
			if (nadj[i].empty()) continue;
			sort(ALL(nadj[i]));
			nadj[i].erase(unique(ALL(nadj[i])), nadj[i].end());
		}
		REP (i, 1, n + 1) {
			if (nadj[i].size() == 1) {
				ans++;
			}
		}
		printf("%d\n", (ans + 1) / 2);
	}
	return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:106:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |   scanf("%d%d", &n, &k);
      |   ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:111:19: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |    int a, b; scanf("%d%d", &a, &b);
      |              ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:116:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |    scanf("%d", &s[i]);
      |    ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35660 KB Output is correct
2 Correct 22 ms 35660 KB Output is correct
3 Correct 22 ms 35660 KB Output is correct
4 Correct 20 ms 35652 KB Output is correct
5 Correct 18 ms 35664 KB Output is correct
6 Correct 19 ms 35660 KB Output is correct
7 Correct 18 ms 35648 KB Output is correct
8 Correct 18 ms 35660 KB Output is correct
9 Correct 18 ms 35668 KB Output is correct
10 Correct 19 ms 35648 KB Output is correct
11 Correct 19 ms 35692 KB Output is correct
12 Correct 18 ms 35660 KB Output is correct
13 Correct 20 ms 35692 KB Output is correct
14 Correct 23 ms 35680 KB Output is correct
15 Correct 19 ms 35672 KB Output is correct
16 Correct 18 ms 35660 KB Output is correct
17 Correct 18 ms 35740 KB Output is correct
18 Correct 19 ms 35708 KB Output is correct
19 Correct 18 ms 35732 KB Output is correct
20 Correct 18 ms 35660 KB Output is correct
21 Correct 18 ms 35660 KB Output is correct
22 Correct 18 ms 35748 KB Output is correct
23 Correct 17 ms 35660 KB Output is correct
24 Correct 18 ms 35716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35660 KB Output is correct
2 Correct 22 ms 35660 KB Output is correct
3 Correct 22 ms 35660 KB Output is correct
4 Correct 20 ms 35652 KB Output is correct
5 Correct 18 ms 35664 KB Output is correct
6 Correct 19 ms 35660 KB Output is correct
7 Correct 18 ms 35648 KB Output is correct
8 Correct 18 ms 35660 KB Output is correct
9 Correct 18 ms 35668 KB Output is correct
10 Correct 19 ms 35648 KB Output is correct
11 Correct 19 ms 35692 KB Output is correct
12 Correct 18 ms 35660 KB Output is correct
13 Correct 20 ms 35692 KB Output is correct
14 Correct 23 ms 35680 KB Output is correct
15 Correct 19 ms 35672 KB Output is correct
16 Correct 18 ms 35660 KB Output is correct
17 Correct 18 ms 35740 KB Output is correct
18 Correct 19 ms 35708 KB Output is correct
19 Correct 18 ms 35732 KB Output is correct
20 Correct 18 ms 35660 KB Output is correct
21 Correct 18 ms 35660 KB Output is correct
22 Correct 18 ms 35748 KB Output is correct
23 Correct 17 ms 35660 KB Output is correct
24 Correct 18 ms 35716 KB Output is correct
25 Correct 18 ms 35660 KB Output is correct
26 Correct 23 ms 36320 KB Output is correct
27 Correct 19 ms 36172 KB Output is correct
28 Correct 20 ms 36424 KB Output is correct
29 Correct 20 ms 36392 KB Output is correct
30 Correct 21 ms 36224 KB Output is correct
31 Correct 19 ms 35656 KB Output is correct
32 Correct 21 ms 36344 KB Output is correct
33 Correct 18 ms 35660 KB Output is correct
34 Correct 21 ms 36172 KB Output is correct
35 Correct 22 ms 36292 KB Output is correct
36 Correct 20 ms 36180 KB Output is correct
37 Correct 20 ms 36184 KB Output is correct
38 Correct 18 ms 35696 KB Output is correct
39 Correct 21 ms 36164 KB Output is correct
40 Correct 20 ms 36188 KB Output is correct
41 Correct 21 ms 36164 KB Output is correct
42 Correct 20 ms 36280 KB Output is correct
43 Correct 21 ms 36312 KB Output is correct
44 Correct 20 ms 35768 KB Output is correct
45 Correct 22 ms 36268 KB Output is correct
46 Correct 22 ms 36192 KB Output is correct
47 Correct 18 ms 35660 KB Output is correct
48 Correct 20 ms 36180 KB Output is correct
49 Correct 19 ms 36312 KB Output is correct
50 Correct 20 ms 36316 KB Output is correct
51 Correct 19 ms 36196 KB Output is correct
52 Correct 20 ms 36236 KB Output is correct
53 Correct 23 ms 36200 KB Output is correct
54 Correct 20 ms 36172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35660 KB Output is correct
2 Correct 22 ms 35660 KB Output is correct
3 Correct 22 ms 35660 KB Output is correct
4 Correct 20 ms 35652 KB Output is correct
5 Correct 18 ms 35664 KB Output is correct
6 Correct 19 ms 35660 KB Output is correct
7 Correct 18 ms 35648 KB Output is correct
8 Correct 18 ms 35660 KB Output is correct
9 Correct 18 ms 35668 KB Output is correct
10 Correct 19 ms 35648 KB Output is correct
11 Correct 19 ms 35692 KB Output is correct
12 Correct 18 ms 35660 KB Output is correct
13 Correct 20 ms 35692 KB Output is correct
14 Correct 23 ms 35680 KB Output is correct
15 Correct 19 ms 35672 KB Output is correct
16 Correct 18 ms 35660 KB Output is correct
17 Correct 18 ms 35740 KB Output is correct
18 Correct 19 ms 35708 KB Output is correct
19 Correct 18 ms 35732 KB Output is correct
20 Correct 18 ms 35660 KB Output is correct
21 Correct 18 ms 35660 KB Output is correct
22 Correct 18 ms 35748 KB Output is correct
23 Correct 17 ms 35660 KB Output is correct
24 Correct 18 ms 35716 KB Output is correct
25 Correct 18 ms 35676 KB Output is correct
26 Correct 125 ms 51404 KB Output is correct
27 Correct 129 ms 51452 KB Output is correct
28 Correct 21 ms 36184 KB Output is correct
29 Correct 19 ms 35660 KB Output is correct
30 Correct 18 ms 35660 KB Output is correct
31 Correct 132 ms 51268 KB Output is correct
32 Correct 20 ms 36172 KB Output is correct
33 Correct 160 ms 57232 KB Output is correct
34 Correct 132 ms 51512 KB Output is correct
35 Correct 22 ms 36192 KB Output is correct
36 Correct 167 ms 51828 KB Output is correct
37 Correct 21 ms 36172 KB Output is correct
38 Correct 21 ms 36180 KB Output is correct
39 Correct 110 ms 51260 KB Output is correct
40 Correct 22 ms 36312 KB Output is correct
41 Correct 126 ms 51208 KB Output is correct
42 Correct 157 ms 53312 KB Output is correct
43 Correct 17 ms 35660 KB Output is correct
44 Correct 146 ms 56952 KB Output is correct
45 Correct 169 ms 54352 KB Output is correct
46 Correct 20 ms 36152 KB Output is correct
47 Correct 24 ms 36192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 51404 KB Output is correct
2 Correct 126 ms 57852 KB Output is correct
3 Correct 21 ms 36224 KB Output is correct
4 Correct 20 ms 36236 KB Output is correct
5 Correct 22 ms 35648 KB Output is correct
6 Correct 19 ms 35744 KB Output is correct
7 Correct 23 ms 36096 KB Output is correct
8 Correct 192 ms 53280 KB Output is correct
9 Correct 23 ms 36172 KB Output is correct
10 Correct 157 ms 51676 KB Output is correct
11 Correct 21 ms 35660 KB Output is correct
12 Correct 153 ms 51676 KB Output is correct
13 Correct 178 ms 53372 KB Output is correct
14 Correct 129 ms 57352 KB Output is correct
15 Correct 122 ms 51512 KB Output is correct
16 Correct 21 ms 36172 KB Output is correct
17 Correct 19 ms 35660 KB Output is correct
18 Correct 119 ms 56896 KB Output is correct
19 Correct 135 ms 60288 KB Output is correct
20 Correct 21 ms 36172 KB Output is correct
21 Correct 19 ms 35656 KB Output is correct
22 Correct 123 ms 53276 KB Output is correct
23 Correct 21 ms 36192 KB Output is correct
24 Correct 171 ms 52348 KB Output is correct
25 Correct 134 ms 59228 KB Output is correct
26 Correct 21 ms 36312 KB Output is correct
27 Correct 21 ms 36324 KB Output is correct
28 Correct 21 ms 36216 KB Output is correct
29 Correct 21 ms 36116 KB Output is correct
30 Correct 19 ms 36164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35660 KB Output is correct
2 Correct 22 ms 35660 KB Output is correct
3 Correct 22 ms 35660 KB Output is correct
4 Correct 20 ms 35652 KB Output is correct
5 Correct 18 ms 35664 KB Output is correct
6 Correct 19 ms 35660 KB Output is correct
7 Correct 18 ms 35648 KB Output is correct
8 Correct 18 ms 35660 KB Output is correct
9 Correct 18 ms 35668 KB Output is correct
10 Correct 19 ms 35648 KB Output is correct
11 Correct 19 ms 35692 KB Output is correct
12 Correct 18 ms 35660 KB Output is correct
13 Correct 20 ms 35692 KB Output is correct
14 Correct 23 ms 35680 KB Output is correct
15 Correct 19 ms 35672 KB Output is correct
16 Correct 18 ms 35660 KB Output is correct
17 Correct 18 ms 35740 KB Output is correct
18 Correct 19 ms 35708 KB Output is correct
19 Correct 18 ms 35732 KB Output is correct
20 Correct 18 ms 35660 KB Output is correct
21 Correct 18 ms 35660 KB Output is correct
22 Correct 18 ms 35748 KB Output is correct
23 Correct 17 ms 35660 KB Output is correct
24 Correct 18 ms 35716 KB Output is correct
25 Correct 18 ms 35660 KB Output is correct
26 Correct 23 ms 36320 KB Output is correct
27 Correct 19 ms 36172 KB Output is correct
28 Correct 20 ms 36424 KB Output is correct
29 Correct 20 ms 36392 KB Output is correct
30 Correct 21 ms 36224 KB Output is correct
31 Correct 19 ms 35656 KB Output is correct
32 Correct 21 ms 36344 KB Output is correct
33 Correct 18 ms 35660 KB Output is correct
34 Correct 21 ms 36172 KB Output is correct
35 Correct 22 ms 36292 KB Output is correct
36 Correct 20 ms 36180 KB Output is correct
37 Correct 20 ms 36184 KB Output is correct
38 Correct 18 ms 35696 KB Output is correct
39 Correct 21 ms 36164 KB Output is correct
40 Correct 20 ms 36188 KB Output is correct
41 Correct 21 ms 36164 KB Output is correct
42 Correct 20 ms 36280 KB Output is correct
43 Correct 21 ms 36312 KB Output is correct
44 Correct 20 ms 35768 KB Output is correct
45 Correct 22 ms 36268 KB Output is correct
46 Correct 22 ms 36192 KB Output is correct
47 Correct 18 ms 35660 KB Output is correct
48 Correct 20 ms 36180 KB Output is correct
49 Correct 19 ms 36312 KB Output is correct
50 Correct 20 ms 36316 KB Output is correct
51 Correct 19 ms 36196 KB Output is correct
52 Correct 20 ms 36236 KB Output is correct
53 Correct 23 ms 36200 KB Output is correct
54 Correct 20 ms 36172 KB Output is correct
55 Correct 18 ms 35676 KB Output is correct
56 Correct 125 ms 51404 KB Output is correct
57 Correct 129 ms 51452 KB Output is correct
58 Correct 21 ms 36184 KB Output is correct
59 Correct 19 ms 35660 KB Output is correct
60 Correct 18 ms 35660 KB Output is correct
61 Correct 132 ms 51268 KB Output is correct
62 Correct 20 ms 36172 KB Output is correct
63 Correct 160 ms 57232 KB Output is correct
64 Correct 132 ms 51512 KB Output is correct
65 Correct 22 ms 36192 KB Output is correct
66 Correct 167 ms 51828 KB Output is correct
67 Correct 21 ms 36172 KB Output is correct
68 Correct 21 ms 36180 KB Output is correct
69 Correct 110 ms 51260 KB Output is correct
70 Correct 22 ms 36312 KB Output is correct
71 Correct 126 ms 51208 KB Output is correct
72 Correct 157 ms 53312 KB Output is correct
73 Correct 17 ms 35660 KB Output is correct
74 Correct 146 ms 56952 KB Output is correct
75 Correct 169 ms 54352 KB Output is correct
76 Correct 20 ms 36152 KB Output is correct
77 Correct 24 ms 36192 KB Output is correct
78 Correct 115 ms 51404 KB Output is correct
79 Correct 126 ms 57852 KB Output is correct
80 Correct 21 ms 36224 KB Output is correct
81 Correct 20 ms 36236 KB Output is correct
82 Correct 22 ms 35648 KB Output is correct
83 Correct 19 ms 35744 KB Output is correct
84 Correct 23 ms 36096 KB Output is correct
85 Correct 192 ms 53280 KB Output is correct
86 Correct 23 ms 36172 KB Output is correct
87 Correct 157 ms 51676 KB Output is correct
88 Correct 21 ms 35660 KB Output is correct
89 Correct 153 ms 51676 KB Output is correct
90 Correct 178 ms 53372 KB Output is correct
91 Correct 129 ms 57352 KB Output is correct
92 Correct 122 ms 51512 KB Output is correct
93 Correct 21 ms 36172 KB Output is correct
94 Correct 19 ms 35660 KB Output is correct
95 Correct 119 ms 56896 KB Output is correct
96 Correct 135 ms 60288 KB Output is correct
97 Correct 21 ms 36172 KB Output is correct
98 Correct 19 ms 35656 KB Output is correct
99 Correct 123 ms 53276 KB Output is correct
100 Correct 21 ms 36192 KB Output is correct
101 Correct 171 ms 52348 KB Output is correct
102 Correct 134 ms 59228 KB Output is correct
103 Correct 21 ms 36312 KB Output is correct
104 Correct 21 ms 36324 KB Output is correct
105 Correct 21 ms 36216 KB Output is correct
106 Correct 21 ms 36116 KB Output is correct
107 Correct 19 ms 36164 KB Output is correct
108 Correct 871 ms 122348 KB Output is correct
109 Correct 1280 ms 126672 KB Output is correct
110 Correct 1211 ms 135672 KB Output is correct
111 Correct 1002 ms 161476 KB Output is correct
112 Correct 1012 ms 148380 KB Output is correct
113 Correct 704 ms 142772 KB Output is correct
114 Correct 825 ms 112348 KB Output is correct
115 Correct 824 ms 112296 KB Output is correct
116 Correct 1313 ms 119488 KB Output is correct
117 Correct 814 ms 144964 KB Output is correct
118 Correct 957 ms 114696 KB Output is correct
119 Correct 796 ms 144916 KB Output is correct
120 Correct 991 ms 155364 KB Output is correct
121 Correct 814 ms 145556 KB Output is correct
122 Correct 1150 ms 127332 KB Output is correct
123 Correct 665 ms 147756 KB Output is correct
124 Correct 824 ms 138448 KB Output is correct