답안 #163813

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
163813 2019-11-15T13:00:39 Z georgerapeanu Mergers (JOI19_mergers) C++11
100 / 100
1636 ms 193172 KB
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 5e5;
const int LGMAX = 19;

int n;
int k;

vector<int> graph[NMAX + 5];
vector<int> nodes[NMAX + 5];

int state[NMAX + 5];
int father[LGMAX + 5][NMAX + 5];
int lvl[NMAX + 5];
int merge_root[NMAX + 5];
bool seen[NMAX + 5];

void predfs(int nod,int tata) {
    father[0][nod] = tata;
    merge_root[nod] = 0;
    lvl[nod] = 1 + lvl[tata];
    for(auto it:graph[nod]) {
        if(it == tata) {
            continue;
        }
        predfs(it,nod);
    }
}

void prelca() {
    for(int h = 1; h <= LGMAX; h++) {
        for(int i = 1; i <= NMAX; i++) {
            father[h][i] = father[h - 1][father[h - 1][i]];
        }
    }
}

int lca(int x,int y) {
    if(lvl[x] > lvl[y]) {
        swap(x,y);
    }

    int diff = lvl[y] - lvl[x];

    for(int h = LGMAX; h >= 0; h--) {
        if((diff >> h) & 1) {
            y = father[h][y];
        }
    }

    if(x == y) {
        return x;
    }

    for(int h = LGMAX; h >= 0; h--) {
        if(father[h][x] != father[h][y]) {
            x = father[h][x];
            y = father[h][y];
        }
    }

    return father[0][x];
}

int col_root[NMAX + 5];

int fi_col_root(int node) {
    if(col_root[node] == 0) {
        return node;
    }
    return col_root[node] = fi_col_root(col_root[node]);
}

bool merge_cols(int u,int v) {
    u = fi_col_root(u);
    v = fi_col_root(v);

    if(u == v) {
        return false;
    }

    col_root[v] = u;

    return true;
}

int fi_merge_root(int node) {
    if(merge_root[node] == 0) {
        return node;
    }
    return merge_root[node] = fi_merge_root(merge_root[node]);
}

void do_merge(int node,int root) {
    node = fi_merge_root(node);
    while(lvl[root] < lvl[node]) {
        merge_cols(state[node],state[father[0][node]]);
        merge_root[node] = father[0][node];
        node = fi_merge_root(node);
    }
}

set<int> col_neighbors[NMAX + 5];

int main() {

    scanf("%d %d",&n,&k);

    for(int i = 1; i < n; i++) {
        int u,v;
        scanf("%d %d",&u,&v);
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    for(int i = 1; i <= n; i++) {
        scanf("%d",&state[i]);
        nodes[state[i]].push_back(i);
    }

    predfs(1,0);
    prelca();

    for(int i = 1; i <= k; i++) {
        if(nodes[i].empty()) {
            continue;
        }

        int u = nodes[i].back();

        for(auto it:nodes[i]) {
            u = lca(u,it);
        }

        for(auto it:nodes[i]) {
            do_merge(it,u);
        }
    }

    int cnt = 0;

    for(int col = 1; col <= k; col++) {
        for(auto it:nodes[col]) {
            for(auto it2:graph[it]) {
                if(fi_col_root(col) != fi_col_root(state[it2])) {
                    col_neighbors[fi_col_root(col)].insert(fi_col_root(state[it2]));
                }
            }
        }
    }

    for(int col = 1; col <= k; col++) {
        cnt += (col_neighbors[col].size() == 1);
    }

    printf("%d\n",(cnt != 1) * (cnt + 1) / 2);
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:112:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&n,&k);
     ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:116:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&u,&v);
         ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:122:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&state[i]);
         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 84472 KB Output is correct
