답안 #341113

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
341113 2020-12-29T03:00:21 Z ijxjdjd Mergers (JOI19_mergers) C++14
100 / 100
1568 ms 147672 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;
            fill(arr.begin(), arr.end(), (int)(1e6));
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (i + j == 4) {
                        arr[2] = min(old[i] + next[j] + 1, arr[2]);
                        arr[0] = min(old[i] + next[j] + 2, arr[0]);
                    }
                    else if (i + j == 3) {
                        arr[1] = min(old[i] + next[j] + 1, arr[1]);
                    }
                    else if (i + j == 2) {
                        if (i == 2 || j ==2) {
                            arr[2] = min(arr[2], old[i] + next[j]);
                        }
                        else {
                            arr[2] = min(arr[2], old[i] + next[j]);
                            arr[0] = min(arr[0], old[i] + next[j] + 1);
                        }
                    }
                    else if (i + j == 1) {
                        arr[1] = min(arr[1], old[i] + next[j]);
                    }
                    else {
                        arr[0] = min(arr[0], old[i] + next[j]);
                    }
                }
            }
            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";
    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], ans[1]+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 8 ms 12140 KB Output is correct
2 Correct 7 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 10 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12160 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 10 ms 12140 KB Output is correct
13 Correct 8 ms 12140 KB Output is correct
14 Correct 9 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 8 ms 12140 KB Output is correct
17 Correct 8 ms 12140 KB Output is correct
18 Correct 9 ms 12140 KB Output is correct
19 Correct 9 ms 12140 KB Output is correct
20 Correct 9 ms 12140 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 9 ms 12140 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 7 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 10 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12160 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 10 ms 12140 KB Output is correct
13 Correct 8 ms 12140 KB Output is correct
14 Correct 9 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 8 ms 12140 KB Output is correct
17 Correct 8 ms 12140 KB Output is correct
18 Correct 9 ms 12140 KB Output is correct
19 Correct 9 ms 12140 KB Output is correct
20 Correct 9 ms 12140 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 9 ms 12140 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
25 Correct 9 ms 12140 KB Output is correct
26 Correct 13 ms 12652 KB Output is correct
27 Correct 12 ms 12652 KB Output is correct
28 Correct 13 ms 12780 KB Output is correct
29 Correct 13 ms 12652 KB Output is correct
30 Correct 13 ms 12524 KB Output is correct
31 Correct 9 ms 12288 KB Output is correct
32 Correct 16 ms 12908 KB Output is correct
33 Correct 9 ms 12140 KB Output is correct
34 Correct 12 ms 12524 KB Output is correct
35 Correct 12 ms 12664 KB Output is correct
36 Correct 12 ms 12524 KB Output is correct
37 Correct 15 ms 12652 KB Output is correct
38 Correct 11 ms 12140 KB Output is correct
39 Correct 13 ms 12524 KB Output is correct
40 Correct 14 ms 12524 KB Output is correct
41 Correct 13 ms 12524 KB Output is correct
42 Correct 13 ms 12652 KB Output is correct
43 Correct 13 ms 13036 KB Output is correct
44 Correct 9 ms 12140 KB Output is correct
45 Correct 13 ms 12652 KB Output is correct
46 Correct 13 ms 12652 KB Output is correct
47 Correct 9 ms 12140 KB Output is correct
48 Correct 13 ms 12524 KB Output is correct
49 Correct 12 ms 12652 KB Output is correct
50 Correct 13 ms 12652 KB Output is correct
51 Correct 12 ms 12524 KB Output is correct
52 Correct 12 ms 12780 KB Output is correct
53 Correct 13 ms 12652 KB Output is correct
54 Correct 12 ms 12524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 7 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 10 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12160 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 10 ms 12140 KB Output is correct
13 Correct 8 ms 12140 KB Output is correct
14 Correct 9 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 8 ms 12140 KB Output is correct
17 Correct 8 ms 12140 KB Output is correct
18 Correct 9 ms 12140 KB Output is correct
19 Correct 9 ms 12140 KB Output is correct
20 Correct 9 ms 12140 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 9 ms 12140 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
25 Correct 9 ms 12140 KB Output is correct
26 Correct 155 ms 27820 KB Output is correct
27 Correct 200 ms 27496 KB Output is correct
28 Correct 14 ms 12652 KB Output is correct
29 Correct 9 ms 12140 KB Output is correct
30 Correct 9 ms 12140 KB Output is correct
31 Correct 200 ms 27452 KB Output is correct
32 Correct 12 ms 12652 KB Output is correct
33 Correct 252 ms 41780 KB Output is correct
34 Correct 207 ms 27752 KB Output is correct
35 Correct 15 ms 12524 KB Output is correct
36 Correct 224 ms 28904 KB Output is correct
37 Correct 14 ms 12524 KB Output is correct
38 Correct 12 ms 12524 KB Output is correct
39 Correct 155 ms 27740 KB Output is correct
40 Correct 12 ms 13036 KB Output is correct
41 Correct 219 ms 27368 KB Output is correct
42 Correct 230 ms 32104 KB Output is correct
43 Correct 9 ms 12140 KB Output is correct
44 Correct 243 ms 42344 KB Output is correct
45 Correct 244 ms 34920 KB Output is correct
46 Correct 12 ms 12780 KB Output is correct
47 Correct 14 ms 12524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 154 ms 26468 KB Output is correct
2 Correct 166 ms 26340 KB Output is correct
3 Correct 12 ms 12524 KB Output is correct
4 Correct 12 ms 12556 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 10 ms 12140 KB Output is correct
7 Correct 15 ms 12524 KB Output is correct
8 Correct 214 ms 28004 KB Output is correct
9 Correct 13 ms 12636 KB Output is correct
10 Correct 200 ms 27620 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 223 ms 27496 KB Output is correct
13 Correct 221 ms 27876 KB Output is correct
14 Correct 209 ms 28132 KB Output is correct
15 Correct 151 ms 27756 KB Output is correct
16 Correct 12 ms 12652 KB Output is correct
17 Correct 9 ms 12140 KB Output is correct
18 Correct 176 ms 28500 KB Output is correct
19 Correct 247 ms 36072 KB Output is correct
20 Correct 13 ms 12652 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 174 ms 28132 KB Output is correct
23 Correct 12 ms 12524 KB Output is correct
24 Correct 221 ms 27620 KB Output is correct
25 Correct 232 ms 33384 KB Output is correct
26 Correct 12 ms 12652 KB Output is correct
27 Correct 15 ms 12652 KB Output is correct
28 Correct 13 ms 12524 KB Output is correct
29 Correct 12 ms 12652 KB Output is correct
30 Correct 12 ms 12652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 7 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 10 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12160 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 10 ms 12140 KB Output is correct
13 Correct 8 ms 12140 KB Output is correct
14 Correct 9 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 8 ms 12140 KB Output is correct
17 Correct 8 ms 12140 KB Output is correct
18 Correct 9 ms 12140 KB Output is correct
19 Correct 9 ms 12140 KB Output is correct
20 Correct 9 ms 12140 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 9 ms 12140 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
25 Correct 9 ms 12140 KB Output is correct
26 Correct 13 ms 12652 KB Output is correct
27 Correct 12 ms 12652 KB Output is correct
28 Correct 13 ms 12780 KB Output is correct
29 Correct 13 ms 12652 KB Output is correct
30 Correct 13 ms 12524 KB Output is correct
31 Correct 9 ms 12288 KB Output is correct
32 Correct 16 ms 12908 KB Output is correct
33 Correct 9 ms 12140 KB Output is correct
34 Correct 12 ms 12524 KB Output is correct
35 Correct 12 ms 12664 KB Output is correct
36 Correct 12 ms 12524 KB Output is correct
37 Correct 15 ms 12652 KB Output is correct
38 Correct 11 ms 12140 KB Output is correct
39 Correct 13 ms 12524 KB Output is correct
40 Correct 14 ms 12524 KB Output is correct
41 Correct 13 ms 12524 KB Output is correct
42 Correct 13 ms 12652 KB Output is correct
43 Correct 13 ms 13036 KB Output is correct
44 Correct 9 ms 12140 KB Output is correct
45 Correct 13 ms 12652 KB Output is correct
46 Correct 13 ms 12652 KB Output is correct
47 Correct 9 ms 12140 KB Output is correct
48 Correct 13 ms 12524 KB Output is correct
49 Correct 12 ms 12652 KB Output is correct
50 Correct 13 ms 12652 KB Output is correct
51 Correct 12 ms 12524 KB Output is correct
52 Correct 12 ms 12780 KB Output is correct
53 Correct 13 ms 12652 KB Output is correct
54 Correct 12 ms 12524 KB Output is correct
55 Correct 9 ms 12140 KB Output is correct
56 Correct 155 ms 27820 KB Output is correct
57 Correct 200 ms 27496 KB Output is correct
58 Correct 14 ms 12652 KB Output is correct
59 Correct 9 ms 12140 KB Output is correct
60 Correct 9 ms 12140 KB Output is correct
61 Correct 200 ms 27452 KB Output is correct
62 Correct 12 ms 12652 KB Output is correct
63 Correct 252 ms 41780 KB Output is correct
64 Correct 207 ms 27752 KB Output is correct
65 Correct 15 ms 12524 KB Output is correct
66 Correct 224 ms 28904 KB Output is correct
67 Correct 14 ms 12524 KB Output is correct
68 Correct 12 ms 12524 KB Output is correct
69 Correct 155 ms 27740 KB Output is correct
70 Correct 12 ms 13036 KB Output is correct
71 Correct 219 ms 27368 KB Output is correct
72 Correct 230 ms 32104 KB Output is correct
73 Correct 9 ms 12140 KB Output is correct
74 Correct 243 ms 42344 KB Output is correct
75 Correct 244 ms 34920 KB Output is correct
76 Correct 12 ms 12780 KB Output is correct
77 Correct 14 ms 12524 KB Output is correct
78 Correct 154 ms 26468 KB Output is correct
79 Correct 166 ms 26340 KB Output is correct
80 Correct 12 ms 12524 KB Output is correct
81 Correct 12 ms 12556 KB Output is correct
82 Correct 9 ms 12140 KB Output is correct
83 Correct 10 ms 12140 KB Output is correct
84 Correct 15 ms 12524 KB Output is correct
85 Correct 214 ms 28004 KB Output is correct
86 Correct 13 ms 12636 KB Output is correct
87 Correct 200 ms 27620 KB Output is correct
88 Correct 9 ms 12140 KB Output is correct
89 Correct 223 ms 27496 KB Output is correct
90 Correct 221 ms 27876 KB Output is correct
91 Correct 209 ms 28132 KB Output is correct
92 Correct 151 ms 27756 KB Output is correct
93 Correct 12 ms 12652 KB Output is correct
94 Correct 9 ms 12140 KB Output is correct
95 Correct 176 ms 28500 KB Output is correct
96 Correct 247 ms 36072 KB Output is correct
97 Correct 13 ms 12652 KB Output is correct
98 Correct 9 ms 12140 KB Output is correct
99 Correct 174 ms 28132 KB Output is correct
100 Correct 12 ms 12524 KB Output is correct
101 Correct 221 ms 27620 KB Output is correct
102 Correct 232 ms 33384 KB Output is correct
103 Correct 12 ms 12652 KB Output is correct
104 Correct 15 ms 12652 KB Output is correct
105 Correct 13 ms 12524 KB Output is correct
106 Correct 12 ms 12652 KB Output is correct
107 Correct 12 ms 12652 KB Output is correct
108 Correct 1057 ms 93564 KB Output is correct
109 Correct 1500 ms 125404 KB Output is correct
110 Correct 1536 ms 147672 KB Output is correct
111 Correct 1568 ms 139576 KB Output is correct
112 Correct 1560 ms 120052 KB Output is correct
113 Correct 1008 ms 94552 KB Output is correct
114 Correct 1180 ms 88800 KB Output is correct
115 Correct 1159 ms 88948 KB Output is correct
116 Correct 1296 ms 91364 KB Output is correct
117 Correct 1222 ms 93156 KB Output is correct
118 Correct 1157 ms 88324 KB Output is correct
119 Correct 1221 ms 93156 KB Output is correct
120 Correct 1539 ms 121572 KB Output is correct
121 Correct 1235 ms 93284 KB Output is correct
122 Correct 1341 ms 92384 KB Output is correct
123 Correct 944 ms 92892 KB Output is correct
124 Correct 1350 ms 91884 KB Output is correct