답안 #1050856

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1050856 2024-08-09T15:40:13 Z Jarif_Rahman Mergers (JOI19_mergers) C++17
100 / 100
470 ms 74944 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

int n, k;
vector<vector<int>> tree;
vector<int> sz;
vector<int> state, scnt;
vector<int> cnt;
int bsc = 0;
vector<bool> sep;
int sep_cnt = 0;

void pre_dfs(int nd, int ss){
    for(int x: tree[nd]) if(x != ss) pre_dfs(x, nd), sz[nd]+=sz[x];
}

void add(int nd, int ss){
    cnt[state[nd]]++;
    if(cnt[state[nd]] == 1) bsc++;
    if(cnt[state[nd]] == scnt[state[nd]]) bsc--;
    for(int x: tree[nd]) if(x != ss) add(x, nd);
}
void remove(int nd, int ss){
    cnt[state[nd]]--;
    if(cnt[state[nd]] == scnt[state[nd]]-1) bsc++;
    if(cnt[state[nd]] == 0) bsc--;
    for(int x: tree[nd]) if(x != ss) remove(x, nd);
}
void dfs(int nd, int ss, bool keep){
    int bigchild = -1, bigchild_size = -1;
    for(int x: tree[nd]) if(x != ss && sz[x] > bigchild_size) bigchild_size = sz[x], bigchild = x;
    for(int x: tree[nd]) if(x != ss && x != bigchild) dfs(x, nd, 0);
    if(bigchild != -1) dfs(bigchild, nd, 1);

    cnt[state[nd]]++;
    if(cnt[state[nd]] == 1) bsc++;
    if(cnt[state[nd]] == scnt[state[nd]]) bsc--;
    for(int x: tree[nd]) if(x != ss && x != bigchild) add(x, nd);
    if(ss != -1 && bsc == 0) sep[nd] = 1;
    if(sep[nd]) sep_cnt++;

    if(keep) return;
    cnt[state[nd]]--;
    if(cnt[state[nd]] == scnt[state[nd]]-1) bsc++;
    if(cnt[state[nd]] == 0) bsc--;
    for(int x: tree[nd]) if(x != ss) remove(x, nd);
}

int ans = 0;
bool exta = 0;
int dfs2(int nd, int ss, int sep_cnt_ = 0){
    int c = 0;
    for(int x: tree[nd]) if(x != ss) c+=dfs2(x, nd, sep_cnt_+sep[nd]);
    if(ss == -1) return 0;
    if(!sep[nd]) return c;
    c++;
    if(c == 1) ans++;
    else if(c+sep_cnt_ == sep_cnt) exta = 1;
    return c;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> k;
    tree.resize(n);
    sz.resize(n, 1);
    state.resize(n);
    scnt.resize(k, 0);
    cnt.resize(k, 0);
    sep.resize(n, 0);

    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--, b--;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }
    for(int i = 0; i < n; i++){
        cin >> state[i]; state[i]--;
        scnt[state[i]]++;
    }

    pre_dfs(0, -1);
    dfs(0, -1, 1);
    dfs2(0, -1);
    ans+=exta;
    ans++;
    ans/=2;
    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 860 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 604 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 1 ms 604 KB Output is correct
