Submission #1049588

# Submission time Handle Problem Language Result Execution time Memory
1049588 2024-08-09T00:17:26 Z ymm Cat Exercise (JOI23_ho_t4) C++17
100 / 100
127 ms 55640 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 200'010;
const int lg = 20;
vector<int> A[N];
int val[N];
int anc[N][lg];
int height[N];

void dfs1(int v, int p, int h)
{
	anc[v][0] = p;
	Loop (i,0,lg-1)
		anc[v][i+1] = anc[anc[v][i]][i];
	height[v] = h;
	for (int u : A[v])
		if (u != p)
			dfs1(u, v, h+1);
}

int lca(int v, int u)
{
	if (height[v] < height[u])
		swap(v, u);
	int diff = height[v] - height[u];
	Loop (i,0,lg)
		if ((diff>>i)&1)
			v = anc[v][i];
	if (v == u)
		return v;
	LoopR (i,0,lg) {
		if (anc[v][i] != anc[u][i]) {
			v = anc[v][i];
			u = anc[u][i];
		}
	}
	return anc[v][0];
}

int dis(int v, int u)
{
	return height[v] + height[u] - 2 * height[lca(v, u)];
}

namespace dsu {
	int par[N];
	int p2[N];

	void init(int n) {
		fill(par, par+n, -1);
		fill(p2, p2+n, -1);
	}

	int rt(int v) { return p2[v] == -1? v: (p2[v] = rt(p2[v])); }

	void unite(int v, int u) {
		v = rt(v);
		u = rt(u);
		par[u] = v;
		p2[u] = v;
	}
}

vector<int> A2[N];
ll ans;

void dfs(int v, ll h)
{
	ans = max(ans, h);
	for (int u : A2[v])
		dfs(u, h + dis(v, u));
}

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	int n;
	cin >> n;
	Loop (i,0,n)
		cin >> val[i];
	Loop (i,1,n) {
		int v, u;
		cin >> v >> u;
		v = val[v-1]-1;
		u = val[u-1]-1;
		A[v].push_back(u);
		A[u].push_back(v);
	}
	dfs1(0, 0, 0);
	dsu::init(n);
	Loop (v,0,n) {
		for (int u : A[v]) {
			if (v > u)
				dsu::unite(v, u);
		}
	}
	int rt = -1;
	Loop (i,0,n) {
		if (dsu::par[i] == -1)
			rt = i;
		else
			A2[dsu::par[i]].push_back(i);
	}
	dfs(rt, 0);
	cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16216 KB Output is correct
