답안 #1095388

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1095388 2024-10-02T03:14:23 Z hahahoang132 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
189 ms 64592 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define bit(x,y) ((x >> y) & 1LL)
const ll mod = 1e9 + 7;
const ll N = 2e5 + 5;
const ll inf = LLONG_MAX/4;
ll p[N],h[N],ans[N],tmp[N];
ll par[21][N];
vector<ll> a[N];
ll fp(ll x)
{
    if(p[x] == -1) return x;
    else
    {
        p[x] = fp(p[x]);
        return p[x];
    }
}
ll lca(ll x, ll y)
{
    if(h[x] > h[y]) swap(x,y);
    ll dis = h[y] - h[x];
    ll i,j;
    for(i = 20;i >= 0;i--)
    {
        if(dis >= (1 << i))
        {
            y = par[i][y];
            dis -= (1 << i);
        }
    }
    if(x == y) return x;
    for(i = 20;i >= 0;i--)
    {
        if(par[i][x] != par[i][y])
        {
            x = par[i][x];
            y = par[i][y];
        }
    }
    return par[0][x];
}
void hop(ll x, ll y)
{
    ll px = x;
    ll py = fp(y);
    ll z = lca(px,py);
    ans[px] = max(ans[px],ans[py] + h[px] + h[py] - 2 * h[z]);
    p[py] = px;
}
void build(ll x, ll px)
{
    for(auto y : a[x])
    {
        if(y != px)
        {
            par[0][y] = x;
            h[y] = h[x] + 1;
            build(y,x);
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin >> n;
    ll i,j;
    for(i = 1;i <= n;i++)
    {
        cin >> tmp[i];
    }
    for(i = 1;i < n;i++)
    {
        ll u,v;
        cin >>  u>> v;
        a[tmp[u]].push_back(tmp[v]);
        a[tmp[v]].push_back(tmp[u]);
    }
    build(n,0);
    for(i = 1;i <= 20;i++)
    {
        for(j = 1;j <= n;j++) par[i][j] = par[i - 1][par[i - 1][j]];
    }
    for(i = 1;i <= n;i++)
    {
        p[i] = -1;
        ans[i] = 0;
    }
    for(i = 1;i <= n;i++)
    {
        for(auto x : a[i])
        {
            if(x < i) hop(i,x);
        }
    }
    cout << ans[n];
}

Compilation message

Main.cpp: In function 'long long int lca(long long int, long long int)':
Main.cpp:24:10: warning: unused variable 'j' [-Wunused-variable]
   24 |     ll i,j;
      |          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5208 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5208 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5464 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5208 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5464 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5268 KB Output is correct
18 Correct 4 ms 6748 KB Output is correct
19 Correct 4 ms 6748 KB Output is correct
20 Correct 4 ms 6744 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 4 ms 6492 KB Output is correct
23 Correct 4 ms 6492 KB Output is correct
24 Correct 4 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5208 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5464 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5268 KB Output is correct
18 Correct 4 ms 6748 KB Output is correct
19 Correct 4 ms 6748 KB Output is correct
20 Correct 4 ms 6744 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 4 ms 6492 KB Output is correct
23 Correct 4 ms 6492 KB Output is correct
24 Correct 4 ms 6492 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 5 ms 6492 KB Output is correct
27 Correct 4 ms 6488 KB Output is correct
28 Correct 4 ms 6492 KB Output is correct
29 Correct 4 ms 6492 KB Output is correct
30 Correct 6 ms 6488 KB Output is correct
31 Correct 5 ms 6488 KB Output is correct
32 Correct 4 ms 6492 KB Output is correct
33 Correct 4 ms 6572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5208 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5464 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5268 KB Output is correct
18 Correct 4 ms 6748 KB Output is correct
19 Correct 4 ms 6748 KB Output is correct
20 Correct 4 ms 6744 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 4 ms 6492 KB Output is correct
23 Correct 4 ms 6492 KB Output is correct
24 Correct 4 ms 6492 KB Output is correct
25 Correct 76 ms 63060 KB Output is correct
26 Correct 90 ms 63060 KB Output is correct
27 Correct 80 ms 62832 KB Output is correct
28 Correct 110 ms 59728 KB Output is correct
29 Correct 104 ms 61524 KB Output is correct
30 Correct 104 ms 61008 KB Output is correct
31 Correct 111 ms 61776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 145 ms 55636 KB Output is correct
4 Correct 148 ms 55920 KB Output is correct
5 Correct 151 ms 55888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5208 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5464 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5268 KB Output is correct
18 Correct 4 ms 6748 KB Output is correct
19 Correct 4 ms 6748 KB Output is correct
20 Correct 4 ms 6744 KB Output is correct
21 Correct 4 ms 6492 KB Output is correct
22 Correct 4 ms 6492 KB Output is correct
23 Correct 4 ms 6492 KB Output is correct
24 Correct 4 ms 6492 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 5 ms 6492 KB Output is correct
27 Correct 4 ms 6488 KB Output is correct
28 Correct 4 ms 6492 KB Output is correct
29 Correct 4 ms 6492 KB Output is correct
30 Correct 6 ms 6488 KB Output is correct
31 Correct 5 ms 6488 KB Output is correct
32 Correct 4 ms 6492 KB Output is correct
33 Correct 4 ms 6572 KB Output is correct
34 Correct 76 ms 63060 KB Output is correct
35 Correct 90 ms 63060 KB Output is correct
36 Correct 80 ms 62832 KB Output is correct
37 Correct 110 ms 59728 KB Output is correct
38 Correct 104 ms 61524 KB Output is correct
39 Correct 104 ms 61008 KB Output is correct
40 Correct 111 ms 61776 KB Output is correct
41 Correct 2 ms 5208 KB Output is correct
42 Correct 2 ms 5212 KB Output is correct
43 Correct 145 ms 55636 KB Output is correct
44 Correct 148 ms 55920 KB Output is correct
45 Correct 151 ms 55888 KB Output is correct
46 Correct 107 ms 63312 KB Output is correct
47 Correct 105 ms 63568 KB Output is correct
48 Correct 108 ms 64592 KB Output is correct
49 Correct 98 ms 63316 KB Output is correct
50 Correct 189 ms 55124 KB Output is correct
51 Correct 152 ms 55132 KB Output is correct
52 Correct 160 ms 55376 KB Output is correct
53 Correct 150 ms 55124 KB Output is correct