답안 #1085890

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1085890 2024-09-09T01:42:07 Z underwaterkillerwhale Mergers (JOI19_mergers) C++17
100 / 100
548 ms 67396 KB
#include <bits/stdc++.h>
#define ll              long long
#define pii             pair<int,int>
#define pll             pair<ll,ll>
#define rep(i,m,n)      for(int i=(m); i<=(n); i++)
#define reb(i,m,n)      for(int i=(m); i>=(n); i--)
#define iter(id, v)     for(auto id : v)
#define fs              first
#define se              second
#define MP              make_pair
#define pb              push_back
#define bit(msk, i)     ((msk >> i) & 1)
#define SZ(v)           (ll)v.size()
#define ALL(v)          v.begin(),v.end()

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }

const int N = 5e5 + 7;
const int Mod = 1e9 + 2022;
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 350;

int n, K;
vector<int> ke[N];
int a[N];
int ccnt[N], num_cpn, tot_cpn[N], num_ok;
int ein[N], nC[N];
pii dp[N];

int res = 0;

void pdfs (int u, int p) {
    static int time_dfs = 0;
    ein[u] = ++time_dfs;
    nC[u] = 1;
    iter (&v, ke[u]) {
        if (v != p) {
            pdfs(v, u);
            nC[u] += nC[v];
        }
    }
}

void Assign (int u, int delta) {
    if (delta == 1) {
        if (ccnt[a[u]] == 0) ++num_cpn;
        ccnt[a[u]]++;
        if (ccnt[a[u]] == tot_cpn[a[u]]) ++num_ok;
    }
    else {
        if (ccnt[a[u]] == tot_cpn[a[u]]) --num_ok;
        ccnt[a[u]]--;
        if (ccnt[a[u]] == 0) --num_cpn;
    }
}

void update (int u, int p, int delta) {
    Assign(u, delta);
    iter (&v, ke[u]) {
        if (v != p) update (v, u, delta);
    }
}

void dfs (int u, int p) {
    int mxV = -1;
    iter (&v, ke[u]) if (v != p && (mxV == -1 || nC[v] > nC[mxV])) mxV = v;
    iter (&v, ke[u]) if (v != p && v != mxV) {
        dfs(v, u);
        update (v, u, -1);
    }
    if (mxV != -1) dfs(mxV, u);
    iter (&v, ke[u]) if (v != p && v != mxV) {
        update(v, u, 1);
    }
    int rem = 0, numP = 0, lstVer = -1;
    iter (&v, ke[u]) {
        if (v != p) {
            numP += min(rem, dp[v].fs);
            rem = abs(rem - dp[v].fs);
            if (dp[v].fs > 0) lstVer = v;
        }
    }
    Assign (u, 1);
    bool okE = 0;
    if (u != 1 && num_ok == num_cpn) okE = 1;
    if (numP == 0) {
        if (lstVer == -1) dp[u] = {okE, 0};
        else {
            dp[u] = {dp[lstVer].fs, dp[lstVer].se | (okE & (rem == 2)) };
        }
        return;
    }
    if (rem == 0 && numP > 0) {
        --numP, rem = 2;
    }
    res += numP;
    dp[u] = {rem, okE & (rem == 2)};
}

void solution() {
    cin >> n >> K;
    rep (i, 1, n - 1) {
        int u, v;
        cin >> u >> v;
        ke[u].pb(v);
        ke[v].pb(u);
    }
    rep (i, 1, n) cin >> a[i], tot_cpn[a[i]]++;
    pdfs(1, 0);
    dfs(1, 0);
    if (dp[1].fs <= 1) res += dp[1].fs;
    else res += 1 + dp[1].se;
    cout << res <<"\n";
}

#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
//    file("c");
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
no bug +5
9 6
7 6
9 1
2 4
4 5
9 2
8 6
9 3
8 1
3
1
6
4
4
2
5
5
6


