답안 #545532

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
545532 2022-04-04T19:25:26 Z LucaDantas Mergers (JOI19_mergers) C++17
100 / 100
1063 ms 156236 KB
#include <bits/stdc++.h>
using namespace std;

constexpr int maxn = 5e5+10, logn = 21;

struct DSU {
    int pai[maxn], peso[maxn], sz;
    DSU(int n = 0, int _sz = 0) {
        sz = _sz;
        for(int i = 1; i <= n; i++)
            pai[i] = i, peso[i] = 1;
    }
    int find(int x) { return pai[x] == x ? x : pai[x] = find(pai[x]); }
    void join(int a, int b) {
        a = find(a), b = find(b);
        if(a == b) return;
        --sz;
        if(peso[a] < peso[b])
            swap(a, b);
        pai[b] = a;
        peso[a] += peso[b];
    }
} dsu;

vector<int> g[maxn];

int p[maxn][logn], depth[maxn];

void pre(int u) {
    for(int v : g[u]) if(v != p[u][0]) {
        p[v][0] = u;
        depth[v] = depth[u]+1;
        pre(v);
    }
    // printf("%d %d <- %d\n", u, depth[u], p[u][0]);
}

void build() {
    for(int l = 1; l < logn; l++)
        for(int u = 0; u < maxn; u++)
            p[u][l] = p[p[u][l-1]][l-1];
}

int LCA(int a, int b) {
    if(depth[a] < depth[b])
        swap(a, b); // a é o que sobe
    for(int l = logn-1; l >= 0; l--)
        if(depth[a] - (1 << l) >= depth[b])
            a = p[a][l];
    if(a == b) return a;
    for(int l = logn-1; l >= 0; l--)
        if(p[a][l] != p[b][l])
            a = p[a][l], b = p[b][l];
    return p[a][0];
}

int lca_cor[maxn], lca[maxn], cor[maxn];

void dfs(int u) {
    lca[u] = lca_cor[cor[u]];
    for(int v : g[u]) if(v != p[u][0]) {
        dfs(v);
        if(lca[v] == v) continue;
        dsu.join(cor[u], cor[v]);
        lca[u] = LCA(lca[u], lca[v]);
    }
}

vector<int> g_cor[maxn];

