Submission #254188

# Submission time Handle Problem Language Result Execution time Memory
254188 2020-07-29T13:28:14 Z Atill83 Unique Cities (JOI19_ho_t5) C++14
100 / 100
447 ms 43884 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n, m;

vector<int> adj[MAXN];
int c[MAXN];
int mxd[MAXN][2];
int ans[MAXN];
struct diam{
    int a, b;
    int mx;


    void dfs(int v, int par, int len){
        if(len > mx){
            mx = len;
            a = v;
        }
        for(int j: adj[v]){
            if(j == par) continue;
            dfs(j, v, len + 1);
        }
    }


    void fnd(){
        mx = 0;
        dfs(1, -1, 0);
        mx = 0;
        b = a;
        dfs(b, -1, 0);
    }

} dia;

int der[MAXN];

struct st{
    vector<int> g, s;
    int sz = 0;
    int cntd = 0;
    void ini(){
        g.resize(m + 1, 0);
    }
    void add(int v){
        s.push_back(v);
        sz++;
        cntd += (g[c[v]] == 0);
        g[c[v]]++;
    }

    void del(int cur){
        while(sz && cur <= der[s.back()]){
            g[c[s.back()]]--;
            cntd -= (g[c[s.back()]] == 0);
            s.pop_back();
            sz--;
        }
    }
} stt;

void upd(int a, int val){
    if(mxd[a][1] < val) mxd[a][1] = val;
    if(mxd[a][1] > mxd[a][0]) swap(mxd[a][0], mxd[a][1]);
}

int deep[MAXN];
void dfs1(int v, int par){
    der[v] = der[par] + 1;
    mxd[v][0] = mxd[v][1] = der[v];
    deep[v] = v;
    for(int j: adj[v]){
        if(j == par) continue;
        dfs1(j, v);
        upd(v, mxd[j][0]);
        if(mxd[j][0] == mxd[v][0]){
            deep[v] = j;
        }
    }
}

void dfs(int v, int par){
    stt.del(2*der[v] - mxd[v][1]);
    stt.add(v);

    for(int j: adj[v]){
        if(j != deep[v]) continue;
        dfs(j, v);
    }
    stt.del(2*der[v] - mxd[v][0]);
    ans[v] = max(ans[v], stt.cntd);

    for(int j: adj[v]){
        if(j == deep[v] || j == par) continue;
        if(stt.sz == 0 || stt.s.back() != v) stt.add(v);
        dfs(j, v);
    }
    stt.del(der[v]);
}





