Submission #126117

# Submission time Handle Problem Language Result Execution time Memory
126117 2019-07-07T05:25:11 Z EntityIT Unique Cities (JOI19_ho_t5) C++14
100 / 100
1081 ms 54036 KB
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back

const int N = (int)2e5 + 5, M = N;
int n, m, c[N], diameterTip[2], depth[N], ans[N], cnt[M], curAns, mx[N][2];
pair<int, bool> group[N];
set< pair<int, int> > valid, path;
vector<int> gr[N];

void dfs (int u, int p) {
    depth[u] = depth[p] + 1;
    for (int v : gr[u]) if (v != p) {
        dfs(v, u);
    }
}

void prep (int u, int p) {
    depth[u] = depth[p] + 1;
    mx[u][0] = mx[u][1] = 0;
    for (int v : gr[u]) if (v != p) {
        prep(v, u);
        int tmp = mx[v][0];
        for (int _ = 0; _ < 2; ++_) if (mx[u][_] < tmp) swap(tmp, mx[u][_]);
    }
    mx[u][0] = max(mx[u][0], depth[u]);
}

void add (int u) {
    if (!cnt[ c[u] ]) ++curAns;
    ++cnt[ c[u] ];
}

void subtract (int u) {
    --cnt[ c[u] ];
    if (!cnt[ c[u] ]) --curAns;
}

void calc (int v, int Mx) {
//    cout << "v = " << v << "  Mx = " << Mx << '\n';
    while (path.size() && (*path.begin() ).fi < depth[v] - (mx[v][0] - depth[v]) ) {
        valid.insert(*path.begin() );
        add( (*path.begin() ).se );
        path.erase(path.begin() );
    }
//    for (auto _ : valid) cout << _.se << ' '; cout << '\n';
    while (valid.size() && (*valid.rbegin() ).fi >= depth[v] - Mx) {
        subtract( (*valid.rbegin() ).se );
        valid.erase(--valid.end() );
    }
    while (path.size() && (*path.rbegin() ).fi >= depth[v] - Mx) {
        path.erase(--path.end() );
    }
//    for (auto _ : valid) cout << _.se << ' '; cout << '\n';
}

void solve (int u, int p, int _) {
//    cout << "u = " << u << "  p = " << p << '\n';
//    cout << curAns << "+++\n";

    if (p && depth[u] == mx[u][0]) add(p);
    if (group[u].se == _) ans[u] += curAns;
    if (p && depth[u] == mx[u][0]) subtract(p);

    if (p) path.insert( { depth[p], p } );

    for (int v : gr[u]) if (v != p && mx[v][0] == mx[u][0]) {
        calc(v, max(0, mx[u][1] - depth[u] + 1) ); solve(v, u, _);
        for (int w : gr[u]) if (w != p && w != v) calc(w, max(0, mx[u][0] - depth[u] + 1) ), solve(w, u, _);
        break ;
    }

    if (valid.find( { depth[p], p } ) != valid.end() ) valid.erase( { depth[p], p } ), subtract(p);
    if (path.find( { depth[p], p } ) != path.end() ) path.erase( { depth[p], p } );
}

int main () {
//    freopen("test.INP", "r", stdin);
    scanf("%d %d", &n, &m);
    for (int _ = 1, u, v; _ < n; ++_) {
        scanf("%d %d", &u, &v);
        gr[u].pb(v);
        gr[v].pb(u);
    }
    for (int _ = 1; _ <= n; ++_) scanf("%d", c + _);

    dfs(1, 0);
    for (int u = 1; u <= n; ++u) if (depth[u] > depth[ diameterTip[0] ]) diameterTip[0] = u;
    dfs(diameterTip[0], 0);
    for (int u = 1; u <= n; ++u) group[u] = { depth[u], 0 };
    for (int u = 1; u <= n; ++u) if (depth[u] > depth[ diameterTip[1] ]) diameterTip[1] = u;
    dfs(diameterTip[1], 0);
    for (int u = 1; u <= n; ++u) group[u] = max(group[u], { depth[u], 1 } );

    for (int _ = 0; _ < 2; ++_) {
//        cerr << diameterTip[_] << "==========================\n";
        prep(diameterTip[_], 0);
        solve(diameterTip[_], 0, _);
    }

    for (int u = 1; u <= n; ++u) printf("%d\n", ans[u]);

    return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:83:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &u, &v);
         ~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:89:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (int _ = 1; _ <= n; ++_) scanf("%d", c + _);
                                  ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 8 ms 5240 KB Output is correct
