답안 #594471

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
594471 2022-07-12T13:33:28 Z qwerasdfzxcl Unique Cities (JOI19_ho_t5) C++14
100 / 100
465 ms 52436 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
vector<int> adj[200200], st;
int n, m, c[200200], d[200200], h[200200], ans[200200], cnt[200200], dist[200200], cur;

void dfs0(int s, int pa = -1){
    if (pa!=-1) d[s] = d[pa] + 1;
    else d[s] = 1;
    h[s] = 1;
    for (auto &v:adj[s]) if (v!=pa){
        dfs0(v, s);
        h[s] = max(h[s], h[v]+1);
    }
}

void _insert(int s){
    if (st.empty() || st.back()!=s){
        st.push_back(s);
        if (!cnt[c[s]]) cur++;
        cnt[c[s]]++;
    }
}

void _pop(int s, int x){
    while(!st.empty() && d[s] - d[st.back()] <= x){
        --cnt[c[st.back()]];
        if (!cnt[c[st.back()]]) cur--;
        st.pop_back();
    }
}

void dfs(int s, int pa = -1){
    vector<int> deep;
    for (auto &v:adj[s]) if (v!=pa){
        deep.push_back(v);
        for (int i=(int)deep.size()-1;i;i--){
            if (h[deep[i-1]] >= h[deep[i]]) break;
            swap(deep[i-1], deep[i]);
        }
        if (deep.size()>2) deep.pop_back();
    }

    if (deep.size()<=1){
        _insert(s);

        for (auto &v:adj[s]) if (v!=pa) dfs(v, s);

        deep.push_back(0);
        _pop(s, h[deep[0]]);
        /*printf("%d: ", s);
        for (auto &x:st) printf("%d ", x);
        printf("\n");*/
        if (dist[s] < d[s]){
            ans[s] = cur;
            dist[s] = d[s];
        }
        if (pa!=-1) _pop(pa, h[s]);
        else _pop(s, 0);
        return;
    }

    _pop(s, h[deep[1]]);
    _insert(s);

    dfs(deep[0], s);

    _pop(s, 0);
    /*printf("%d: ", s);
    for (auto &x:st) printf("%d ", x);
    printf("\n");*/
    if (dist[s] < d[s]){
        ans[s] = cur;
        dist[s] = d[s];
    }
    _insert(s);

    for (auto &v:adj[s]) if (v!=pa && v!=deep[0]){
        dfs(v, s);
        _insert(s);
    }
    if (pa!=-1) _pop(pa, h[s]);
    else _pop(s, 0);
}

