답안 #341108

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
341108 2020-12-29T02:43:20 Z ijxjdjd Mergers (JOI19_mergers) C++14
0 / 100
168 ms 28260 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int MAXN = (int)(5e5) + 5;
vector<int> adj[MAXN];
int up[MAXN][21];
int timer = 0;
int tin[MAXN];
int tout[MAXN];
int S[MAXN];
int last[MAXN];
int lcm[MAXN];
int cnt[MAXN];
void root(int n, int p) {
    tin[n] = timer++;
    up[n][0] = p;
    for (int k = 1; k <= 20; k++) {
        up[n][k] = up[up[n][k-1]][k-1];
    }
    for (int e : adj[n]) {
        if (e != p) {
            root(e, n);
        }
    }
    tout[n] = timer++;
}
vector<int> dfs(int n, int p) {
    int paths = 0;
    vector<int> arr(3, 0);
    arr[2] = (int)(1e6);
    arr[1] = (int)(1e6);
    for (int e : adj[n]) {
        if (e != p) {
            vector<int> next = dfs(e, n);
            vector<int> old = arr;
            arr[1] = min(old[0] + next[1], old[1] + min(next[0], next[1]+1));;
            arr[2] = min(old[2] + min(next[2]+1, next[0]), old[0] + next[2]);
            arr[0] = min(next[1] + old[1] + 1, min(old[0], old[2]+1) + min(next[2]+1, next[0]));
            cnt[n] += cnt[e];
        }
    }
    if (cnt[n] == 0) {
        if (n != p) {
            arr[1] = min(arr[0], arr[1]);
            arr[0] = (int)(1e6);
        }
    }
    return arr;
}
bool is_ancestor(int a, int b) {
    return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}
int lca(int a, int b) {
    if (is_ancestor(a, b)) {
        return a;
    }
    if (is_ancestor(b, a)) {
        return b;
    }
    int u = a;
    for (int k = 20; k >= 0; k--) {
        if (!is_ancestor(up[u][k], b)) {
            u = up[u][k];
        }
    }
    return up[u][0];
}
int main() {
    string asdf = "input.in";
//    freopen(asdf.c_str(), "r", stdin);
    int N, s;
    cin >> N >> s;
    for (int i = 0; i < N-1; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    vector<int> df;
    for (int i = 0; i < N; i++) {
        df.push_back(i);
        last[i] = -1;
        cin >> S[i];
        S[i]--;
    }
    root(0, 0);
    sort(df.begin(), df.end(), [](const int& lhs, const int& rhs) {
            return tin[lhs] < tin[rhs];
         }
     );
    for (int i : df) {
        if (last[S[i]] == -1) {
            last[S[i]] = i;
            lcm[S[i]] = i;
        }
        else {
            if (is_ancestor(lcm[S[i]], i)) {
                cnt[i]++;
                cnt[lcm[S[i]]]--;
            }
            else {
                int lc = lca(lcm[S[i]], i);
                cnt[lcm[S[i]]]++;
                cnt[lc]-=2;
                cnt[i]++;
                lcm[S[i]] = lc;
            }
        }
    }
    vector<int> ans = dfs(0, 0);
    cout << min(ans[0], min(ans[1], ans[2])+1);
    return 0;
}

Compilation message

mergers.cpp: In function 'std::vector<int> dfs(int, int)':
mergers.cpp:29:9: warning: unused variable 'paths' [-Wunused-variable]
   29 |     int paths = 0;
      |         ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 8 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 8 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Incorrect 9 ms 12140 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 8 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 8 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Incorrect 9 ms 12140 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 8 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 8 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Incorrect 9 ms 12140 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 153 ms 27880 KB Output is correct
2 Correct 168 ms 28260 KB Output is correct
3 Incorrect 13 ms 12652 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 8 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 8 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Incorrect 9 ms 12140 KB Output isn't correct
13 Halted 0 ms 0 KB -