Submission #447220

# Submission time Handle Problem Language Result Execution time Memory
447220 2021-07-25T07:54:08 Z blue Mergers (JOI19_mergers) C++17
100 / 100
1346 ms 160932 KB
#include <iostream>
#include <set>
#include <vector>
#include <set>
using namespace std;

const int maxN = 500'000;

vector<int> edge[1+maxN];
vector<int> parent(1+maxN, 0);
vector<int> subtree(1+maxN, 1);
vector<int> max_child(1+maxN, -1);

int rt = 1;

void dfs1(int u)
{
    for(int v: edge[u])
    {
        if(parent[u] == v) continue;
        parent[v] = u;
        dfs1(v);
        subtree[u] += subtree[v];
        if(max_child[u] == -1 || subtree[v] > subtree[ max_child[u] ])
            max_child[u] = v;
    }
}




struct state
{
    int i;
    int c;
};

bool operator < (state A, state B)
{
    return A.i < B.i;
}

int S[1+maxN];
vector<int> state_count(1+maxN, 0);

set<state>* T[1+maxN];



void add_state(int u, state x)
{
    // cerr << "add_state " << u << ' ' << x.i << ' ' << x.c << '\n';
    set<state>::iterator it = T[u]->find(x);
    if(it == T[u]->end())
    {
        // cerr << "case A\n";
        if(x.c != state_count[x.i])
            T[u]->insert(x);
    }
    else
    {
        // cerr << "case B\n";
        int ct = it->c + x.c;
        T[u]->erase(it);
        if(ct != state_count[x.i])
        {
            T[u]->insert(state{x.i, ct});
        }
    }
}

vector<int> is_bad(1+maxN, 0);

vector<int> bad_count(1+maxN, 0);

int meeting_point = 0;

void dfs2(int u)
{
    // cerr << "dfs2 " << u << ' ' << max_child[u] << '\n';
    if(max_child[u] == -1)
    {
        // cerr << "case 1\n";
        T[u] = new set<state>;
        add_state(u, state{S[u], 1});
    }
    else
    {
        for(int v: edge[u])
        {
            if(v == parent[u]) continue;
            dfs2(v);
        }

        // cerr << "case 2\n";
        T[u] = T[ max_child[u] ];
        // cerr << "x\n";
        add_state(u, state{S[u], 1});
        // cerr << "check X\n";
        for(int v: edge[u])
        {
            // cerr << "v = " << v << '\n';
            if(v == parent[u] || v == max_child[u]) continue;

            for(state s: *(T[v]))
                add_state(u, s);

            T[v]->clear();
        }
    }

    int lower_bad_count = 0;;
    for(int v: edge[u])
    {
        if(v == parent[u]) continue;
        lower_bad_count += bad_count[v];
    }

    if(T[u]->size() == 0 && u != rt)
    {
        is_bad[u] = 1;
    }

    if(lower_bad_count)
    {
        bad_count[u] = lower_bad_count;
    }
    else if(is_bad[u])
    {
        bad_count[u] = 1;
    }
    else
    {
        bad_count[u] = 0;
    }

    if(bad_count[u] > bad_count[meeting_point]) meeting_point = u;
    else if(bad_count[u] == bad_count[meeting_point] && subtree[u] < subtree[meeting_point])
        meeting_point = u;
}






vector<int> dsu_parent(1+maxN);
vector<int> dsu_size(1+maxN, 1);

int getRoot(int u)
{
    while(dsu_parent[u] != u)
        u = dsu_parent[u];

    return u;
}

bool dsu_connected(int u, int v)
{
    return getRoot(u) == getRoot(v);
}

