Submission #907403

# Submission time Handle Problem Language Result Execution time Memory
907403 2024-01-15T13:33:05 Z bachhoangxuan Unique Cities (JOI19_ho_t5) C++17
100 / 100
516 ms 57148 KB
// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/

#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define mpp make_pair
#define fi first
#define se second
const int inf=1e18;
const int mod=998244353;
const int maxn=200005;
const int bl=650;
const int maxs=655;
const int maxm=200005;
const int maxq=1000005;
const int maxl=25;
const int maxa=1000000;
const int root=3;
int power(int a,int n){
    int res=1;
    while(n){
        if(n&1) res=res*a%mod;
        a=a*a%mod;n>>=1;
    }
    return res;
}
const int iroot=power(3,mod-2);
const int base=131;

int n,m,dp[maxn],dep[maxn],c[maxn],res[maxn];
vector<int> edge[maxn];

void solve(){
    cin >> n >> m;
    for(int i=1;i<n;i++){
        int u,v;cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    for(int i=1;i<=n;i++) cin >> c[i];
    int root=1;
    for(int i=0;i<=1;i++){
        int x=1;
        function<void(int,int)> dfs = [&](int u,int p){
            dp[u]=dep[u]=dep[p]+1;
            if(dep[u]>dep[x]) x=u;
            for(int v:edge[u]) if(v!=p) dfs(v,u),dp[u]=max(dp[u],dp[v]);
        };
        dfs(root,0);
        //cout << '*' << x << '\n';
        root=x;
        dfs(root,0);

        int cnt=0;
        vector<int> d(m+1,0);
        vector<int> cur;
        auto add = [&](int u){
            //cout << "add " << u << '\n';
            cur.push_back(u);
            cnt+=(!d[c[u]]);
            d[c[u]]++;
        };
        auto del = [&](int u){
            //cout << "del " << u << '\n';
            d[c[u]]--;
            cnt-=(!d[c[u]]);
            cur.pop_back();
        };
        function<void(int,int)> dfs2 = [&](int u,int p){
            //cout << u << '\n';
            if(p) add(p);
            vector<int> cc;
            for(int v:edge[u]) if(v!=p) cc.push_back(v);
            sort(cc.begin(),cc.end(),[](int a,int b){
                return dp[a]>dp[b];
            });
            for(int v:cc){
                if(v==p) continue;
                int d2=(v!=cc[0]?dp[cc[0]]:((int)cc.size()<=1?0:dp[cc[1]]));
                while(!cur.empty() && dep[u]-dep[cur.back()]<=d2-dep[u]) del(cur.back());
                dfs2(v,u);
            }
            while(!cur.empty() && dep[u]-dep[cur.back()]<=dp[u]-dep[u]) del(cur.back());
            //cout << u << ' ' << cnt << '\n';
            res[u]=max(res[u],cnt);
        };
        dfs2(root,0);
    }
    for(int i=1;i<=n;i++) cout << res[i] << '\n';
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int test=1;//cin >> test;
    while(test--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8792 KB Output is correct
2 Correct 3 ms 8796 KB Output is correct
3 Correct 3 ms 9052 KB Output is correct
4 Correct 4 ms 9052 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 4 ms 9052 KB Output is correct
7 Correct 3 ms 9052 KB Output is correct
8 Correct 4 ms 8792 KB Output is correct
9 Correct 3 ms 8796 KB Output is correct
10 Correct 3 ms 8796 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 8796 KB Output is correct
13 Correct 3 ms 9052 KB Output is correct
14 Correct 3 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 3 ms 9052 KB Output is correct
18 Correct 3 ms 9048 KB Output is correct
19 Correct 4 ms 8796 KB Output is correct
20 Correct 4 ms 9308 KB Output is correct
21 Correct 3 ms 9052 KB Output is correct
22 Correct 3 ms 8796 KB Output is correct
23 Correct 3 ms 8796 KB Output is correct
24 Correct 3 ms 8860 KB Output is correct
25 Correct 4 ms 8796 KB Output is correct
26 Correct 4 ms 8796 KB Output is correct
27 Correct 4 ms 9060 KB Output is correct
28 Correct 3 ms 9048 KB Output is correct
29 Correct 3 ms 9052 KB Output is correct
30 Correct 3 ms 8792 KB Output is correct
31 Correct 4 ms 9048 KB Output is correct
32 Correct 3 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 16852 KB Output is correct
2 Correct 193 ms 35268 KB Output is correct
3 Correct 22 ms 14428 KB Output is correct
4 Correct 299 ms 22080 KB Output is correct
5 Correct 461 ms 53652 KB Output is correct
6 Correct 463 ms 37504 KB Output is correct
7 Correct 280 ms 22352 KB Output is correct
8 Correct 275 ms 25228 KB Output is correct
9 Correct 298 ms 24148 KB Output is correct
10 Correct 270 ms 24140 KB Output is correct
11 Correct 140 ms 25484 KB Output is correct
12 Correct 431 ms 41984 KB Output is correct
13 Correct 366 ms 38204 KB Output is correct
14 Correct 480 ms 36744 KB Output is correct
15 Correct 119 ms 25632 KB Output is correct
16 Correct 423 ms 44444 KB Output is correct
17 Correct 454 ms 37940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 192 ms 21576 KB Output is correct
2 Correct 446 ms 55036 KB Output is correct
3 Correct 38 ms 15356 KB Output is correct
4 Correct 270 ms 24496 KB Output is correct
5 Correct 509 ms 57148 KB Output is correct
6 Correct 426 ms 39976 KB Output is correct
7 Correct 271 ms 24772 KB Output is correct
8 Correct 363 ms 31124 KB Output is correct
9 Correct 345 ms 28760 KB Output is correct
10 Correct 381 ms 27012 KB Output is correct
11 Correct 244 ms 26316 KB Output is correct
12 Correct 504 ms 50632 KB Output is correct
13 Correct 376 ms 38544 KB Output is correct
14 Correct 423 ms 38572 KB Output is correct
15 Correct 126 ms 28316 KB Output is correct
16 Correct 411 ms 46968 KB Output is correct
17 Correct 398 ms 40372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8792 KB Output is correct
2 Correct 3 ms 8796 KB Output is correct
3 Correct 3 ms 9052 KB Output is correct
4 Correct 4 ms 9052 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 4 ms 9052 KB Output is correct
7 Correct 3 ms 9052 KB Output is correct
8 Correct 4 ms 8792 KB Output is correct
9 Correct 3 ms 8796 KB Output is correct
10 Correct 3 ms 8796 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 8796 KB Output is correct
13 Correct 3 ms 9052 KB Output is correct
14 Correct 3 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 3 ms 9052 KB Output is correct
18 Correct 3 ms 9048 KB Output is correct
19 Correct 4 ms 8796 KB Output is correct
20 Correct 4 ms 9308 KB Output is correct
21 Correct 3 ms 9052 KB Output is correct
22 Correct 3 ms 8796 KB Output is correct
23 Correct 3 ms 8796 KB Output is correct
24 Correct 3 ms 8860 KB Output is correct
25 Correct 4 ms 8796 KB Output is correct
26 Correct 4 ms 8796 KB Output is correct
27 Correct 4 ms 9060 KB Output is correct
28 Correct 3 ms 9048 KB Output is correct
29 Correct 3 ms 9052 KB Output is correct
30 Correct 3 ms 8792 KB Output is correct
31 Correct 4 ms 9048 KB Output is correct
32 Correct 3 ms 9052 KB Output is correct
33 Correct 76 ms 16852 KB Output is correct
34 Correct 193 ms 35268 KB Output is correct
35 Correct 22 ms 14428 KB Output is correct
36 Correct 299 ms 22080 KB Output is correct
37 Correct 461 ms 53652 KB Output is correct
38 Correct 463 ms 37504 KB Output is correct
39 Correct 280 ms 22352 KB Output is correct
40 Correct 275 ms 25228 KB Output is correct
41 Correct 298 ms 24148 KB Output is correct
42 Correct 270 ms 24140 KB Output is correct
43 Correct 140 ms 25484 KB Output is correct
44 Correct 431 ms 41984 KB Output is correct
45 Correct 366 ms 38204 KB Output is correct
46 Correct 480 ms 36744 KB Output is correct
47 Correct 119 ms 25632 KB Output is correct
48 Correct 423 ms 44444 KB Output is correct
49 Correct 454 ms 37940 KB Output is correct
50 Correct 192 ms 21576 KB Output is correct
51 Correct 446 ms 55036 KB Output is correct
52 Correct 38 ms 15356 KB Output is correct
53 Correct 270 ms 24496 KB Output is correct
54 Correct 509 ms 57148 KB Output is correct
55 Correct 426 ms 39976 KB Output is correct
56 Correct 271 ms 24772 KB Output is correct
57 Correct 363 ms 31124 KB Output is correct
58 Correct 345 ms 28760 KB Output is correct
59 Correct 381 ms 27012 KB Output is correct
60 Correct 244 ms 26316 KB Output is correct
61 Correct 504 ms 50632 KB Output is correct
62 Correct 376 ms 38544 KB Output is correct
63 Correct 423 ms 38572 KB Output is correct
64 Correct 126 ms 28316 KB Output is correct
65 Correct 411 ms 46968 KB Output is correct
66 Correct 398 ms 40372 KB Output is correct
67 Correct 18 ms 12448 KB Output is correct
68 Correct 158 ms 29944 KB Output is correct
69 Correct 153 ms 27440 KB Output is correct
70 Correct 235 ms 21940 KB Output is correct
71 Correct 480 ms 53828 KB Output is correct
72 Correct 436 ms 37172 KB Output is correct
73 Correct 325 ms 22012 KB Output is correct
74 Correct 335 ms 28500 KB Output is correct
75 Correct 365 ms 24912 KB Output is correct
76 Correct 332 ms 24560 KB Output is correct
77 Correct 205 ms 23600 KB Output is correct
78 Correct 435 ms 45460 KB Output is correct
79 Correct 370 ms 41512 KB Output is correct
80 Correct 380 ms 34824 KB Output is correct
81 Correct 139 ms 25296 KB Output is correct
82 Correct 374 ms 44948 KB Output is correct
83 Correct 394 ms 37904 KB Output is correct
84 Correct 329 ms 22356 KB Output is correct
85 Correct 516 ms 54396 KB Output is correct
86 Correct 427 ms 37652 KB Output is correct
87 Correct 299 ms 22364 KB Output is correct
88 Correct 328 ms 29080 KB Output is correct
89 Correct 331 ms 25844 KB Output is correct
90 Correct 301 ms 24912 KB Output is correct
91 Correct 225 ms 23752 KB Output is correct
92 Correct 457 ms 53472 KB Output is correct
93 Correct 362 ms 33852 KB Output is correct
94 Correct 364 ms 30600 KB Output is correct
95 Correct 116 ms 25988 KB Output is correct
96 Correct 410 ms 44624 KB Output is correct
97 Correct 386 ms 38464 KB Output is correct
98 Correct 359 ms 24500 KB Output is correct
99 Correct 473 ms 54840 KB Output is correct
100 Correct 435 ms 39432 KB Output is correct
101 Correct 269 ms 23312 KB Output is correct
102 Correct 358 ms 28276 KB Output is correct
103 Correct 292 ms 26312 KB Output is correct
104 Correct 298 ms 25676 KB Output is correct
105 Correct 248 ms 24420 KB Output is correct
106 Correct 439 ms 41640 KB Output is correct
107 Correct 383 ms 42240 KB Output is correct
108 Correct 397 ms 34196 KB Output is correct
109 Correct 128 ms 27420 KB Output is correct
110 Correct 368 ms 45508 KB Output is correct
111 Correct 431 ms 39564 KB Output is correct