Submission #1057331

# Submission time Handle Problem Language Result Execution time Memory
1057331 2024-08-13T17:20:37 Z mnbvcxz123 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
146 ms 57372 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 3 ms 16220 KB Output is correct
2 Correct 3 ms 16220 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 3 ms 16284 KB Output is correct
8 Correct 2 ms 16220 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16220 KB Output is correct
2 Correct 3 ms 16220 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 3 ms 16284 KB Output is correct
8 Correct 2 ms 16220 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16320 KB Output is correct
11 Correct 2 ms 16220 KB Output is correct
12 Correct 3 ms 16216 KB Output is correct
13 Correct 3 ms 16220 KB Output is correct
14 Correct 3 ms 16220 KB Output is correct
15 Correct 2 ms 16220 KB Output is correct
16 Correct 3 ms 16220 KB Output is correct
17 Correct 2 ms 16220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16220 KB Output is correct
2 Correct 3 ms 16220 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 3 ms 16284 KB Output is correct
8 Correct 2 ms 16220 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16320 KB Output is correct
11 Correct 2 ms 16220 KB Output is correct
12 Correct 3 ms 16216 KB Output is correct
13 Correct 3 ms 16220 KB Output is correct
14 Correct 3 ms 16220 KB Output is correct
15 Correct 2 ms 16220 KB Output is correct
16 Correct 3 ms 16220 KB Output is correct
17 Correct 2 ms 16220 KB Output is correct
18 Correct 4 ms 16908 KB Output is correct
19 Correct 3 ms 16848 KB Output is correct
20 Correct 4 ms 16732 KB Output is correct
21 Correct 4 ms 16732 KB Output is correct
22 Correct 4 ms 16732 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 3 ms 16220 KB Output is correct
2 Correct 3 ms 16220 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 3 ms 16284 KB Output is correct
8 Correct 2 ms 16220 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16320 KB Output is correct
11 Correct 2 ms 16220 KB Output is correct
12 Correct 3 ms 16216 KB Output is correct
13 Correct 3 ms 16220 KB Output is correct
14 Correct 3 ms 16220 KB Output is correct
15 Correct 2 ms 16220 KB Output is correct
16 Correct 3 ms 16220 KB Output is correct
17 Correct 2 ms 16220 KB Output is correct
18 Correct 4 ms 16908 KB Output is correct
19 Correct 3 ms 16848 KB Output is correct
20 Correct 4 ms 16732 KB Output is correct
21 Correct 4 ms 16732 KB Output is correct
22 Correct 4 ms 16732 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 5 ms 16732 KB Output is correct
27 Correct 3 ms 16800 KB Output is correct
28 Correct 3 ms 16780 KB Output is correct
29 Correct 4 ms 16732 KB Output is correct
30 Correct 3 ms 16476 KB Output is correct
31 Correct 5 ms 16588 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 3 ms 16220 KB Output is correct
2 Correct 3 ms 16220 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 3 ms 16284 KB Output is correct
8 Correct 2 ms 16220 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16320 KB Output is correct
11 Correct 2 ms 16220 KB Output is correct
12 Correct 3 ms 16216 KB Output is correct
13 Correct 3 ms 16220 KB Output is correct
14 Correct 3 ms 16220 KB Output is correct
15 Correct 2 ms 16220 KB Output is correct
16 Correct 3 ms 16220 KB Output is correct
17 Correct 2 ms 16220 KB Output is correct
18 Correct 4 ms 16908 KB Output is correct
19 Correct 3 ms 16848 KB Output is correct
20 Correct 4 ms 16732 KB Output is correct
21 Correct 4 ms 16732 KB Output is correct
22 Correct 4 ms 16732 KB Output is correct
23 Correct 4 ms 16732 KB Output is correct
24 Correct 4 ms 16732 KB Output is correct
25 Correct 77 ms 57372 KB Output is correct
26 Correct 70 ms 55636 KB Output is correct
27 Correct 75 ms 55596 KB Output is correct
28 Correct 103 ms 51612 KB Output is correct
29 Correct 96 ms 47452 KB Output is correct
30 Correct 117 ms 50600 KB Output is correct
31 Correct 101 ms 49460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16220 KB Output is correct
2 Correct 3 ms 16140 KB Output is correct
3 Correct 118 ms 43360 KB Output is correct
4 Correct 117 ms 43772 KB Output is correct
5 Correct 101 ms 43304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16220 KB Output is correct
2 Correct 3 ms 16220 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 3 ms 16284 KB Output is correct
8 Correct 2 ms 16220 KB Output is correct
9 Correct 2 ms 16220 KB Output is correct
10 Correct 3 ms 16320 KB Output is correct
11 Correct 2 ms 16220 KB Output is correct
12 Correct 3 ms 16216 KB Output is correct
13 Correct 3 ms 16220 KB Output is correct
14 Correct 3 ms 16220 KB Output is correct
15 Correct 2 ms 16220 KB Output is correct
16 Correct 3 ms 16220 KB Output is correct
17 Correct 2 ms 16220 KB Output is correct
18 Correct 4 ms 16908 KB Output is correct
19 Correct 3 ms 16848 KB Output is correct
20 Correct 4 ms 16732 KB Output is correct
21 Correct 4 ms 16732 KB Output is correct
22 Correct 4 ms 16732 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 5 ms 16732 KB Output is correct
27 Correct 3 ms 16800 KB Output is correct
28 Correct 3 ms 16780 KB Output is correct
29 Correct 4 ms 16732 KB Output is correct
30 Correct 3 ms 16476 KB Output is correct
31 Correct 5 ms 16588 KB Output is correct
32 Correct 4 ms 16476 KB Output is correct
33 Correct 3 ms 16476 KB Output is correct
34 Correct 77 ms 57372 KB Output is correct
35 Correct 70 ms 55636 KB Output is correct
36 Correct 75 ms 55596 KB Output is correct
37 Correct 103 ms 51612 KB Output is correct
38 Correct 96 ms 47452 KB Output is correct
39 Correct 117 ms 50600 KB Output is correct
40 Correct 101 ms 49460 KB Output is correct
41 Correct 3 ms 16220 KB Output is correct
42 Correct 3 ms 16140 KB Output is correct
43 Correct 118 ms 43360 KB Output is correct
44 Correct 117 ms 43772 KB Output is correct
45 Correct 101 ms 43304 KB Output is correct
46 Correct 92 ms 52560 KB Output is correct
47 Correct 130 ms 52664 KB Output is correct
48 Correct 108 ms 52540 KB Output is correct
49 Correct 116 ms 52680 KB Output is correct
50 Correct 146 ms 42864 KB Output is correct
51 Correct 130 ms 42832 KB Output is correct
52 Correct 122 ms 42720 KB Output is correct
53 Correct 127 ms 42744 KB Output is correct