void dsu_join(int u, int v)
{
    u = getRoot(u);
    v = getRoot(v);

    if(dsu_size[u] < dsu_size[v]) swap(u, v);

    dsu_parent[v] = u;
    dsu_size[u] += dsu_size[v];
}





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

    bad_count[0] = -1;
    subtree[0] = 1e9;

    int N, K;
    cin >> N >> K;

    for(int i = 1; i <= N-1; i++)
    {
        int A, B;
        cin >> A >> B;
        edge[A].push_back(B);
        edge[B].push_back(A);
    }

    for(int i = 1; i <= N; i++)
    {
        cin >> S[i];
        state_count[ S[i] ]++;
    }

    if(N == 2)
    {
        cout << "1\n";
        return 0;
    }

    // cerr << "check 0\n";

    for(rt = 1; edge[rt].size() == 1; rt++);

    parent[rt] = rt;
    dfs1(rt);

    // cerr << "check 1\n";

    dfs2(rt);

    for(int i = 1; i <= N; i++) dsu_parent[i] = i;

    for(int i = 1; i <= N; i++)
    {
        if(i == rt) continue;
        if(!is_bad[i]) dsu_join(i, parent[i]);
    }

    set<int> edg[1+N];
    for(int i = 1; i <= N; i++)
    {
        for(int j: edge[i])
        {
            if(!dsu_connected(i, j))
            {
                edg[ getRoot(i) ].insert( getRoot(j) );
            }
        }
    }

    int cnt = 0;
    for(int i = 1; i <= N; i++)
    {
        if(getRoot(i) == i && edg[i].size() == 1)
            cnt++;
    }

    cout << (cnt+1)/2 << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 27724 KB Output is correct
