답안 #750274

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
750274 2023-05-29T09:04:25 Z puppy Mergers (JOI19_mergers) C++17
100 / 100
2298 ms 186512 KB
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
using ll = long long;
vector<int> adj[500005];
int di[500005];
int in[500005], out[500005];
vector<int> ett;
int prf[1000005];
int sparse[500005][19];
int dep[500005];
vector<int> area[500005];
int N, K;
vector<int> g[500005];

int main()
{
    cin >> N >> K;
    for (int i = 1; i < N; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    function<void(int, int)> dfs = [&](int v, int p)
    {
        dep[v] = dep[p] + 1;
        sparse[v][0] = p;
        for (int i = 1; i < 19; i++) sparse[v][i] = sparse[sparse[v][i-1]][i-1];
        in[v] = ett.size();
        ett.push_back(v);
        for (int i:adj[v]) {
            if (i != p) {
                dfs(i, v);
            }
        }
        out[v] = ett.size();
        ett.push_back(-v);
    };
    dfs(1, 0);
    function<int(int, int)> lca = [&](int u, int v)
    {
        if (dep[u] > dep[v]) swap(u, v);
        for (int i = 18; i >= 0; i--) {
            if (dep[sparse[v][i]] >= dep[u]) v = sparse[v][i];
        }
        if (u == v) return u;
        for (int i = 18; i >= 0; i--) {
            if (sparse[u][i] != sparse[v][i]) {
                u = sparse[u][i];
                v = sparse[v][i];
            }
        }
        return sparse[u][0];
    };
    function<int(int, int)> last = [&](int p, int v)
    {
        for (int i = 18; i >= 0; i--) {
            if (dep[sparse[v][i]] > dep[p]) v = sparse[v][i];
        }
        return v;
    };
    function<void(int, int)> add_anc = [&](int p, int v)
    {
        prf[in[p]]++;
        prf[in[v]+1]--;
    };
    function<void(int, int)> add_path = [&](int u, int v)
    {
        int p = lca(u, v);
        if (p != u) add_anc(last(p, u), u);
        if (p != v) add_anc(last(p, v), v);
    };
    for (int i = 1; i <= N; i++) {
        int x; cin >> x;
        area[x].push_back(i);
    }
    for (int i = 1; i <= K; i++) {
        for (int j = 0; j < (int)area[i].size(); j++) {
            add_path(area[i][j], area[i][(j+1)%area[i].size()]);
        }
    }
    for (int i = 0; i < (int)ett.size(); i++) {
        if (i) prf[i] += prf[i-1];
        if (ett[i] > 0) di[ett[i]] += prf[i];
        else di[-ett[i]] -= prf[i];
    }
    int ord = 0;
    function<void(int, int, int)> dfs2 = [&](int v, int p, int num)
    {
        for (int i:adj[v]) {
            if (i != p) {
                if (di[i]) {
                    dfs2(i, v, num);
                }
                else {
                    ord++;
                    g[num].push_back(ord);
                    g[ord].push_back(num);
                    dfs2(i, v, ord);
                }
            }
        }
    };
    dfs2(1, 0, 0);
    int ans = 0;
    for (int i = 0; i <= ord; i++) ans += (g[i].size() == 1);
    if (ans == 0) cout << 0 << '\n';
    else cout << (ans + 1) / 2 << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 35540 KB Output is correct
2 Correct 18 ms 35540 KB Output is correct
3 Correct 21 ms 35528 KB Output is correct
4 Correct 23 ms 35588 KB Output is correct
5 Correct 21 ms 35540 KB Output is correct
6 Correct 20 ms 35532 KB Output is correct
7 Correct 24 ms 35492 KB Output is correct
8 Correct 23 ms 35540 KB Output is correct
9 Correct 18 ms 35668 KB Output is correct
10 Correct 18 ms 35532 KB Output is correct
11 Correct 20 ms 35540 KB Output is correct
12 Correct 22 ms 35540 KB Output is correct
13 Correct 18 ms 35540 KB Output is correct
14 Correct 19 ms 35564 KB Output is correct
15 Correct 22 ms 35556 KB Output is correct
16 Correct 19 ms 35548 KB Output is correct
17 Correct 20 ms 35548 KB Output is correct
18 Correct 18 ms 35548 KB Output is correct
19 Correct 18 ms 35548 KB Output is correct
20 Correct 18 ms 35536 KB Output is correct
21 Correct 18 ms 35544 KB Output is correct
22 Correct 18 ms 35544 KB Output is correct
23 Correct 18 ms 35540 KB Output is correct
24 Correct 18 ms 35540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 35540 KB Output is correct
2 Correct 18 ms 35540 KB Output is correct
3 Correct 21 ms 35528 KB Output is correct
4 Correct 23 ms 35588 KB Output is correct
5 Correct 21 ms 35540 KB Output is correct
6 Correct 20 ms 35532 KB Output is correct
7 Correct 24 ms 35492 KB Output is correct
8 Correct 23 ms 35540 KB Output is correct
9 Correct 18 ms 35668 KB Output is correct
10 Correct 18 ms 35532 KB Output is correct
11 Correct 20 ms 35540 KB Output is correct
12 Correct 22 ms 35540 KB Output is correct
13 Correct 18 ms 35540 KB Output is correct
14 Correct 19 ms 35564 KB Output is correct
15 Correct 22 ms 35556 KB Output is correct
16 Correct 19 ms 35548 KB Output is correct
17 Correct 20 ms 35548 KB Output is correct
18 Correct 18 ms 35548 KB Output is correct
19 Correct 18 ms 35548 KB Output is correct
20 Correct 18 ms 35536 KB Output is correct
21 Correct 18 ms 35544 KB Output is correct
22 Correct 18 ms 35544 KB Output is correct
23 Correct 18 ms 35540 KB Output is correct
24 Correct 18 ms 35540 KB Output is correct
25 Correct 22 ms 35540 KB Output is correct
26 Correct 25 ms 36092 KB Output is correct
27 Correct 26 ms 36072 KB Output is correct
28 Correct 24 ms 36308 KB Output is correct
29 Correct 22 ms 36180 KB Output is correct
30 Correct 23 ms 35964 KB Output is correct
31 Correct 19 ms 35540 KB Output is correct
32 Correct 23 ms 36320 KB Output is correct
33 Correct 20 ms 35548 KB Output is correct
34 Correct 29 ms 36060 KB Output is correct
35 Correct 28 ms 36188 KB Output is correct
36 Correct 24 ms 35948 KB Output is correct
37 Correct 25 ms 36052 KB Output is correct
38 Correct 22 ms 35564 KB Output is correct
39 Correct 24 ms 36044 KB Output is correct
40 Correct 24 ms 36044 KB Output is correct
41 Correct 24 ms 36052 KB Output is correct
42 Correct 28 ms 36088 KB Output is correct
43 Correct 23 ms 36428 KB Output is correct
44 Correct 19 ms 35540 KB Output is correct
45 Correct 23 ms 36180 KB Output is correct
46 Correct 26 ms 36068 KB Output is correct
47 Correct 19 ms 35548 KB Output is correct
48 Correct 26 ms 36052 KB Output is correct
49 Correct 23 ms 36192 KB Output is correct
50 Correct 26 ms 36304 KB Output is correct
51 Correct 22 ms 36052 KB Output is correct
52 Correct 23 ms 36052 KB Output is correct
53 Correct 23 ms 36076 KB Output is correct
54 Correct 25 ms 35944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 35540 KB Output is correct
2 Correct 18 ms 35540 KB Output is correct
3 Correct 21 ms 35528 KB Output is correct
4 Correct 23 ms 35588 KB Output is correct
5 Correct 21 ms 35540 KB Output is correct
6 Correct 20 ms 35532 KB Output is correct
7 Correct 24 ms 35492 KB Output is correct
8 Correct 23 ms 35540 KB Output is correct
9 Correct 18 ms 35668 KB Output is correct
10 Correct 18 ms 35532 KB Output is correct
11 Correct 20 ms 35540 KB Output is correct
12 Correct 22 ms 35540 KB Output is correct
13 Correct 18 ms 35540 KB Output is correct
14 Correct 19 ms 35564 KB Output is correct
15 Correct 22 ms 35556 KB Output is correct
16 Correct 19 ms 35548 KB Output is correct
17 Correct 20 ms 35548 KB Output is correct
18 Correct 18 ms 35548 KB Output is correct
19 Correct 18 ms 35548 KB Output is correct
20 Correct 18 ms 35536 KB Output is correct
21 Correct 18 ms 35544 KB Output is correct
22 Correct 18 ms 35544 KB Output is correct
23 Correct 18 ms 35540 KB Output is correct
24 Correct 18 ms 35540 KB Output is correct
25 Correct 19 ms 35544 KB Output is correct
26 Correct 189 ms 51644 KB Output is correct
27 Correct 204 ms 51320 KB Output is correct
28 Correct 23 ms 36072 KB Output is correct
29 Correct 20 ms 35656 KB Output is correct
30 Correct 20 ms 35540 KB Output is correct
31 Correct 226 ms 51292 KB Output is correct
32 Correct 24 ms 36052 KB Output is correct
33 Correct 259 ms 64236 KB Output is correct
34 Correct 213 ms 51272 KB Output is correct
35 Correct 24 ms 36052 KB Output is correct
36 Correct 213 ms 52704 KB Output is correct
37 Correct 24 ms 35944 KB Output is correct
38 Correct 23 ms 36052 KB Output is correct
39 Correct 168 ms 51524 KB Output is correct
40 Correct 23 ms 36360 KB Output is correct
41 Correct 204 ms 51520 KB Output is correct
42 Correct 303 ms 55584 KB Output is correct
43 Correct 19 ms 35540 KB Output is correct
44 Correct 255 ms 65092 KB Output is correct
45 Correct 280 ms 58152 KB Output is correct
46 Correct 23 ms 36044 KB Output is correct
47 Correct 24 ms 35928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 183 ms 50200 KB Output is correct
2 Correct 195 ms 56276 KB Output is correct
3 Correct 24 ms 36052 KB Output is correct
4 Correct 28 ms 36016 KB Output is correct
5 Correct 20 ms 35576 KB Output is correct
6 Correct 18 ms 35544 KB Output is correct
7 Correct 25 ms 36052 KB Output is correct
8 Correct 227 ms 53064 KB Output is correct
9 Correct 28 ms 36052 KB Output is correct
10 Correct 262 ms 51388 KB Output is correct
11 Correct 20 ms 35492 KB Output is correct
12 Correct 216 ms 51400 KB Output is correct
13 Correct 253 ms 53224 KB Output is correct
14 Correct 200 ms 57392 KB Output is correct
15 Correct 183 ms 51536 KB Output is correct
16 Correct 25 ms 36052 KB Output is correct
17 Correct 21 ms 35540 KB Output is correct
18 Correct 208 ms 57004 KB Output is correct
19 Correct 287 ms 64448 KB Output is correct
20 Correct 24 ms 36080 KB Output is correct
21 Correct 19 ms 35536 KB Output is correct
22 Correct 187 ms 53460 KB Output is correct
23 Correct 24 ms 36008 KB Output is correct
24 Correct 239 ms 52232 KB Output is correct
25 Correct 259 ms 61992 KB Output is correct
26 Correct 25 ms 36192 KB Output is correct
27 Correct 28 ms 36300 KB Output is correct
28 Correct 24 ms 36040 KB Output is correct
29 Correct 26 ms 36044 KB Output is correct
30 Correct 24 ms 36084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 35540 KB Output is correct
2 Correct 18 ms 35540 KB Output is correct
3 Correct 21 ms 35528 KB Output is correct
4 Correct 23 ms 35588 KB Output is correct
5 Correct 21 ms 35540 KB Output is correct
6 Correct 20 ms 35532 KB Output is correct
7 Correct 24 ms 35492 KB Output is correct
8 Correct 23 ms 35540 KB Output is correct
9 Correct 18 ms 35668 KB Output is correct
10 Correct 18 ms 35532 KB Output is correct
11 Correct 20 ms 35540 KB Output is correct
12 Correct 22 ms 35540 KB Output is correct
13 Correct 18 ms 35540 KB Output is correct
14 Correct 19 ms 35564 KB Output is correct
15 Correct 22 ms 35556 KB Output is correct
16 Correct 19 ms 35548 KB Output is correct
17 Correct 20 ms 35548 KB Output is correct
18 Correct 18 ms 35548 KB Output is correct
19 Correct 18 ms 35548 KB Output is correct
20 Correct 18 ms 35536 KB Output is correct
21 Correct 18 ms 35544 KB Output is correct
22 Correct 18 ms 35544 KB Output is correct
23 Correct 18 ms 35540 KB Output is correct
24 Correct 18 ms 35540 KB Output is correct
25 Correct 22 ms 35540 KB Output is correct
26 Correct 25 ms 36092 KB Output is correct
27 Correct 26 ms 36072 KB Output is correct
28 Correct 24 ms 36308 KB Output is correct
29 Correct 22 ms 36180 KB Output is correct
30 Correct 23 ms 35964 KB Output is correct
31 Correct 19 ms 35540 KB Output is correct
32 Correct 23 ms 36320 KB Output is correct
33 Correct 20 ms 35548 KB Output is correct
34 Correct 29 ms 36060 KB Output is correct
35 Correct 28 ms 36188 KB Output is correct
36 Correct 24 ms 35948 KB Output is correct
37 Correct 25 ms 36052 KB Output is correct
38 Correct 22 ms 35564 KB Output is correct
39 Correct 24 ms 36044 KB Output is correct
40 Correct 24 ms 36044 KB Output is correct
41 Correct 24 ms 36052 KB Output is correct
42 Correct 28 ms 36088 KB Output is correct
43 Correct 23 ms 36428 KB Output is correct
44 Correct 19 ms 35540 KB Output is correct
45 Correct 23 ms 36180 KB Output is correct
46 Correct 26 ms 36068 KB Output is correct
47 Correct 19 ms 35548 KB Output is correct
48 Correct 26 ms 36052 KB Output is correct
49 Correct 23 ms 36192 KB Output is correct
50 Correct 26 ms 36304 KB Output is correct
51 Correct 22 ms 36052 KB Output is correct
52 Correct 23 ms 36052 KB Output is correct
53 Correct 23 ms 36076 KB Output is correct
54 Correct 25 ms 35944 KB Output is correct
55 Correct 19 ms 35544 KB Output is correct
56 Correct 189 ms 51644 KB Output is correct
57 Correct 204 ms 51320 KB Output is correct
58 Correct 23 ms 36072 KB Output is correct
59 Correct 20 ms 35656 KB Output is correct
60 Correct 20 ms 35540 KB Output is correct
61 Correct 226 ms 51292 KB Output is correct
62 Correct 24 ms 36052 KB Output is correct
63 Correct 259 ms 64236 KB Output is correct
64 Correct 213 ms 51272 KB Output is correct
65 Correct 24 ms 36052 KB Output is correct
66 Correct 213 ms 52704 KB Output is correct
67 Correct 24 ms 35944 KB Output is correct
68 Correct 23 ms 36052 KB Output is correct
69 Correct 168 ms 51524 KB Output is correct
70 Correct 23 ms 36360 KB Output is correct
71 Correct 204 ms 51520 KB Output is correct
72 Correct 303 ms 55584 KB Output is correct
73 Correct 19 ms 35540 KB Output is correct
74 Correct 255 ms 65092 KB Output is correct
75 Correct 280 ms 58152 KB Output is correct
76 Correct 23 ms 36044 KB Output is correct
77 Correct 24 ms 35928 KB Output is correct
78 Correct 183 ms 50200 KB Output is correct
79 Correct 195 ms 56276 KB Output is correct
80 Correct 24 ms 36052 KB Output is correct
81 Correct 28 ms 36016 KB Output is correct
82 Correct 20 ms 35576 KB Output is correct
83 Correct 18 ms 35544 KB Output is correct
84 Correct 25 ms 36052 KB Output is correct
85 Correct 227 ms 53064 KB Output is correct
86 Correct 28 ms 36052 KB Output is correct
87 Correct 262 ms 51388 KB Output is correct
88 Correct 20 ms 35492 KB Output is correct
89 Correct 216 ms 51400 KB Output is correct
90 Correct 253 ms 53224 KB Output is correct
91 Correct 200 ms 57392 KB Output is correct
92 Correct 183 ms 51536 KB Output is correct
93 Correct 25 ms 36052 KB Output is correct
94 Correct 21 ms 35540 KB Output is correct
95 Correct 208 ms 57004 KB Output is correct
96 Correct 287 ms 64448 KB Output is correct
97 Correct 24 ms 36080 KB Output is correct
98 Correct 19 ms 35536 KB Output is correct
99 Correct 187 ms 53460 KB Output is correct
100 Correct 24 ms 36008 KB Output is correct
101 Correct 239 ms 52232 KB Output is correct
102 Correct 259 ms 61992 KB Output is correct
103 Correct 25 ms 36192 KB Output is correct
104 Correct 28 ms 36300 KB Output is correct
105 Correct 24 ms 36040 KB Output is correct
106 Correct 26 ms 36044 KB Output is correct
107 Correct 24 ms 36084 KB Output is correct
108 Correct 1265 ms 123832 KB Output is correct
109 Correct 1946 ms 148720 KB Output is correct
110 Correct 2298 ms 169292 KB Output is correct
111 Correct 1599 ms 186512 KB Output is correct
112 Correct 1547 ms 163792 KB Output is correct
113 Correct 1101 ms 144360 KB Output is correct
114 Correct 1254 ms 115556 KB Output is correct
115 Correct 1204 ms 116172 KB Output is correct
116 Correct 1481 ms 119020 KB Output is correct
117 Correct 1328 ms 146368 KB Output is correct
118 Correct 1273 ms 114704 KB Output is correct
119 Correct 1399 ms 146444 KB Output is correct
120 Correct 1607 ms 171296 KB Output is correct
121 Correct 1318 ms 146316 KB Output is correct
122 Correct 1427 ms 126800 KB Output is correct
123 Correct 1059 ms 149284 KB Output is correct
124 Correct 1314 ms 138432 KB Output is correct