답안 #711802

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
711802 2023-03-17T14:25:06 Z lukameladze Mergers (JOI19_mergers) C++14
70 / 100
174 ms 59408 KB
# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
//#define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 3e5 + 5;
int t,n,a[N],sz[N],tin,in[N],out[N],p[N],par[N][20],k,x[N],lv[N],deg[N];
vector <int> v[N];
vector <pii> vec[N];
void dfs(int a, int p) {
    tin++;
    in[a] = tin;
    par[a][0] = p;
    for (int i = 1; i <= 18; i++) {
        if (par[a][i - 1]) par[a][i] = par[par[a][i - 1]][i - 1];
    }
    lv[a] = lv[p] + 1;
    for (int to : v[a]) {
        if (to == p) continue;
        dfs(to, a);
    }
    out[a] = tin;
}
void init() {
    for (int i = 1; i <= n; i++) {
        p[i] = i; sz[i] = 1;
    }
}
int get_col(int a) {
    if (a == p[a]) return a;
    else return p[a] = get_col(p[a]); 
}
void col(int a, int b) {
    a = get_col(a); b = get_col(b);
    if (a == b) return ;
    if (lv[a] > lv[b]) swap(a, b);
    p[b] = a;
}
void merge(int a, int lc) {
    while (get_col(a) != get_col(lc)) {
        col(a, par[get_col(a)][0]);
    }
}
int upper(int a, int b) {
    return (in[a] <= in[b] && out[a] >= out[b]);
}
int lca(int a, int b) {
    if (upper(a, b)) return a;
    for (int i = 18; i >= 0; i--) {
        if (par[a][i] && !upper(par[a][i], b)) a = par[a][i];
    }
    return par[a][0];
}
main() {
    std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    vector <pii> edges;
    for (int i = 1; i <= n - 1; i++) {
        int a, b;
        cin>>a>>b;
        v[a].pb(b);
        v[b].pb(a);
        edges.pb({a,b});
    }
    dfs(1, 0);
    for (int i = 1; i <= n; i++) {
        cin>>x[i];
    }
    init();
    for (int i = 1; i <= n; i++) {
        vec[x[i]].pb({in[i], i});
    }
    for (int i = 1; i <= k; i++) {
        for (int j = 0; j < vec[i].size(); j++) {
            int a = vec[i][j].s, b = vec[i][(j + 1) % vec[i].size()].s, lc = lca(a, b);
            merge(a, lc); merge(b, lc);
        }
    }
    for (pii sth : edges) {
        if (get_col(sth.f) != get_col(sth.s)) {
            deg[get_col(sth.f)]++; deg[get_col(sth.s)]++;
        }
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        cnt += (int)(deg[i] == 1);
    }
    cout<<(cnt + 1) / 2<<"\n";
}

Compilation message