int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif

    cin>>n>>m;

    stt.ini();

    for(int i = 0; i < n - 1; i++){
        int a, b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    for(int i = 1; i <= n; i++)
        cin>>c[i];

    dia.fnd();

    dfs1(dia.a, 0);
    dfs(dia.a, 0);

    dfs1(dia.b, 0);
    dfs(dia.b, 0);

    for(int i = 1; i <= n; i++) cout<<ans[i]<<endl;
    

    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 6 ms 7552 KB Output is correct
3 Correct 6 ms 7552 KB Output is correct
4 Correct 6 ms 7680 KB Output is correct
5 Correct 6 ms 7552 KB Output is correct
6 Correct 6 ms 7680 KB Output is correct
7 Correct 6 ms 7680 KB Output is correct
8 Correct 6 ms 7552 KB Output is correct
9 Correct 6 ms 7552 KB Output is correct
10 Correct 6 ms 7552 KB Output is correct
11 Correct 6 ms 7552 KB Output is correct
12 Correct 6 ms 7552 KB Output is correct
13 Correct 7 ms 7680 KB Output is correct
14 Correct 6 ms 7680 KB Output is correct
15 Correct 6 ms 7680 KB Output is correct
16 Correct 7 ms 7552 KB Output is correct
17 Correct 6 ms 7680 KB Output is correct
18 Correct 6 ms 7680 KB Output is correct
19 Correct 7 ms 7552 KB Output is correct
20 Correct 6 ms 7808 KB Output is correct
21 Correct 6 ms 7680 KB Output is correct
22 Correct 6 ms 7552 KB Output is correct
23 Correct 6 ms 7552 KB Output is correct
24 Correct 6 ms 7552 KB Output is correct
25 Correct 6 ms 7552 KB Output is correct
26 Correct 9 ms 7552 KB Output is correct
27 Correct 6 ms 7680 KB Output is correct
28 Correct 6 ms 7680 KB Output is correct
29 Correct 9 ms 7680 KB Output is correct
30 Correct 6 ms 7552 KB Output is correct
31 Correct 6 ms 7680 KB Output is correct
32 Correct 6 ms 7680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 15604 KB Output is correct
2 Correct 186 ms 26868 KB Output is correct
3 Correct 31 ms 10880 KB Output is correct
4 Correct 331 ms 21880 KB Output is correct
5 Correct 399 ms 41452 KB Output is correct
6 Correct 434 ms 31472 KB Output is correct
7 Correct 317 ms 21876 KB Output is correct
8 Correct 322 ms 23672 KB Output is correct
9 Correct 320 ms 23160 KB Output is correct
10 Correct 318 ms 23032 KB Output is correct
11 Correct 176 ms 22508 KB Output is correct
12 Correct 377 ms 33904 KB Output is correct
13 Correct 326 ms 31340 KB Output is correct
14 Correct 337 ms 30960 KB Output is correct
15 Correct 172 ms 22380 KB Output is correct
16 Correct 323 ms 35180 KB Output is correct
17 Correct 341 ms 31728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 20088 KB Output is correct
2 Correct 395 ms 42348 KB Output is correct
3 Correct 34 ms 11512 KB Output is correct
4 Correct 344 ms 23548 KB Output is correct
5 Correct 445 ms 43884 KB Output is correct
6 Correct 447 ms 33392 KB Output is correct
7 Correct 311 ms 23544 KB Output is correct
8 Correct 351 ms 27640 KB Output is correct
9 Correct 340 ms 26232 KB Output is correct
10 Correct 336 ms 25080 KB Output is correct
11 Correct 269 ms 23796 KB Output is correct
12 Correct 410 ms 39920 KB Output is correct
13 Correct 364 ms 32184 KB Output is correct
14 Correct 372 ms 32500 KB Output is correct
15 Correct 178 ms 24300 KB Output is correct
16 Correct 346 ms 37484 KB Output is correct
17 Correct 363 ms 33652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 6 ms 7552 KB Output is correct
3 Correct 6 ms 7552 KB Output is correct
4 Correct 6 ms 7680 KB Output is correct
5 Correct 6 ms 7552 KB Output is correct
6 Correct 6 ms 7680 KB Output is correct
7 Correct 6 ms 7680 KB Output is correct
8 Correct 6 ms 7552 KB Output is correct
9 Correct 6 ms 7552 KB Output is correct
10 Correct 6 ms 7552 KB Output is correct
11 Correct 6 ms 7552 KB Output is correct
12 Correct 6 ms 7552 KB Output is correct
13 Correct 7 ms 7680 KB Output is correct
14 Correct 6 ms 7680 KB Output is correct
15 Correct 6 ms 7680 KB Output is correct
16 Correct 7 ms 7552 KB Output is correct
17 Correct 6 ms 7680 KB Output is correct
18 Correct 6 ms 7680 KB Output is correct
19 Correct 7 ms 7552 KB Output is correct
20 Correct 6 ms 7808 KB Output is correct
21 Correct 6 ms 7680 KB Output is correct
22 Correct 6 ms 7552 KB Output is correct
23 Correct 6 ms 7552 KB Output is correct
24 Correct 6 ms 7552 KB Output is correct
25 Correct 6 ms 7552 KB Output is correct
26 Correct 9 ms 7552 KB Output is correct
27 Correct 6 ms 7680 KB Output is correct
28 Correct 6 ms 7680 KB Output is correct
29 Correct 9 ms 7680 KB Output is correct
30 Correct 6 ms 7552 KB Output is correct
31 Correct 6 ms 7680 KB Output is correct
32 Correct 6 ms 7680 KB Output is correct
33 Correct 138 ms 15604 KB Output is correct
34 Correct 186 ms 26868 KB Output is correct
35 Correct 31 ms 10880 KB Output is correct
36 Correct 331 ms 21880 KB Output is correct
37 Correct 399 ms 41452 KB Output is correct
38 Correct 434 ms 31472 KB Output is correct
39 Correct 317 ms 21876 KB Output is correct
40 Correct 322 ms 23672 KB Output is correct
41 Correct 320 ms 23160 KB Output is correct
42 Correct 318 ms 23032 KB Output is correct
43 Correct 176 ms 22508 KB Output is correct
44 Correct 377 ms 33904 KB Output is correct
45 Correct 326 ms 31340 KB Output is correct
46 Correct 337 ms 30960 KB Output is correct
47 Correct 172 ms 22380 KB Output is correct
48 Correct 323 ms 35180 KB Output is correct
49 Correct 341 ms 31728 KB Output is correct
50 Correct 237 ms 20088 KB Output is correct
51 Correct 395 ms 42348 KB Output is correct
52 Correct 34 ms 11512 KB Output is correct
53 Correct 344 ms 23548 KB Output is correct
54 Correct 445 ms 43884 KB Output is correct
55 Correct 447 ms 33392 KB Output is correct
56 Correct 311 ms 23544 KB Output is correct
57 Correct 351 ms 27640 KB Output is correct
58 Correct 340 ms 26232 KB Output is correct
59 Correct 336 ms 25080 KB Output is correct
60 Correct 269 ms 23796 KB Output is correct
61 Correct 410 ms 39920 KB Output is correct
62 Correct 364 ms 32184 KB Output is correct
63 Correct 372 ms 32500 KB Output is correct
64 Correct 178 ms 24300 KB Output is correct
65 Correct 346 ms 37484 KB Output is correct
66 Correct 363 ms 33652 KB Output is correct
67 Correct 28 ms 9472 KB Output is correct
68 Correct 120 ms 22900 KB Output is correct
69 Correct 203 ms 22904 KB Output is correct
70 Correct 317 ms 21880 KB Output is correct
71 Correct 420 ms 41448 KB Output is correct
72 Correct 386 ms 31476 KB Output is correct
73 Correct 300 ms 21880 KB Output is correct
74 Correct 337 ms 25468 KB Output is correct
75 Correct 335 ms 23544 KB Output is correct
76 Correct 372 ms 23288 KB Output is correct
77 Correct 282 ms 22128 KB Output is correct
78 Correct 408 ms 35952 KB Output is correct
79 Correct 327 ms 33904 KB Output is correct
80 Correct 337 ms 29680 KB Output is correct
81 Correct 160 ms 22252 KB Output is correct
82 Correct 321 ms 35052 KB Output is correct
83 Correct 351 ms 31704 KB Output is correct
84 Correct 306 ms 22136 KB Output is correct
85 Correct 394 ms 42220 KB Output is correct
86 Correct 380 ms 31924 KB Output is correct
87 Correct 299 ms 22264 KB Output is correct
88 Correct 334 ms 26232 KB Output is correct
89 Correct 335 ms 24312 KB Output is correct
90 Correct 330 ms 23724 KB Output is correct
91 Correct 261 ms 22512 KB Output is correct
92 Correct 441 ms 41580 KB Output is correct
93 Correct 389 ms 29044 KB Output is correct
94 Correct 358 ms 27128 KB Output is correct
95 Correct 185 ms 22760 KB Output is correct
96 Correct 347 ms 35636 KB Output is correct
97 Correct 355 ms 32372 KB Output is correct
98 Correct 333 ms 23416 KB Output is correct
99 Correct 413 ms 42600 KB Output is correct
100 Correct 390 ms 33012 KB Output is correct
101 Correct 320 ms 23028 KB Output is correct
102 Correct 343 ms 25848 KB Output is correct
103 Correct 352 ms 24696 KB Output is correct
104 Correct 336 ms 24184 KB Output is correct
105 Correct 268 ms 23024 KB Output is correct
106 Correct 402 ms 34344 KB Output is correct
107 Correct 374 ms 34672 KB Output is correct
108 Correct 365 ms 29448 KB Output is correct
109 Correct 188 ms 23788 KB Output is correct
110 Correct 346 ms 36464 KB Output is correct
111 Correct 408 ms 33104 KB Output is correct