Submission #763577

# Submission time Handle Problem Language Result Execution time Memory
763577 2023-06-22T13:12:30 Z MISM06 Mergers (JOI19_mergers) C++14
48 / 100
208 ms 262144 KB
//0 1 1 0 1
//0 1 0 0 1
//1 0 0 1 1
//0 1 1 0 1
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")

using namespace std;

#define F 			first
#define S 			second
#define pb 			push_back
#define sze			size()
#define	all(x)		x.begin() , x.end()
#define wall__		cout << "--------------------------------------\n";
#define kids		int mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1
#define file_io		freopen("input.cpp", "r", stdin); freopen("output.cpp", "w", stdout);

typedef long long ll;
typedef long double dl;
typedef pair < int , int > pii;
typedef pair < int , ll > pil;
typedef pair < ll , int > pli;
typedef pair < ll , ll > pll;
typedef pair < int , pii > piii;
typedef pair < ll, pll > plll;


const ll N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll inf = 2e16;
const ll INF = 1e9 + 10;
const ll lg = 32;

int n, k, a[N], timer, c[N], sub[N], cnt, lst = 0;
vector < int > g[N];
vector < pii > edg[N];
vector < int > mark[N];
int we[N];

void dfs (int v, int p) {
	sub[v] = 1; mark[v][a[v]] = 1; 
	for (auto u : g[v]) {
		if (u == p) continue;
		dfs(u, v);
		sub[v] += sub[u];
		for (int i = 1; i <= k; i++) mark[v][i] |= mark[u][i];
	}
	ll x = 0;
	for (int y = 1; y <= k; y++) {
		if (mark[v][y]) x += c[y];
	}
	int w = 0;
	if (x == sub[v]) {
		w = 1;
	}
	we[lst] = w;
	if (v != 1) {
		edg[v].pb({p, lst});
		edg[p].pb({v, lst});
	}
	++lst;
	cnt += w;
}

int h[N];
pii par[N];
void dfs0(int v, int p) {
	for (auto e : edg[v]) {
		int u = e.F;
		if (u == p) continue;
		h[u] = h[v] + we[e.S];
		dfs0(u, v);
	}
}
void dfs1(int v, int p, int id) {
	par[v] = {p, id};
	h[v] = h[p] + we[id];
	for (auto e : edg[v]) {
		int u = e.F;
		if (u == p) continue;
		dfs1(u, v, e.S);
	}
}

void solve () {

	cin >> n >> k;
	for (int i = 1; i < n; i++) {
		int v, u; cin >> v >> u;
		g[v].pb(u); g[u].pb(v);
	}
	for (int i = 1; i <= n; i++) {
		mark[i].resize(k + 1, 0);
	}
	for (int i = 1; i <= n; i++) {
		cin >> a[i]; ++c[a[i]];
	}
	dfs(1, 0);
	int ans = 0;
	--cnt;
	while(cnt) {
		fill(h, h + (n + 1), 0);
		dfs0(1, 0);
		int mx = 0;
		for (int i = 1; i <= n; i++) if (h[mx] < h[i]) mx = i;
		++ans;
		fill(h, h + (n + 1), 0);
		fill(par, par + (n + 1), make_pair(0, 0));
		dfs1(mx, 0, 0);
		int mx2 = 0;
		for (int i = 1; i <= n; i++) if (h[mx2] < h[i]) mx2 = i;
		while(mx2 != mx) {
			int id = par[mx2].S;
			cnt -= we[id];
			we[id] = 0;
			mx2 = par[mx2].F;
		}
	}
	cout << ans << '\n';


}