int main(){
    scanf("%d %d", &n, &m);
    for (int i=1;i<=n-1;i++){
        int x, y;
        scanf("%d %d", &x, &y);
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    for (int i=1;i<=n;i++) scanf("%d", c+i);

    dfs0(1);
    int r1 = max_element(d+1, d+n+1) - d;
    dfs0(r1);
    dfs(r1);

    /*printf("r1 = %d\n", r1);
    for (int i=1;i<=n;i++) printf("%d\n", ans[i]);*/

    int r2 = max_element(d+1, d+n+1) - d;
    dfs0(r2);
    dfs(r2);

    //printf("r2 = %d\n", r2);
    for (int i=1;i<=n;i++) printf("%d\n", ans[i]);
    return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:88:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:96:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |     for (int i=1;i<=n;i++) scanf("%d", c+i);
      |                            ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5008 KB Output is correct
2 Correct 6 ms 5168 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 6 ms 5276 KB Output is correct
5 Correct 5 ms 5144 KB Output is correct
6 Correct 6 ms 5468 KB Output is correct
7 Correct 5 ms 5224 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 4 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 5 ms 5460 KB Output is correct
14 Correct 4 ms 5268 KB Output is correct
15 Correct 6 ms 5204 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 4 ms 5332 KB Output is correct
18 Correct 6 ms 5208 KB Output is correct
19 Correct 6 ms 5076 KB Output is correct
20 Correct 5 ms 5484 KB Output is correct
21 Correct 5 ms 5276 KB Output is correct
22 Correct 4 ms 5152 KB Output is correct
23 Correct 5 ms 5204 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 5 ms 5332 KB Output is correct
28 Correct 5 ms 5272 KB Output is correct
29 Correct 4 ms 5204 KB Output is correct
30 Correct 6 ms 5076 KB Output is correct
31 Correct 6 ms 5332 KB Output is correct
32 Correct 7 ms 5304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 167 ms 11500 KB Output is correct
2 Correct 195 ms 31160 KB Output is correct
3 Correct 43 ms 9036 KB Output is correct
4 Correct 314 ms 18628 KB Output is correct
5 Correct 425 ms 50560 KB Output is correct
6 Correct 383 ms 34296 KB Output is correct
7 Correct 362 ms 18652 KB Output is correct
8 Correct 341 ms 21768 KB Output is correct
9 Correct 363 ms 20756 KB Output is correct
10 Correct 362 ms 20632 KB Output is correct
11 Correct 209 ms 19288 KB Output is correct
12 Correct 404 ms 38252 KB Output is correct
13 Correct 407 ms 34032 KB Output is correct
14 Correct 376 ms 33100 KB Output is correct
15 Correct 162 ms 19120 KB Output is correct
16 Correct 385 ms 40088 KB Output is correct
17 Correct 336 ms 34492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 290 ms 14444 KB Output is correct
2 Correct 386 ms 50560 KB Output is correct
3 Correct 38 ms 9812 KB Output is correct
4 Correct 352 ms 19676 KB Output is correct
5 Correct 423 ms 52436 KB Output is correct
6 Correct 374 ms 35664 KB Output is correct
7 Correct 322 ms 19796 KB Output is correct
8 Correct 361 ms 26172 KB Output is correct
9 Correct 361 ms 23884 KB Output is correct
10 Correct 364 ms 22096 KB Output is correct
11 Correct 279 ms 19936 KB Output is correct
12 Correct 455 ms 45932 KB Output is correct
13 Correct 452 ms 33444 KB Output is correct
14 Correct 382 ms 33756 KB Output is correct
15 Correct 237 ms 20328 KB Output is correct
16 Correct 443 ms 41712 KB Output is correct
17 Correct 429 ms 35836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5008 KB Output is correct
2 Correct 6 ms 5168 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 6 ms 5276 KB Output is correct
5 Correct 5 ms 5144 KB Output is correct
6 Correct 6 ms 5468 KB Output is correct
7 Correct 5 ms 5224 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 4 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 5 ms 5460 KB Output is correct
14 Correct 4 ms 5268 KB Output is correct
15 Correct 6 ms 5204 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 4 ms 5332 KB Output is correct
18 Correct 6 ms 5208 KB Output is correct
19 Correct 6 ms 5076 KB Output is correct
20 Correct 5 ms 5484 KB Output is correct
21 Correct 5 ms 5276 KB Output is correct
22 Correct 4 ms 5152 KB Output is correct
23 Correct 5 ms 5204 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 5 ms 5332 KB Output is correct
28 Correct 5 ms 5272 KB Output is correct
29 Correct 4 ms 5204 KB Output is correct
30 Correct 6 ms 5076 KB Output is correct
31 Correct 6 ms 5332 KB Output is correct
32 Correct 7 ms 5304 KB Output is correct
33 Correct 167 ms 11500 KB Output is correct
34 Correct 195 ms 31160 KB Output is correct
35 Correct 43 ms 9036 KB Output is correct
36 Correct 314 ms 18628 KB Output is correct
37 Correct 425 ms 50560 KB Output is correct
38 Correct 383 ms 34296 KB Output is correct
39 Correct 362 ms 18652 KB Output is correct
40 Correct 341 ms 21768 KB Output is correct
41 Correct 363 ms 20756 KB Output is correct
42 Correct 362 ms 20632 KB Output is correct
43 Correct 209 ms 19288 KB Output is correct
44 Correct 404 ms 38252 KB Output is correct
45 Correct 407 ms 34032 KB Output is correct
46 Correct 376 ms 33100 KB Output is correct
47 Correct 162 ms 19120 KB Output is correct
48 Correct 385 ms 40088 KB Output is correct
49 Correct 336 ms 34492 KB Output is correct
50 Correct 290 ms 14444 KB Output is correct
51 Correct 386 ms 50560 KB Output is correct
52 Correct 38 ms 9812 KB Output is correct
53 Correct 352 ms 19676 KB Output is correct
54 Correct 423 ms 52436 KB Output is correct
55 Correct 374 ms 35664 KB Output is correct
56 Correct 322 ms 19796 KB Output is correct
57 Correct 361 ms 26172 KB Output is correct
58 Correct 361 ms 23884 KB Output is correct
59 Correct 364 ms 22096 KB Output is correct
60 Correct 279 ms 19936 KB Output is correct
61 Correct 455 ms 45932 KB Output is correct
62 Correct 452 ms 33444 KB Output is correct
63 Correct 382 ms 33756 KB Output is correct
64 Correct 237 ms 20328 KB Output is correct
65 Correct 443 ms 41712 KB Output is correct
66 Correct 429 ms 35836 KB Output is correct
67 Correct 29 ms 6960 KB Output is correct
68 Correct 127 ms 25320 KB Output is correct
69 Correct 220 ms 23764 KB Output is correct
70 Correct 346 ms 18648 KB Output is correct
71 Correct 465 ms 50568 KB Output is correct
72 Correct 388 ms 34360 KB Output is correct
73 Correct 342 ms 18632 KB Output is correct
74 Correct 368 ms 24768 KB Output is correct
75 Correct 325 ms 21356 KB Output is correct
76 Correct 392 ms 20944 KB Output is correct
77 Correct 254 ms 18888 KB Output is correct
78 Correct 425 ms 41844 KB Output is correct
79 Correct 362 ms 37828 KB Output is correct
80 Correct 400 ms 31300 KB Output is correct
81 Correct 179 ms 18980 KB Output is correct
82 Correct 340 ms 40092 KB Output is correct
83 Correct 373 ms 34352 KB Output is correct
84 Correct 346 ms 19000 KB Output is correct
85 Correct 435 ms 51212 KB Output is correct
86 Correct 403 ms 34796 KB Output is correct
87 Correct 311 ms 18912 KB Output is correct
88 Correct 398 ms 25476 KB Output is correct
89 Correct 411 ms 22240 KB Output is correct
90 Correct 354 ms 21424 KB Output is correct
91 Correct 229 ms 19208 KB Output is correct
92 Correct 397 ms 50052 KB Output is correct
93 Correct 363 ms 29836 KB Output is correct
94 Correct 333 ms 26980 KB Output is correct
95 Correct 160 ms 19520 KB Output is correct
96 Correct 366 ms 40624 KB Output is correct
97 Correct 354 ms 35004 KB Output is correct
98 Correct 350 ms 20388 KB Output is correct
99 Correct 392 ms 51648 KB Output is correct
100 Correct 408 ms 35960 KB Output is correct
101 Correct 321 ms 19440 KB Output is correct
102 Correct 369 ms 24612 KB Output is correct
103 Correct 385 ms 22604 KB Output is correct
104 Correct 339 ms 21980 KB Output is correct
105 Correct 254 ms 19692 KB Output is correct
106 Correct 425 ms 37704 KB Output is correct
107 Correct 389 ms 38328 KB Output is correct
108 Correct 356 ms 29900 KB Output is correct
109 Correct 191 ms 20516 KB Output is correct
110 Correct 329 ms 41504 KB Output is correct
111 Correct 350 ms 35872 KB Output is correct