2 Correct 83 ms 84472 KB Output is correct
3 Correct 82 ms 84472 KB Output is correct
4 Correct 81 ms 84472 KB Output is correct
5 Correct 83 ms 84600 KB Output is correct
6 Correct 83 ms 84600 KB Output is correct
7 Correct 82 ms 84444 KB Output is correct
8 Correct 83 ms 84472 KB Output is correct
9 Correct 91 ms 84472 KB Output is correct
10 Correct 85 ms 84624 KB Output is correct
11 Correct 82 ms 84472 KB Output is correct
12 Correct 82 ms 84472 KB Output is correct
13 Correct 82 ms 84472 KB Output is correct
14 Correct 85 ms 84472 KB Output is correct
15 Correct 83 ms 84472 KB Output is correct
16 Correct 82 ms 84472 KB Output is correct
17 Correct 82 ms 84472 KB Output is correct
18 Correct 82 ms 84572 KB Output is correct
19 Correct 86 ms 84600 KB Output is correct
20 Correct 82 ms 84476 KB Output is correct
21 Correct 82 ms 84444 KB Output is correct
22 Correct 81 ms 84556 KB Output is correct
23 Correct 82 ms 84600 KB Output is correct
24 Correct 83 ms 84444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 84472 KB Output is correct
2 Correct 83 ms 84472 KB Output is correct
3 Correct 82 ms 84472 KB Output is correct
4 Correct 81 ms 84472 KB Output is correct
5 Correct 83 ms 84600 KB Output is correct
6 Correct 83 ms 84600 KB Output is correct
7 Correct 82 ms 84444 KB Output is correct
8 Correct 83 ms 84472 KB Output is correct
9 Correct 91 ms 84472 KB Output is correct
10 Correct 85 ms 84624 KB Output is correct
11 Correct 82 ms 84472 KB Output is correct
12 Correct 82 ms 84472 KB Output is correct
13 Correct 82 ms 84472 KB Output is correct
14 Correct 85 ms 84472 KB Output is correct
15 Correct 83 ms 84472 KB Output is correct
16 Correct 82 ms 84472 KB Output is correct
17 Correct 82 ms 84472 KB Output is correct
18 Correct 82 ms 84572 KB Output is correct
19 Correct 86 ms 84600 KB Output is correct
20 Correct 82 ms 84476 KB Output is correct
21 Correct 82 ms 84444 KB Output is correct
22 Correct 81 ms 84556 KB Output is correct
23 Correct 82 ms 84600 KB Output is correct
24 Correct 83 ms 84444 KB Output is correct
25 Correct 90 ms 84548 KB Output is correct
26 Correct 86 ms 84984 KB Output is correct
27 Correct 84 ms 84728 KB Output is correct
28 Correct 86 ms 84984 KB Output is correct
29 Correct 88 ms 85112 KB Output is correct
30 Correct 93 ms 84740 KB Output is correct
31 Correct 88 ms 84604 KB Output is correct
32 Correct 91 ms 85008 KB Output is correct
33 Correct 82 ms 84472 KB Output is correct
34 Correct 85 ms 84668 KB Output is correct
35 Correct 85 ms 85112 KB Output is correct
36 Correct 86 ms 84688 KB Output is correct
37 Correct 85 ms 84824 KB Output is correct
38 Correct 83 ms 84604 KB Output is correct
39 Correct 85 ms 84728 KB Output is correct
40 Correct 93 ms 84736 KB Output is correct
41 Correct 88 ms 84728 KB Output is correct
42 Correct 85 ms 84856 KB Output is correct
43 Correct 114 ms 84984 KB Output is correct
44 Correct 81 ms 84728 KB Output is correct
45 Correct 134 ms 84856 KB Output is correct
46 Correct 86 ms 84760 KB Output is correct
47 Correct 82 ms 84600 KB Output is correct
48 Correct 85 ms 84728 KB Output is correct
49 Correct 86 ms 85112 KB Output is correct
50 Correct 85 ms 85112 KB Output is correct
51 Correct 86 ms 84728 KB Output is correct
52 Correct 180 ms 84728 KB Output is correct
53 Correct 85 ms 84856 KB Output is correct
54 Correct 84 ms 84704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 84472 KB Output is correct
2 Correct 83 ms 84472 KB Output is correct
3 Correct 82 ms 84472 KB Output is correct
4 Correct 81 ms 84472 KB Output is correct
5 Correct 83 ms 84600 KB Output is correct
6 Correct 83 ms 84600 KB Output is correct
7 Correct 82 ms 84444 KB Output is correct
8 Correct 83 ms 84472 KB Output is correct
9 Correct 91 ms 84472 KB Output is correct
10 Correct 85 ms 84624 KB Output is correct
11 Correct 82 ms 84472 KB Output is correct
12 Correct 82 ms 84472 KB Output is correct
13 Correct 82 ms 84472 KB Output is correct
14 Correct 85 ms 84472 KB Output is correct
15 Correct 83 ms 84472 KB Output is correct
16 Correct 82 ms 84472 KB Output is correct
17 Correct 82 ms 84472 KB Output is correct
18 Correct 82 ms 84572 KB Output is correct
19 Correct 86 ms 84600 KB Output is correct
20 Correct 82 ms 84476 KB Output is correct
21 Correct 82 ms 84444 KB Output is correct
22 Correct 81 ms 84556 KB Output is correct
23 Correct 82 ms 84600 KB Output is correct
24 Correct 83 ms 84444 KB Output is correct
25 Correct 84 ms 84604 KB Output is correct
26 Correct 168 ms 91596 KB Output is correct
27 Correct 197 ms 91340 KB Output is correct
28 Correct 85 ms 84964 KB Output is correct
29 Correct 83 ms 84472 KB Output is correct
30 Correct 82 ms 84472 KB Output is correct
31 Correct 184 ms 91304 KB Output is correct
32 Correct 83 ms 84728 KB Output is correct
33 Correct 216 ms 95612 KB Output is correct
34 Correct 208 ms 91384 KB Output is correct
35 Correct 91 ms 84728 KB Output is correct
36 Correct 194 ms 91488 KB Output is correct
37 Correct 85 ms 84728 KB Output is correct
38 Correct 85 ms 84720 KB Output is correct
39 Correct 171 ms 91504 KB Output is correct
40 Correct 85 ms 84848 KB Output is correct
41 Correct 169 ms 91124 KB Output is correct
42 Correct 227 ms 92664 KB Output is correct
43 Correct 83 ms 84472 KB Output is correct
44 Correct 199 ms 95748 KB Output is correct
45 Correct 216 ms 93432 KB Output is correct
46 Correct 84 ms 84728 KB Output is correct
47 Correct 85 ms 84728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 91632 KB Output is correct
2 Correct 266 ms 103964 KB Output is correct
3 Correct 88 ms 84956 KB Output is correct
4 Correct 84 ms 84728 KB Output is correct
5 Correct 81 ms 84472 KB Output is correct
6 Correct 82 ms 84472 KB Output is correct
7 Correct 85 ms 84728 KB Output is correct
8 Correct 256 ms 93816 KB Output is correct
9 Correct 84 ms 84812 KB Output is correct
10 Correct 184 ms 91536 KB Output is correct
11 Correct 85 ms 84528 KB Output is correct
12 Correct 198 ms 91384 KB Output is correct
13 Correct 256 ms 94684 KB Output is correct
14 Correct 266 ms 103672 KB Output is correct
15 Correct 172 ms 91504 KB Output is correct
16 Correct 86 ms 84784 KB Output is correct
17 Correct 83 ms 84600 KB Output is correct
18 Correct 278 ms 102256 KB Output is correct
19 Correct 274 ms 106032 KB Output is correct
20 Correct 85 ms 84728 KB Output is correct
21 Correct 84 ms 84548 KB Output is correct
22 Correct 227 ms 95192 KB Output is correct
23 Correct 86 ms 84800 KB Output is correct
24 Correct 220 ms 92664 KB Output is correct
25 Correct 286 ms 105104 KB Output is correct
26 Correct 84 ms 85112 KB Output is correct
27 Correct 86 ms 85128 KB Output is correct
28 Correct 84 ms 84728 KB Output is correct
29 Correct 85 ms 84692 KB Output is correct
30 Correct 84 ms 84856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 84472 KB Output is correct
2 Correct 83 ms 84472 KB Output is correct
3 Correct 82 ms 84472 KB Output is correct
4 Correct 81 ms 84472 KB Output is correct
5 Correct 83 ms 84600 KB Output is correct
6 Correct 83 ms 84600 KB Output is correct
7 Correct 82 ms 84444 KB Output is correct
8 Correct 83 ms 84472 KB Output is correct
9 Correct 91 ms 84472 KB Output is correct
10 Correct 85 ms 84624 KB Output is correct
11 Correct 82 ms 84472 KB Output is correct
12 Correct 82 ms 84472 KB Output is correct
13 Correct 82 ms 84472 KB Output is correct
14 Correct 85 ms 84472 KB Output is correct
15 Correct 83 ms 84472 KB Output is correct
16 Correct 82 ms 84472 KB Output is correct
17 Correct 82 ms 84472 KB Output is correct
18 Correct 82 ms 84572 KB Output is correct
19 Correct 86 ms 84600 KB Output is correct
20 Correct 82 ms 84476 KB Output is correct
21 Correct 82 ms 84444 KB Output is correct
22 Correct 81 ms 84556 KB Output is correct
23 Correct 82 ms 84600 KB Output is correct
24 Correct 83 ms 84444 KB Output is correct
25 Correct 90 ms 84548 KB Output is correct
26 Correct 86 ms 84984 KB Output is correct
27 Correct 84 ms 84728 KB Output is correct
28 Correct 86 ms 84984 KB Output is correct
29 Correct 88 ms 85112 KB Output is correct
30 Correct 93 ms 84740 KB Output is correct
31 Correct 88 ms 84604 KB Output is correct
32 Correct 91 ms 85008 KB Output is correct
33 Correct 82 ms 84472 KB Output is correct
34 Correct 85 ms 84668 KB Output is correct
35 Correct 85 ms 85112 KB Output is correct
36 Correct 86 ms 84688 KB Output is correct
37 Correct 85 ms 84824 KB Output is correct
38 Correct 83 ms 84604 KB Output is correct
39 Correct 85 ms 84728 KB Output is correct
40 Correct 93 ms 84736 KB Output is correct
41 Correct 88 ms 84728 KB Output is correct
42 Correct 85 ms 84856 KB Output is correct
43 Correct 114 ms 84984 KB Output is correct
44 Correct 81 ms 84728 KB Output is correct
45 Correct 134 ms 84856 KB Output is correct
46 Correct 86 ms 84760 KB Output is correct
47 Correct 82 ms 84600 KB Output is correct
48 Correct 85 ms 84728 KB Output is correct
49 Correct 86 ms 85112 KB Output is correct
50 Correct 85 ms 85112 KB Output is correct
51 Correct 86 ms 84728 KB Output is correct
52 Correct 180 ms 84728 KB Output is correct
53 Correct 85 ms 84856 KB Output is correct
54 Correct 84 ms 84704 KB Output is correct
55 Correct 84 ms 84604 KB Output is correct
56 Correct 168 ms 91596 KB Output is correct
57 Correct 197 ms 91340 KB Output is correct
58 Correct 85 ms 84964 KB Output is correct
59 Correct 83 ms 84472 KB Output is correct
60 Correct 82 ms 84472 KB Output is correct
61 Correct 184 ms 91304 KB Output is correct
62 Correct 83 ms 84728 KB Output is correct
63 Correct 216 ms 95612 KB Output is correct
64 Correct 208 ms 91384 KB Output is correct
65 Correct 91 ms 84728 KB Output is correct
66 Correct 194 ms 91488 KB Output is correct
67 Correct 85 ms 84728 KB Output is correct
68 Correct 85 ms 84720 KB Output is correct
69 Correct 171 ms 91504 KB Output is correct
70 Correct 85 ms 84848 KB Output is correct
71 Correct 169 ms 91124 KB Output is correct
72 Correct 227 ms 92664 KB Output is correct
73 Correct 83 ms 84472 KB Output is correct
74 Correct 199 ms 95748 KB Output is correct
75 Correct 216 ms 93432 KB Output is correct
76 Correct 84 ms 84728 KB Output is correct
77 Correct 85 ms 84728 KB Output is correct
78 Correct 166 ms 91632 KB Output is correct
79 Correct 266 ms 103964 KB Output is correct
80 Correct 88 ms 84956 KB Output is correct
81 Correct 84 ms 84728 KB Output is correct
82 Correct 81 ms 84472 KB Output is correct
83 Correct 82 ms 84472 KB Output is correct
84 Correct 85 ms 84728 KB Output is correct
85 Correct 256 ms 93816 KB Output is correct
86 Correct 84 ms 84812 KB Output is correct
87 Correct 184 ms 91536 KB Output is correct
88 Correct 85 ms 84528 KB Output is correct
89 Correct 198 ms 91384 KB Output is correct
90 Correct 256 ms 94684 KB Output is correct
91 Correct 266 ms 103672 KB Output is correct
92 Correct 172 ms 91504 KB Output is correct
93 Correct 86 ms 84784 KB Output is correct
94 Correct 83 ms 84600 KB Output is correct
95 Correct 278 ms 102256 KB Output is correct
96 Correct 274 ms 106032 KB Output is correct
97 Correct 85 ms 84728 KB Output is correct
98 Correct 84 ms 84548 KB Output is correct
99 Correct 227 ms 95192 KB Output is correct
100 Correct 86 ms 84800 KB Output is correct
101 Correct 220 ms 92664 KB Output is correct
102 Correct 286 ms 105104 KB Output is correct
103 Correct 84 ms 85112 KB Output is correct
104 Correct 86 ms 85128 KB Output is correct
105 Correct 84 ms 84728 KB Output is correct
106 Correct 85 ms 84692 KB Output is correct
107 Correct 84 ms 84856 KB Output is correct
108 Correct 1188 ms 133780 KB Output is correct
109 Correct 903 ms 129064 KB Output is correct
110 Correct 891 ms 135516 KB Output is correct
111 Correct 1525 ms 193172 KB Output is correct
112 Correct 1636 ms 168972 KB Output is correct
113 Correct 1406 ms 174176 KB Output is correct
114 Correct 727 ms 118496 KB Output is correct
115 Correct 738 ms 118336 KB Output is correct
116 Correct 1407 ms 123384 KB Output is correct
117 Correct 1336 ms 181292 KB Output is correct
118 Correct 818 ms 119572 KB Output is correct
119 Correct 1339 ms 181244 KB Output is correct
120 Correct 1403 ms 189244 KB Output is correct
121 Correct 1408 ms 182856 KB Output is correct
122 Correct 1599 ms 137252 KB Output is correct
123 Correct 1478 ms 182688 KB Output is correct
124 Correct 1436 ms 164964 KB Output is correct