Submission #894925

# Submission time Handle Problem Language Result Execution time Memory
894925 2023-12-29T08:45:31 Z vjudge1 Mergers (JOI19_mergers) C++17
100 / 100
915 ms 170108 KB
//In His Name
#include <bits/stdc++.h>
//#pragma GCC optimization("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx2")
using namespace std;
#define ll long long
#define int ll
typedef pair<int, int> pii;
#define F first
#define S second
#define pb push_back
#define bug(x) cout << "Ah shit , here we go again : " << x <<endl
#define all(x) x.begin() , x.end()
const int maxn = 5e5 + 10, MOD = 1e9 + 7;
const ll INF = 1e18;

int n , k , state[maxn] , a[maxn] , ans , ok[maxn];
vector<int> adj[maxn];
map<int,int> gooni[maxn];

void Dfs(int v , int p){
    for(int u : adj[v]) {
        if (u == p) continue;
        Dfs(u, v);
        if (gooni[v].size() < gooni[u].size()) swap(gooni[v], gooni[u]) , swap(ok[v] , ok[u]);
    }
    for(int u : adj[v]){
        if(u == p) continue;
        ok[v] += ok[u];
        for(auto i : gooni[u]){
            gooni[v][i.F] += i.S;
            if(gooni[v][i.F] == state[i.F]) gooni[v].erase(i.F);
        }
    }
    gooni[v][a[v]]++;
    if(gooni[v][a[v]] == state[a[v]]) gooni[v].erase(a[v]);
    if(gooni[v].size() == 0 and v != 1){
        if(!ok[v]) ans ++;
        ok[v] = 1;
    }
}

