답안 #888267

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
888267 2023-12-16T18:04:48 Z ace5 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
483 ms 69972 KB
#include <bits/stdc++.h>

using namespace std;

#define int int64_t

const int maxn = 200001;
const int maxlogn = 19;

vector<int> g[maxn];
int h[maxn];
int p[maxn];
pair<int,int> mp[maxn];
int dp[maxn];
int pp[maxn];
int la[maxlogn][maxn];
int d[maxn];
int tin[maxn],tout[maxn];

int times = 0;

int get(int v)
{
    if(p[v] == v)
        return v;
    else
        return p[v] = get(p[v]);
}
void unio(int u,int v)
{
    u = get(u);
    v = get(v);
    if(u == v)
        return ;
    mp[v] = max(mp[u],mp[v]);
    p[u] = v;
    return ;
}
void dfs(int v,int pf,int dd)
{
    tin[v] = times++;
    pp[v] = pf;
    d[v] = dd;
    for(auto u:g[v])
    {
        if(u != pf)
            dfs(u,v,dd+1);
    }
    tout[v] = times++;
    return ;
}
int lp(int a,int b)
{
    int a2 = a;
    for(int i = maxlogn-1;i >= 0;--i)
    {
        //if(i == 0)
       // {
       //     cout << la[i][a] << ' ';
      //  }
        if(tin[la[i][a]] > tin[b] || tout[la[i][a]] < tout[b])
        {
            a = la[i][a];
        }
    }
   // cout << a << ' ';
    if(tin[a] > tin[b] || tout[a] < tout[b])
    {
        a = pp[a];
    }
  //  cout << a2 << ' ' << b << ' ' << a << "\n";
    return d[a2] + d[b] - 2*d[a];
}


signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i = 0;i < n;++i)
    {
        cin >> h[i];
    }
    for(int i = 0;i < n-1;++i)
    {
        int u,v;
        cin >> u >> v;
        u--;
        v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(0,0,0);
    for(int i = 0;i < maxlogn;++i)
    {
        for(int j = 0;j < n;++j)
        {
            if(i == 0)
                la[i][j] = pp[j];
            else
                la[i][j] = la[i-1][la[i-1][j]];
        }
    }
    vector<int> ind(n);
    vector<bool> en(n);
    for(int j = 0;j < n;++j)
    {
        p[j] = j;
        mp[j] = {h[j],j};
        ind[j] = j;
        en[j] = 0;
    }
    sort(ind.begin(),ind.end(),[&](int a,int b){return h[a] < h[b];});
    for(int j = 0;j < n;++j)
    {
        int v = ind[j];
        en[v] = 1;
        dp[v] = 0;
        for(auto u:g[v])
        {
            if(en[u])
            {
               int tv = mp[get(u)].second;
            //   cout << tv << ' ' << dp[tv] << ' ' << v << ' ' << lp(v,tv);
               dp[v] = max(dp[v],dp[tv] + lp(v,tv));
            }

        }
        for(auto u:g[v])
        {
            if(en[u])
            {
                unio(v,u);
            }
        }
      //  cout << dp[v] << "!\n";
        if(j == n-1)
            cout << dp[v] << "\n";

    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 48732 KB Output is correct
2 Correct 6 ms 48820 KB Output is correct
3 Correct 6 ms 48732 KB Output is correct
4 Correct 7 ms 48728 KB Output is correct
5 Correct 6 ms 48728 KB Output is correct
6 Correct 6 ms 48728 KB Output is correct
7 Correct 6 ms 48732 KB Output is correct
8 Correct 6 ms 48816 KB Output is correct
9 Correct 6 ms 48728 KB Output is correct
10 Correct 6 ms 48984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 48732 KB Output is correct
2 Correct 6 ms 48820 KB Output is correct
3 Correct 6 ms 48732 KB Output is correct
4 Correct 7 ms 48728 KB Output is correct
5 Correct 6 ms 48728 KB Output is correct
6 Correct 6 ms 48728 KB Output is correct
7 Correct 6 ms 48732 KB Output is correct
8 Correct 6 ms 48816 KB Output is correct
9 Correct 6 ms 48728 KB Output is correct
10 Correct 6 ms 48984 KB Output is correct
11 Correct 6 ms 48728 KB Output is correct
12 Correct 6 ms 48728 KB Output is correct
13 Correct 6 ms 48728 KB Output is correct
14 Correct 6 ms 48736 KB Output is correct
15 Correct 6 ms 48728 KB Output is correct
16 Correct 6 ms 48732 KB Output is correct
17 Correct 6 ms 48732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 48732 KB Output is correct
2 Correct 6 ms 48820 KB Output is correct
3 Correct 6 ms 48732 KB Output is correct
4 Correct 7 ms 48728 KB Output is correct
5 Correct 6 ms 48728 KB Output is correct
6 Correct 6 ms 48728 KB Output is correct
7 Correct 6 ms 48732 KB Output is correct
8 Correct 6 ms 48816 KB Output is correct
9 Correct 6 ms 48728 KB Output is correct
10 Correct 6 ms 48984 KB Output is correct
11 Correct 6 ms 48728 KB Output is correct
12 Correct 6 ms 48728 KB Output is correct
13 Correct 6 ms 48728 KB Output is correct
14 Correct 6 ms 48736 KB Output is correct
15 Correct 6 ms 48728 KB Output is correct
16 Correct 6 ms 48732 KB Output is correct
17 Correct 6 ms 48732 KB Output is correct
18 Correct 9 ms 49244 KB Output is correct
19 Correct 9 ms 49276 KB Output is correct
20 Correct 9 ms 49364 KB Output is correct
21 Correct 10 ms 49244 KB Output is correct
22 Correct 10 ms 49336 KB Output is correct
23 Correct 9 ms 49252 KB Output is correct
24 Correct 10 ms 49240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 48732 KB Output is correct
2 Correct 6 ms 48820 KB Output is correct
3 Correct 6 ms 48732 KB Output is correct
4 Correct 7 ms 48728 KB Output is correct
5 Correct 6 ms 48728 KB Output is correct
6 Correct 6 ms 48728 KB Output is correct
7 Correct 6 ms 48732 KB Output is correct
8 Correct 6 ms 48816 KB Output is correct
9 Correct 6 ms 48728 KB Output is correct
10 Correct 6 ms 48984 KB Output is correct
11 Correct 6 ms 48728 KB Output is correct
12 Correct 6 ms 48728 KB Output is correct
13 Correct 6 ms 48728 KB Output is correct
14 Correct 6 ms 48736 KB Output is correct
15 Correct 6 ms 48728 KB Output is correct
16 Correct 6 ms 48732 KB Output is correct
17 Correct 6 ms 48732 KB Output is correct
18 Correct 9 ms 49244 KB Output is correct
19 Correct 9 ms 49276 KB Output is correct
20 Correct 9 ms 49364 KB Output is correct
21 Correct 10 ms 49244 KB Output is correct
22 Correct 10 ms 49336 KB Output is correct
23 Correct 9 ms 49252 KB Output is correct
24 Correct 10 ms 49240 KB Output is correct
25 Correct 6 ms 48732 KB Output is correct
26 Correct 10 ms 49244 KB Output is correct
27 Correct 9 ms 49244 KB Output is correct
28 Correct 10 ms 49252 KB Output is correct
29 Correct 9 ms 49244 KB Output is correct
30 Correct 10 ms 49080 KB Output is correct
31 Correct 10 ms 49000 KB Output is correct
32 Correct 10 ms 48988 KB Output is correct
33 Correct 10 ms 49116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 48732 KB Output is correct
2 Correct 6 ms 48820 KB Output is correct
3 Correct 6 ms 48732 KB Output is correct
4 Correct 7 ms 48728 KB Output is correct
5 Correct 6 ms 48728 KB Output is correct
6 Correct 6 ms 48728 KB Output is correct
7 Correct 6 ms 48732 KB Output is correct
8 Correct 6 ms 48816 KB Output is correct
9 Correct 6 ms 48728 KB Output is correct
10 Correct 6 ms 48984 KB Output is correct
11 Correct 6 ms 48728 KB Output is correct
12 Correct 6 ms 48728 KB Output is correct
13 Correct 6 ms 48728 KB Output is correct
14 Correct 6 ms 48736 KB Output is correct
15 Correct 6 ms 48728 KB Output is correct
16 Correct 6 ms 48732 KB Output is correct
17 Correct 6 ms 48732 KB Output is correct
18 Correct 9 ms 49244 KB Output is correct
19 Correct 9 ms 49276 KB Output is correct
20 Correct 9 ms 49364 KB Output is correct
21 Correct 10 ms 49244 KB Output is correct
22 Correct 10 ms 49336 KB Output is correct
23 Correct 9 ms 49252 KB Output is correct
24 Correct 10 ms 49240 KB Output is correct
25 Correct 141 ms 69952 KB Output is correct
26 Correct 136 ms 69828 KB Output is correct
27 Correct 135 ms 69968 KB Output is correct
28 Correct 419 ms 69968 KB Output is correct
29 Correct 409 ms 69872 KB Output is correct
30 Correct 407 ms 69972 KB Output is correct
31 Correct 428 ms 69836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 48728 KB Output is correct
2 Correct 6 ms 48728 KB Output is correct
3 Correct 389 ms 62036 KB Output is correct
4 Correct 362 ms 62032 KB Output is correct
5 Correct 377 ms 62032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 48732 KB Output is correct
2 Correct 6 ms 48820 KB Output is correct
3 Correct 6 ms 48732 KB Output is correct
4 Correct 7 ms 48728 KB Output is correct
5 Correct 6 ms 48728 KB Output is correct
6 Correct 6 ms 48728 KB Output is correct
7 Correct 6 ms 48732 KB Output is correct
8 Correct 6 ms 48816 KB Output is correct
9 Correct 6 ms 48728 KB Output is correct
10 Correct 6 ms 48984 KB Output is correct
11 Correct 6 ms 48728 KB Output is correct
12 Correct 6 ms 48728 KB Output is correct
13 Correct 6 ms 48728 KB Output is correct
14 Correct 6 ms 48736 KB Output is correct
15 Correct 6 ms 48728 KB Output is correct
16 Correct 6 ms 48732 KB Output is correct
17 Correct 6 ms 48732 KB Output is correct
18 Correct 9 ms 49244 KB Output is correct
19 Correct 9 ms 49276 KB Output is correct
20 Correct 9 ms 49364 KB Output is correct
21 Correct 10 ms 49244 KB Output is correct
22 Correct 10 ms 49336 KB Output is correct
23 Correct 9 ms 49252 KB Output is correct
24 Correct 10 ms 49240 KB Output is correct
25 Correct 6 ms 48732 KB Output is correct
26 Correct 10 ms 49244 KB Output is correct
27 Correct 9 ms 49244 KB Output is correct
28 Correct 10 ms 49252 KB Output is correct
29 Correct 9 ms 49244 KB Output is correct
30 Correct 10 ms 49080 KB Output is correct
31 Correct 10 ms 49000 KB Output is correct
32 Correct 10 ms 48988 KB Output is correct
33 Correct 10 ms 49116 KB Output is correct
34 Correct 141 ms 69952 KB Output is correct
35 Correct 136 ms 69828 KB Output is correct
36 Correct 135 ms 69968 KB Output is correct
37 Correct 419 ms 69968 KB Output is correct
38 Correct 409 ms 69872 KB Output is correct
39 Correct 407 ms 69972 KB Output is correct
40 Correct 428 ms 69836 KB Output is correct
41 Correct 6 ms 48728 KB Output is correct
42 Correct 6 ms 48728 KB Output is correct
43 Correct 389 ms 62036 KB Output is correct
44 Correct 362 ms 62032 KB Output is correct
45 Correct 377 ms 62032 KB Output is correct
46 Correct 426 ms 66132 KB Output is correct
47 Correct 444 ms 65460 KB Output is correct
48 Correct 431 ms 66500 KB Output is correct
49 Correct 443 ms 65236 KB Output is correct
50 Correct 436 ms 61532 KB Output is correct
51 Correct 410 ms 61524 KB Output is correct
52 Correct 472 ms 61520 KB Output is correct
53 Correct 483 ms 61520 KB Output is correct