int main() {
    int n, k; scanf("%d %d", &n, &k);
    for(int i = 1, u, v; i < n; i++)
        scanf("%d %d", &u, &v), g[u].push_back(v), g[v].push_back(u);
    pre(1);
    build();

    for(int i = 1; i <= n; i++)
        scanf("%d", &cor[i]);

    for(int i = 1; i <= n; i++) {
        if(!lca_cor[cor[i]])
            lca_cor[cor[i]] = i;
        else
            lca_cor[cor[i]] = LCA(lca_cor[cor[i]], i);
    }

    dsu = DSU(n, k);
    dfs(1);

    int leafs = 0;
    for(int u = 1; u <= n; u++)
        for(int v : g[u])
            if(dsu.find(cor[v]) != dsu.find(cor[u]))
                g_cor[dsu.find(cor[u])].push_back(dsu.find(cor[v]));

    for(int i = 1; i <= n; i++) {
        sort(g_cor[i].begin(), g_cor[i].end());
        g_cor[i].erase(unique(g_cor[i].begin(), g_cor[i].end()), g_cor[i].end());
        // if(g_cor[i].size()) {
        //     printf("%d ==", i);
        //     for(int x : g_cor[i])
        //         printf(" %d", x);
        //     puts("");
        // }
    }

    for(int i = 1; i <= n; i++)
        leafs += g_cor[i].size() == 1;
    printf("%d\n", (leafs+1)>>1);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:72:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |     int n, k; scanf("%d %d", &n, &k);
      |               ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:74:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         scanf("%d %d", &u, &v), g[u].push_back(v), g[v].push_back(u);
      |         ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:79:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         scanf("%d", &cor[i]);
      |         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 72648 KB Output is correct
2 Correct 101 ms 72640 KB Output is correct
3 Correct 99 ms 72652 KB Output is correct
4 Correct 100 ms 72652 KB Output is correct
5 Correct 108 ms 72640 KB Output is correct
6 Correct 100 ms 72700 KB Output is correct
7 Correct 106 ms 72636 KB Output is correct
8 Correct 103 ms 72684 KB Output is correct
9 Correct 103 ms 72640 KB Output is correct
10 Correct 101 ms 72652 KB Output is correct
11 Correct 103 ms 72716 KB Output is correct
12 Correct 106 ms 72636 KB Output is correct
13 Correct 102 ms 72652 KB Output is correct
14 Correct 112 ms 72652 KB Output is correct
15 Correct 103 ms 72652 KB Output is correct
16 Correct 103 ms 72640 KB Output is correct
17 Correct 101 ms 72644 KB Output is correct
18 Correct 101 ms 72648 KB Output is correct
19 Correct 107 ms 72760 KB Output is correct
20 Correct 102 ms 72648 KB Output is correct
21 Correct 102 ms 72608 KB Output is correct
22 Correct 105 ms 72652 KB Output is correct
23 Correct 105 ms 72648 KB Output is correct
24 Correct 108 ms 72648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 72648 KB Output is correct
2 Correct 101 ms 72640 KB Output is correct
3 Correct 99 ms 72652 KB Output is correct
4 Correct 100 ms 72652 KB Output is correct
5 Correct 108 ms 72640 KB Output is correct
6 Correct 100 ms 72700 KB Output is correct
7 Correct 106 ms 72636 KB Output is correct
8 Correct 103 ms 72684 KB Output is correct
9 Correct 103 ms 72640 KB Output is correct
10 Correct 101 ms 72652 KB Output is correct
11 Correct 103 ms 72716 KB Output is correct
12 Correct 106 ms 72636 KB Output is correct
13 Correct 102 ms 72652 KB Output is correct
14 Correct 112 ms 72652 KB Output is correct
15 Correct 103 ms 72652 KB Output is correct
16 Correct 103 ms 72640 KB Output is correct
17 Correct 101 ms 72644 KB Output is correct
18 Correct 101 ms 72648 KB Output is correct
19 Correct 107 ms 72760 KB Output is correct
20 Correct 102 ms 72648 KB Output is correct
21 Correct 102 ms 72608 KB Output is correct
22 Correct 105 ms 72652 KB Output is correct
23 Correct 105 ms 72648 KB Output is correct
24 Correct 108 ms 72648 KB Output is correct
25 Correct 114 ms 72668 KB Output is correct
26 Correct 108 ms 72904 KB Output is correct
27 Correct 106 ms 72900 KB Output is correct
28 Correct 112 ms 73128 KB Output is correct
29 Correct 107 ms 72976 KB Output is correct
30 Correct 109 ms 72784 KB Output is correct
31 Correct 103 ms 72644 KB Output is correct
32 Correct 105 ms 73164 KB Output is correct
33 Correct 108 ms 72720 KB Output is correct
34 Correct 109 ms 72792 KB Output is correct
35 Correct 104 ms 72984 KB Output is correct
36 Correct 105 ms 72828 KB Output is correct
37 Correct 105 ms 72908 KB Output is correct
38 Correct 107 ms 72652 KB Output is correct
39 Correct 110 ms 72948 KB Output is correct
40 Correct 108 ms 72788 KB Output is correct
41 Correct 110 ms 72780 KB Output is correct
42 Correct 116 ms 72928 KB Output is correct
43 Correct 109 ms 73292 KB Output is correct
44 Correct 108 ms 72648 KB Output is correct
45 Correct 112 ms 72904 KB Output is correct
46 Correct 105 ms 72888 KB Output is correct
47 Correct 102 ms 72648 KB Output is correct
48 Correct 108 ms 72816 KB Output is correct
49 Correct 104 ms 72928 KB Output is correct
50 Correct 104 ms 73088 KB Output is correct
51 Correct 107 ms 72912 KB Output is correct
52 Correct 114 ms 72796 KB Output is correct
53 Correct 107 ms 72908 KB Output is correct
54 Correct 103 ms 72868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 72648 KB Output is correct
2 Correct 101 ms 72640 KB Output is correct
3 Correct 99 ms 72652 KB Output is correct
4 Correct 100 ms 72652 KB Output is correct
5 Correct 108 ms 72640 KB Output is correct
6 Correct 100 ms 72700 KB Output is correct
7 Correct 106 ms 72636 KB Output is correct
8 Correct 103 ms 72684 KB Output is correct
9 Correct 103 ms 72640 KB Output is correct
10 Correct 101 ms 72652 KB Output is correct
11 Correct 103 ms 72716 KB Output is correct
12 Correct 106 ms 72636 KB Output is correct
13 Correct 102 ms 72652 KB Output is correct
14 Correct 112 ms 72652 KB Output is correct
15 Correct 103 ms 72652 KB Output is correct
16 Correct 103 ms 72640 KB Output is correct
17 Correct 101 ms 72644 KB Output is correct
18 Correct 101 ms 72648 KB Output is correct
19 Correct 107 ms 72760 KB Output is correct
20 Correct 102 ms 72648 KB Output is correct
21 Correct 102 ms 72608 KB Output is correct
22 Correct 105 ms 72652 KB Output is correct
23 Correct 105 ms 72648 KB Output is correct
24 Correct 108 ms 72648 KB Output is correct
25 Correct 101 ms 72648 KB Output is correct
26 Correct 162 ms 78840 KB Output is correct
27 Correct 195 ms 78524 KB Output is correct
28 Correct 107 ms 72908 KB Output is correct
29 Correct 103 ms 72640 KB Output is correct
30 Correct 101 ms 72660 KB Output is correct
31 Correct 187 ms 78536 KB Output is correct
32 Correct 106 ms 72784 KB Output is correct
33 Correct 238 ms 91456 KB Output is correct
34 Correct 184 ms 78540 KB Output is correct
35 Correct 109 ms 72784 KB Output is correct
36 Correct 205 ms 79896 KB Output is correct
37 Correct 107 ms 72768 KB Output is correct
38 Correct 107 ms 72872 KB Output is correct
39 Correct 172 ms 78784 KB Output is correct
40 Correct 105 ms 73212 KB Output is correct
41 Correct 182 ms 78368 KB Output is correct
42 Correct 239 ms 83020 KB Output is correct
43 Correct 104 ms 72652 KB Output is correct
44 Correct 215 ms 92000 KB Output is correct
45 Correct 210 ms 85260 KB Output is correct
46 Correct 111 ms 72908 KB Output is correct
47 Correct 103 ms 72780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 78820 KB Output is correct
2 Correct 171 ms 83072 KB Output is correct
3 Correct 114 ms 72964 KB Output is correct
4 Correct 107 ms 72920 KB Output is correct
5 Correct 109 ms 72640 KB Output is correct
6 Correct 110 ms 72644 KB Output is correct
7 Correct 108 ms 72848 KB Output is correct
8 Correct 200 ms 79436 KB Output is correct
9 Correct 105 ms 72856 KB Output is correct
10 Correct 179 ms 78660 KB Output is correct
11 Correct 109 ms 72648 KB Output is correct
12 Correct 202 ms 78616 KB Output is correct
13 Correct 205 ms 79568 KB Output is correct
14 Correct 187 ms 82436 KB Output is correct
15 Correct 174 ms 78756 KB Output is correct
16 Correct 108 ms 72924 KB Output is correct
17 Correct 110 ms 72648 KB Output is correct
18 Correct 171 ms 82356 KB Output is correct
19 Correct 229 ms 89652 KB Output is correct
20 Correct 104 ms 72912 KB Output is correct
21 Correct 110 ms 72652 KB Output is correct
22 Correct 164 ms 79792 KB Output is correct
23 Correct 104 ms 72916 KB Output is correct
24 Correct 186 ms 78924 KB Output is correct
25 Correct 198 ms 87012 KB Output is correct
26 Correct 110 ms 72928 KB Output is correct
27 Correct 104 ms 73100 KB Output is correct
28 Correct 108 ms 72912 KB Output is correct
29 Correct 103 ms 72912 KB Output is correct
30 Correct 102 ms 72920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 72648 KB Output is correct
2 Correct 101 ms 72640 KB Output is correct
3 Correct 99 ms 72652 KB Output is correct
4 Correct 100 ms 72652 KB Output is correct
5 Correct 108 ms 72640 KB Output is correct
6 Correct 100 ms 72700 KB Output is correct
7 Correct 106 ms 72636 KB Output is correct
8 Correct 103 ms 72684 KB Output is correct
9 Correct 103 ms 72640 KB Output is correct
10 Correct 101 ms 72652 KB Output is correct
11 Correct 103 ms 72716 KB Output is correct
12 Correct 106 ms 72636 KB Output is correct
13 Correct 102 ms 72652 KB Output is correct
14 Correct 112 ms 72652 KB Output is correct
15 Correct 103 ms 72652 KB Output is correct
16 Correct 103 ms 72640 KB Output is correct
17 Correct 101 ms 72644 KB Output is correct
18 Correct 101 ms 72648 KB Output is correct
19 Correct 107 ms 72760 KB Output is correct
20 Correct 102 ms 72648 KB Output is correct
21 Correct 102 ms 72608 KB Output is correct
22 Correct 105 ms 72652 KB Output is correct
23 Correct 105 ms 72648 KB Output is correct
24 Correct 108 ms 72648 KB Output is correct
25 Correct 114 ms 72668 KB Output is correct
26 Correct 108 ms 72904 KB Output is correct
27 Correct 106 ms 72900 KB Output is correct
28 Correct 112 ms 73128 KB Output is correct
29 Correct 107 ms 72976 KB Output is correct
30 Correct 109 ms 72784 KB Output is correct
31 Correct 103 ms 72644 KB Output is correct
32 Correct 105 ms 73164 KB Output is correct
33 Correct 108 ms 72720 KB Output is correct
34 Correct 109 ms 72792 KB Output is correct
35 Correct 104 ms 72984 KB Output is correct
36 Correct 105 ms 72828 KB Output is correct
37 Correct 105 ms 72908 KB Output is correct
38 Correct 107 ms 72652 KB Output is correct
39 Correct 110 ms 72948 KB Output is correct
40 Correct 108 ms 72788 KB Output is correct
41 Correct 110 ms 72780 KB Output is correct
42 Correct 116 ms 72928 KB Output is correct
43 Correct 109 ms 73292 KB Output is correct
44 Correct 108 ms 72648 KB Output is correct
45 Correct 112 ms 72904 KB Output is correct
46 Correct 105 ms 72888 KB Output is correct
47 Correct 102 ms 72648 KB Output is correct
48 Correct 108 ms 72816 KB Output is correct
49 Correct 104 ms 72928 KB Output is correct
50 Correct 104 ms 73088 KB Output is correct
51 Correct 107 ms 72912 KB Output is correct
52 Correct 114 ms 72796 KB Output is correct
53 Correct 107 ms 72908 KB Output is correct
54 Correct 103 ms 72868 KB Output is correct
55 Correct 101 ms 72648 KB Output is correct
56 Correct 162 ms 78840 KB Output is correct
57 Correct 195 ms 78524 KB Output is correct
58 Correct 107 ms 72908 KB Output is correct
59 Correct 103 ms 72640 KB Output is correct
60 Correct 101 ms 72660 KB Output is correct
61 Correct 187 ms 78536 KB Output is correct
62 Correct 106 ms 72784 KB Output is correct
63 Correct 238 ms 91456 KB Output is correct
64 Correct 184 ms 78540 KB Output is correct
65 Correct 109 ms 72784 KB Output is correct
66 Correct 205 ms 79896 KB Output is correct
67 Correct 107 ms 72768 KB Output is correct
68 Correct 107 ms 72872 KB Output is correct
69 Correct 172 ms 78784 KB Output is correct
70 Correct 105 ms 73212 KB Output is correct
71 Correct 182 ms 78368 KB Output is correct
72 Correct 239 ms 83020 KB Output is correct
73 Correct 104 ms 72652 KB Output is correct
74 Correct 215 ms 92000 KB Output is correct
75 Correct 210 ms 85260 KB Output is correct
76 Correct 111 ms 72908 KB Output is correct
77 Correct 103 ms 72780 KB Output is correct
78 Correct 159 ms 78820 KB Output is correct
79 Correct 171 ms 83072 KB Output is correct
80 Correct 114 ms 72964 KB Output is correct
81 Correct 107 ms 72920 KB Output is correct
82 Correct 109 ms 72640 KB Output is correct
83 Correct 110 ms 72644 KB Output is correct
84 Correct 108 ms 72848 KB Output is correct
85 Correct 200 ms 79436 KB Output is correct
86 Correct 105 ms 72856 KB Output is correct
87 Correct 179 ms 78660 KB Output is correct
88 Correct 109 ms 72648 KB Output is correct
89 Correct 202 ms 78616 KB Output is correct
90 Correct 205 ms 79568 KB Output is correct
91 Correct 187 ms 82436 KB Output is correct
92 Correct 174 ms 78756 KB Output is correct
93 Correct 108 ms 72924 KB Output is correct
94 Correct 110 ms 72648 KB Output is correct
95 Correct 171 ms 82356 KB Output is correct
96 Correct 229 ms 89652 KB Output is correct
97 Correct 104 ms 72912 KB Output is correct
98 Correct 110 ms 72652 KB Output is correct
99 Correct 164 ms 79792 KB Output is correct
100 Correct 104 ms 72916 KB Output is correct
101 Correct 186 ms 78924 KB Output is correct
102 Correct 198 ms 87012 KB Output is correct
103 Correct 110 ms 72928 KB Output is correct
104 Correct 104 ms 73100 KB Output is correct
105 Correct 108 ms 72912 KB Output is correct
106 Correct 103 ms 72912 KB Output is correct
107 Correct 102 ms 72920 KB Output is correct
108 Correct 572 ms 101356 KB Output is correct
109 Correct 1024 ms 130428 KB Output is correct
110 Correct 1063 ms 150676 KB Output is correct
111 Correct 1024 ms 156236 KB Output is correct
112 Correct 1035 ms 133432 KB Output is correct
113 Correct 641 ms 115216 KB Output is correct
114 Correct 715 ms 97864 KB Output is correct
115 Correct 694 ms 97996 KB Output is correct
116 Correct 815 ms 98372 KB Output is correct
117 Correct 820 ms 115996 KB Output is correct
118 Correct 695 ms 97492 KB Output is correct
119 Correct 820 ms 115996 KB Output is correct
120 Correct 922 ms 140748 KB Output is correct
121 Correct 796 ms 115644 KB Output is correct
122 Correct 846 ms 101728 KB Output is correct
123 Correct 595 ms 118540 KB Output is correct
124 Correct 928 ms 109480 KB Output is correct