mergers.cpp:56:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   56 | main() {
      | ^~~~
mergers.cpp: In function 'int main()':
mergers.cpp:76:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |         for (int j = 0; j < vec[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14416 KB Output is correct
6 Correct 8 ms 14472 KB Output is correct
7 Correct 8 ms 14440 KB Output is correct
8 Correct 9 ms 14416 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 9 ms 14420 KB Output is correct
11 Correct 10 ms 14420 KB Output is correct
12 Correct 9 ms 14416 KB Output is correct
13 Correct 8 ms 14400 KB Output is correct
14 Correct 8 ms 14400 KB Output is correct
15 Correct 9 ms 14420 KB Output is correct
16 Correct 10 ms 14420 KB Output is correct
17 Correct 9 ms 14416 KB Output is correct
18 Correct 8 ms 14424 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
20 Correct 8 ms 14364 KB Output is correct
21 Correct 8 ms 14400 KB Output is correct
22 Correct 7 ms 14416 KB Output is correct
23 Correct 8 ms 14440 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14416 KB Output is correct
6 Correct 8 ms 14472 KB Output is correct
7 Correct 8 ms 14440 KB Output is correct
8 Correct 9 ms 14416 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 9 ms 14420 KB Output is correct
11 Correct 10 ms 14420 KB Output is correct
12 Correct 9 ms 14416 KB Output is correct
13 Correct 8 ms 14400 KB Output is correct
14 Correct 8 ms 14400 KB Output is correct
15 Correct 9 ms 14420 KB Output is correct
16 Correct 10 ms 14420 KB Output is correct
17 Correct 9 ms 14416 KB Output is correct
18 Correct 8 ms 14424 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
20 Correct 8 ms 14364 KB Output is correct
21 Correct 8 ms 14400 KB Output is correct
22 Correct 7 ms 14416 KB Output is correct
23 Correct 8 ms 14440 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 9 ms 14420 KB Output is correct
26 Correct 9 ms 14932 KB Output is correct
27 Correct 11 ms 15036 KB Output is correct
28 Correct 10 ms 15060 KB Output is correct
29 Correct 16 ms 14944 KB Output is correct
30 Correct 14 ms 14948 KB Output is correct
31 Correct 9 ms 14420 KB Output is correct
32 Correct 9 ms 15060 KB Output is correct
33 Correct 7 ms 14384 KB Output is correct
34 Correct 13 ms 14944 KB Output is correct
35 Correct 10 ms 14948 KB Output is correct
36 Correct 10 ms 14968 KB Output is correct
37 Correct 10 ms 14944 KB Output is correct
38 Correct 8 ms 14420 KB Output is correct
39 Correct 13 ms 14932 KB Output is correct
40 Correct 10 ms 14984 KB Output is correct
41 Correct 11 ms 14900 KB Output is correct
42 Correct 10 ms 14912 KB Output is correct
43 Correct 10 ms 15076 KB Output is correct
44 Correct 8 ms 14480 KB Output is correct
45 Correct 10 ms 14944 KB Output is correct
46 Correct 10 ms 14932 KB Output is correct
47 Correct 8 ms 14464 KB Output is correct
48 Correct 10 ms 14996 KB Output is correct
49 Correct 9 ms 15024 KB Output is correct
50 Correct 9 ms 14944 KB Output is correct
51 Correct 10 ms 14904 KB Output is correct
52 Correct 10 ms 14932 KB Output is correct
53 Correct 9 ms 14928 KB Output is correct
54 Correct 11 ms 14932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14416 KB Output is correct
6 Correct 8 ms 14472 KB Output is correct
7 Correct 8 ms 14440 KB Output is correct
8 Correct 9 ms 14416 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 9 ms 14420 KB Output is correct
11 Correct 10 ms 14420 KB Output is correct
12 Correct 9 ms 14416 KB Output is correct
13 Correct 8 ms 14400 KB Output is correct
14 Correct 8 ms 14400 KB Output is correct
15 Correct 9 ms 14420 KB Output is correct
16 Correct 10 ms 14420 KB Output is correct
17 Correct 9 ms 14416 KB Output is correct
18 Correct 8 ms 14424 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
20 Correct 8 ms 14364 KB Output is correct
21 Correct 8 ms 14400 KB Output is correct
22 Correct 7 ms 14416 KB Output is correct
23 Correct 8 ms 14440 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 7 ms 14420 KB Output is correct
26 Correct 74 ms 31348 KB Output is correct
27 Correct 100 ms 31048 KB Output is correct
28 Correct 10 ms 14944 KB Output is correct
29 Correct 8 ms 14420 KB Output is correct
30 Correct 10 ms 14420 KB Output is correct
31 Correct 110 ms 31136 KB Output is correct
32 Correct 12 ms 14948 KB Output is correct
33 Correct 173 ms 35336 KB Output is correct
34 Correct 107 ms 31168 KB Output is correct
35 Correct 11 ms 14896 KB Output is correct
36 Correct 122 ms 31528 KB Output is correct
37 Correct 11 ms 14896 KB Output is correct
38 Correct 10 ms 14932 KB Output is correct
39 Correct 83 ms 31344 KB Output is correct
40 Correct 10 ms 15076 KB Output is correct
41 Correct 85 ms 31660 KB Output is correct
42 Correct 133 ms 32496 KB Output is correct
43 Correct 8 ms 14420 KB Output is correct
44 Correct 174 ms 35988 KB Output is correct
45 Correct 158 ms 33276 KB Output is correct
46 Correct 9 ms 14932 KB Output is correct
47 Correct 9 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 29912 KB Output is correct
2 Correct 69 ms 32352 KB Output is correct
3 Correct 10 ms 14932 KB Output is correct
4 Correct 12 ms 14932 KB Output is correct
5 Correct 9 ms 14468 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 10 ms 14932 KB Output is correct
8 Correct 108 ms 32548 KB Output is correct
9 Correct 9 ms 14976 KB Output is correct
10 Correct 91 ms 31680 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 101 ms 31636 KB Output is correct
13 Correct 115 ms 32500 KB Output is correct
14 Correct 95 ms 33796 KB Output is correct
15 Correct 72 ms 31368 KB Output is correct
16 Correct 9 ms 14940 KB Output is correct
17 Correct 8 ms 14472 KB Output is correct
18 Correct 77 ms 33852 KB Output is correct
19 Correct 100 ms 36156 KB Output is correct
20 Correct 10 ms 15060 KB Output is correct
21 Correct 8 ms 14480 KB Output is correct
22 Correct 77 ms 32696 KB Output is correct
23 Correct 12 ms 14932 KB Output is correct
24 Correct 93 ms 32112 KB Output is correct
25 Correct 92 ms 35372 KB Output is correct
26 Correct 10 ms 15036 KB Output is correct
27 Correct 11 ms 15060 KB Output is correct
28 Correct 10 ms 14932 KB Output is correct
29 Correct 9 ms 14996 KB Output is correct
30 Correct 10 ms 14944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14416 KB Output is correct
6 Correct 8 ms 14472 KB Output is correct
7 Correct 8 ms 14440 KB Output is correct
8 Correct 9 ms 14416 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 9 ms 14420 KB Output is correct
11 Correct 10 ms 14420 KB Output is correct
12 Correct 9 ms 14416 KB Output is correct
13 Correct 8 ms 14400 KB Output is correct
14 Correct 8 ms 14400 KB Output is correct
15 Correct 9 ms 14420 KB Output is correct
16 Correct 10 ms 14420 KB Output is correct
17 Correct 9 ms 14416 KB Output is correct
18 Correct 8 ms 14424 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
20 Correct 8 ms 14364 KB Output is correct
21 Correct 8 ms 14400 KB Output is correct
22 Correct 7 ms 14416 KB Output is correct
23 Correct 8 ms 14440 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 9 ms 14420 KB Output is correct
26 Correct 9 ms 14932 KB Output is correct
27 Correct 11 ms 15036 KB Output is correct
28 Correct 10 ms 15060 KB Output is correct
29 Correct 16 ms 14944 KB Output is correct
30 Correct 14 ms 14948 KB Output is correct
31 Correct 9 ms 14420 KB Output is correct
32 Correct 9 ms 15060 KB Output is correct
33 Correct 7 ms 14384 KB Output is correct
34 Correct 13 ms 14944 KB Output is correct
35 Correct 10 ms 14948 KB Output is correct
36 Correct 10 ms 14968 KB Output is correct
37 Correct 10 ms 14944 KB Output is correct
38 Correct 8 ms 14420 KB Output is correct
39 Correct 13 ms 14932 KB Output is correct
40 Correct 10 ms 14984 KB Output is correct
41 Correct 11 ms 14900 KB Output is correct
42 Correct 10 ms 14912 KB Output is correct
43 Correct 10 ms 15076 KB Output is correct
44 Correct 8 ms 14480 KB Output is correct
45 Correct 10 ms 14944 KB Output is correct
46 Correct 10 ms 14932 KB Output is correct
47 Correct 8 ms 14464 KB Output is correct
48 Correct 10 ms 14996 KB Output is correct
49 Correct 9 ms 15024 KB Output is correct
50 Correct 9 ms 14944 KB Output is correct
51 Correct 10 ms 14904 KB Output is correct
52 Correct 10 ms 14932 KB Output is correct
53 Correct 9 ms 14928 KB Output is correct
54 Correct 11 ms 14932 KB Output is correct
55 Correct 7 ms 14420 KB Output is correct
56 Correct 74 ms 31348 KB Output is correct
57 Correct 100 ms 31048 KB Output is correct
58 Correct 10 ms 14944 KB Output is correct
59 Correct 8 ms 14420 KB Output is correct
60 Correct 10 ms 14420 KB Output is correct
61 Correct 110 ms 31136 KB Output is correct
62 Correct 12 ms 14948 KB Output is correct
63 Correct 173 ms 35336 KB Output is correct
64 Correct 107 ms 31168 KB Output is correct
65 Correct 11 ms 14896 KB Output is correct
66 Correct 122 ms 31528 KB Output is correct
67 Correct 11 ms 14896 KB Output is correct
68 Correct 10 ms 14932 KB Output is correct
69 Correct 83 ms 31344 KB Output is correct
70 Correct 10 ms 15076 KB Output is correct
71 Correct 85 ms 31660 KB Output is correct
72 Correct 133 ms 32496 KB Output is correct
73 Correct 8 ms 14420 KB Output is correct
74 Correct 174 ms 35988 KB Output is correct
75 Correct 158 ms 33276 KB Output is correct
76 Correct 9 ms 14932 KB Output is correct
77 Correct 9 ms 14940 KB Output is correct
78 Correct 70 ms 29912 KB Output is correct
79 Correct 69 ms 32352 KB Output is correct
80 Correct 10 ms 14932 KB Output is correct
81 Correct 12 ms 14932 KB Output is correct
82 Correct 9 ms 14468 KB Output is correct
83 Correct 8 ms 14420 KB Output is correct
84 Correct 10 ms 14932 KB Output is correct
85 Correct 108 ms 32548 KB Output is correct
86 Correct 9 ms 14976 KB Output is correct
87 Correct 91 ms 31680 KB Output is correct
88 Correct 8 ms 14420 KB Output is correct
89 Correct 101 ms 31636 KB Output is correct
90 Correct 115 ms 32500 KB Output is correct
91 Correct 95 ms 33796 KB Output is correct
92 Correct 72 ms 31368 KB Output is correct
93 Correct 9 ms 14940 KB Output is correct
94 Correct 8 ms 14472 KB Output is correct
95 Correct 77 ms 33852 KB Output is correct
96 Correct 100 ms 36156 KB Output is correct
97 Correct 10 ms 15060 KB Output is correct
98 Correct 8 ms 14480 KB Output is correct
99 Correct 77 ms 32696 KB Output is correct
100 Correct 12 ms 14932 KB Output is correct
101 Correct 93 ms 32112 KB Output is correct
102 Correct 92 ms 35372 KB Output is correct
103 Correct 10 ms 15036 KB Output is correct
104 Correct 11 ms 15060 KB Output is correct
105 Correct 10 ms 14932 KB Output is correct
106 Correct 9 ms 14996 KB Output is correct
107 Correct 10 ms 14944 KB Output is correct
108 Runtime error 109 ms 59408 KB Execution killed with signal 11
109 Halted 0 ms 0 KB -