Submission #916647

# Submission time Handle Problem Language Result Execution time Memory
916647 2024-01-26T08:30:20 Z a_l_i_r_e_z_a Mergers (JOI19_mergers) C++14
100 / 100
1010 ms 88972 KB
// In the name oF the God 
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
using namespace std;

const int inf = 1e9+7;
const int maxn = 5e5+5;

int n, k, how_many_col = 0, c[maxn], w[maxn], cnt[maxn], cnt_c[maxn];
bool kamel[maxn];
vector <pii> adj[maxn];
vector <int>  past;

void setW(int v, int p) {
    for (auto u : adj[v]) {
        if (u.F != p) {
            setW(u.F, v); w[v] += w[u.F];
        }
    }
    w[v]++;
}

void reset() {
    how_many_col = 0;
    for (int cc : past) cnt_c[cc] = 0;
    past.clear();
}

void DFS_(int v, int p) {
    for (auto u : adj[v]) {
        if (u.F != p ) {
            DFS_(u.F, v);
        }
    }
    if (cnt_c[c[v]] == 0) how_many_col++;
    cnt_c[c[v]]++;
    past.pb(c[v]);
    if (cnt[c[v]] == cnt_c[c[v]]) how_many_col--;
}

void DFS(int v, int p, int e) {
    int mx = 0, mxv = -1, mxe = -1;
    for (auto u : adj[v]) {
        if (u.F != p and w[u.F] > mx) {
            mx = w[u.F]; mxv = u.F; mxe = u.S;
        }
    }
    for (auto u : adj[v]) {
        if (u.F != p and u.F != mxv) {
            reset();
            DFS(u.F, v, u.S);
        }
    }
    if (mxv != -1) {
        reset();
        DFS(mxv, v, mxe);
        for (auto u : adj[v]) {
            if (u.F != p and u.F != mxv) {
                DFS_(u.F, v);
            }
        }
    }
    if (cnt_c[c[v]] == 0) how_many_col++;
    cnt_c[c[v]]++;
    past.pb(c[v]);
    if (cnt[c[v]] == cnt_c[c[v]]) how_many_col--;
    if (!how_many_col and e != -1) kamel[e] = 1;
}

bool seen[maxn];
pii ed[maxn];
int r = -1;
int amir = 0;

void DFS__(int v, int p){
    for(auto hh: adj[v]){
        int u = hh.F, e = hh.S;
        if(u == p) continue;
        DFS__(u, v);
        if(kamel[e]){
            seen[v] = 1;
            if(!seen[u]) amir++;
            seen[u] = 1;
        }
        seen[v] |= seen[u];
    }
    if(v == r){
        int x = 0;
        for(auto hh: adj[v]) x += seen[hh.F];
        if(x <= 1){
            amir++;
            // cout << "HI\n";
        }
    }
}

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> k;
    for (int i = 0; i < n-1; i++) {
        int v, u; cin >> v >> u;
        adj[--v].pb(mp(--u, i));
        adj[u].pb(mp(v, i));
        ed[i] = mp(u, v);
    }
    for (int i = 0; i < n; i++) {
        cin >> c[i]; cnt[c[i]]++;
    }
    setW(0, -1);
    DFS(0, -1, -1);
    for(int i = 0; i < n - 1; i++){
        if(kamel[i]) r = ed[i].F;
    }
    if(r == -1){
        cout << 0 << '\n';
        exit(0);
    }
    DFS__(r, -1);
    cout << (amir + 1) / 2 << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20828 KB Output is correct
