답안 #254660

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
254660 2020-07-30T12:10:09 Z egekabas Unique Cities (JOI19_ho_t5) C++14
100 / 100
700 ms 38508 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
int n, m;
vector<int> g[200009];
int col[200009];
int curans;
int cnt[200009];
vector<int> added;
void add(int v){
    added.pb(v);
    cnt[col[v]]++;
    if(cnt[col[v]] == 1) ++curans;
}
void del(int v){
    cnt[col[v]]--;
    if(cnt[col[v]] == 0) --curans;
}
int depth[200009];
void deluntil(int d){
    while(added.size() && depth[added.back()] >= d){
        del(added.back());
        added.pop_back();
    }
}
int len[200009];
int maxlen[200009][2];
void dfs1(int v, int prt){
    depth[v] = depth[prt]+1;
    len[v] = maxlen[v][0] = maxlen[v][1] = 0;
    for(auto u : g[v]){
        if(u == prt) continue;
        dfs1(u, v);
        len[v] = max(len[v], len[u]+1);
        if(len[u]+1 >= maxlen[v][0]){
            maxlen[v][1] = maxlen[v][0];
            maxlen[v][0] = len[u]+1;
        }
        else if(len[u]+1 > maxlen[v][1])
            maxlen[v][1] = len[u]+1;
    }
}
pii maxi = {0, 0};
void findfar(int v, int prt, int dis){
    maxi = max(maxi, {dis, v});
    for(auto u : g[v])
        if(u != prt)
            findfar(u, v, dis+1);
}
pii dia(){
    maxi = {0, 0};
    findfar(1, 0, 0);
    int v1 = maxi.ss;
    maxi = {0, 0};
    findfar(v1, 0, 0);
    return {v1, maxi.ss};
}
int ans[200009];
void dfs2(int v, int prt){
    deluntil(depth[v]-maxlen[v][1]);
    add(v);
    int deep = -1;
    for(auto u : g[v])
        if(u != prt && len[u]+1 == maxlen[v][0]){
            deep = u;
            dfs2(u, v);
            break;
        }
    deluntil(depth[v]-maxlen[v][0]);
    ans[v] = max(ans[v], curans);
    for(auto u : g[v]){
        if(u == prt || u == deep) continue;
        if(added.empty() || added.back() != v) add(v);
        dfs2(u, v);
    }
    deluntil(depth[v]);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    cin >> n >> m;
    for(int i = 0; i < n-1; ++i){
        int x, y;
        cin >> x >> y;
        g[x].pb(y);
        g[y].pb(x);
    }
    for(int i = 1; i <= n; ++i)
        cin >> col[i];
    pii di = dia();
    dfs1(di.ff, 0);
    dfs2(di.ff, 0);
    
    dfs1(di.ss, 0);
    dfs2(di.ss, 0);
    
    for(int i = 1; i <= n; ++i)
        cout << ans[i] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 5 ms 5248 KB Output is correct
3 Correct 4 ms 5248 KB Output is correct
4 Correct 4 ms 5248 KB Output is correct
5 Correct 5 ms 5248 KB Output is correct
6 Correct 5 ms 5376 KB Output is correct
7 Correct 6 ms 5248 KB Output is correct
8 Correct 4 ms 5248 KB Output is correct
9 Correct 5 ms 5248 KB Output is correct
10 Correct 5 ms 5248 KB Output is correct
11 Correct 5 ms 5248 KB Output is correct
12 Correct 6 ms 5248 KB Output is correct
13 Correct 6 ms 5376 KB Output is correct
14 Correct 4 ms 5248 KB Output is correct
15 Correct 4 ms 5248 KB Output is correct
16 Correct 6 ms 5248 KB Output is correct
17 Correct 6 ms 5376 KB Output is correct
18 Correct 5 ms 5248 KB Output is correct
19 Correct 5 ms 5248 KB Output is correct
20 Correct 5 ms 5376 KB Output is correct
21 Correct 5 ms 5248 KB Output is correct
22 Correct 6 ms 5248 KB Output is correct
23 Correct 5 ms 5280 KB Output is correct
24 Correct 6 ms 5248 KB Output is correct
25 Correct 5 ms 5232 KB Output is correct
26 Correct 5 ms 5248 KB Output is correct
27 Correct 5 ms 5376 KB Output is correct
28 Correct 5 ms 5376 KB Output is correct
29 Correct 4 ms 5248 KB Output is correct
30 Correct 6 ms 5248 KB Output is correct
31 Correct 4 ms 5356 KB Output is correct
32 Correct 5 ms 5248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 170 ms 13436 KB Output is correct
2 Correct 216 ms 22900 KB Output is correct
3 Correct 28 ms 8184 KB Output is correct
4 Correct 411 ms 19452 KB Output is correct
5 Correct 464 ms 35948 KB Output is correct
6 Correct 482 ms 27656 KB Output is correct
7 Correct 372 ms 19444 KB Output is correct
8 Correct 352 ms 20984 KB Output is correct
9 Correct 352 ms 20472 KB Output is correct
10 Correct 373 ms 20600 KB Output is correct
11 Correct 182 ms 20188 KB Output is correct
12 Correct 429 ms 29684 KB Output is correct
13 Correct 385 ms 27508 KB Output is correct
14 Correct 438 ms 26996 KB Output is correct
15 Correct 151 ms 20076 KB Output is correct
16 Correct 386 ms 30860 KB Output is correct
17 Correct 377 ms 27892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 264 ms 17844 KB Output is correct
2 Correct 613 ms 36968 KB Output is correct
3 Correct 32 ms 8824 KB Output is correct
4 Correct 346 ms 21112 KB Output is correct
5 Correct 484 ms 38508 KB Output is correct
6 Correct 451 ms 29424 KB Output is correct
7 Correct 343 ms 21240 KB Output is correct
8 Correct 380 ms 24696 KB Output is correct
9 Correct 384 ms 23416 KB Output is correct
10 Correct 381 ms 22648 KB Output is correct
11 Correct 286 ms 21492 KB Output is correct
12 Correct 498 ms 34928 KB Output is correct
13 Correct 372 ms 28532 KB Output is correct
14 Correct 504 ms 28700 KB Output is correct
15 Correct 243 ms 21852 KB Output is correct
16 Correct 445 ms 33004 KB Output is correct
17 Correct 431 ms 29812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 5 ms 5248 KB Output is correct
3 Correct 4 ms 5248 KB Output is correct
4 Correct 4 ms 5248 KB Output is correct
5 Correct 5 ms 5248 KB Output is correct
6 Correct 5 ms 5376 KB Output is correct
7 Correct 6 ms 5248 KB Output is correct
8 Correct 4 ms 5248 KB Output is correct
9 Correct 5 ms 5248 KB Output is correct
10 Correct 5 ms 5248 KB Output is correct
11 Correct 5 ms 5248 KB Output is correct
12 Correct 6 ms 5248 KB Output is correct
13 Correct 6 ms 5376 KB Output is correct
14 Correct 4 ms 5248 KB Output is correct
15 Correct 4 ms 5248 KB Output is correct
16 Correct 6 ms 5248 KB Output is correct
17 Correct 6 ms 5376 KB Output is correct
18 Correct 5 ms 5248 KB Output is correct
19 Correct 5 ms 5248 KB Output is correct
20 Correct 5 ms 5376 KB Output is correct
21 Correct 5 ms 5248 KB Output is correct
22 Correct 6 ms 5248 KB Output is correct
23 Correct 5 ms 5280 KB Output is correct
24 Correct 6 ms 5248 KB Output is correct
25 Correct 5 ms 5232 KB Output is correct
26 Correct 5 ms 5248 KB Output is correct
27 Correct 5 ms 5376 KB Output is correct
28 Correct 5 ms 5376 KB Output is correct
29 Correct 4 ms 5248 KB Output is correct
30 Correct 6 ms 5248 KB Output is correct
31 Correct 4 ms 5356 KB Output is correct
32 Correct 5 ms 5248 KB Output is correct
33 Correct 170 ms 13436 KB Output is correct
34 Correct 216 ms 22900 KB Output is correct
35 Correct 28 ms 8184 KB Output is correct
36 Correct 411 ms 19452 KB Output is correct
37 Correct 464 ms 35948 KB Output is correct
38 Correct 482 ms 27656 KB Output is correct
39 Correct 372 ms 19444 KB Output is correct
40 Correct 352 ms 20984 KB Output is correct
41 Correct 352 ms 20472 KB Output is correct
42 Correct 373 ms 20600 KB Output is correct
43 Correct 182 ms 20188 KB Output is correct
44 Correct 429 ms 29684 KB Output is correct
45 Correct 385 ms 27508 KB Output is correct
46 Correct 438 ms 26996 KB Output is correct
47 Correct 151 ms 20076 KB Output is correct
48 Correct 386 ms 30860 KB Output is correct
49 Correct 377 ms 27892 KB Output is correct
50 Correct 264 ms 17844 KB Output is correct
51 Correct 613 ms 36968 KB Output is correct
52 Correct 32 ms 8824 KB Output is correct
53 Correct 346 ms 21112 KB Output is correct
54 Correct 484 ms 38508 KB Output is correct
55 Correct 451 ms 29424 KB Output is correct
56 Correct 343 ms 21240 KB Output is correct
57 Correct 380 ms 24696 KB Output is correct
58 Correct 384 ms 23416 KB Output is correct
59 Correct 381 ms 22648 KB Output is correct
60 Correct 286 ms 21492 KB Output is correct
61 Correct 498 ms 34928 KB Output is correct
62 Correct 372 ms 28532 KB Output is correct
63 Correct 504 ms 28700 KB Output is correct
64 Correct 243 ms 21852 KB Output is correct
65 Correct 445 ms 33004 KB Output is correct
66 Correct 431 ms 29812 KB Output is correct
67 Correct 25 ms 7160 KB Output is correct
68 Correct 134 ms 19184 KB Output is correct
69 Correct 222 ms 19576 KB Output is correct
70 Correct 331 ms 19448 KB Output is correct
71 Correct 431 ms 35948 KB Output is correct
72 Correct 418 ms 27644 KB Output is correct
73 Correct 318 ms 19468 KB Output is correct
74 Correct 341 ms 22520 KB Output is correct
75 Correct 382 ms 21000 KB Output is correct
76 Correct 379 ms 20856 KB Output is correct
77 Correct 299 ms 19696 KB Output is correct
78 Correct 578 ms 31296 KB Output is correct
79 Correct 563 ms 29680 KB Output is correct
80 Correct 700 ms 26056 KB Output is correct
81 Correct 342 ms 19824 KB Output is correct
82 Correct 422 ms 30684 KB Output is correct
83 Correct 426 ms 27812 KB Output is correct
84 Correct 446 ms 19836 KB Output is correct
85 Correct 516 ms 36592 KB Output is correct
86 Correct 586 ms 28148 KB Output is correct
87 Correct 359 ms 19776 KB Output is correct
88 Correct 464 ms 23160 KB Output is correct
89 Correct 377 ms 21624 KB Output is correct
90 Correct 474 ms 21104 KB Output is correct
91 Correct 264 ms 20284 KB Output is correct
92 Correct 488 ms 36200 KB Output is correct
93 Correct 456 ms 25708 KB Output is correct
94 Correct 476 ms 23988 KB Output is correct
95 Correct 176 ms 20456 KB Output is correct
96 Correct 520 ms 31468 KB Output is correct
97 Correct 547 ms 28388 KB Output is correct
98 Correct 502 ms 21112 KB Output is correct
99 Correct 535 ms 37224 KB Output is correct
100 Correct 542 ms 29172 KB Output is correct
101 Correct 351 ms 20468 KB Output is correct
102 Correct 528 ms 23032 KB Output is correct
103 Correct 380 ms 22088 KB Output is correct
104 Correct 370 ms 21752 KB Output is correct
105 Correct 253 ms 20464 KB Output is correct
106 Correct 536 ms 30196 KB Output is correct
107 Correct 522 ms 30320 KB Output is correct
108 Correct 512 ms 26104 KB Output is correct
109 Correct 245 ms 21352 KB Output is correct
110 Correct 569 ms 32108 KB Output is correct
111 Correct 409 ms 29200 KB Output is correct