int main() {
	// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int t = 1;
	// cin >> t;
	while (t--) {solve();}
    return 0;
}
/*
*/
//shrek will AC this;
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7360 KB Output is correct
2 Correct 3 ms 7364 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 4 ms 7360 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7356 KB Output is correct
8 Correct 3 ms 7364 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 3 ms 7380 KB Output is correct
13 Correct 3 ms 7328 KB Output is correct
14 Correct 3 ms 7380 KB Output is correct
15 Correct 3 ms 7380 KB Output is correct
16 Correct 4 ms 7360 KB Output is correct
17 Correct 3 ms 7252 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 3 ms 7252 KB Output is correct
21 Correct 3 ms 7380 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 5 ms 7364 KB Output is correct
24 Correct 4 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7360 KB Output is correct
2 Correct 3 ms 7364 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 4 ms 7360 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7356 KB Output is correct
8 Correct 3 ms 7364 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 3 ms 7380 KB Output is correct
13 Correct 3 ms 7328 KB Output is correct
14 Correct 3 ms 7380 KB Output is correct
15 Correct 3 ms 7380 KB Output is correct
16 Correct 4 ms 7360 KB Output is correct
17 Correct 3 ms 7252 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 3 ms 7252 KB Output is correct
21 Correct 3 ms 7380 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 5 ms 7364 KB Output is correct
24 Correct 4 ms 7380 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 40 ms 31212 KB Output is correct
27 Correct 8 ms 8268 KB Output is correct
28 Correct 67 ms 43084 KB Output is correct
29 Correct 74 ms 42964 KB Output is correct
30 Correct 10 ms 10068 KB Output is correct
31 Correct 5 ms 7400 KB Output is correct
32 Correct 38 ms 43088 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 7 ms 8276 KB Output is correct
35 Correct 74 ms 42956 KB Output is correct
36 Correct 7 ms 8340 KB Output is correct
37 Correct 26 ms 25324 KB Output is correct
38 Correct 4 ms 7380 KB Output is correct
39 Correct 25 ms 25288 KB Output is correct
40 Correct 5 ms 7664 KB Output is correct
41 Correct 6 ms 7748 KB Output is correct
42 Correct 36 ms 25372 KB Output is correct
43 Correct 7 ms 8020 KB Output is correct
44 Correct 5 ms 7380 KB Output is correct
45 Correct 32 ms 41772 KB Output is correct
46 Correct 26 ms 24996 KB Output is correct
47 Correct 4 ms 7356 KB Output is correct
48 Correct 15 ms 14676 KB Output is correct
49 Correct 89 ms 43008 KB Output is correct
50 Correct 70 ms 43060 KB Output is correct
51 Correct 19 ms 14676 KB Output is correct
52 Correct 8 ms 8276 KB Output is correct
53 Correct 30 ms 25316 KB Output is correct
54 Correct 6 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7360 KB Output is correct
2 Correct 3 ms 7364 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 4 ms 7360 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7356 KB Output is correct
8 Correct 3 ms 7364 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 3 ms 7380 KB Output is correct
13 Correct 3 ms 7328 KB Output is correct
14 Correct 3 ms 7380 KB Output is correct
15 Correct 3 ms 7380 KB Output is correct
16 Correct 4 ms 7360 KB Output is correct
17 Correct 3 ms 7252 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 3 ms 7252 KB Output is correct
21 Correct 3 ms 7380 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 5 ms 7364 KB Output is correct
24 Correct 4 ms 7380 KB Output is correct
25 Correct 5 ms 7380 KB Output is correct
26 Correct 125 ms 39328 KB Output is correct
27 Correct 151 ms 38752 KB Output is correct
28 Correct 7 ms 8272 KB Output is correct
29 Correct 4 ms 7380 KB Output is correct
30 Correct 3 ms 7360 KB Output is correct
31 Correct 208 ms 40020 KB Output is correct
32 Correct 6 ms 8308 KB Output is correct
33 Correct 143 ms 49760 KB Output is correct
34 Correct 138 ms 40024 KB Output is correct
35 Correct 7 ms 8276 KB Output is correct
36 Correct 202 ms 40744 KB Output is correct
37 Correct 6 ms 7628 KB Output is correct
38 Correct 6 ms 7756 KB Output is correct
39 Correct 109 ms 39192 KB Output is correct
40 Correct 6 ms 8012 KB Output is correct
41 Correct 100 ms 19972 KB Output is correct
42 Correct 150 ms 43164 KB Output is correct
43 Correct 4 ms 7380 KB Output is correct
44 Correct 119 ms 31308 KB Output is correct
45 Correct 185 ms 45008 KB Output is correct
46 Correct 5 ms 8276 KB Output is correct
47 Correct 6 ms 8212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 39324 KB Output is correct
2 Runtime error 190 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7360 KB Output is correct
2 Correct 3 ms 7364 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 4 ms 7360 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7356 KB Output is correct
8 Correct 3 ms 7364 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 3 ms 7380 KB Output is correct
13 Correct 3 ms 7328 KB Output is correct
14 Correct 3 ms 7380 KB Output is correct
15 Correct 3 ms 7380 KB Output is correct
16 Correct 4 ms 7360 KB Output is correct
17 Correct 3 ms 7252 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 3 ms 7252 KB Output is correct
21 Correct 3 ms 7380 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 5 ms 7364 KB Output is correct
24 Correct 4 ms 7380 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 40 ms 31212 KB Output is correct
27 Correct 8 ms 8268 KB Output is correct
28 Correct 67 ms 43084 KB Output is correct
29 Correct 74 ms 42964 KB Output is correct
30 Correct 10 ms 10068 KB Output is correct
31 Correct 5 ms 7400 KB Output is correct
32 Correct 38 ms 43088 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 7 ms 8276 KB Output is correct
35 Correct 74 ms 42956 KB Output is correct
36 Correct 7 ms 8340 KB Output is correct
37 Correct 26 ms 25324 KB Output is correct
38 Correct 4 ms 7380 KB Output is correct
39 Correct 25 ms 25288 KB Output is correct
40 Correct 5 ms 7664 KB Output is correct
41 Correct 6 ms 7748 KB Output is correct
42 Correct 36 ms 25372 KB Output is correct
43 Correct 7 ms 8020 KB Output is correct
44 Correct 5 ms 7380 KB Output is correct
45 Correct 32 ms 41772 KB Output is correct
46 Correct 26 ms 24996 KB Output is correct
47 Correct 4 ms 7356 KB Output is correct
48 Correct 15 ms 14676 KB Output is correct
49 Correct 89 ms 43008 KB Output is correct
50 Correct 70 ms 43060 KB Output is correct
51 Correct 19 ms 14676 KB Output is correct
52 Correct 8 ms 8276 KB Output is correct
53 Correct 30 ms 25316 KB Output is correct
54 Correct 6 ms 8276 KB Output is correct
55 Correct 5 ms 7380 KB Output is correct
56 Correct 125 ms 39328 KB Output is correct
57 Correct 151 ms 38752 KB Output is correct
58 Correct 7 ms 8272 KB Output is correct
59 Correct 4 ms 7380 KB Output is correct
60 Correct 3 ms 7360 KB Output is correct
61 Correct 208 ms 40020 KB Output is correct
62 Correct 6 ms 8308 KB Output is correct
63 Correct 143 ms 49760 KB Output is correct
64 Correct 138 ms 40024 KB Output is correct
65 Correct 7 ms 8276 KB Output is correct
66 Correct 202 ms 40744 KB Output is correct
67 Correct 6 ms 7628 KB Output is correct
68 Correct 6 ms 7756 KB Output is correct
69 Correct 109 ms 39192 KB Output is correct
70 Correct 6 ms 8012 KB Output is correct
71 Correct 100 ms 19972 KB Output is correct
72 Correct 150 ms 43164 KB Output is correct
73 Correct 4 ms 7380 KB Output is correct
74 Correct 119 ms 31308 KB Output is correct
75 Correct 185 ms 45008 KB Output is correct
76 Correct 5 ms 8276 KB Output is correct
77 Correct 6 ms 8212 KB Output is correct
78 Correct 119 ms 39324 KB Output is correct
79 Runtime error 190 ms 262144 KB Execution killed with signal 9
80 Halted 0 ms 0 KB -