2 Correct 6 ms 18924 KB Output is correct
3 Correct 5 ms 18780 KB Output is correct
4 Correct 5 ms 20828 KB Output is correct
5 Correct 5 ms 18780 KB Output is correct
6 Correct 5 ms 18780 KB Output is correct
7 Correct 5 ms 18924 KB Output is correct
8 Correct 5 ms 18780 KB Output is correct
9 Correct 4 ms 18924 KB Output is correct
10 Correct 4 ms 18780 KB Output is correct
11 Correct 5 ms 18780 KB Output is correct
12 Correct 5 ms 18780 KB Output is correct
13 Correct 5 ms 20828 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 5 ms 18780 KB Output is correct
16 Correct 5 ms 20828 KB Output is correct
17 Correct 5 ms 20828 KB Output is correct
18 Correct 5 ms 18780 KB Output is correct
19 Correct 5 ms 20828 KB Output is correct
20 Correct 5 ms 20828 KB Output is correct
21 Correct 4 ms 18776 KB Output is correct
22 Correct 5 ms 18780 KB Output is correct
23 Correct 5 ms 18780 KB Output is correct
24 Correct 5 ms 18780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20828 KB Output is correct
2 Correct 6 ms 18924 KB Output is correct
3 Correct 5 ms 18780 KB Output is correct
4 Correct 5 ms 20828 KB Output is correct
5 Correct 5 ms 18780 KB Output is correct
6 Correct 5 ms 18780 KB Output is correct
7 Correct 5 ms 18924 KB Output is correct
8 Correct 5 ms 18780 KB Output is correct
9 Correct 4 ms 18924 KB Output is correct
10 Correct 4 ms 18780 KB Output is correct
11 Correct 5 ms 18780 KB Output is correct
12 Correct 5 ms 18780 KB Output is correct
13 Correct 5 ms 20828 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 5 ms 18780 KB Output is correct
16 Correct 5 ms 20828 KB Output is correct
17 Correct 5 ms 20828 KB Output is correct
18 Correct 5 ms 18780 KB Output is correct
19 Correct 5 ms 20828 KB Output is correct
20 Correct 5 ms 20828 KB Output is correct
21 Correct 4 ms 18776 KB Output is correct
22 Correct 5 ms 18780 KB Output is correct
23 Correct 5 ms 18780 KB Output is correct
24 Correct 5 ms 18780 KB Output is correct
25 Correct 5 ms 20996 KB Output is correct
26 Correct 6 ms 19056 KB Output is correct
27 Correct 7 ms 19036 KB Output is correct
28 Correct 7 ms 21340 KB Output is correct
29 Correct 6 ms 21168 KB Output is correct
30 Correct 6 ms 19036 KB Output is correct
31 Correct 5 ms 20828 KB Output is correct
32 Correct 6 ms 21444 KB Output is correct
33 Correct 5 ms 20828 KB Output is correct
34 Correct 5 ms 19036 KB Output is correct
35 Correct 7 ms 21336 KB Output is correct
36 Correct 6 ms 19152 KB Output is correct
37 Correct 7 ms 18980 KB Output is correct
38 Correct 5 ms 18780 KB Output is correct
39 Correct 6 ms 19036 KB Output is correct
40 Correct 6 ms 19036 KB Output is correct
41 Correct 7 ms 19036 KB Output is correct
42 Correct 6 ms 21084 KB Output is correct
43 Correct 7 ms 19292 KB Output is correct
44 Correct 6 ms 20828 KB Output is correct
45 Correct 6 ms 21084 KB Output is correct
46 Correct 7 ms 19052 KB Output is correct
47 Correct 5 ms 18780 KB Output is correct
48 Correct 7 ms 19156 KB Output is correct
49 Correct 6 ms 21084 KB Output is correct
50 Correct 6 ms 21336 KB Output is correct
51 Correct 6 ms 19036 KB Output is correct
52 Correct 5 ms 19036 KB Output is correct
53 Correct 6 ms 19136 KB Output is correct
54 Correct 6 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20828 KB Output is correct
2 Correct 6 ms 18924 KB Output is correct
3 Correct 5 ms 18780 KB Output is correct
4 Correct 5 ms 20828 KB Output is correct
5 Correct 5 ms 18780 KB Output is correct
6 Correct 5 ms 18780 KB Output is correct
7 Correct 5 ms 18924 KB Output is correct
8 Correct 5 ms 18780 KB Output is correct
9 Correct 4 ms 18924 KB Output is correct
10 Correct 4 ms 18780 KB Output is correct
11 Correct 5 ms 18780 KB Output is correct
12 Correct 5 ms 18780 KB Output is correct
13 Correct 5 ms 20828 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 5 ms 18780 KB Output is correct
16 Correct 5 ms 20828 KB Output is correct
17 Correct 5 ms 20828 KB Output is correct
18 Correct 5 ms 18780 KB Output is correct
19 Correct 5 ms 20828 KB Output is correct
20 Correct 5 ms 20828 KB Output is correct
21 Correct 4 ms 18776 KB Output is correct
22 Correct 5 ms 18780 KB Output is correct
23 Correct 5 ms 18780 KB Output is correct
24 Correct 5 ms 18780 KB Output is correct
25 Correct 5 ms 21080 KB Output is correct
26 Correct 37 ms 27436 KB Output is correct
27 Correct 50 ms 26880 KB Output is correct
28 Correct 5 ms 19036 KB Output is correct
29 Correct 5 ms 20976 KB Output is correct
30 Correct 6 ms 21236 KB Output is correct
31 Correct 52 ms 27084 KB Output is correct
32 Correct 5 ms 19036 KB Output is correct
33 Correct 64 ms 35124 KB Output is correct
34 Correct 57 ms 26900 KB Output is correct
35 Correct 5 ms 19036 KB Output is correct
36 Correct 51 ms 27320 KB Output is correct
37 Correct 7 ms 19036 KB Output is correct
38 Correct 6 ms 18940 KB Output is correct
39 Correct 38 ms 27204 KB Output is correct
40 Correct 5 ms 19292 KB Output is correct
41 Correct 52 ms 26832 KB Output is correct
42 Correct 57 ms 29544 KB Output is correct
43 Correct 4 ms 18780 KB Output is correct
44 Correct 58 ms 35280 KB Output is correct
45 Correct 64 ms 32224 KB Output is correct
46 Correct 5 ms 19036 KB Output is correct
47 Correct 5 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 27336 KB Output is correct
2 Correct 38 ms 29788 KB Output is correct
3 Correct 6 ms 19036 KB Output is correct
4 Correct 6 ms 19288 KB Output is correct
5 Correct 5 ms 20828 KB Output is correct
6 Correct 6 ms 20828 KB Output is correct
7 Correct 6 ms 19036 KB Output is correct
8 Correct 61 ms 27400 KB Output is correct
9 Correct 6 ms 19036 KB Output is correct
10 Correct 55 ms 27084 KB Output is correct
11 Correct 5 ms 18780 KB Output is correct
12 Correct 61 ms 26480 KB Output is correct
13 Correct 65 ms 27088 KB Output is correct
14 Correct 59 ms 29132 KB Output is correct
15 Correct 32 ms 27136 KB Output is correct
16 Correct 5 ms 21084 KB Output is correct
17 Correct 4 ms 20828 KB Output is correct
18 Correct 49 ms 29516 KB Output is correct
19 Correct 83 ms 34360 KB Output is correct
20 Correct 6 ms 19036 KB Output is correct
21 Correct 4 ms 18780 KB Output is correct
22 Correct 36 ms 27852 KB Output is correct
23 Correct 6 ms 19036 KB Output is correct
24 Correct 59 ms 27092 KB Output is correct
25 Correct 67 ms 32972 KB Output is correct
26 Correct 6 ms 21084 KB Output is correct
27 Correct 6 ms 21340 KB Output is correct
28 Correct 5 ms 18940 KB Output is correct
29 Correct 7 ms 19032 KB Output is correct
30 Correct 7 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20828 KB Output is correct
2 Correct 6 ms 18924 KB Output is correct
3 Correct 5 ms 18780 KB Output is correct
4 Correct 5 ms 20828 KB Output is correct
5 Correct 5 ms 18780 KB Output is correct
6 Correct 5 ms 18780 KB Output is correct
7 Correct 5 ms 18924 KB Output is correct
8 Correct 5 ms 18780 KB Output is correct
9 Correct 4 ms 18924 KB Output is correct
10 Correct 4 ms 18780 KB Output is correct
11 Correct 5 ms 18780 KB Output is correct
12 Correct 5 ms 18780 KB Output is correct
13 Correct 5 ms 20828 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 5 ms 18780 KB Output is correct
16 Correct 5 ms 20828 KB Output is correct
17 Correct 5 ms 20828 KB Output is correct
18 Correct 5 ms 18780 KB Output is correct
19 Correct 5 ms 20828 KB Output is correct
20 Correct 5 ms 20828 KB Output is correct
21 Correct 4 ms 18776 KB Output is correct
22 Correct 5 ms 18780 KB Output is correct
23 Correct 5 ms 18780 KB Output is correct
24 Correct 5 ms 18780 KB Output is correct
25 Correct 5 ms 20996 KB Output is correct
26 Correct 6 ms 19056 KB Output is correct
27 Correct 7 ms 19036 KB Output is correct
28 Correct 7 ms 21340 KB Output is correct
29 Correct 6 ms 21168 KB Output is correct
30 Correct 6 ms 19036 KB Output is correct
31 Correct 5 ms 20828 KB Output is correct
32 Correct 6 ms 21444 KB Output is correct
33 Correct 5 ms 20828 KB Output is correct
34 Correct 5 ms 19036 KB Output is correct
35 Correct 7 ms 21336 KB Output is correct
36 Correct 6 ms 19152 KB Output is correct
37 Correct 7 ms 18980 KB Output is correct
38 Correct 5 ms 18780 KB Output is correct
39 Correct 6 ms 19036 KB Output is correct
40 Correct 6 ms 19036 KB Output is correct
41 Correct 7 ms 19036 KB Output is correct
42 Correct 6 ms 21084 KB Output is correct
43 Correct 7 ms 19292 KB Output is correct
44 Correct 6 ms 20828 KB Output is correct
45 Correct 6 ms 21084 KB Output is correct
46 Correct 7 ms 19052 KB Output is correct
47 Correct 5 ms 18780 KB Output is correct
48 Correct 7 ms 19156 KB Output is correct
49 Correct 6 ms 21084 KB Output is correct
50 Correct 6 ms 21336 KB Output is correct
51 Correct 6 ms 19036 KB Output is correct
52 Correct 5 ms 19036 KB Output is correct
53 Correct 6 ms 19136 KB Output is correct
54 Correct 6 ms 19036 KB Output is correct
55 Correct 5 ms 21080 KB Output is correct
56 Correct 37 ms 27436 KB Output is correct
57 Correct 50 ms 26880 KB Output is correct
58 Correct 5 ms 19036 KB Output is correct
59 Correct 5 ms 20976 KB Output is correct
60 Correct 6 ms 21236 KB Output is correct
61 Correct 52 ms 27084 KB Output is correct
62 Correct 5 ms 19036 KB Output is correct
63 Correct 64 ms 35124 KB Output is correct
64 Correct 57 ms 26900 KB Output is correct
65 Correct 5 ms 19036 KB Output is correct
66 Correct 51 ms 27320 KB Output is correct
67 Correct 7 ms 19036 KB Output is correct
68 Correct 6 ms 18940 KB Output is correct
69 Correct 38 ms 27204 KB Output is correct
70 Correct 5 ms 19292 KB Output is correct
71 Correct 52 ms 26832 KB Output is correct
72 Correct 57 ms 29544 KB Output is correct
73 Correct 4 ms 18780 KB Output is correct
74 Correct 58 ms 35280 KB Output is correct
75 Correct 64 ms 32224 KB Output is correct
76 Correct 5 ms 19036 KB Output is correct
77 Correct 5 ms 19036 KB Output is correct
78 Correct 33 ms 27336 KB Output is correct
79 Correct 38 ms 29788 KB Output is correct
80 Correct 6 ms 19036 KB Output is correct
81 Correct 6 ms 19288 KB Output is correct
82 Correct 5 ms 20828 KB Output is correct
83 Correct 6 ms 20828 KB Output is correct
84 Correct 6 ms 19036 KB Output is correct
85 Correct 61 ms 27400 KB Output is correct
86 Correct 6 ms 19036 KB Output is correct
87 Correct 55 ms 27084 KB Output is correct
88 Correct 5 ms 18780 KB Output is correct
89 Correct 61 ms 26480 KB Output is correct
90 Correct 65 ms 27088 KB Output is correct
91 Correct 59 ms 29132 KB Output is correct
92 Correct 32 ms 27136 KB Output is correct
93 Correct 5 ms 21084 KB Output is correct
94 Correct 4 ms 20828 KB Output is correct
95 Correct 49 ms 29516 KB Output is correct
96 Correct 83 ms 34360 KB Output is correct
97 Correct 6 ms 19036 KB Output is correct
98 Correct 4 ms 18780 KB Output is correct
99 Correct 36 ms 27852 KB Output is correct
100 Correct 6 ms 19036 KB Output is correct
101 Correct 59 ms 27092 KB Output is correct
102 Correct 67 ms 32972 KB Output is correct
103 Correct 6 ms 21084 KB Output is correct
104 Correct 6 ms 21340 KB Output is correct
105 Correct 5 ms 18940 KB Output is correct
106 Correct 7 ms 19032 KB Output is correct
107 Correct 7 ms 19036 KB Output is correct
108 Correct 319 ms 58296 KB Output is correct
109 Correct 911 ms 72708 KB Output is correct
110 Correct 711 ms 85928 KB Output is correct
111 Correct 943 ms 88972 KB Output is correct
112 Correct 744 ms 77584 KB Output is correct
113 Correct 313 ms 57616 KB Output is correct
114 Correct 653 ms 53396 KB Output is correct
115 Correct 503 ms 53188 KB Output is correct
116 Correct 711 ms 55240 KB Output is correct
117 Correct 751 ms 56048 KB Output is correct
118 Correct 650 ms 53716 KB Output is correct
119 Correct 810 ms 55844 KB Output is correct
120 Correct 708 ms 76024 KB Output is correct
121 Correct 874 ms 56024 KB Output is correct
122 Correct 746 ms 55500 KB Output is correct
123 Correct 305 ms 58296 KB Output is correct
124 Correct 1010 ms 52608 KB Output is correct