int32_t main(){
    ios_base::sync_with_stdio(false);

    cin >> n >> k;
    for(int i = 1 ; i < n ; i++){
        int u , v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for(int i = 1 ; i <= n ; i++){
        cin >> a[i];
        state[a[i]]++;
    }
    Dfs(1 , 0);
    if(n == 1) return cout << 0 , 0;
    if(ok[1] == 1) ans++;
    cout << (ans+1)/2 << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 41560 KB Output is correct
2 Correct 8 ms 41564 KB Output is correct
3 Correct 8 ms 41564 KB Output is correct
4 Correct 8 ms 41616 KB Output is correct
5 Correct 8 ms 41564 KB Output is correct
6 Correct 8 ms 41564 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 9 ms 41652 KB Output is correct
9 Correct 9 ms 41564 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 8 ms 41564 KB Output is correct
12 Correct 8 ms 41564 KB Output is correct
13 Correct 8 ms 41664 KB Output is correct
14 Correct 8 ms 41816 KB Output is correct
15 Correct 8 ms 41672 KB Output is correct
16 Correct 8 ms 41564 KB Output is correct
17 Correct 8 ms 41668 KB Output is correct
18 Correct 10 ms 41564 KB Output is correct
19 Correct 8 ms 41560 KB Output is correct
20 Correct 8 ms 41564 KB Output is correct
21 Correct 8 ms 41608 KB Output is correct
22 Correct 8 ms 41564 KB Output is correct
23 Correct 9 ms 41660 KB Output is correct
24 Correct 8 ms 41560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 41560 KB Output is correct
2 Correct 8 ms 41564 KB Output is correct
3 Correct 8 ms 41564 KB Output is correct
4 Correct 8 ms 41616 KB Output is correct
5 Correct 8 ms 41564 KB Output is correct
6 Correct 8 ms 41564 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 9 ms 41652 KB Output is correct
9 Correct 9 ms 41564 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 8 ms 41564 KB Output is correct
12 Correct 8 ms 41564 KB Output is correct
13 Correct 8 ms 41664 KB Output is correct
14 Correct 8 ms 41816 KB Output is correct
15 Correct 8 ms 41672 KB Output is correct
16 Correct 8 ms 41564 KB Output is correct
17 Correct 8 ms 41668 KB Output is correct
18 Correct 10 ms 41564 KB Output is correct
19 Correct 8 ms 41560 KB Output is correct
20 Correct 8 ms 41564 KB Output is correct
21 Correct 8 ms 41608 KB Output is correct
22 Correct 8 ms 41564 KB Output is correct
23 Correct 9 ms 41660 KB Output is correct
24 Correct 8 ms 41560 KB Output is correct
25 Correct 8 ms 41560 KB Output is correct
26 Correct 11 ms 43868 KB Output is correct
27 Correct 9 ms 43660 KB Output is correct
28 Correct 10 ms 43868 KB Output is correct
29 Correct 11 ms 43868 KB Output is correct
30 Correct 12 ms 44124 KB Output is correct
31 Correct 8 ms 41564 KB Output is correct
32 Correct 9 ms 44196 KB Output is correct
33 Correct 8 ms 41564 KB Output is correct
34 Correct 10 ms 43988 KB Output is correct
35 Correct 10 ms 43868 KB Output is correct
36 Correct 10 ms 43868 KB Output is correct
37 Correct 12 ms 44132 KB Output is correct
38 Correct 8 ms 41564 KB Output is correct
39 Correct 11 ms 44124 KB Output is correct
40 Correct 10 ms 43868 KB Output is correct
41 Correct 10 ms 43732 KB Output is correct
42 Correct 10 ms 43868 KB Output is correct
43 Correct 11 ms 44160 KB Output is correct
44 Correct 8 ms 41564 KB Output is correct
45 Correct 9 ms 43868 KB Output is correct
46 Correct 9 ms 41820 KB Output is correct
47 Correct 8 ms 41564 KB Output is correct
48 Correct 10 ms 43868 KB Output is correct
49 Correct 11 ms 43712 KB Output is correct
50 Correct 11 ms 43868 KB Output is correct
51 Correct 10 ms 43864 KB Output is correct
52 Correct 9 ms 44120 KB Output is correct
53 Correct 10 ms 43868 KB Output is correct
54 Correct 10 ms 44164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 41560 KB Output is correct
2 Correct 8 ms 41564 KB Output is correct
3 Correct 8 ms 41564 KB Output is correct
4 Correct 8 ms 41616 KB Output is correct
5 Correct 8 ms 41564 KB Output is correct
6 Correct 8 ms 41564 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 9 ms 41652 KB Output is correct
9 Correct 9 ms 41564 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 8 ms 41564 KB Output is correct
12 Correct 8 ms 41564 KB Output is correct
13 Correct 8 ms 41664 KB Output is correct
14 Correct 8 ms 41816 KB Output is correct
15 Correct 8 ms 41672 KB Output is correct
16 Correct 8 ms 41564 KB Output is correct
17 Correct 8 ms 41668 KB Output is correct
18 Correct 10 ms 41564 KB Output is correct
19 Correct 8 ms 41560 KB Output is correct
20 Correct 8 ms 41564 KB Output is correct
21 Correct 8 ms 41608 KB Output is correct
22 Correct 8 ms 41564 KB Output is correct
23 Correct 9 ms 41660 KB Output is correct
24 Correct 8 ms 41560 KB Output is correct
25 Correct 8 ms 41560 KB Output is correct
26 Correct 63 ms 55084 KB Output is correct
27 Correct 91 ms 59456 KB Output is correct
28 Correct 9 ms 43864 KB Output is correct
29 Correct 8 ms 41564 KB Output is correct
30 Correct 8 ms 41612 KB Output is correct
31 Correct 54 ms 51928 KB Output is correct
32 Correct 9 ms 43868 KB Output is correct
33 Correct 61 ms 62780 KB Output is correct
34 Correct 84 ms 57928 KB Output is correct
35 Correct 11 ms 43868 KB Output is correct
36 Correct 69 ms 49964 KB Output is correct
37 Correct 9 ms 43868 KB Output is correct
38 Correct 10 ms 43728 KB Output is correct
39 Correct 74 ms 55160 KB Output is correct
40 Correct 10 ms 44120 KB Output is correct
41 Correct 58 ms 51768 KB Output is correct
42 Correct 64 ms 52960 KB Output is correct
43 Correct 8 ms 41560 KB Output is correct
44 Correct 61 ms 63128 KB Output is correct
45 Correct 71 ms 59968 KB Output is correct
46 Correct 10 ms 43984 KB Output is correct
47 Correct 13 ms 43972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 53780 KB Output is correct
2 Correct 59 ms 49608 KB Output is correct
3 Correct 9 ms 43612 KB Output is correct
4 Correct 9 ms 43868 KB Output is correct
5 Correct 8 ms 41560 KB Output is correct
6 Correct 8 ms 41680 KB Output is correct
7 Correct 10 ms 43828 KB Output is correct
8 Correct 78 ms 57060 KB Output is correct
9 Correct 10 ms 43864 KB Output is correct
10 Correct 60 ms 51796 KB Output is correct
11 Correct 9 ms 41560 KB Output is correct
12 Correct 69 ms 48724 KB Output is correct
13 Correct 70 ms 53076 KB Output is correct
14 Correct 56 ms 51032 KB Output is correct
15 Correct 57 ms 55248 KB Output is correct
16 Correct 10 ms 43864 KB Output is correct
17 Correct 8 ms 41560 KB Output is correct
18 Correct 59 ms 52884 KB Output is correct
19 Correct 63 ms 58680 KB Output is correct
20 Correct 10 ms 41812 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 89 ms 57624 KB Output is correct
23 Correct 10 ms 43868 KB Output is correct
24 Correct 55 ms 49236 KB Output is correct
25 Correct 72 ms 56940 KB Output is correct
26 Correct 9 ms 43864 KB Output is correct
27 Correct 10 ms 43868 KB Output is correct
28 Correct 10 ms 43868 KB Output is correct
29 Correct 11 ms 43864 KB Output is correct
30 Correct 10 ms 43868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 41560 KB Output is correct
2 Correct 8 ms 41564 KB Output is correct
3 Correct 8 ms 41564 KB Output is correct
4 Correct 8 ms 41616 KB Output is correct
5 Correct 8 ms 41564 KB Output is correct
6 Correct 8 ms 41564 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 9 ms 41652 KB Output is correct
9 Correct 9 ms 41564 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 8 ms 41564 KB Output is correct
12 Correct 8 ms 41564 KB Output is correct
13 Correct 8 ms 41664 KB Output is correct
14 Correct 8 ms 41816 KB Output is correct
15 Correct 8 ms 41672 KB Output is correct
16 Correct 8 ms 41564 KB Output is correct
17 Correct 8 ms 41668 KB Output is correct
18 Correct 10 ms 41564 KB Output is correct
19 Correct 8 ms 41560 KB Output is correct
20 Correct 8 ms 41564 KB Output is correct
21 Correct 8 ms 41608 KB Output is correct
22 Correct 8 ms 41564 KB Output is correct
23 Correct 9 ms 41660 KB Output is correct
24 Correct 8 ms 41560 KB Output is correct
25 Correct 8 ms 41560 KB Output is correct
26 Correct 11 ms 43868 KB Output is correct
27 Correct 9 ms 43660 KB Output is correct
28 Correct 10 ms 43868 KB Output is correct
29 Correct 11 ms 43868 KB Output is correct
30 Correct 12 ms 44124 KB Output is correct
31 Correct 8 ms 41564 KB Output is correct
32 Correct 9 ms 44196 KB Output is correct
33 Correct 8 ms 41564 KB Output is correct
34 Correct 10 ms 43988 KB Output is correct
35 Correct 10 ms 43868 KB Output is correct
36 Correct 10 ms 43868 KB Output is correct
37 Correct 12 ms 44132 KB Output is correct
38 Correct 8 ms 41564 KB Output is correct
39 Correct 11 ms 44124 KB Output is correct
40 Correct 10 ms 43868 KB Output is correct
41 Correct 10 ms 43732 KB Output is correct
42 Correct 10 ms 43868 KB Output is correct
43 Correct 11 ms 44160 KB Output is correct
44 Correct 8 ms 41564 KB Output is correct
45 Correct 9 ms 43868 KB Output is correct
46 Correct 9 ms 41820 KB Output is correct
47 Correct 8 ms 41564 KB Output is correct
48 Correct 10 ms 43868 KB Output is correct
49 Correct 11 ms 43712 KB Output is correct
50 Correct 11 ms 43868 KB Output is correct
51 Correct 10 ms 43864 KB Output is correct
52 Correct 9 ms 44120 KB Output is correct
53 Correct 10 ms 43868 KB Output is correct
54 Correct 10 ms 44164 KB Output is correct
55 Correct 8 ms 41560 KB Output is correct
56 Correct 63 ms 55084 KB Output is correct
57 Correct 91 ms 59456 KB Output is correct
58 Correct 9 ms 43864 KB Output is correct
59 Correct 8 ms 41564 KB Output is correct
60 Correct 8 ms 41612 KB Output is correct
61 Correct 54 ms 51928 KB Output is correct
62 Correct 9 ms 43868 KB Output is correct
63 Correct 61 ms 62780 KB Output is correct
64 Correct 84 ms 57928 KB Output is correct
65 Correct 11 ms 43868 KB Output is correct
66 Correct 69 ms 49964 KB Output is correct
67 Correct 9 ms 43868 KB Output is correct
68 Correct 10 ms 43728 KB Output is correct
69 Correct 74 ms 55160 KB Output is correct
70 Correct 10 ms 44120 KB Output is correct
71 Correct 58 ms 51768 KB Output is correct
72 Correct 64 ms 52960 KB Output is correct
73 Correct 8 ms 41560 KB Output is correct
74 Correct 61 ms 63128 KB Output is correct
75 Correct 71 ms 59968 KB Output is correct
76 Correct 10 ms 43984 KB Output is correct
77 Correct 13 ms 43972 KB Output is correct
78 Correct 61 ms 53780 KB Output is correct
79 Correct 59 ms 49608 KB Output is correct
80 Correct 9 ms 43612 KB Output is correct
81 Correct 9 ms 43868 KB Output is correct
82 Correct 8 ms 41560 KB Output is correct
83 Correct 8 ms 41680 KB Output is correct
84 Correct 10 ms 43828 KB Output is correct
85 Correct 78 ms 57060 KB Output is correct
86 Correct 10 ms 43864 KB Output is correct
87 Correct 60 ms 51796 KB Output is correct
88 Correct 9 ms 41560 KB Output is correct
89 Correct 69 ms 48724 KB Output is correct
90 Correct 70 ms 53076 KB Output is correct
91 Correct 56 ms 51032 KB Output is correct
92 Correct 57 ms 55248 KB Output is correct
93 Correct 10 ms 43864 KB Output is correct
94 Correct 8 ms 41560 KB Output is correct
95 Correct 59 ms 52884 KB Output is correct
96 Correct 63 ms 58680 KB Output is correct
97 Correct 10 ms 41812 KB Output is correct
98 Correct 8 ms 41564 KB Output is correct
99 Correct 89 ms 57624 KB Output is correct
100 Correct 10 ms 43868 KB Output is correct
101 Correct 55 ms 49236 KB Output is correct
102 Correct 72 ms 56940 KB Output is correct
103 Correct 9 ms 43864 KB Output is correct
104 Correct 10 ms 43868 KB Output is correct
105 Correct 10 ms 43868 KB Output is correct
106 Correct 11 ms 43864 KB Output is correct
107 Correct 10 ms 43868 KB Output is correct
108 Correct 565 ms 110260 KB Output is correct
109 Correct 462 ms 104016 KB Output is correct
110 Correct 494 ms 126288 KB Output is correct
111 Correct 584 ms 120020 KB Output is correct
112 Correct 511 ms 106712 KB Output is correct
113 Correct 378 ms 84012 KB Output is correct
114 Correct 442 ms 85928 KB Output is correct
115 Correct 419 ms 85432 KB Output is correct
116 Correct 759 ms 153208 KB Output is correct
117 Correct 477 ms 76116 KB Output is correct
118 Correct 416 ms 85640 KB Output is correct
119 Correct 452 ms 75924 KB Output is correct
120 Correct 524 ms 107844 KB Output is correct
121 Correct 429 ms 75860 KB Output is correct
122 Correct 915 ms 170108 KB Output is correct
123 Correct 297 ms 77068 KB Output is correct
124 Correct 525 ms 73124 KB Output is correct