Submission #128966

# Submission time Handle Problem Language Result Execution time Memory
128966 2019-07-11T11:31:06 Z PeppaPig Mergers (JOI19_mergers) C++14
0 / 100
145 ms 41952 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 5e5+5;

int n, k, ans;
vector<int> g[N], st[N];
vector<pair<int, int> > E;

int par[N][20], dep[N];

void init_lca(int u, int p) {
    par[u][0] = p, dep[u] = dep[p] + 1;
    for(int i = 1; i < 20; i++) par[u][i] = par[par[u][i-1]][i-1];
    for(int v : g[u]) if(v != p) init_lca(v, u);
}

int lca(int a, int b) {
    if(dep[a] < dep[b]) swap(a, b);
    for(int i = 19; ~i; i--) if(dep[par[a][i]] >= dep[b]) a = par[a][i];
    if(a == b) return a;
    for(int i = 19; ~i; i--) if(par[a][i] != par[b][i]) a = par[a][i], b = par[b][i];
    return par[a][0];
}

int dsu[N], deg[N];

int find(int x) { return dsu[x] = x == dsu[x] ? x : find(dsu[x]); }

int main() {
    iota(dsu, dsu+N, 0);
    scanf("%d %d", &n, &k);
    for(int i = 1, a, b; i < n; i++) {
        scanf("%d %d", &a, &b);
        g[a].emplace_back(b);
        g[b].emplace_back(a);
        E.emplace_back(a, b);
    }
    init_lca(1, 0);
    for(int i = 1, a; i <= n; i++) {
        scanf("%d", &a);
        st[a].emplace_back(i);
    }
    for(int i = 1; i <= k; i++) for(int j = 1; j < st[i].size(); j++) {
        int a = st[i][j-1], b = st[i][j];
        int l = lca(a, b);
        while(a != l) {
            if(find(a) == find(par[a][0])) break;
            dsu[a] = find(par[a][0]); 
            a = par[a][0];
        }
        while(b != l) {
            if(find(b) == find(par[b][0])) break;
            dsu[b] = find(par[b][0]);
            b = par[b][0];
        }
    }
    for(pair<int, int> p : E) {
        int a, b; tie(a, b) = p;
        if(find(a) != find(b)) ++deg[find(a)], ++deg[find(b)];
    }
    for(int i = 1; i <= n; i++) if(dsu[i] == i && deg[i] == 1) ++ans;
    printf("%d\n", (ans + 1) / 2);

    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:45:50: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1; i <= k; i++) for(int j = 1; j < st[i].size(); j++) {
                                                ~~^~~~~~~~~~~~~~
mergers.cpp:33: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:35:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &a);
         ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 25720 KB Output is correct
2 Correct 25 ms 25848 KB Output is correct
3 Correct 25 ms 25848 KB Output is correct
4 Correct 25 ms 25720 KB Output is correct
5 Incorrect 25 ms 25720 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 25720 KB Output is correct
2 Correct 25 ms 25848 KB Output is correct
3 Correct 25 ms 25848 KB Output is correct
4 Correct 25 ms 25720 KB Output is correct
5 Incorrect 25 ms 25720 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 25720 KB Output is correct
2 Correct 25 ms 25848 KB Output is correct
3 Correct 25 ms 25848 KB Output is correct
4 Correct 25 ms 25720 KB Output is correct
5 Incorrect 25 ms 25720 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 132 ms 39044 KB Output is correct
2 Correct 145 ms 41952 KB Output is correct
3 Correct 28 ms 26280 KB Output is correct
4 Incorrect 27 ms 26232 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 25720 KB Output is correct
2 Correct 25 ms 25848 KB Output is correct
3 Correct 25 ms 25848 KB Output is correct
4 Correct 25 ms 25720 KB Output is correct
5 Incorrect 25 ms 25720 KB Output isn't correct
6 Halted 0 ms 0 KB -