Submission #1085889

# Submission time Handle Problem Language Result Execution time Memory
1085889 2024-09-09T01:36:35 Z underwaterkillerwhale Mergers (JOI19_mergers) C++17
100 / 100
652 ms 74832 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;
    int numP = 0;
    iter (&v, ke[u]) {
        if (v != p) {
            numP += min(rem, dp[v].fs);
            rem = abs(rem - dp[v].fs);
        }
    }
    Assign (u, 1);
    bool okE = 0;
    if (u != 1 && num_ok == num_cpn) okE = 1;
    if (numP == 0) {
        if (rem == 0) dp[u] = {okE, 0};
        else {
            iter (&v, ke[u]) if (v != p && dp[v].fs != 0) {
                dp[u] = dp[v];
            }
            dp[u].se = dp[u].se | (okE & (rem >= 2));
        }
        return;
    }
    if (rem == 0 && numP > 0) {
        --numP;
        rem = 2;
    }
    res += numP;
    dp[u] = {rem, okE & (rem >= 2)};
    assert(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


*/
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12120 KB Output is correct
2 Correct 5 ms 12260 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Correct 6 ms 12060 KB Output is correct
5 Correct 5 ms 12124 KB Output is correct
6 Correct 5 ms 12124 KB Output is correct
7 Correct 5 ms 12124 KB Output is correct
8 Correct 5 ms 12120 KB Output is correct
9 Correct 5 ms 12124 KB Output is correct
10 Correct 6 ms 12376 KB Output is correct
11 Correct 5 ms 12120 KB Output is correct
12 Correct 5 ms 12124 KB Output is correct
13 Correct 6 ms 12124 KB Output is correct
14 Correct 5 ms 12124 KB Output is correct
15 Correct 6 ms 12060 KB Output is correct
16 Correct 7 ms 12124 KB Output is correct
17 Correct 7 ms 12124 KB Output is correct
18 Correct 5 ms 12124 KB Output is correct
19 Correct 5 ms 12128 KB Output is correct
20 Correct 6 ms 12124 KB Output is correct
21 Correct 5 ms 12124 KB Output is correct
22 Correct 5 ms 12124 KB Output is correct
23 Correct 5 ms 12208 KB Output is correct
24 Correct 5 ms 12120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12120 KB Output is correct
2 Correct 5 ms 12260 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Correct 6 ms 12060 KB Output is correct
5 Correct 5 ms 12124 KB Output is correct
6 Correct 5 ms 12124 KB Output is correct
7 Correct 5 ms 12124 KB Output is correct
8 Correct 5 ms 12120 KB Output is correct
9 Correct 5 ms 12124 KB Output is correct
10 Correct 6 ms 12376 KB Output is correct
11 Correct 5 ms 12120 KB Output is correct
12 Correct 5 ms 12124 KB Output is correct
13 Correct 6 ms 12124 KB Output is correct
14 Correct 5 ms 12124 KB Output is correct
15 Correct 6 ms 12060 KB Output is correct
16 Correct 7 ms 12124 KB Output is correct
17 Correct 7 ms 12124 KB Output is correct
18 Correct 5 ms 12124 KB Output is correct
19 Correct 5 ms 12128 KB Output is correct
20 Correct 6 ms 12124 KB Output is correct
21 Correct 5 ms 12124 KB Output is correct
22 Correct 5 ms 12124 KB Output is correct
23 Correct 5 ms 12208 KB Output is correct
24 Correct 5 ms 12120 KB Output is correct
25 Correct 5 ms 12124 KB Output is correct
26 Correct 7 ms 12376 KB Output is correct
27 Correct 8 ms 12380 KB Output is correct
28 Correct 5 ms 12380 KB Output is correct
29 Correct 6 ms 12320 KB Output is correct
30 Correct 6 ms 12376 KB Output is correct
31 Correct 5 ms 12168 KB Output is correct
32 Correct 7 ms 12636 KB Output is correct
33 Correct 5 ms 12124 KB Output is correct
34 Correct 6 ms 12632 KB Output is correct
35 Correct 6 ms 12376 KB Output is correct
36 Correct 6 ms 12328 KB Output is correct
37 Correct 6 ms 12380 KB Output is correct
38 Correct 5 ms 12124 KB Output is correct
39 Correct 7 ms 12316 KB Output is correct
40 Correct 7 ms 12380 KB Output is correct
41 Correct 7 ms 12380 KB Output is correct
42 Correct 6 ms 12376 KB Output is correct
43 Correct 7 ms 12636 KB Output is correct
44 Correct 5 ms 12044 KB Output is correct
45 Correct 7 ms 12376 KB Output is correct
46 Correct 7 ms 12376 KB Output is correct
47 Correct 6 ms 12124 KB Output is correct
48 Correct 7 ms 12376 KB Output is correct
49 Correct 6 ms 12380 KB Output is correct
50 Correct 7 ms 12484 KB Output is correct
51 Correct 6 ms 12376 KB Output is correct
52 Correct 6 ms 12460 KB Output is correct
53 Correct 6 ms 12380 KB Output is correct
54 Correct 7 ms 12380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12120 KB Output is correct
2 Correct 5 ms 12260 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Correct 6 ms 12060 KB Output is correct
5 Correct 5 ms 12124 KB Output is correct
6 Correct 5 ms 12124 KB Output is correct
7 Correct 5 ms 12124 KB Output is correct
8 Correct 5 ms 12120 KB Output is correct
9 Correct 5 ms 12124 KB Output is correct
10 Correct 6 ms 12376 KB Output is correct
11 Correct 5 ms 12120 KB Output is correct
12 Correct 5 ms 12124 KB Output is correct
13 Correct 6 ms 12124 KB Output is correct
14 Correct 5 ms 12124 KB Output is correct
15 Correct 6 ms 12060 KB Output is correct
16 Correct 7 ms 12124 KB Output is correct
17 Correct 7 ms 12124 KB Output is correct
18 Correct 5 ms 12124 KB Output is correct
19 Correct 5 ms 12128 KB Output is correct
20 Correct 6 ms 12124 KB Output is correct
21 Correct 5 ms 12124 KB Output is correct
22 Correct 5 ms 12124 KB Output is correct
23 Correct 5 ms 12208 KB Output is correct
24 Correct 5 ms 12120 KB Output is correct
25 Correct 6 ms 12120 KB Output is correct
26 Correct 30 ms 19152 KB Output is correct
27 Correct 63 ms 18768 KB Output is correct
28 Correct 7 ms 12376 KB Output is correct
29 Correct 5 ms 12124 KB Output is correct
30 Correct 6 ms 12124 KB Output is correct
31 Correct 54 ms 18656 KB Output is correct
32 Correct 6 ms 12376 KB Output is correct
33 Correct 48 ms 25944 KB Output is correct
34 Correct 60 ms 18768 KB Output is correct
35 Correct 6 ms 12376 KB Output is correct
36 Correct 53 ms 19416 KB Output is correct
37 Correct 7 ms 12380 KB Output is correct
38 Correct 6 ms 12380 KB Output is correct
39 Correct 47 ms 19084 KB Output is correct
40 Correct 6 ms 12636 KB Output is correct
41 Correct 53 ms 18764 KB Output is correct
42 Correct 54 ms 21328 KB Output is correct
43 Correct 5 ms 12124 KB Output is correct
44 Correct 46 ms 26044 KB Output is correct
45 Correct 55 ms 22352 KB Output is correct
46 Correct 5 ms 12376 KB Output is correct
47 Correct 6 ms 12380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 19148 KB Output is correct
2 Correct 33 ms 20172 KB Output is correct
3 Correct 9 ms 12384 KB Output is correct
4 Correct 6 ms 12380 KB Output is correct
5 Correct 6 ms 12204 KB Output is correct
6 Correct 5 ms 12124 KB Output is correct
7 Correct 5 ms 12272 KB Output is correct
8 Correct 65 ms 19336 KB Output is correct
9 Correct 7 ms 12380 KB Output is correct
10 Correct 56 ms 18772 KB Output is correct
11 Correct 7 ms 12124 KB Output is correct
12 Correct 60 ms 18688 KB Output is correct
13 Correct 70 ms 19280 KB Output is correct
14 Correct 63 ms 19700 KB Output is correct
15 Correct 32 ms 18944 KB Output is correct
16 Correct 9 ms 12380 KB Output is correct
17 Correct 5 ms 12264 KB Output is correct
18 Correct 35 ms 20068 KB Output is correct
19 Correct 50 ms 23636 KB Output is correct
20 Correct 7 ms 12380 KB Output is correct
21 Correct 5 ms 12260 KB Output is correct
22 Correct 34 ms 19624 KB Output is correct
23 Correct 8 ms 12376 KB Output is correct
24 Correct 69 ms 19028 KB Output is correct
25 Correct 49 ms 22352 KB Output is correct
26 Correct 5 ms 12380 KB Output is correct
27 Correct 6 ms 12388 KB Output is correct
28 Correct 6 ms 12380 KB Output is correct
29 Correct 6 ms 12380 KB Output is correct
30 Correct 6 ms 12376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12120 KB Output is correct
2 Correct 5 ms 12260 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Correct 6 ms 12060 KB Output is correct
5 Correct 5 ms 12124 KB Output is correct
6 Correct 5 ms 12124 KB Output is correct
7 Correct 5 ms 12124 KB Output is correct
8 Correct 5 ms 12120 KB Output is correct
9 Correct 5 ms 12124 KB Output is correct
10 Correct 6 ms 12376 KB Output is correct
11 Correct 5 ms 12120 KB Output is correct
12 Correct 5 ms 12124 KB Output is correct
13 Correct 6 ms 12124 KB Output is correct
14 Correct 5 ms 12124 KB Output is correct
15 Correct 6 ms 12060 KB Output is correct
16 Correct 7 ms 12124 KB Output is correct
17 Correct 7 ms 12124 KB Output is correct
18 Correct 5 ms 12124 KB Output is correct
19 Correct 5 ms 12128 KB Output is correct
20 Correct 6 ms 12124 KB Output is correct
21 Correct 5 ms 12124 KB Output is correct
22 Correct 5 ms 12124 KB Output is correct
23 Correct 5 ms 12208 KB Output is correct
24 Correct 5 ms 12120 KB Output is correct
25 Correct 5 ms 12124 KB Output is correct
26 Correct 7 ms 12376 KB Output is correct
27 Correct 8 ms 12380 KB Output is correct
28 Correct 5 ms 12380 KB Output is correct
29 Correct 6 ms 12320 KB Output is correct
30 Correct 6 ms 12376 KB Output is correct
31 Correct 5 ms 12168 KB Output is correct
32 Correct 7 ms 12636 KB Output is correct
33 Correct 5 ms 12124 KB Output is correct
34 Correct 6 ms 12632 KB Output is correct
35 Correct 6 ms 12376 KB Output is correct
36 Correct 6 ms 12328 KB Output is correct
37 Correct 6 ms 12380 KB Output is correct
38 Correct 5 ms 12124 KB Output is correct
39 Correct 7 ms 12316 KB Output is correct
40 Correct 7 ms 12380 KB Output is correct
41 Correct 7 ms 12380 KB Output is correct
42 Correct 6 ms 12376 KB Output is correct
43 Correct 7 ms 12636 KB Output is correct
44 Correct 5 ms 12044 KB Output is correct
45 Correct 7 ms 12376 KB Output is correct
46 Correct 7 ms 12376 KB Output is correct
47 Correct 6 ms 12124 KB Output is correct
48 Correct 7 ms 12376 KB Output is correct
49 Correct 6 ms 12380 KB Output is correct
50 Correct 7 ms 12484 KB Output is correct
51 Correct 6 ms 12376 KB Output is correct
52 Correct 6 ms 12460 KB Output is correct
53 Correct 6 ms 12380 KB Output is correct
54 Correct 7 ms 12380 KB Output is correct
55 Correct 6 ms 12120 KB Output is correct
56 Correct 30 ms 19152 KB Output is correct
57 Correct 63 ms 18768 KB Output is correct
58 Correct 7 ms 12376 KB Output is correct
59 Correct 5 ms 12124 KB Output is correct
60 Correct 6 ms 12124 KB Output is correct
61 Correct 54 ms 18656 KB Output is correct
62 Correct 6 ms 12376 KB Output is correct
63 Correct 48 ms 25944 KB Output is correct
64 Correct 60 ms 18768 KB Output is correct
65 Correct 6 ms 12376 KB Output is correct
66 Correct 53 ms 19416 KB Output is correct
67 Correct 7 ms 12380 KB Output is correct
68 Correct 6 ms 12380 KB Output is correct
69 Correct 47 ms 19084 KB Output is correct
70 Correct 6 ms 12636 KB Output is correct
71 Correct 53 ms 18764 KB Output is correct
72 Correct 54 ms 21328 KB Output is correct
73 Correct 5 ms 12124 KB Output is correct
74 Correct 46 ms 26044 KB Output is correct
75 Correct 55 ms 22352 KB Output is correct
76 Correct 5 ms 12376 KB Output is correct
77 Correct 6 ms 12380 KB Output is correct
78 Correct 31 ms 19148 KB Output is correct
79 Correct 33 ms 20172 KB Output is correct
80 Correct 9 ms 12384 KB Output is correct
81 Correct 6 ms 12380 KB Output is correct
82 Correct 6 ms 12204 KB Output is correct
83 Correct 5 ms 12124 KB Output is correct
84 Correct 5 ms 12272 KB Output is correct
85 Correct 65 ms 19336 KB Output is correct
86 Correct 7 ms 12380 KB Output is correct
87 Correct 56 ms 18772 KB Output is correct
88 Correct 7 ms 12124 KB Output is correct
89 Correct 60 ms 18688 KB Output is correct
90 Correct 70 ms 19280 KB Output is correct
91 Correct 63 ms 19700 KB Output is correct
92 Correct 32 ms 18944 KB Output is correct
93 Correct 9 ms 12380 KB Output is correct
94 Correct 5 ms 12264 KB Output is correct
95 Correct 35 ms 20068 KB Output is correct
96 Correct 50 ms 23636 KB Output is correct
97 Correct 7 ms 12380 KB Output is correct
98 Correct 5 ms 12260 KB Output is correct
99 Correct 34 ms 19624 KB Output is correct
100 Correct 8 ms 12376 KB Output is correct
101 Correct 69 ms 19028 KB Output is correct
102 Correct 49 ms 22352 KB Output is correct
103 Correct 5 ms 12380 KB Output is correct
104 Correct 6 ms 12388 KB Output is correct
105 Correct 6 ms 12380 KB Output is correct
106 Correct 6 ms 12380 KB Output is correct
107 Correct 6 ms 12376 KB Output is correct
108 Correct 229 ms 50992 KB Output is correct
109 Correct 510 ms 63776 KB Output is correct
110 Correct 464 ms 74820 KB Output is correct
111 Correct 551 ms 74832 KB Output is correct
112 Correct 435 ms 65108 KB Output is correct
113 Correct 240 ms 53136 KB Output is correct
114 Correct 497 ms 46040 KB Output is correct
115 Correct 455 ms 45904 KB Output is correct
116 Correct 464 ms 48684 KB Output is correct
117 Correct 530 ms 52036 KB Output is correct
118 Correct 426 ms 45908 KB Output is correct
119 Correct 537 ms 51796 KB Output is correct
120 Correct 422 ms 65872 KB Output is correct
121 Correct 455 ms 51792 KB Output is correct
122 Correct 505 ms 50260 KB Output is correct
123 Correct 212 ms 53440 KB Output is correct
124 Correct 652 ms 51104 KB Output is correct