Submission #834705

# Submission time Handle Problem Language Result Execution time Memory
834705 2023-08-22T17:26:30 Z NK_ Mergers (JOI19_mergers) C++17
10 / 100
124 ms 21240 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
 
using namespace std;
 
#define nl '\n'
#define pb push_back
#define pf push_front
 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
 
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
 
using db = long double;
 
template<class T> using pq = priority_queue<T, V<T>, less<T>>;
 
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int LG = 19;

int main() {
	cin.tie(0)->sync_with_stdio(0);
  	
	int N, K; cin >> N >> K;

	V<vi> adj(N); for(int i = 0; i < N - 1; i++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	vi A(N); for(auto& x : A) { cin >> x; --x; }

	int R = -1; for(int i = 0; i < N; i++) if (sz(adj[i]) >= 2) R = i;

	if (R == -1) {
		if (N == 1) cout << 0 << nl;
		else {
			if (A.front() == A.back()) cout << 0 << nl;
			else cout << 1 << nl;
		}
		exit(0-0);
	}

	V<vi> up(N, vi(LG)); vi dep(N);
	function<void(int, int)> gen = [&](int u, int p) {
		up[u][0] = p; dep[u] = (u == p ? 0 : dep[p] + 1);
		for(int i = 1; i < LG; i++) up[u][i] = up[up[u][i-1]][i-1];

		for(auto& v : adj[u]) if (v != p) gen(v, u);
	};
	
	auto jmp = [&](int u, int d) {
		for(int i = 0; i < LG; i++) if ((d >> i) & 1) u = up[u][i];
		return u;
	};

	auto lca = [&](int a, int b) {
		if (dep[a] < dep[b]) swap(a, b);
		a = jmp(a, dep[a] - dep[b]);
		if (a == b) return a;

		for(int i = LG - 1; i >= 0; i--) {
			if (up[a][i] != up[b][i]) a = up[a][i], b = up[b][i];
		}
		return up[a][0];
	};

	gen(R, R);

	vi L(K, -1); for(int u = 0; u < N; u++) {
		if (L[A[u]] == -1) L[A[u]] = u;
		else L[A[u]] = lca(L[A[u]], u);
	}

	// cerr << "R: " << R << endl;

	int ans = 0; vi U(N);
	function<int(int, int)> dfs = [&](int u, int p) {
		int amt = 0; U[u] = L[A[u]];
		for(auto& v : adj[u]) if (v != p) {
			amt += dfs(v, u);
			U[u] = lca(U[u], U[v]);
		}

		if (U[u] == u) {
			if (amt == 0) ans++;
			amt++;
		}

		return amt;
	};

	if (dfs(R, R) == 1) cout << 0 << nl;
	else cout << (ans + 1) / 2 << nl;

	// cerr << "ANS: " << ans << endl;

    return 0;	
} 	
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 0 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 320 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 0 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 320 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 2 ms 852 KB Output is correct
27 Correct 2 ms 852 KB Output is correct
28 Correct 3 ms 976 KB Output is correct
29 Correct 2 ms 840 KB Output is correct
30 Correct 2 ms 840 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 2 ms 1108 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 2 ms 852 KB Output is correct
35 Correct 2 ms 852 KB Output is correct
36 Incorrect 2 ms 852 KB Output isn't correct
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 0 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 320 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 65 ms 20536 KB Output is correct
27 Correct 108 ms 20236 KB Output is correct
28 Correct 2 ms 852 KB Output is correct
29 Correct 0 ms 300 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Incorrect 92 ms 20232 KB Output isn't correct
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 61 ms 20544 KB Output is correct
2 Correct 78 ms 21240 KB Output is correct
3 Correct 2 ms 852 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 124 ms 20740 KB Output is correct
9 Incorrect 2 ms 852 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 0 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 320 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 2 ms 852 KB Output is correct
27 Correct 2 ms 852 KB Output is correct
28 Correct 3 ms 976 KB Output is correct
29 Correct 2 ms 840 KB Output is correct
30 Correct 2 ms 840 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 2 ms 1108 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 2 ms 852 KB Output is correct
35 Correct 2 ms 852 KB Output is correct
36 Incorrect 2 ms 852 KB Output isn't correct
37 Halted 0 ms 0 KB -