50 Correct 1 ms 604 KB Output is correct
51 Correct 1 ms 604 KB Output is correct
52 Correct 1 ms 604 KB Output is correct
53 Correct 1 ms 676 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 23 ms 7084 KB Output is correct
27 Correct 44 ms 6744 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 42 ms 6772 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 37 ms 15376 KB Output is correct
34 Correct 42 ms 8280 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 49 ms 9088 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 23 ms 8316 KB Output is correct
40 Correct 1 ms 860 KB Output is correct
41 Correct 41 ms 8028 KB Output is correct
42 Correct 44 ms 10832 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 40 ms 16976 KB Output is correct
45 Correct 39 ms 12628 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 7124 KB Output is correct
2 Correct 23 ms 7892 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 664 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 51 ms 8796 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 42 ms 8364 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 48 ms 8280 KB Output is correct
13 Correct 53 ms 8880 KB Output is correct
14 Correct 51 ms 9172 KB Output is correct
15 Correct 21 ms 8532 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 24 ms 9380 KB Output is correct
19 Correct 43 ms 14072 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 25 ms 9096 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 66 ms 8284 KB Output is correct
25 Correct 39 ms 12368 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 720 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 860 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 604 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 1 ms 604 KB Output is correct
50 Correct 1 ms 604 KB Output is correct
51 Correct 1 ms 604 KB Output is correct
52 Correct 1 ms 604 KB Output is correct
53 Correct 1 ms 676 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 23 ms 7084 KB Output is correct
57 Correct 44 ms 6744 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 42 ms 6772 KB Output is correct
62 Correct 1 ms 604 KB Output is correct
63 Correct 37 ms 15376 KB Output is correct
64 Correct 42 ms 8280 KB Output is correct
65 Correct 1 ms 604 KB Output is correct
66 Correct 49 ms 9088 KB Output is correct
67 Correct 1 ms 604 KB Output is correct
68 Correct 1 ms 604 KB Output is correct
69 Correct 23 ms 8316 KB Output is correct
70 Correct 1 ms 860 KB Output is correct
71 Correct 41 ms 8028 KB Output is correct
72 Correct 44 ms 10832 KB Output is correct
73 Correct 1 ms 344 KB Output is correct
74 Correct 40 ms 16976 KB Output is correct
75 Correct 39 ms 12628 KB Output is correct
76 Correct 1 ms 604 KB Output is correct
77 Correct 2 ms 604 KB Output is correct
78 Correct 24 ms 7124 KB Output is correct
79 Correct 23 ms 7892 KB Output is correct
80 Correct 1 ms 604 KB Output is correct
81 Correct 1 ms 664 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 1 ms 604 KB Output is correct
85 Correct 51 ms 8796 KB Output is correct
86 Correct 1 ms 600 KB Output is correct
87 Correct 42 ms 8364 KB Output is correct
88 Correct 0 ms 348 KB Output is correct
89 Correct 48 ms 8280 KB Output is correct
90 Correct 53 ms 8880 KB Output is correct
91 Correct 51 ms 9172 KB Output is correct
92 Correct 21 ms 8532 KB Output is correct
93 Correct 1 ms 600 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 24 ms 9380 KB Output is correct
96 Correct 43 ms 14072 KB Output is correct
97 Correct 1 ms 604 KB Output is correct
98 Correct 0 ms 348 KB Output is correct
99 Correct 25 ms 9096 KB Output is correct
100 Correct 1 ms 604 KB Output is correct
101 Correct 66 ms 8284 KB Output is correct
102 Correct 39 ms 12368 KB Output is correct
103 Correct 1 ms 604 KB Output is correct
104 Correct 1 ms 720 KB Output is correct
105 Correct 1 ms 604 KB Output is correct
106 Correct 1 ms 604 KB Output is correct
107 Correct 2 ms 604 KB Output is correct
108 Correct 149 ms 44996 KB Output is correct
109 Correct 391 ms 61520 KB Output is correct
110 Correct 392 ms 74944 KB Output is correct
111 Correct 455 ms 73796 KB Output is correct
112 Correct 315 ms 62032 KB Output is correct
113 Correct 165 ms 47160 KB Output is correct
114 Correct 313 ms 39764 KB Output is correct
115 Correct 324 ms 39968 KB Output is correct
116 Correct 338 ms 42580 KB Output is correct
117 Correct 424 ms 46224 KB Output is correct
118 Correct 311 ms 39848 KB Output is correct
119 Correct 354 ms 46164 KB Output is correct
120 Correct 319 ms 63072 KB Output is correct
121 Correct 408 ms 46164 KB Output is correct
122 Correct 354 ms 44432 KB Output is correct
123 Correct 142 ms 47548 KB Output is correct
124 Correct 470 ms 45140 KB Output is correct