3 Correct 8 ms 5236 KB Output is correct
4 Correct 9 ms 5240 KB Output is correct
5 Correct 9 ms 5280 KB Output is correct
6 Correct 10 ms 5496 KB Output is correct
7 Correct 9 ms 5240 KB Output is correct
8 Correct 9 ms 5112 KB Output is correct
9 Correct 9 ms 5240 KB Output is correct
10 Correct 8 ms 5240 KB Output is correct
11 Correct 8 ms 5212 KB Output is correct
12 Correct 8 ms 5208 KB Output is correct
13 Correct 9 ms 5496 KB Output is correct
14 Correct 9 ms 5240 KB Output is correct
15 Correct 9 ms 5240 KB Output is correct
16 Correct 8 ms 5240 KB Output is correct
17 Correct 9 ms 5372 KB Output is correct
18 Correct 9 ms 5368 KB Output is correct
19 Correct 8 ms 5240 KB Output is correct
20 Correct 10 ms 5496 KB Output is correct
21 Correct 9 ms 5372 KB Output is correct
22 Correct 9 ms 5112 KB Output is correct
23 Correct 8 ms 5240 KB Output is correct
24 Correct 9 ms 5204 KB Output is correct
25 Correct 9 ms 5260 KB Output is correct
26 Correct 8 ms 5240 KB Output is correct
27 Correct 9 ms 5368 KB Output is correct
28 Correct 9 ms 5368 KB Output is correct
29 Correct 9 ms 5368 KB Output is correct
30 Correct 8 ms 5240 KB Output is correct
31 Correct 9 ms 5368 KB Output is correct
32 Correct 9 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 310 ms 13796 KB Output is correct
2 Correct 535 ms 31540 KB Output is correct
3 Correct 59 ms 8952 KB Output is correct
4 Correct 631 ms 20324 KB Output is correct
5 Correct 1014 ms 51404 KB Output is correct
6 Correct 778 ms 33348 KB Output is correct
7 Correct 533 ms 20360 KB Output is correct
8 Correct 585 ms 22428 KB Output is correct
9 Correct 657 ms 21752 KB Output is correct
10 Correct 572 ms 21692 KB Output is correct
11 Correct 298 ms 20968 KB Output is correct
12 Correct 753 ms 37192 KB Output is correct
13 Correct 642 ms 34040 KB Output is correct
14 Correct 662 ms 32204 KB Output is correct
15 Correct 264 ms 20820 KB Output is correct
16 Correct 750 ms 41280 KB Output is correct
17 Correct 710 ms 33564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 436 ms 18264 KB Output is correct
2 Correct 966 ms 51832 KB Output is correct
3 Correct 67 ms 9720 KB Output is correct
4 Correct 582 ms 21972 KB Output is correct
5 Correct 1081 ms 54036 KB Output is correct
6 Correct 802 ms 35420 KB Output is correct
7 Correct 554 ms 21924 KB Output is correct
8 Correct 620 ms 26680 KB Output is correct
9 Correct 579 ms 25116 KB Output is correct
10 Correct 596 ms 23800 KB Output is correct
11 Correct 481 ms 22332 KB Output is correct
12 Correct 858 ms 45944 KB Output is correct
13 Correct 658 ms 33908 KB Output is correct
14 Correct 676 ms 33668 KB Output is correct
15 Correct 255 ms 21868 KB Output is correct
16 Correct 801 ms 43404 KB Output is correct
17 Correct 710 ms 35608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 8 ms 5240 KB Output is correct
3 Correct 8 ms 5236 KB Output is correct
4 Correct 9 ms 5240 KB Output is correct
5 Correct 9 ms 5280 KB Output is correct
6 Correct 10 ms 5496 KB Output is correct
7 Correct 9 ms 5240 KB Output is correct
8 Correct 9 ms 5112 KB Output is correct
9 Correct 9 ms 5240 KB Output is correct
10 Correct 8 ms 5240 KB Output is correct
11 Correct 8 ms 5212 KB Output is correct
12 Correct 8 ms 5208 KB Output is correct
13 Correct 9 ms 5496 KB Output is correct
14 Correct 9 ms 5240 KB Output is correct
15 Correct 9 ms 5240 KB Output is correct
16 Correct 8 ms 5240 KB Output is correct
17 Correct 9 ms 5372 KB Output is correct
18 Correct 9 ms 5368 KB Output is correct
19 Correct 8 ms 5240 KB Output is correct
20 Correct 10 ms 5496 KB Output is correct
21 Correct 9 ms 5372 KB Output is correct
22 Correct 9 ms 5112 KB Output is correct
23 Correct 8 ms 5240 KB Output is correct
24 Correct 9 ms 5204 KB Output is correct
25 Correct 9 ms 5260 KB Output is correct
26 Correct 8 ms 5240 KB Output is correct
27 Correct 9 ms 5368 KB Output is correct
28 Correct 9 ms 5368 KB Output is correct
29 Correct 9 ms 5368 KB Output is correct
30 Correct 8 ms 5240 KB Output is correct
31 Correct 9 ms 5368 KB Output is correct
32 Correct 9 ms 5368 KB Output is correct
33 Correct 310 ms 13796 KB Output is correct
34 Correct 535 ms 31540 KB Output is correct
35 Correct 59 ms 8952 KB Output is correct
36 Correct 631 ms 20324 KB Output is correct
37 Correct 1014 ms 51404 KB Output is correct
38 Correct 778 ms 33348 KB Output is correct
39 Correct 533 ms 20360 KB Output is correct
40 Correct 585 ms 22428 KB Output is correct
41 Correct 657 ms 21752 KB Output is correct
42 Correct 572 ms 21692 KB Output is correct
43 Correct 298 ms 20968 KB Output is correct
44 Correct 753 ms 37192 KB Output is correct
45 Correct 642 ms 34040 KB Output is correct
46 Correct 662 ms 32204 KB Output is correct
47 Correct 264 ms 20820 KB Output is correct
48 Correct 750 ms 41280 KB Output is correct
49 Correct 710 ms 33564 KB Output is correct
50 Correct 436 ms 18264 KB Output is correct
51 Correct 966 ms 51832 KB Output is correct
52 Correct 67 ms 9720 KB Output is correct
53 Correct 582 ms 21972 KB Output is correct
54 Correct 1081 ms 54036 KB Output is correct
55 Correct 802 ms 35420 KB Output is correct
56 Correct 554 ms 21924 KB Output is correct
57 Correct 620 ms 26680 KB Output is correct
58 Correct 579 ms 25116 KB Output is correct
59 Correct 596 ms 23800 KB Output is correct
60 Correct 481 ms 22332 KB Output is correct
61 Correct 858 ms 45944 KB Output is correct
62 Correct 658 ms 33908 KB Output is correct
63 Correct 676 ms 33668 KB Output is correct
64 Correct 255 ms 21868 KB Output is correct
65 Correct 801 ms 43404 KB Output is correct
66 Correct 710 ms 35608 KB Output is correct
67 Correct 47 ms 7264 KB Output is correct
68 Correct 338 ms 25876 KB Output is correct
69 Correct 434 ms 23288 KB Output is correct
70 Correct 521 ms 20344 KB Output is correct
71 Correct 1024 ms 51576 KB Output is correct
72 Correct 829 ms 33556 KB Output is correct
73 Correct 563 ms 20324 KB Output is correct
74 Correct 571 ms 24712 KB Output is correct
75 Correct 557 ms 22264 KB Output is correct
76 Correct 595 ms 22040 KB Output is correct
77 Correct 403 ms 20636 KB Output is correct
78 Correct 774 ms 40952 KB Output is correct
79 Correct 668 ms 38516 KB Output is correct
80 Correct 626 ms 30580 KB Output is correct
81 Correct 271 ms 20584 KB Output is correct
82 Correct 772 ms 41336 KB Output is correct
83 Correct 701 ms 33608 KB Output is correct
84 Correct 563 ms 20600 KB Output is correct
85 Correct 1077 ms 52088 KB Output is correct
86 Correct 780 ms 33904 KB Output is correct
87 Correct 549 ms 20600 KB Output is correct
88 Correct 592 ms 25304 KB Output is correct
89 Correct 569 ms 23032 KB Output is correct
90 Correct 628 ms 22564 KB Output is correct
91 Correct 409 ms 20852 KB Output is correct
92 Correct 885 ms 50764 KB Output is correct
93 Correct 610 ms 29556 KB Output is correct
94 Correct 584 ms 26744 KB Output is correct
95 Correct 256 ms 21096 KB Output is correct
96 Correct 761 ms 41968 KB Output is correct
97 Correct 715 ms 34084 KB Output is correct
98 Correct 607 ms 22104 KB Output is correct
99 Correct 1039 ms 52548 KB Output is correct
100 Correct 799 ms 35064 KB Output is correct
101 Correct 522 ms 21236 KB Output is correct
102 Correct 576 ms 24808 KB Output is correct
103 Correct 626 ms 23480 KB Output is correct
104 Correct 642 ms 22996 KB Output is correct
105 Correct 451 ms 21236 KB Output is correct
106 Correct 711 ms 36472 KB Output is correct
107 Correct 681 ms 38844 KB Output is correct
108 Correct 622 ms 29360 KB Output is correct
109 Correct 270 ms 21816 KB Output is correct
110 Correct 729 ms 42620 KB Output is correct
111 Correct 739 ms 35076 KB Output is correct