답안 #880176

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
880176 2023-11-29T01:41:45 Z browntoad Mergers (JOI19_mergers) C++14
100 / 100
1140 ms 197080 KB
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define int ll
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n+1)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())

const ll maxn = 5e5+5;
const ll inf = (1ll<<60);

int n, k;
vector<int> g[maxn], g2[maxn];
vector<int> col(maxn), par(maxn);

int fin(int a){
    if (a == par[a]) return a;
    return par[a] = fin(par[a]);
}
void merg(int a, int b){
    a = fin(a); b = fin(b);
    par[a] = b;
}

map<int, int> st[maxn]; // oo
vector<int> mx(maxn), crs(maxn), sz(maxn);
void dfs(int x, int prev){
    sz[x]++;
    pii cid = {-1, -1};
    int u;
    REP(i, SZ(g[x])){
        u = g[x][i];
        if (u == prev) continue;
        dfs(u, x);
        cid = max(cid, {sz[u], u});
        sz[x] += sz[u];
    }

    if (cid.s != -1) {
        st[x].swap(st[cid.s]);
        crs[x] = crs[cid.s];
    }
    REP(i, SZ(g[x])){
        u = g[x][i];
        if (u == prev || u == cid.s) continue;
        for (auto p:st[u]){
            if (st[x][p.f] == 0) crs[x]++;
            st[x][p.f] += p.s;
            if (st[x][p.f] == mx[p.f]) crs[x]--;
        }
        st[u].clear();
    }
    if (st[x][col[x]] == 0) crs[x]++;
    st[x][col[x]]++;
    if (st[x][col[x]] == mx[col[x]]) crs[x]--;

    if (crs[x] > 0) {
        merg(x, prev);
        //cout<<x<<' '<<prev<<endl;
    }


}



signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin>>n>>k;
    REP1(i, n) par[i] = i;
    REP(i, n-1){
        int u, v; cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
    }
    REP1(i, n){
        cin>>col[i];
        mx[col[i]]++;
    }
    dfs(1, -1);
    vector<int> deg(n+1);
    REP1(i, n){
        for (auto u:g[i]) if (fin(u) != fin(i) && u < i) deg[fin(i)]++, deg[fin(u)]++; // so it only counts once
    }
    int lf = 0;
    REP1(i, n) if (deg[i] == 1) lf++;
    cout<<(lf/2)+(lf&1)<<endl;


}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 66908 KB Output is correct
2 Correct 17 ms 66908 KB Output is correct
3 Correct 16 ms 66904 KB Output is correct
4 Correct 15 ms 66904 KB Output is correct
5 Correct 16 ms 66908 KB Output is correct
6 Correct 16 ms 66908 KB Output is correct
7 Correct 15 ms 66992 KB Output is correct
8 Correct 15 ms 66804 KB Output is correct
9 Correct 15 ms 66908 KB Output is correct
10 Correct 15 ms 66908 KB Output is correct
11 Correct 16 ms 66904 KB Output is correct
12 Correct 15 ms 66908 KB Output is correct
13 Correct 15 ms 66908 KB Output is correct
14 Correct 15 ms 66908 KB Output is correct
15 Correct 16 ms 67016 KB Output is correct
16 Correct 15 ms 66908 KB Output is correct
17 Correct 15 ms 66984 KB Output is correct
18 Correct 15 ms 66908 KB Output is correct
19 Correct 15 ms 66980 KB Output is correct
20 Correct 16 ms 66908 KB Output is correct
21 Correct 15 ms 66908 KB Output is correct
22 Correct 15 ms 66908 KB Output is correct
23 Correct 16 ms 66908 KB Output is correct
24 Correct 15 ms 66908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 66908 KB Output is correct
2 Correct 17 ms 66908 KB Output is correct
3 Correct 16 ms 66904 KB Output is correct
4 Correct 15 ms 66904 KB Output is correct
5 Correct 16 ms 66908 KB Output is correct
6 Correct 16 ms 66908 KB Output is correct
7 Correct 15 ms 66992 KB Output is correct
8 Correct 15 ms 66804 KB Output is correct
9 Correct 15 ms 66908 KB Output is correct
10 Correct 15 ms 66908 KB Output is correct
11 Correct 16 ms 66904 KB Output is correct
12 Correct 15 ms 66908 KB Output is correct
13 Correct 15 ms 66908 KB Output is correct
14 Correct 15 ms 66908 KB Output is correct
15 Correct 16 ms 67016 KB Output is correct
16 Correct 15 ms 66908 KB Output is correct
17 Correct 15 ms 66984 KB Output is correct
18 Correct 15 ms 66908 KB Output is correct
19 Correct 15 ms 66980 KB Output is correct
20 Correct 16 ms 66908 KB Output is correct
21 Correct 15 ms 66908 KB Output is correct
22 Correct 15 ms 66908 KB Output is correct
23 Correct 16 ms 66908 KB Output is correct
24 Correct 15 ms 66908 KB Output is correct
25 Correct 17 ms 67064 KB Output is correct
26 Correct 18 ms 67164 KB Output is correct
27 Correct 17 ms 67032 KB Output is correct
28 Correct 19 ms 67432 KB Output is correct
29 Correct 18 ms 67420 KB Output is correct
30 Correct 17 ms 67160 KB Output is correct
31 Correct 15 ms 66908 KB Output is correct
32 Correct 17 ms 67676 KB Output is correct
33 Correct 15 ms 66968 KB Output is correct
34 Correct 17 ms 67164 KB Output is correct
35 Correct 18 ms 67164 KB Output is correct
36 Correct 18 ms 67116 KB Output is correct
37 Correct 18 ms 67164 KB Output is correct
38 Correct 15 ms 66908 KB Output is correct
39 Correct 18 ms 67160 KB Output is correct
40 Correct 16 ms 67164 KB Output is correct
41 Correct 17 ms 67416 KB Output is correct
42 Correct 17 ms 67160 KB Output is correct
43 Correct 16 ms 67676 KB Output is correct
44 Correct 16 ms 66908 KB Output is correct
45 Correct 17 ms 67396 KB Output is correct
46 Correct 17 ms 67280 KB Output is correct
47 Correct 18 ms 66908 KB Output is correct
48 Correct 17 ms 67164 KB Output is correct
49 Correct 17 ms 67168 KB Output is correct
50 Correct 18 ms 67420 KB Output is correct
51 Correct 17 ms 67024 KB Output is correct
52 Correct 17 ms 67164 KB Output is correct
53 Correct 17 ms 67160 KB Output is correct
54 Correct 17 ms 67160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 66908 KB Output is correct
2 Correct 17 ms 66908 KB Output is correct
3 Correct 16 ms 66904 KB Output is correct
4 Correct 15 ms 66904 KB Output is correct
5 Correct 16 ms 66908 KB Output is correct
6 Correct 16 ms 66908 KB Output is correct
7 Correct 15 ms 66992 KB Output is correct
8 Correct 15 ms 66804 KB Output is correct
9 Correct 15 ms 66908 KB Output is correct
10 Correct 15 ms 66908 KB Output is correct
11 Correct 16 ms 66904 KB Output is correct
12 Correct 15 ms 66908 KB Output is correct
13 Correct 15 ms 66908 KB Output is correct
14 Correct 15 ms 66908 KB Output is correct
15 Correct 16 ms 67016 KB Output is correct
16 Correct 15 ms 66908 KB Output is correct
17 Correct 15 ms 66984 KB Output is correct
18 Correct 15 ms 66908 KB Output is correct
19 Correct 15 ms 66980 KB Output is correct
20 Correct 16 ms 66908 KB Output is correct
21 Correct 15 ms 66908 KB Output is correct
22 Correct 15 ms 66908 KB Output is correct
23 Correct 16 ms 66908 KB Output is correct
24 Correct 15 ms 66908 KB Output is correct
25 Correct 15 ms 66908 KB Output is correct
26 Correct 79 ms 78548 KB Output is correct
27 Correct 82 ms 73020 KB Output is correct
28 Correct 17 ms 67164 KB Output is correct
29 Correct 15 ms 66908 KB Output is correct
30 Correct 15 ms 66908 KB Output is correct
31 Correct 62 ms 72788 KB Output is correct
32 Correct 17 ms 67164 KB Output is correct
33 Correct 78 ms 89684 KB Output is correct
34 Correct 77 ms 73044 KB Output is correct
35 Correct 17 ms 67076 KB Output is correct
36 Correct 67 ms 74324 KB Output is correct
37 Correct 16 ms 67160 KB Output is correct
38 Correct 16 ms 67164 KB Output is correct
39 Correct 68 ms 77044 KB Output is correct
40 Correct 18 ms 67672 KB Output is correct
41 Correct 63 ms 72788 KB Output is correct
42 Correct 72 ms 78004 KB Output is correct
43 Correct 15 ms 66908 KB Output is correct
44 Correct 73 ms 90416 KB Output is correct
45 Correct 83 ms 83284 KB Output is correct
46 Correct 18 ms 67164 KB Output is correct
47 Correct 17 ms 67164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 78616 KB Output is correct
2 Correct 110 ms 79680 KB Output is correct
3 Correct 22 ms 67416 KB Output is correct
4 Correct 17 ms 67164 KB Output is correct
5 Correct 15 ms 66908 KB Output is correct
6 Correct 15 ms 66908 KB Output is correct
7 Correct 16 ms 67292 KB Output is correct
8 Correct 121 ms 76736 KB Output is correct
9 Correct 17 ms 67164 KB Output is correct
10 Correct 72 ms 73068 KB Output is correct
11 Correct 15 ms 67160 KB Output is correct
12 Correct 66 ms 72528 KB Output is correct
13 Correct 152 ms 77216 KB Output is correct
14 Correct 165 ms 81236 KB Output is correct
15 Correct 90 ms 77120 KB Output is correct
16 Correct 17 ms 67164 KB Output is correct
17 Correct 15 ms 66908 KB Output is correct
18 Correct 124 ms 80004 KB Output is correct
19 Correct 135 ms 90708 KB Output is correct
20 Correct 17 ms 67264 KB Output is correct
21 Correct 15 ms 66872 KB Output is correct
22 Correct 105 ms 79560 KB Output is correct
23 Correct 17 ms 67160 KB Output is correct
24 Correct 86 ms 73504 KB Output is correct
25 Correct 137 ms 88400 KB Output is correct
26 Correct 17 ms 67164 KB Output is correct
27 Correct 18 ms 67416 KB Output is correct
28 Correct 17 ms 67160 KB Output is correct
29 Correct 16 ms 67164 KB Output is correct
30 Correct 17 ms 67164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 66908 KB Output is correct
2 Correct 17 ms 66908 KB Output is correct
3 Correct 16 ms 66904 KB Output is correct
4 Correct 15 ms 66904 KB Output is correct
5 Correct 16 ms 66908 KB Output is correct
6 Correct 16 ms 66908 KB Output is correct
7 Correct 15 ms 66992 KB Output is correct
8 Correct 15 ms 66804 KB Output is correct
9 Correct 15 ms 66908 KB Output is correct
10 Correct 15 ms 66908 KB Output is correct
11 Correct 16 ms 66904 KB Output is correct
12 Correct 15 ms 66908 KB Output is correct
13 Correct 15 ms 66908 KB Output is correct
14 Correct 15 ms 66908 KB Output is correct
15 Correct 16 ms 67016 KB Output is correct
16 Correct 15 ms 66908 KB Output is correct
17 Correct 15 ms 66984 KB Output is correct
18 Correct 15 ms 66908 KB Output is correct
19 Correct 15 ms 66980 KB Output is correct
20 Correct 16 ms 66908 KB Output is correct
21 Correct 15 ms 66908 KB Output is correct
22 Correct 15 ms 66908 KB Output is correct
23 Correct 16 ms 66908 KB Output is correct
24 Correct 15 ms 66908 KB Output is correct
25 Correct 17 ms 67064 KB Output is correct
26 Correct 18 ms 67164 KB Output is correct
27 Correct 17 ms 67032 KB Output is correct
28 Correct 19 ms 67432 KB Output is correct
29 Correct 18 ms 67420 KB Output is correct
30 Correct 17 ms 67160 KB Output is correct
31 Correct 15 ms 66908 KB Output is correct
32 Correct 17 ms 67676 KB Output is correct
33 Correct 15 ms 66968 KB Output is correct
34 Correct 17 ms 67164 KB Output is correct
35 Correct 18 ms 67164 KB Output is correct
36 Correct 18 ms 67116 KB Output is correct
37 Correct 18 ms 67164 KB Output is correct
38 Correct 15 ms 66908 KB Output is correct
39 Correct 18 ms 67160 KB Output is correct
40 Correct 16 ms 67164 KB Output is correct
41 Correct 17 ms 67416 KB Output is correct
42 Correct 17 ms 67160 KB Output is correct
43 Correct 16 ms 67676 KB Output is correct
44 Correct 16 ms 66908 KB Output is correct
45 Correct 17 ms 67396 KB Output is correct
46 Correct 17 ms 67280 KB Output is correct
47 Correct 18 ms 66908 KB Output is correct
48 Correct 17 ms 67164 KB Output is correct
49 Correct 17 ms 67168 KB Output is correct
50 Correct 18 ms 67420 KB Output is correct
51 Correct 17 ms 67024 KB Output is correct
52 Correct 17 ms 67164 KB Output is correct
53 Correct 17 ms 67160 KB Output is correct
54 Correct 17 ms 67160 KB Output is correct
55 Correct 15 ms 66908 KB Output is correct
56 Correct 79 ms 78548 KB Output is correct
57 Correct 82 ms 73020 KB Output is correct
58 Correct 17 ms 67164 KB Output is correct
59 Correct 15 ms 66908 KB Output is correct
60 Correct 15 ms 66908 KB Output is correct
61 Correct 62 ms 72788 KB Output is correct
62 Correct 17 ms 67164 KB Output is correct
63 Correct 78 ms 89684 KB Output is correct
64 Correct 77 ms 73044 KB Output is correct
65 Correct 17 ms 67076 KB Output is correct
66 Correct 67 ms 74324 KB Output is correct
67 Correct 16 ms 67160 KB Output is correct
68 Correct 16 ms 67164 KB Output is correct
69 Correct 68 ms 77044 KB Output is correct
70 Correct 18 ms 67672 KB Output is correct
71 Correct 63 ms 72788 KB Output is correct
72 Correct 72 ms 78004 KB Output is correct
73 Correct 15 ms 66908 KB Output is correct
74 Correct 73 ms 90416 KB Output is correct
75 Correct 83 ms 83284 KB Output is correct
76 Correct 18 ms 67164 KB Output is correct
77 Correct 17 ms 67164 KB Output is correct
78 Correct 72 ms 78616 KB Output is correct
79 Correct 110 ms 79680 KB Output is correct
80 Correct 22 ms 67416 KB Output is correct
81 Correct 17 ms 67164 KB Output is correct
82 Correct 15 ms 66908 KB Output is correct
83 Correct 15 ms 66908 KB Output is correct
84 Correct 16 ms 67292 KB Output is correct
85 Correct 121 ms 76736 KB Output is correct
86 Correct 17 ms 67164 KB Output is correct
87 Correct 72 ms 73068 KB Output is correct
88 Correct 15 ms 67160 KB Output is correct
89 Correct 66 ms 72528 KB Output is correct
90 Correct 152 ms 77216 KB Output is correct
91 Correct 165 ms 81236 KB Output is correct
92 Correct 90 ms 77120 KB Output is correct
93 Correct 17 ms 67164 KB Output is correct
94 Correct 15 ms 66908 KB Output is correct
95 Correct 124 ms 80004 KB Output is correct
96 Correct 135 ms 90708 KB Output is correct
97 Correct 17 ms 67264 KB Output is correct
98 Correct 15 ms 66872 KB Output is correct
99 Correct 105 ms 79560 KB Output is correct
100 Correct 17 ms 67160 KB Output is correct
101 Correct 86 ms 73504 KB Output is correct
102 Correct 137 ms 88400 KB Output is correct
103 Correct 17 ms 67164 KB Output is correct
104 Correct 18 ms 67416 KB Output is correct
105 Correct 17 ms 67160 KB Output is correct
106 Correct 16 ms 67164 KB Output is correct
107 Correct 17 ms 67164 KB Output is correct
108 Correct 728 ms 131600 KB Output is correct
109 Correct 549 ms 138824 KB Output is correct
110 Correct 552 ms 165460 KB Output is correct
111 Correct 1029 ms 197080 KB Output is correct
112 Correct 1072 ms 176028 KB Output is correct
113 Correct 787 ms 131168 KB Output is correct
114 Correct 480 ms 97384 KB Output is correct
115 Correct 444 ms 97064 KB Output is correct
116 Correct 794 ms 107232 KB Output is correct
117 Correct 1140 ms 138824 KB Output is correct
118 Correct 462 ms 97572 KB Output is correct
119 Correct 1102 ms 133856 KB Output is correct
120 Correct 1014 ms 176720 KB Output is correct
121 Correct 1125 ms 132444 KB Output is correct
122 Correct 1077 ms 124888 KB Output is correct
123 Correct 782 ms 131784 KB Output is correct
124 Correct 1038 ms 130196 KB Output is correct