답안 #447222

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
447222 2021-07-25T07:58:00 Z blue Mergers (JOI19_mergers) C++17
100 / 100
1267 ms 151476 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);


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();
        }
    }


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






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);

    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;
    }

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

    parent[rt] = rt;
    dfs1(rt);
    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';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 25768 KB Output is correct
2 Correct 14 ms 25708 KB Output is correct
3 Correct 14 ms 25676 KB Output is correct
4 Correct 14 ms 25788 KB Output is correct
5 Correct 14 ms 25696 KB Output is correct
6 Correct 14 ms 25804 KB Output is correct
7 Correct 14 ms 25700 KB Output is correct
8 Correct 14 ms 25800 KB Output is correct
9 Correct 14 ms 25676 KB Output is correct
10 Correct 14 ms 25676 KB Output is correct
11 Correct 14 ms 25804 KB Output is correct
12 Correct 14 ms 25804 KB Output is correct
13 Correct 14 ms 25672 KB Output is correct
14 Correct 14 ms 25804 KB Output is correct
15 Correct 15 ms 25800 KB Output is correct
16 Correct 14 ms 25676 KB Output is correct
17 Correct 14 ms 25676 KB Output is correct
18 Correct 14 ms 25804 KB Output is correct
19 Correct 14 ms 25808 KB Output is correct
20 Correct 15 ms 25744 KB Output is correct
21 Correct 14 ms 25772 KB Output is correct
22 Correct 14 ms 25804 KB Output is correct
23 Correct 14 ms 25692 KB Output is correct
24 Correct 14 ms 25688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 25768 KB Output is correct
2 Correct 14 ms 25708 KB Output is correct
3 Correct 14 ms 25676 KB Output is correct
4 Correct 14 ms 25788 KB Output is correct
5 Correct 14 ms 25696 KB Output is correct
6 Correct 14 ms 25804 KB Output is correct
7 Correct 14 ms 25700 KB Output is correct
8 Correct 14 ms 25800 KB Output is correct
9 Correct 14 ms 25676 KB Output is correct
10 Correct 14 ms 25676 KB Output is correct
11 Correct 14 ms 25804 KB Output is correct
12 Correct 14 ms 25804 KB Output is correct
13 Correct 14 ms 25672 KB Output is correct
14 Correct 14 ms 25804 KB Output is correct
15 Correct 15 ms 25800 KB Output is correct
16 Correct 14 ms 25676 KB Output is correct
17 Correct 14 ms 25676 KB Output is correct
18 Correct 14 ms 25804 KB Output is correct
19 Correct 14 ms 25808 KB Output is correct
20 Correct 15 ms 25744 KB Output is correct
21 Correct 14 ms 25772 KB Output is correct
22 Correct 14 ms 25804 KB Output is correct
23 Correct 14 ms 25692 KB Output is correct
24 Correct 14 ms 25688 KB Output is correct
25 Correct 14 ms 25692 KB Output is correct
26 Correct 16 ms 26324 KB Output is correct
27 Correct 16 ms 26060 KB Output is correct
28 Correct 16 ms 26356 KB Output is correct
29 Correct 17 ms 26444 KB Output is correct
30 Correct 17 ms 26228 KB Output is correct
31 Correct 14 ms 25804 KB Output is correct
32 Correct 16 ms 26172 KB Output is correct
33 Correct 14 ms 25704 KB Output is correct
34 Correct 16 ms 26316 KB Output is correct
35 Correct 16 ms 26448 KB Output is correct
36 Correct 16 ms 26184 KB Output is correct
37 Correct 17 ms 26220 KB Output is correct
38 Correct 14 ms 25808 KB Output is correct
39 Correct 17 ms 26188 KB Output is correct
40 Correct 17 ms 26156 KB Output is correct
41 Correct 16 ms 26104 KB Output is correct
42 Correct 16 ms 26348 KB Output is correct
43 Correct 16 ms 26188 KB Output is correct
44 Correct 14 ms 25804 KB Output is correct
45 Correct 17 ms 26060 KB Output is correct
46 Correct 16 ms 26180 KB Output is correct
47 Correct 14 ms 25820 KB Output is correct
48 Correct 16 ms 26188 KB Output is correct
49 Correct 17 ms 26524 KB Output is correct
50 Correct 17 ms 26444 KB Output is correct
51 Correct 16 ms 26092 KB Output is correct
52 Correct 16 ms 26316 KB Output is correct
53 Correct 16 ms 26332 KB Output is correct
54 Correct 17 ms 26136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 25768 KB Output is correct
2 Correct 14 ms 25708 KB Output is correct
3 Correct 14 ms 25676 KB Output is correct
4 Correct 14 ms 25788 KB Output is correct
5 Correct 14 ms 25696 KB Output is correct
6 Correct 14 ms 25804 KB Output is correct
7 Correct 14 ms 25700 KB Output is correct
8 Correct 14 ms 25800 KB Output is correct
9 Correct 14 ms 25676 KB Output is correct
10 Correct 14 ms 25676 KB Output is correct
11 Correct 14 ms 25804 KB Output is correct
12 Correct 14 ms 25804 KB Output is correct
13 Correct 14 ms 25672 KB Output is correct
14 Correct 14 ms 25804 KB Output is correct
15 Correct 15 ms 25800 KB Output is correct
16 Correct 14 ms 25676 KB Output is correct
17 Correct 14 ms 25676 KB Output is correct
18 Correct 14 ms 25804 KB Output is correct
19 Correct 14 ms 25808 KB Output is correct
20 Correct 15 ms 25744 KB Output is correct
21 Correct 14 ms 25772 KB Output is correct
22 Correct 14 ms 25804 KB Output is correct
23 Correct 14 ms 25692 KB Output is correct
24 Correct 14 ms 25688 KB Output is correct
25 Correct 14 ms 25800 KB Output is correct
26 Correct 90 ms 46568 KB Output is correct
27 Correct 144 ms 38556 KB Output is correct
28 Correct 16 ms 26012 KB Output is correct
29 Correct 14 ms 25804 KB Output is correct
30 Correct 14 ms 25812 KB Output is correct
31 Correct 106 ms 38516 KB Output is correct
32 Correct 16 ms 26444 KB Output is correct
33 Correct 125 ms 40788 KB Output is correct
34 Correct 134 ms 38592 KB Output is correct
35 Correct 17 ms 26188 KB Output is correct
36 Correct 109 ms 35408 KB Output is correct
37 Correct 16 ms 26188 KB Output is correct
38 Correct 16 ms 26112 KB Output is correct
39 Correct 98 ms 45520 KB Output is correct
40 Correct 16 ms 26188 KB Output is correct
41 Correct 108 ms 38460 KB Output is correct
42 Correct 122 ms 35268 KB Output is correct
43 Correct 14 ms 25804 KB Output is correct
44 Correct 128 ms 41240 KB Output is correct
45 Correct 136 ms 40200 KB Output is correct
46 Correct 16 ms 26316 KB Output is correct
47 Correct 17 ms 26128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 46532 KB Output is correct
2 Correct 122 ms 51396 KB Output is correct
3 Correct 16 ms 26316 KB Output is correct
4 Correct 17 ms 26060 KB Output is correct
5 Correct 14 ms 25804 KB Output is correct
6 Correct 14 ms 25760 KB Output is correct
7 Correct 16 ms 26444 KB Output is correct
8 Correct 126 ms 39380 KB Output is correct
9 Correct 16 ms 26188 KB Output is correct
10 Correct 107 ms 38524 KB Output is correct
11 Correct 14 ms 25808 KB Output is correct
12 Correct 102 ms 35400 KB Output is correct
13 Correct 119 ms 39732 KB Output is correct
14 Correct 118 ms 47932 KB Output is correct
15 Correct 96 ms 45464 KB Output is correct
16 Correct 16 ms 26316 KB Output is correct
17 Correct 14 ms 25804 KB Output is correct
18 Correct 122 ms 49432 KB Output is correct
19 Correct 128 ms 45564 KB Output is correct
20 Correct 16 ms 26188 KB Output is correct
21 Correct 14 ms 25756 KB Output is correct
22 Correct 121 ms 45756 KB Output is correct
23 Correct 16 ms 26188 KB Output is correct
24 Correct 124 ms 37220 KB Output is correct
25 Correct 130 ms 47704 KB Output is correct
26 Correct 19 ms 26580 KB Output is correct
27 Correct 16 ms 26368 KB Output is correct
28 Correct 16 ms 26060 KB Output is correct
29 Correct 16 ms 26316 KB Output is correct
30 Correct 17 ms 26316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 25768 KB Output is correct
2 Correct 14 ms 25708 KB Output is correct
3 Correct 14 ms 25676 KB Output is correct
4 Correct 14 ms 25788 KB Output is correct
5 Correct 14 ms 25696 KB Output is correct
6 Correct 14 ms 25804 KB Output is correct
7 Correct 14 ms 25700 KB Output is correct
8 Correct 14 ms 25800 KB Output is correct
9 Correct 14 ms 25676 KB Output is correct
10 Correct 14 ms 25676 KB Output is correct
11 Correct 14 ms 25804 KB Output is correct
12 Correct 14 ms 25804 KB Output is correct
13 Correct 14 ms 25672 KB Output is correct
14 Correct 14 ms 25804 KB Output is correct
15 Correct 15 ms 25800 KB Output is correct
16 Correct 14 ms 25676 KB Output is correct
17 Correct 14 ms 25676 KB Output is correct
18 Correct 14 ms 25804 KB Output is correct
19 Correct 14 ms 25808 KB Output is correct
20 Correct 15 ms 25744 KB Output is correct
21 Correct 14 ms 25772 KB Output is correct
22 Correct 14 ms 25804 KB Output is correct
23 Correct 14 ms 25692 KB Output is correct
24 Correct 14 ms 25688 KB Output is correct
25 Correct 14 ms 25692 KB Output is correct
26 Correct 16 ms 26324 KB Output is correct
27 Correct 16 ms 26060 KB Output is correct
28 Correct 16 ms 26356 KB Output is correct
29 Correct 17 ms 26444 KB Output is correct
30 Correct 17 ms 26228 KB Output is correct
31 Correct 14 ms 25804 KB Output is correct
32 Correct 16 ms 26172 KB Output is correct
33 Correct 14 ms 25704 KB Output is correct
34 Correct 16 ms 26316 KB Output is correct
35 Correct 16 ms 26448 KB Output is correct
36 Correct 16 ms 26184 KB Output is correct
37 Correct 17 ms 26220 KB Output is correct
38 Correct 14 ms 25808 KB Output is correct
39 Correct 17 ms 26188 KB Output is correct
40 Correct 17 ms 26156 KB Output is correct
41 Correct 16 ms 26104 KB Output is correct
42 Correct 16 ms 26348 KB Output is correct
43 Correct 16 ms 26188 KB Output is correct
44 Correct 14 ms 25804 KB Output is correct
45 Correct 17 ms 26060 KB Output is correct
46 Correct 16 ms 26180 KB Output is correct
47 Correct 14 ms 25820 KB Output is correct
48 Correct 16 ms 26188 KB Output is correct
49 Correct 17 ms 26524 KB Output is correct
50 Correct 17 ms 26444 KB Output is correct
51 Correct 16 ms 26092 KB Output is correct
52 Correct 16 ms 26316 KB Output is correct
53 Correct 16 ms 26332 KB Output is correct
54 Correct 17 ms 26136 KB Output is correct
55 Correct 14 ms 25800 KB Output is correct
56 Correct 90 ms 46568 KB Output is correct
57 Correct 144 ms 38556 KB Output is correct
58 Correct 16 ms 26012 KB Output is correct
59 Correct 14 ms 25804 KB Output is correct
60 Correct 14 ms 25812 KB Output is correct
61 Correct 106 ms 38516 KB Output is correct
62 Correct 16 ms 26444 KB Output is correct
63 Correct 125 ms 40788 KB Output is correct
64 Correct 134 ms 38592 KB Output is correct
65 Correct 17 ms 26188 KB Output is correct
66 Correct 109 ms 35408 KB Output is correct
67 Correct 16 ms 26188 KB Output is correct
68 Correct 16 ms 26112 KB Output is correct
69 Correct 98 ms 45520 KB Output is correct
70 Correct 16 ms 26188 KB Output is correct
71 Correct 108 ms 38460 KB Output is correct
72 Correct 122 ms 35268 KB Output is correct
73 Correct 14 ms 25804 KB Output is correct
74 Correct 128 ms 41240 KB Output is correct
75 Correct 136 ms 40200 KB Output is correct
76 Correct 16 ms 26316 KB Output is correct
77 Correct 17 ms 26128 KB Output is correct
78 Correct 91 ms 46532 KB Output is correct
79 Correct 122 ms 51396 KB Output is correct
80 Correct 16 ms 26316 KB Output is correct
81 Correct 17 ms 26060 KB Output is correct
82 Correct 14 ms 25804 KB Output is correct
83 Correct 14 ms 25760 KB Output is correct
84 Correct 16 ms 26444 KB Output is correct
85 Correct 126 ms 39380 KB Output is correct
86 Correct 16 ms 26188 KB Output is correct
87 Correct 107 ms 38524 KB Output is correct
88 Correct 14 ms 25808 KB Output is correct
89 Correct 102 ms 35400 KB Output is correct
90 Correct 119 ms 39732 KB Output is correct
91 Correct 118 ms 47932 KB Output is correct
92 Correct 96 ms 45464 KB Output is correct
93 Correct 16 ms 26316 KB Output is correct
94 Correct 14 ms 25804 KB Output is correct
95 Correct 122 ms 49432 KB Output is correct
96 Correct 128 ms 45564 KB Output is correct
97 Correct 16 ms 26188 KB Output is correct
98 Correct 14 ms 25756 KB Output is correct
99 Correct 121 ms 45756 KB Output is correct
100 Correct 16 ms 26188 KB Output is correct
101 Correct 124 ms 37220 KB Output is correct
102 Correct 130 ms 47704 KB Output is correct
103 Correct 19 ms 26580 KB Output is correct
104 Correct 16 ms 26368 KB Output is correct
105 Correct 16 ms 26060 KB Output is correct
106 Correct 16 ms 26316 KB Output is correct
107 Correct 17 ms 26316 KB Output is correct
108 Correct 941 ms 126812 KB Output is correct
109 Correct 882 ms 74004 KB Output is correct
110 Correct 877 ms 89544 KB Output is correct
111 Correct 899 ms 124700 KB Output is correct
112 Correct 1132 ms 112832 KB Output is correct
113 Correct 917 ms 142816 KB Output is correct
114 Correct 762 ms 87492 KB Output is correct
115 Correct 763 ms 87276 KB Output is correct
116 Correct 1053 ms 88644 KB Output is correct
117 Correct 827 ms 134720 KB Output is correct
118 Correct 746 ms 86556 KB Output is correct
119 Correct 836 ms 134608 KB Output is correct
120 Correct 865 ms 134748 KB Output is correct
121 Correct 829 ms 134276 KB Output is correct
122 Correct 1267 ms 96168 KB Output is correct
123 Correct 899 ms 151476 KB Output is correct
124 Correct 851 ms 100164 KB Output is correct