*/
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13404 KB Output is correct
2 Correct 5 ms 13404 KB Output is correct
3 Correct 4 ms 13400 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 4 ms 13400 KB Output is correct
6 Correct 4 ms 13404 KB Output is correct
7 Correct 4 ms 13400 KB Output is correct
8 Correct 5 ms 13404 KB Output is correct
9 Correct 4 ms 13404 KB Output is correct
10 Correct 5 ms 13360 KB Output is correct
11 Correct 4 ms 13404 KB Output is correct
12 Correct 5 ms 13404 KB Output is correct
13 Correct 4 ms 13404 KB Output is correct
14 Correct 5 ms 13404 KB Output is correct
15 Correct 4 ms 13404 KB Output is correct
16 Correct 4 ms 13404 KB Output is correct
17 Correct 4 ms 13404 KB Output is correct
18 Correct 5 ms 13404 KB Output is correct
19 Correct 5 ms 13404 KB Output is correct
20 Correct 4 ms 13404 KB Output is correct
21 Correct 4 ms 13404 KB Output is correct
22 Correct 4 ms 13404 KB Output is correct
23 Correct 5 ms 13276 KB Output is correct
24 Correct 4 ms 13272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13404 KB Output is correct
2 Correct 5 ms 13404 KB Output is correct
3 Correct 4 ms 13400 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 4 ms 13400 KB Output is correct
6 Correct 4 ms 13404 KB Output is correct
7 Correct 4 ms 13400 KB Output is correct
8 Correct 5 ms 13404 KB Output is correct
9 Correct 4 ms 13404 KB Output is correct
10 Correct 5 ms 13360 KB Output is correct
11 Correct 4 ms 13404 KB Output is correct
12 Correct 5 ms 13404 KB Output is correct
13 Correct 4 ms 13404 KB Output is correct
14 Correct 5 ms 13404 KB Output is correct
15 Correct 4 ms 13404 KB Output is correct
16 Correct 4 ms 13404 KB Output is correct
17 Correct 4 ms 13404 KB Output is correct
18 Correct 5 ms 13404 KB Output is correct
19 Correct 5 ms 13404 KB Output is correct
20 Correct 4 ms 13404 KB Output is correct
21 Correct 4 ms 13404 KB Output is correct
22 Correct 4 ms 13404 KB Output is correct
23 Correct 5 ms 13276 KB Output is correct
24 Correct 4 ms 13272 KB Output is correct
25 Correct 5 ms 13404 KB Output is correct
26 Correct 7 ms 13656 KB Output is correct
27 Correct 5 ms 13400 KB Output is correct
28 Correct 6 ms 13656 KB Output is correct
29 Correct 7 ms 13404 KB Output is correct
30 Correct 5 ms 13404 KB Output is correct
31 Correct 5 ms 13404 KB Output is correct
32 Correct 6 ms 13660 KB Output is correct
33 Correct 4 ms 13452 KB Output is correct
34 Correct 5 ms 13404 KB Output is correct
35 Correct 5 ms 13404 KB Output is correct
36 Correct 6 ms 13404 KB Output is correct
37 Correct 6 ms 13620 KB Output is correct
38 Correct 5 ms 13656 KB Output is correct
39 Correct 6 ms 13404 KB Output is correct
40 Correct 6 ms 13404 KB Output is correct
41 Correct 6 ms 13404 KB Output is correct
42 Correct 5 ms 13404 KB Output is correct
43 Correct 5 ms 13660 KB Output is correct
44 Correct 5 ms 13404 KB Output is correct
45 Correct 6 ms 13660 KB Output is correct
46 Correct 5 ms 13544 KB Output is correct
47 Correct 5 ms 13404 KB Output is correct
48 Correct 5 ms 13404 KB Output is correct
49 Correct 5 ms 13404 KB Output is correct
50 Correct 6 ms 13500 KB Output is correct
51 Correct 6 ms 13612 KB Output is correct
52 Correct 5 ms 13400 KB Output is correct
53 Correct 5 ms 13400 KB Output is correct
54 Correct 5 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13404 KB Output is correct
2 Correct 5 ms 13404 KB Output is correct
3 Correct 4 ms 13400 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 4 ms 13400 KB Output is correct
6 Correct 4 ms 13404 KB Output is correct
7 Correct 4 ms 13400 KB Output is correct
8 Correct 5 ms 13404 KB Output is correct
9 Correct 4 ms 13404 KB Output is correct
10 Correct 5 ms 13360 KB Output is correct
11 Correct 4 ms 13404 KB Output is correct
12 Correct 5 ms 13404 KB Output is correct
13 Correct 4 ms 13404 KB Output is correct
14 Correct 5 ms 13404 KB Output is correct
15 Correct 4 ms 13404 KB Output is correct
16 Correct 4 ms 13404 KB Output is correct
17 Correct 4 ms 13404 KB Output is correct
18 Correct 5 ms 13404 KB Output is correct
19 Correct 5 ms 13404 KB Output is correct
20 Correct 4 ms 13404 KB Output is correct
21 Correct 4 ms 13404 KB Output is correct
22 Correct 4 ms 13404 KB Output is correct
23 Correct 5 ms 13276 KB Output is correct
24 Correct 4 ms 13272 KB Output is correct
25 Correct 5 ms 13404 KB Output is correct
26 Correct 31 ms 18704 KB Output is correct
27 Correct 49 ms 18524 KB Output is correct
28 Correct 6 ms 13400 KB Output is correct
29 Correct 5 ms 13212 KB Output is correct
30 Correct 4 ms 13404 KB Output is correct
31 Correct 47 ms 18628 KB Output is correct
32 Correct 5 ms 13400 KB Output is correct
33 Correct 44 ms 25628 KB Output is correct
34 Correct 54 ms 18512 KB Output is correct
35 Correct 6 ms 13400 KB Output is correct
36 Correct 64 ms 19280 KB Output is correct
37 Correct 6 ms 13404 KB Output is correct
38 Correct 5 ms 13404 KB Output is correct
39 Correct 28 ms 18760 KB Output is correct
40 Correct 6 ms 13656 KB Output is correct
41 Correct 47 ms 18552 KB Output is correct
42 Correct 45 ms 20828 KB Output is correct
43 Correct 5 ms 13404 KB Output is correct
44 Correct 41 ms 26068 KB Output is correct
45 Correct 40 ms 22108 KB Output is correct
46 Correct 5 ms 13404 KB Output is correct
47 Correct 6 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 18900 KB Output is correct
2 Correct 32 ms 19660 KB Output is correct
3 Correct 6 ms 13404 KB Output is correct
4 Correct 5 ms 13404 KB Output is correct
5 Correct 4 ms 13400 KB Output is correct
6 Correct 5 ms 13404 KB Output is correct
7 Correct 5 ms 13404 KB Output is correct
8 Correct 58 ms 19020 KB Output is correct
9 Correct 6 ms 13404 KB Output is correct
10 Correct 51 ms 18632 KB Output is correct
11 Correct 5 ms 13404 KB Output is correct
12 Correct 58 ms 18408 KB Output is correct
13 Correct 65 ms 18772 KB Output is correct
14 Correct 53 ms 19408 KB Output is correct
15 Correct 28 ms 18840 KB Output is correct
16 Correct 5 ms 13404 KB Output is correct
17 Correct 5 ms 13404 KB Output is correct
18 Correct 32 ms 19520 KB Output is correct
19 Correct 47 ms 23124 KB Output is correct
20 Correct 6 ms 13400 KB Output is correct
21 Correct 5 ms 13400 KB Output is correct
22 Correct 31 ms 19276 KB Output is correct
23 Correct 6 ms 13400 KB Output is correct
24 Correct 60 ms 18516 KB Output is correct
25 Correct 42 ms 21864 KB Output is correct
26 Correct 5 ms 13404 KB Output is correct
27 Correct 6 ms 13660 KB Output is correct
28 Correct 5 ms 13620 KB Output is correct
29 Correct 5 ms 13520 KB Output is correct
30 Correct 5 ms 13400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13404 KB Output is correct
2 Correct 5 ms 13404 KB Output is correct
3 Correct 4 ms 13400 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 4 ms 13400 KB Output is correct
6 Correct 4 ms 13404 KB Output is correct
7 Correct 4 ms 13400 KB Output is correct
8 Correct 5 ms 13404 KB Output is correct
9 Correct 4 ms 13404 KB Output is correct
10 Correct 5 ms 13360 KB Output is correct
11 Correct 4 ms 13404 KB Output is correct
12 Correct 5 ms 13404 KB Output is correct
13 Correct 4 ms 13404 KB Output is correct
14 Correct 5 ms 13404 KB Output is correct
15 Correct 4 ms 13404 KB Output is correct
16 Correct 4 ms 13404 KB Output is correct
17 Correct 4 ms 13404 KB Output is correct
18 Correct 5 ms 13404 KB Output is correct
19 Correct 5 ms 13404 KB Output is correct
20 Correct 4 ms 13404 KB Output is correct
21 Correct 4 ms 13404 KB Output is correct
22 Correct 4 ms 13404 KB Output is correct
23 Correct 5 ms 13276 KB Output is correct
24 Correct 4 ms 13272 KB Output is correct
25 Correct 5 ms 13404 KB Output is correct
26 Correct 7 ms 13656 KB Output is correct
27 Correct 5 ms 13400 KB Output is correct
28 Correct 6 ms 13656 KB Output is correct
29 Correct 7 ms 13404 KB Output is correct
30 Correct 5 ms 13404 KB Output is correct
31 Correct 5 ms 13404 KB Output is correct
32 Correct 6 ms 13660 KB Output is correct
33 Correct 4 ms 13452 KB Output is correct
34 Correct 5 ms 13404 KB Output is correct
35 Correct 5 ms 13404 KB Output is correct
36 Correct 6 ms 13404 KB Output is correct
37 Correct 6 ms 13620 KB Output is correct
38 Correct 5 ms 13656 KB Output is correct
39 Correct 6 ms 13404 KB Output is correct
40 Correct 6 ms 13404 KB Output is correct
41 Correct 6 ms 13404 KB Output is correct
42 Correct 5 ms 13404 KB Output is correct
43 Correct 5 ms 13660 KB Output is correct
44 Correct 5 ms 13404 KB Output is correct
45 Correct 6 ms 13660 KB Output is correct
46 Correct 5 ms 13544 KB Output is correct
47 Correct 5 ms 13404 KB Output is correct
48 Correct 5 ms 13404 KB Output is correct
49 Correct 5 ms 13404 KB Output is correct
50 Correct 6 ms 13500 KB Output is correct
51 Correct 6 ms 13612 KB Output is correct
52 Correct 5 ms 13400 KB Output is correct
53 Correct 5 ms 13400 KB Output is correct
54 Correct 5 ms 13404 KB Output is correct
55 Correct 5 ms 13404 KB Output is correct
56 Correct 31 ms 18704 KB Output is correct
57 Correct 49 ms 18524 KB Output is correct
58 Correct 6 ms 13400 KB Output is correct
59 Correct 5 ms 13212 KB Output is correct
60 Correct 4 ms 13404 KB Output is correct
61 Correct 47 ms 18628 KB Output is correct
62 Correct 5 ms 13400 KB Output is correct
63 Correct 44 ms 25628 KB Output is correct
64 Correct 54 ms 18512 KB Output is correct
65 Correct 6 ms 13400 KB Output is correct
66 Correct 64 ms 19280 KB Output is correct
67 Correct 6 ms 13404 KB Output is correct
68 Correct 5 ms 13404 KB Output is correct
69 Correct 28 ms 18760 KB Output is correct
70 Correct 6 ms 13656 KB Output is correct
71 Correct 47 ms 18552 KB Output is correct
72 Correct 45 ms 20828 KB Output is correct
73 Correct 5 ms 13404 KB Output is correct
74 Correct 41 ms 26068 KB Output is correct
75 Correct 40 ms 22108 KB Output is correct
76 Correct 5 ms 13404 KB Output is correct
77 Correct 6 ms 13404 KB Output is correct
78 Correct 26 ms 18900 KB Output is correct
79 Correct 32 ms 19660 KB Output is correct
80 Correct 6 ms 13404 KB Output is correct
81 Correct 5 ms 13404 KB Output is correct
82 Correct 4 ms 13400 KB Output is correct
83 Correct 5 ms 13404 KB Output is correct
84 Correct 5 ms 13404 KB Output is correct
85 Correct 58 ms 19020 KB Output is correct
86 Correct 6 ms 13404 KB Output is correct
87 Correct 51 ms 18632 KB Output is correct
88 Correct 5 ms 13404 KB Output is correct
89 Correct 58 ms 18408 KB Output is correct
90 Correct 65 ms 18772 KB Output is correct
91 Correct 53 ms 19408 KB Output is correct
92 Correct 28 ms 18840 KB Output is correct
93 Correct 5 ms 13404 KB Output is correct
94 Correct 5 ms 13404 KB Output is correct
95 Correct 32 ms 19520 KB Output is correct
96 Correct 47 ms 23124 KB Output is correct
97 Correct 6 ms 13400 KB Output is correct
98 Correct 5 ms 13400 KB Output is correct
99 Correct 31 ms 19276 KB Output is correct
100 Correct 6 ms 13400 KB Output is correct
101 Correct 60 ms 18516 KB Output is correct
102 Correct 42 ms 21864 KB Output is correct
103 Correct 5 ms 13404 KB Output is correct
104 Correct 6 ms 13660 KB Output is correct
105 Correct 5 ms 13620 KB Output is correct
106 Correct 5 ms 13520 KB Output is correct
107 Correct 5 ms 13400 KB Output is correct
108 Correct 195 ms 41152 KB Output is correct
109 Correct 453 ms 55988 KB Output is correct
110 Correct 422 ms 67396 KB Output is correct
111 Correct 479 ms 65048 KB Output is correct
112 Correct 389 ms 55292 KB Output is correct
113 Correct 195 ms 42980 KB Output is correct
114 Correct 403 ms 38224 KB Output is correct
115 Correct 377 ms 37968 KB Output is correct
116 Correct 453 ms 38736 KB Output is correct
117 Correct 444 ms 42064 KB Output is correct
118 Correct 387 ms 37696 KB Output is correct
119 Correct 454 ms 42068 KB Output is correct
120 Correct 379 ms 55888 KB Output is correct
121 Correct 454 ms 42068 KB Output is correct
122 Correct 456 ms 40372 KB Output is correct
123 Correct 187 ms 43488 KB Output is correct
124 Correct 548 ms 41232 KB Output is correct