2 Correct 2 ms 16216 KB Output is correct
3 Correct 2 ms 16220 KB Output is correct
4 Correct 2 ms 16220 KB Output is correct
5 Correct 2 ms 16220 KB Output is correct
6 Correct 2 ms 16220 KB Output is correct
7 Correct 2 ms 16220 KB Output is correct
8 Correct 2 ms 16216 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16216 KB Output is correct
2 Correct 2 ms 16216 KB Output is correct
3 Correct 2 ms 16220 KB Output is correct
4 Correct 2 ms 16220 KB Output is correct
5 Correct 2 ms 16220 KB Output is correct
6 Correct 2 ms 16220 KB Output is correct
7 Correct 2 ms 16220 KB Output is correct
8 Correct 2 ms 16216 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16216 KB Output is correct
11 Correct 2 ms 16220 KB Output is correct
12 Correct 2 ms 16220 KB Output is correct
13 Correct 2 ms 16220 KB Output is correct
14 Correct 2 ms 16344 KB Output is correct
15 Correct 2 ms 16220 KB Output is correct
16 Correct 2 ms 16220 KB Output is correct
17 Correct 2 ms 16220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16216 KB Output is correct
2 Correct 2 ms 16216 KB Output is correct
3 Correct 2 ms 16220 KB Output is correct
4 Correct 2 ms 16220 KB Output is correct
5 Correct 2 ms 16220 KB Output is correct
6 Correct 2 ms 16220 KB Output is correct
7 Correct 2 ms 16220 KB Output is correct
8 Correct 2 ms 16216 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16216 KB Output is correct
11 Correct 2 ms 16220 KB Output is correct
12 Correct 2 ms 16220 KB Output is correct
13 Correct 2 ms 16220 KB Output is correct
14 Correct 2 ms 16344 KB Output is correct
15 Correct 2 ms 16220 KB Output is correct
16 Correct 2 ms 16220 KB Output is correct
17 Correct 2 ms 16220 KB Output is correct
18 Correct 5 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 4 ms 16728 KB Output is correct
21 Correct 3 ms 16476 KB Output is correct
22 Correct 3 ms 16556 KB Output is correct
23 Correct 4 ms 16732 KB Output is correct
24 Correct 4 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16216 KB Output is correct
2 Correct 2 ms 16216 KB Output is correct
3 Correct 2 ms 16220 KB Output is correct
4 Correct 2 ms 16220 KB Output is correct
5 Correct 2 ms 16220 KB Output is correct
6 Correct 2 ms 16220 KB Output is correct
7 Correct 2 ms 16220 KB Output is correct
8 Correct 2 ms 16216 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16216 KB Output is correct
11 Correct 2 ms 16220 KB Output is correct
12 Correct 2 ms 16220 KB Output is correct
13 Correct 2 ms 16220 KB Output is correct
14 Correct 2 ms 16344 KB Output is correct
15 Correct 2 ms 16220 KB Output is correct
16 Correct 2 ms 16220 KB Output is correct
17 Correct 2 ms 16220 KB Output is correct
18 Correct 5 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 4 ms 16728 KB Output is correct
21 Correct 3 ms 16476 KB Output is correct
22 Correct 3 ms 16556 KB Output is correct
23 Correct 4 ms 16732 KB Output is correct
24 Correct 4 ms 16732 KB Output is correct
25 Correct 2 ms 16220 KB Output is correct
26 Correct 4 ms 16732 KB Output is correct
27 Correct 3 ms 16732 KB Output is correct
28 Correct 3 ms 16828 KB Output is correct
29 Correct 4 ms 16732 KB Output is correct
30 Correct 4 ms 16476 KB Output is correct
31 Correct 4 ms 16476 KB Output is correct
32 Correct 4 ms 16476 KB Output is correct
33 Correct 3 ms 16476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16216 KB Output is correct
2 Correct 2 ms 16216 KB Output is correct
3 Correct 2 ms 16220 KB Output is correct
4 Correct 2 ms 16220 KB Output is correct
5 Correct 2 ms 16220 KB Output is correct
6 Correct 2 ms 16220 KB Output is correct
7 Correct 2 ms 16220 KB Output is correct
8 Correct 2 ms 16216 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16216 KB Output is correct
11 Correct 2 ms 16220 KB Output is correct
12 Correct 2 ms 16220 KB Output is correct
13 Correct 2 ms 16220 KB Output is correct
14 Correct 2 ms 16344 KB Output is correct
15 Correct 2 ms 16220 KB Output is correct
16 Correct 2 ms 16220 KB Output is correct
17 Correct 2 ms 16220 KB Output is correct
18 Correct 5 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 4 ms 16728 KB Output is correct
21 Correct 3 ms 16476 KB Output is correct
22 Correct 3 ms 16556 KB Output is correct
23 Correct 4 ms 16732 KB Output is correct
24 Correct 4 ms 16732 KB Output is correct
25 Correct 71 ms 53588 KB Output is correct
26 Correct 91 ms 55636 KB Output is correct
27 Correct 82 ms 55640 KB Output is correct
28 Correct 91 ms 51540 KB Output is correct
29 Correct 90 ms 47440 KB Output is correct
30 Correct 91 ms 50512 KB Output is correct
31 Correct 86 ms 49488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16220 KB Output is correct
2 Correct 2 ms 16220 KB Output is correct
3 Correct 84 ms 39660 KB Output is correct
4 Correct 81 ms 39764 KB Output is correct
5 Correct 95 ms 39472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16216 KB Output is correct
2 Correct 2 ms 16216 KB Output is correct
3 Correct 2 ms 16220 KB Output is correct
4 Correct 2 ms 16220 KB Output is correct
5 Correct 2 ms 16220 KB Output is correct
6 Correct 2 ms 16220 KB Output is correct
7 Correct 2 ms 16220 KB Output is correct
8 Correct 2 ms 16216 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16216 KB Output is correct
11 Correct 2 ms 16220 KB Output is correct
12 Correct 2 ms 16220 KB Output is correct
13 Correct 2 ms 16220 KB Output is correct
14 Correct 2 ms 16344 KB Output is correct
15 Correct 2 ms 16220 KB Output is correct
16 Correct 2 ms 16220 KB Output is correct
17 Correct 2 ms 16220 KB Output is correct
18 Correct 5 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 4 ms 16728 KB Output is correct
21 Correct 3 ms 16476 KB Output is correct
22 Correct 3 ms 16556 KB Output is correct
23 Correct 4 ms 16732 KB Output is correct
24 Correct 4 ms 16732 KB Output is correct
25 Correct 2 ms 16220 KB Output is correct
26 Correct 4 ms 16732 KB Output is correct
27 Correct 3 ms 16732 KB Output is correct
28 Correct 3 ms 16828 KB Output is correct
29 Correct 4 ms 16732 KB Output is correct
30 Correct 4 ms 16476 KB Output is correct
31 Correct 4 ms 16476 KB Output is correct
32 Correct 4 ms 16476 KB Output is correct
33 Correct 3 ms 16476 KB Output is correct
34 Correct 71 ms 53588 KB Output is correct
35 Correct 91 ms 55636 KB Output is correct
36 Correct 82 ms 55640 KB Output is correct
37 Correct 91 ms 51540 KB Output is correct
38 Correct 90 ms 47440 KB Output is correct
39 Correct 91 ms 50512 KB Output is correct
40 Correct 86 ms 49488 KB Output is correct
41 Correct 2 ms 16220 KB Output is correct
42 Correct 2 ms 16220 KB Output is correct
43 Correct 84 ms 39660 KB Output is correct
44 Correct 81 ms 39764 KB Output is correct
45 Correct 95 ms 39472 KB Output is correct
46 Correct 83 ms 52560 KB Output is correct
47 Correct 86 ms 52640 KB Output is correct
48 Correct 94 ms 52592 KB Output is correct
49 Correct 82 ms 52492 KB Output is correct
50 Correct 119 ms 42832 KB Output is correct
51 Correct 106 ms 42836 KB Output is correct
52 Correct 127 ms 42836 KB Output is correct
53 Correct 99 ms 42836 KB Output is correct