2 Correct 15 ms 27760 KB Output is correct
3 Correct 15 ms 27732 KB Output is correct
4 Correct 15 ms 27724 KB Output is correct
5 Correct 15 ms 27656 KB Output is correct
6 Correct 15 ms 27724 KB Output is correct
7 Correct 15 ms 27648 KB Output is correct
8 Correct 15 ms 27676 KB Output is correct
9 Correct 15 ms 27724 KB Output is correct
10 Correct 15 ms 27724 KB Output is correct
11 Correct 15 ms 27652 KB Output is correct
12 Correct 15 ms 27648 KB Output is correct
13 Correct 15 ms 27760 KB Output is correct
14 Correct 15 ms 27724 KB Output is correct
15 Correct 15 ms 27772 KB Output is correct
16 Correct 17 ms 27752 KB Output is correct
17 Correct 17 ms 27724 KB Output is correct
18 Correct 15 ms 27724 KB Output is correct
19 Correct 15 ms 27768 KB Output is correct
20 Correct 15 ms 27644 KB Output is correct
21 Correct 15 ms 27668 KB Output is correct
22 Correct 15 ms 27652 KB Output is correct
23 Correct 15 ms 27680 KB Output is correct
24 Correct 15 ms 27724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 27724 KB Output is correct
2 Correct 15 ms 27760 KB Output is correct
3 Correct 15 ms 27732 KB Output is correct
4 Correct 15 ms 27724 KB Output is correct
5 Correct 15 ms 27656 KB Output is correct
6 Correct 15 ms 27724 KB Output is correct
7 Correct 15 ms 27648 KB Output is correct
8 Correct 15 ms 27676 KB Output is correct
9 Correct 15 ms 27724 KB Output is correct
10 Correct 15 ms 27724 KB Output is correct
11 Correct 15 ms 27652 KB Output is correct
12 Correct 15 ms 27648 KB Output is correct
13 Correct 15 ms 27760 KB Output is correct
14 Correct 15 ms 27724 KB Output is correct
15 Correct 15 ms 27772 KB Output is correct
16 Correct 17 ms 27752 KB Output is correct
17 Correct 17 ms 27724 KB Output is correct
18 Correct 15 ms 27724 KB Output is correct
19 Correct 15 ms 27768 KB Output is correct
20 Correct 15 ms 27644 KB Output is correct
21 Correct 15 ms 27668 KB Output is correct
22 Correct 15 ms 27652 KB Output is correct
23 Correct 15 ms 27680 KB Output is correct
24 Correct 15 ms 27724 KB Output is correct
25 Correct 15 ms 27684 KB Output is correct
26 Correct 17 ms 28184 KB Output is correct
27 Correct 18 ms 27980 KB Output is correct
28 Correct 17 ms 28276 KB Output is correct
29 Correct 18 ms 28408 KB Output is correct
30 Correct 18 ms 28108 KB Output is correct
31 Correct 15 ms 27752 KB Output is correct
32 Correct 17 ms 28108 KB Output is correct
33 Correct 16 ms 27736 KB Output is correct
34 Correct 18 ms 28364 KB Output is correct
35 Correct 17 ms 28356 KB Output is correct
36 Correct 17 ms 28108 KB Output is correct
37 Correct 18 ms 28160 KB Output is correct
38 Correct 15 ms 27760 KB Output is correct
39 Correct 18 ms 28236 KB Output is correct
40 Correct 18 ms 28120 KB Output is correct
41 Correct 18 ms 28156 KB Output is correct
42 Correct 18 ms 28364 KB Output is correct
43 Correct 17 ms 28156 KB Output is correct
44 Correct 15 ms 27792 KB Output is correct
45 Correct 17 ms 28028 KB Output is correct
46 Correct 17 ms 28124 KB Output is correct
47 Correct 15 ms 27680 KB Output is correct
48 Correct 18 ms 28108 KB Output is correct
49 Correct 17 ms 28492 KB Output is correct
50 Correct 18 ms 28424 KB Output is correct
51 Correct 17 ms 28052 KB Output is correct
52 Correct 17 ms 28272 KB Output is correct
53 Correct 17 ms 28332 KB Output is correct
54 Correct 18 ms 28108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 27724 KB Output is correct
2 Correct 15 ms 27760 KB Output is correct
3 Correct 15 ms 27732 KB Output is correct
4 Correct 15 ms 27724 KB Output is correct
5 Correct 15 ms 27656 KB Output is correct
6 Correct 15 ms 27724 KB Output is correct
7 Correct 15 ms 27648 KB Output is correct
8 Correct 15 ms 27676 KB Output is correct
9 Correct 15 ms 27724 KB Output is correct
10 Correct 15 ms 27724 KB Output is correct
11 Correct 15 ms 27652 KB Output is correct
12 Correct 15 ms 27648 KB Output is correct
13 Correct 15 ms 27760 KB Output is correct
14 Correct 15 ms 27724 KB Output is correct
15 Correct 15 ms 27772 KB Output is correct
16 Correct 17 ms 27752 KB Output is correct
17 Correct 17 ms 27724 KB Output is correct
18 Correct 15 ms 27724 KB Output is correct
19 Correct 15 ms 27768 KB Output is correct
20 Correct 15 ms 27644 KB Output is correct
21 Correct 15 ms 27668 KB Output is correct
22 Correct 15 ms 27652 KB Output is correct
23 Correct 15 ms 27680 KB Output is correct
24 Correct 15 ms 27724 KB Output is correct
25 Correct 15 ms 27656 KB Output is correct
26 Correct 97 ms 49532 KB Output is correct
27 Correct 143 ms 41464 KB Output is correct
28 Correct 17 ms 27980 KB Output is correct
29 Correct 15 ms 27724 KB Output is correct
30 Correct 15 ms 27724 KB Output is correct
31 Correct 115 ms 41396 KB Output is correct
32 Correct 17 ms 28352 KB Output is correct
33 Correct 137 ms 43796 KB Output is correct
34 Correct 147 ms 41420 KB Output is correct
35 Correct 18 ms 28128 KB Output is correct
36 Correct 122 ms 38152 KB Output is correct
37 Correct 17 ms 28128 KB Output is correct
38 Correct 17 ms 28108 KB Output is correct
39 Correct 108 ms 48348 KB Output is correct
40 Correct 17 ms 28224 KB Output is correct
41 Correct 130 ms 41300 KB Output is correct
42 Correct 152 ms 38084 KB Output is correct
43 Correct 15 ms 27744 KB Output is correct
44 Correct 129 ms 43976 KB Output is correct
45 Correct 137 ms 43076 KB Output is correct
46 Correct 17 ms 28288 KB Output is correct
47 Correct 18 ms 28108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 49536 KB Output is correct
2 Correct 122 ms 54460 KB Output is correct
3 Correct 18 ms 28280 KB Output is correct
4 Correct 17 ms 28024 KB Output is correct
5 Correct 16 ms 27744 KB Output is correct
6 Correct 15 ms 27700 KB Output is correct
7 Correct 17 ms 28364 KB Output is correct
8 Correct 128 ms 42616 KB Output is correct
9 Correct 17 ms 28164 KB Output is correct
10 Correct 116 ms 41496 KB Output is correct
11 Correct 15 ms 27724 KB Output is correct
12 Correct 108 ms 38468 KB Output is correct
13 Correct 124 ms 42940 KB Output is correct
14 Correct 125 ms 50992 KB Output is correct
15 Correct 108 ms 48444 KB Output is correct
16 Correct 19 ms 28364 KB Output is correct
17 Correct 15 ms 27760 KB Output is correct
18 Correct 132 ms 52736 KB Output is correct
19 Correct 136 ms 48788 KB Output is correct
20 Correct 18 ms 28120 KB Output is correct
21 Correct 15 ms 27740 KB Output is correct
22 Correct 133 ms 48840 KB Output is correct
23 Correct 17 ms 28056 KB Output is correct
24 Correct 114 ms 40280 KB Output is correct
25 Correct 135 ms 50904 KB Output is correct
26 Correct 19 ms 28556 KB Output is correct
27 Correct 17 ms 28340 KB Output is correct
28 Correct 17 ms 28100 KB Output is correct
29 Correct 17 ms 28272 KB Output is correct
30 Correct 17 ms 28288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 27724 KB Output is correct
2 Correct 15 ms 27760 KB Output is correct
3 Correct 15 ms 27732 KB Output is correct
4 Correct 15 ms 27724 KB Output is correct
5 Correct 15 ms 27656 KB Output is correct
6 Correct 15 ms 27724 KB Output is correct
7 Correct 15 ms 27648 KB Output is correct
8 Correct 15 ms 27676 KB Output is correct
9 Correct 15 ms 27724 KB Output is correct
10 Correct 15 ms 27724 KB Output is correct
11 Correct 15 ms 27652 KB Output is correct
12 Correct 15 ms 27648 KB Output is correct
13 Correct 15 ms 27760 KB Output is correct
14 Correct 15 ms 27724 KB Output is correct
15 Correct 15 ms 27772 KB Output is correct
16 Correct 17 ms 27752 KB Output is correct
17 Correct 17 ms 27724 KB Output is correct
18 Correct 15 ms 27724 KB Output is correct
19 Correct 15 ms 27768 KB Output is correct
20 Correct 15 ms 27644 KB Output is correct
21 Correct 15 ms 27668 KB Output is correct
22 Correct 15 ms 27652 KB Output is correct
23 Correct 15 ms 27680 KB Output is correct
24 Correct 15 ms 27724 KB Output is correct
25 Correct 15 ms 27684 KB Output is correct
26 Correct 17 ms 28184 KB Output is correct
27 Correct 18 ms 27980 KB Output is correct
28 Correct 17 ms 28276 KB Output is correct
29 Correct 18 ms 28408 KB Output is correct
30 Correct 18 ms 28108 KB Output is correct
31 Correct 15 ms 27752 KB Output is correct
32 Correct 17 ms 28108 KB Output is correct
33 Correct 16 ms 27736 KB Output is correct
34 Correct 18 ms 28364 KB Output is correct
35 Correct 17 ms 28356 KB Output is correct
36 Correct 17 ms 28108 KB Output is correct
37 Correct 18 ms 28160 KB Output is correct
38 Correct 15 ms 27760 KB Output is correct
39 Correct 18 ms 28236 KB Output is correct
40 Correct 18 ms 28120 KB Output is correct
41 Correct 18 ms 28156 KB Output is correct
42 Correct 18 ms 28364 KB Output is correct
43 Correct 17 ms 28156 KB Output is correct
44 Correct 15 ms 27792 KB Output is correct
45 Correct 17 ms 28028 KB Output is correct
46 Correct 17 ms 28124 KB Output is correct
47 Correct 15 ms 27680 KB Output is correct
48 Correct 18 ms 28108 KB Output is correct
49 Correct 17 ms 28492 KB Output is correct
50 Correct 18 ms 28424 KB Output is correct
51 Correct 17 ms 28052 KB Output is correct
52 Correct 17 ms 28272 KB Output is correct
53 Correct 17 ms 28332 KB Output is correct
54 Correct 18 ms 28108 KB Output is correct
55 Correct 15 ms 27656 KB Output is correct
56 Correct 97 ms 49532 KB Output is correct
57 Correct 143 ms 41464 KB Output is correct
58 Correct 17 ms 27980 KB Output is correct
59 Correct 15 ms 27724 KB Output is correct
60 Correct 15 ms 27724 KB Output is correct
61 Correct 115 ms 41396 KB Output is correct
62 Correct 17 ms 28352 KB Output is correct
63 Correct 137 ms 43796 KB Output is correct
64 Correct 147 ms 41420 KB Output is correct
65 Correct 18 ms 28128 KB Output is correct
66 Correct 122 ms 38152 KB Output is correct
67 Correct 17 ms 28128 KB Output is correct
68 Correct 17 ms 28108 KB Output is correct
69 Correct 108 ms 48348 KB Output is correct
70 Correct 17 ms 28224 KB Output is correct
71 Correct 130 ms 41300 KB Output is correct
72 Correct 152 ms 38084 KB Output is correct
73 Correct 15 ms 27744 KB Output is correct
74 Correct 129 ms 43976 KB Output is correct
75 Correct 137 ms 43076 KB Output is correct
76 Correct 17 ms 28288 KB Output is correct
77 Correct 18 ms 28108 KB Output is correct
78 Correct 98 ms 49536 KB Output is correct
79 Correct 122 ms 54460 KB Output is correct
80 Correct 18 ms 28280 KB Output is correct
81 Correct 17 ms 28024 KB Output is correct
82 Correct 16 ms 27744 KB Output is correct
83 Correct 15 ms 27700 KB Output is correct
84 Correct 17 ms 28364 KB Output is correct
85 Correct 128 ms 42616 KB Output is correct
86 Correct 17 ms 28164 KB Output is correct
87 Correct 116 ms 41496 KB Output is correct
88 Correct 15 ms 27724 KB Output is correct
89 Correct 108 ms 38468 KB Output is correct
90 Correct 124 ms 42940 KB Output is correct
91 Correct 125 ms 50992 KB Output is correct
92 Correct 108 ms 48444 KB Output is correct
93 Correct 19 ms 28364 KB Output is correct
94 Correct 15 ms 27760 KB Output is correct
95 Correct 132 ms 52736 KB Output is correct
96 Correct 136 ms 48788 KB Output is correct
97 Correct 18 ms 28120 KB Output is correct
98 Correct 15 ms 27740 KB Output is correct
99 Correct 133 ms 48840 KB Output is correct
100 Correct 17 ms 28056 KB Output is correct
101 Correct 114 ms 40280 KB Output is correct
102 Correct 135 ms 50904 KB Output is correct
103 Correct 19 ms 28556 KB Output is correct
104 Correct 17 ms 28340 KB Output is correct
105 Correct 17 ms 28100 KB Output is correct
106 Correct 17 ms 28272 KB Output is correct
107 Correct 17 ms 28288 KB Output is correct
108 Correct 1032 ms 134932 KB Output is correct
109 Correct 903 ms 82756 KB Output is correct
110 Correct 906 ms 98592 KB Output is correct
111 Correct 935 ms 133136 KB Output is correct
112 Correct 1213 ms 121284 KB Output is correct
113 Correct 985 ms 151460 KB Output is correct
114 Correct 905 ms 96636 KB Output is correct
115 Correct 863 ms 96480 KB Output is correct
116 Correct 1135 ms 97520 KB Output is correct
117 Correct 898 ms 143732 KB Output is correct
118 Correct 816 ms 95680 KB Output is correct
119 Correct 887 ms 143640 KB Output is correct
120 Correct 962 ms 144076 KB Output is correct
121 Correct 916 ms 143580 KB Output is correct
122 Correct 1346 ms 105520 KB Output is correct
123 Correct 999 ms 160932 KB Output is correct
124 Correct 921 ms 110172 KB Output is correct