Submission #567850

# Submission time Handle Problem Language Result Execution time Memory
567850 2022-05-24T09:22:59 Z stevancv Mergers (JOI19_mergers) C++14
0 / 100
86 ms 32252 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 5e5 + 2;
const int M = 5e5 + 2;
int mod = 1000000007;
vector<int> g[N];
int lift[N][19], in[N], out[N], tsz;
void Dfs(int s, int e) {
    in[s] = ++tsz;
    lift[s][0] = e;
    for (int i = 0; i < 19; i++) lift[s][i] = lift[lift[s][i - 1]][i - 1];
    for (int u : g[s]) {
        if (u != e) {
            Dfs(u, s);
        }
    }
    out[s] = tsz;
}
bool Ancestor(int a, int b) {
    return in[a] <= in[b] && out[b] <= out[a];
}
int Lca(int a, int b) {
    if (Ancestor(a, b)) return a;
    if (Ancestor(b, a)) return b;
    for (int i = 18; i >= 0; i--) {
        if (lift[a][i] != -1 && !Ancestor(lift[a][i], b)) a = lift[a][i];
    }
    return lift[a][0];
}
int sum[N], sz[N], cnt[N];
void Dfs1(int s, int e) {
    sz[s] = 1;
    for (int u : g[s]) {
        if (u != e) {
            Dfs1(u, s);
            sz[s] += sz[u];
            sum[s] += sum[u];
        }
    }
}
void Dfs2(int s, int e, int p) {
    if (sum[s] == sz[s]) {
        if (p != -1) {
            cnt[p]++;
            cnt[s]++;
        }
        p = s;
    }
    for (int u : g[s]) {
        if (u != e) {
            Dfs2(u, s, p);
        }
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g[u - 1].push_back(v - 1);
        g[v - 1].push_back(u - 1);
    }
    Dfs(0, -1);
    vector<vector<int>> pos(k);
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        x -= 1;
        pos[x].push_back(i);
    }
    for (int i = 0; i < k; i++) {
        int tr = pos[i][0];
        for (int j = 0; j < pos[i].size(); j++) {
            tr = Lca(tr, pos[i][j]);
        }
        sum[tr] += pos[i].size();
    }
    Dfs1(0, -1);
    Dfs2(0, -1, -1);
    int ans = 0;
    for (int i = 0; i < n; i++) if (cnt[i] == 1) ans++;
    cout << (ans + 1) / 2 << en;
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:82:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         for (int j = 0; j < pos[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Incorrect 7 ms 12068 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Incorrect 7 ms 12068 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Incorrect 7 ms 12068 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 79 ms 26168 KB Output is correct
2 Correct 86 ms 32252 KB Output is correct
3 Incorrect 10 ms 12628 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Incorrect 7 ms 12068 KB Output isn't correct
3 Halted 0 ms 0 KB -