답안 #895049

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895049 2023-12-29T11:16:35 Z Mohammadamin__Sh Mergers (JOI19_mergers) C++17
100 / 100
777 ms 170044 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';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 41672 KB Output is correct
2 Correct 10 ms 41628 KB Output is correct
3 Correct 9 ms 41560 KB Output is correct
4 Correct 8 ms 41564 KB Output is correct
5 Correct 9 ms 41676 KB Output is correct
6 Correct 8 ms 41560 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 10 ms 41680 KB Output is correct
9 Correct 9 ms 41560 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 10 ms 41684 KB Output is correct
12 Correct 11 ms 41832 KB Output is correct
13 Correct 8 ms 41668 KB Output is correct
14 Correct 8 ms 41564 KB Output is correct
15 Correct 8 ms 41688 KB Output is correct
16 Correct 8 ms 41564 KB Output is correct
17 Correct 8 ms 41596 KB Output is correct
18 Correct 8 ms 41564 KB Output is correct
19 Correct 8 ms 41564 KB Output is correct
20 Correct 8 ms 41564 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 9 ms 41564 KB Output is correct
23 Correct 8 ms 41560 KB Output is correct
24 Correct 9 ms 41564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 41672 KB Output is correct
2 Correct 10 ms 41628 KB Output is correct
3 Correct 9 ms 41560 KB Output is correct
4 Correct 8 ms 41564 KB Output is correct
5 Correct 9 ms 41676 KB Output is correct
6 Correct 8 ms 41560 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 10 ms 41680 KB Output is correct
9 Correct 9 ms 41560 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 10 ms 41684 KB Output is correct
12 Correct 11 ms 41832 KB Output is correct
13 Correct 8 ms 41668 KB Output is correct
14 Correct 8 ms 41564 KB Output is correct
15 Correct 8 ms 41688 KB Output is correct
16 Correct 8 ms 41564 KB Output is correct
17 Correct 8 ms 41596 KB Output is correct
18 Correct 8 ms 41564 KB Output is correct
19 Correct 8 ms 41564 KB Output is correct
20 Correct 8 ms 41564 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 9 ms 41564 KB Output is correct
23 Correct 8 ms 41560 KB Output is correct
24 Correct 9 ms 41564 KB Output is correct
25 Correct 15 ms 41560 KB Output is correct
26 Correct 10 ms 43864 KB Output is correct
27 Correct 10 ms 43864 KB Output is correct
28 Correct 13 ms 44124 KB Output is correct
29 Correct 12 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 12 ms 44112 KB Output is correct
33 Correct 10 ms 41676 KB Output is correct
34 Correct 12 ms 43980 KB Output is correct
35 Correct 10 ms 43868 KB Output is correct
36 Correct 12 ms 43868 KB Output is correct
37 Correct 11 ms 44124 KB Output is correct
38 Correct 10 ms 41680 KB Output is correct
39 Correct 14 ms 44124 KB Output is correct
40 Correct 11 ms 43972 KB Output is correct
41 Correct 11 ms 44000 KB Output is correct
42 Correct 12 ms 44124 KB Output is correct
43 Correct 11 ms 44228 KB Output is correct
44 Correct 9 ms 41672 KB Output is correct
45 Correct 10 ms 43868 KB Output is correct
46 Correct 10 ms 41924 KB Output is correct
47 Correct 10 ms 41564 KB Output is correct
48 Correct 10 ms 43872 KB Output is correct
49 Correct 10 ms 43868 KB Output is correct
50 Correct 10 ms 43876 KB Output is correct
51 Correct 10 ms 43868 KB Output is correct
52 Correct 10 ms 43868 KB Output is correct
53 Correct 11 ms 43864 KB Output is correct
54 Correct 13 ms 44124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 41672 KB Output is correct
2 Correct 10 ms 41628 KB Output is correct
3 Correct 9 ms 41560 KB Output is correct
4 Correct 8 ms 41564 KB Output is correct
5 Correct 9 ms 41676 KB Output is correct
6 Correct 8 ms 41560 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 10 ms 41680 KB Output is correct
9 Correct 9 ms 41560 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 10 ms 41684 KB Output is correct
12 Correct 11 ms 41832 KB Output is correct
13 Correct 8 ms 41668 KB Output is correct
14 Correct 8 ms 41564 KB Output is correct
15 Correct 8 ms 41688 KB Output is correct
16 Correct 8 ms 41564 KB Output is correct
17 Correct 8 ms 41596 KB Output is correct
18 Correct 8 ms 41564 KB Output is correct
19 Correct 8 ms 41564 KB Output is correct
20 Correct 8 ms 41564 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 9 ms 41564 KB Output is correct
23 Correct 8 ms 41560 KB Output is correct
24 Correct 9 ms 41564 KB Output is correct
25 Correct 9 ms 41564 KB Output is correct
26 Correct 70 ms 55300 KB Output is correct
27 Correct 142 ms 59220 KB Output is correct
28 Correct 12 ms 44032 KB Output is correct
29 Correct 9 ms 41564 KB Output is correct
30 Correct 10 ms 41564 KB Output is correct
31 Correct 64 ms 52008 KB Output is correct
32 Correct 10 ms 43864 KB Output is correct
33 Correct 66 ms 62728 KB Output is correct
34 Correct 71 ms 57732 KB Output is correct
35 Correct 10 ms 43864 KB Output is correct
36 Correct 53 ms 49756 KB Output is correct
37 Correct 10 ms 43868 KB Output is correct
38 Correct 9 ms 43732 KB Output is correct
39 Correct 65 ms 55160 KB Output is correct
40 Correct 11 ms 44124 KB Output is correct
41 Correct 63 ms 51908 KB Output is correct
42 Correct 55 ms 53076 KB Output is correct
43 Correct 9 ms 41564 KB Output is correct
44 Correct 60 ms 63184 KB Output is correct
45 Correct 72 ms 59988 KB Output is correct
46 Correct 11 ms 43864 KB Output is correct
47 Correct 11 ms 44644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 55236 KB Output is correct
2 Correct 49 ms 51408 KB Output is correct
3 Correct 9 ms 43864 KB Output is correct
4 Correct 10 ms 43868 KB Output is correct
5 Correct 8 ms 41564 KB Output is correct
6 Correct 8 ms 41628 KB Output is correct
7 Correct 10 ms 43868 KB Output is correct
8 Correct 87 ms 57000 KB Output is correct
9 Correct 10 ms 43864 KB Output is correct
10 Correct 55 ms 51796 KB Output is correct
11 Correct 9 ms 41560 KB Output is correct
12 Correct 51 ms 48464 KB Output is correct
13 Correct 71 ms 53240 KB Output is correct
14 Correct 60 ms 51232 KB Output is correct
15 Correct 60 ms 55168 KB Output is correct
16 Correct 10 ms 43868 KB Output is correct
17 Correct 8 ms 41620 KB Output is correct
18 Correct 57 ms 52760 KB Output is correct
19 Correct 71 ms 58448 KB Output is correct
20 Correct 10 ms 41820 KB Output is correct
21 Correct 9 ms 41560 KB Output is correct
22 Correct 93 ms 57540 KB Output is correct
23 Correct 9 ms 43868 KB Output is correct
24 Correct 50 ms 49216 KB Output is correct
25 Correct 75 ms 57072 KB Output is correct
26 Correct 9 ms 43868 KB Output is correct
27 Correct 10 ms 43868 KB Output is correct
28 Correct 10 ms 43864 KB Output is correct
29 Correct 9 ms 43868 KB Output is correct
30 Correct 10 ms 43868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 41672 KB Output is correct
2 Correct 10 ms 41628 KB Output is correct
3 Correct 9 ms 41560 KB Output is correct
4 Correct 8 ms 41564 KB Output is correct
5 Correct 9 ms 41676 KB Output is correct
6 Correct 8 ms 41560 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 10 ms 41680 KB Output is correct
9 Correct 9 ms 41560 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 10 ms 41684 KB Output is correct
12 Correct 11 ms 41832 KB Output is correct
13 Correct 8 ms 41668 KB Output is correct
14 Correct 8 ms 41564 KB Output is correct
15 Correct 8 ms 41688 KB Output is correct
16 Correct 8 ms 41564 KB Output is correct
17 Correct 8 ms 41596 KB Output is correct
18 Correct 8 ms 41564 KB Output is correct
19 Correct 8 ms 41564 KB Output is correct
20 Correct 8 ms 41564 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 9 ms 41564 KB Output is correct
23 Correct 8 ms 41560 KB Output is correct
24 Correct 9 ms 41564 KB Output is correct
25 Correct 15 ms 41560 KB Output is correct
26 Correct 10 ms 43864 KB Output is correct
27 Correct 10 ms 43864 KB Output is correct
28 Correct 13 ms 44124 KB Output is correct
29 Correct 12 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 12 ms 44112 KB Output is correct
33 Correct 10 ms 41676 KB Output is correct
34 Correct 12 ms 43980 KB Output is correct
35 Correct 10 ms 43868 KB Output is correct
36 Correct 12 ms 43868 KB Output is correct
37 Correct 11 ms 44124 KB Output is correct
38 Correct 10 ms 41680 KB Output is correct
39 Correct 14 ms 44124 KB Output is correct
40 Correct 11 ms 43972 KB Output is correct
41 Correct 11 ms 44000 KB Output is correct
42 Correct 12 ms 44124 KB Output is correct
43 Correct 11 ms 44228 KB Output is correct
44 Correct 9 ms 41672 KB Output is correct
45 Correct 10 ms 43868 KB Output is correct
46 Correct 10 ms 41924 KB Output is correct
47 Correct 10 ms 41564 KB Output is correct
48 Correct 10 ms 43872 KB Output is correct
49 Correct 10 ms 43868 KB Output is correct
50 Correct 10 ms 43876 KB Output is correct
51 Correct 10 ms 43868 KB Output is correct
52 Correct 10 ms 43868 KB Output is correct
53 Correct 11 ms 43864 KB Output is correct
54 Correct 13 ms 44124 KB Output is correct
55 Correct 9 ms 41564 KB Output is correct
56 Correct 70 ms 55300 KB Output is correct
57 Correct 142 ms 59220 KB Output is correct
58 Correct 12 ms 44032 KB Output is correct
59 Correct 9 ms 41564 KB Output is correct
60 Correct 10 ms 41564 KB Output is correct
61 Correct 64 ms 52008 KB Output is correct
62 Correct 10 ms 43864 KB Output is correct
63 Correct 66 ms 62728 KB Output is correct
64 Correct 71 ms 57732 KB Output is correct
65 Correct 10 ms 43864 KB Output is correct
66 Correct 53 ms 49756 KB Output is correct
67 Correct 10 ms 43868 KB Output is correct
68 Correct 9 ms 43732 KB Output is correct
69 Correct 65 ms 55160 KB Output is correct
70 Correct 11 ms 44124 KB Output is correct
71 Correct 63 ms 51908 KB Output is correct
72 Correct 55 ms 53076 KB Output is correct
73 Correct 9 ms 41564 KB Output is correct
74 Correct 60 ms 63184 KB Output is correct
75 Correct 72 ms 59988 KB Output is correct
76 Correct 11 ms 43864 KB Output is correct
77 Correct 11 ms 44644 KB Output is correct
78 Correct 56 ms 55236 KB Output is correct
79 Correct 49 ms 51408 KB Output is correct
80 Correct 9 ms 43864 KB Output is correct
81 Correct 10 ms 43868 KB Output is correct
82 Correct 8 ms 41564 KB Output is correct
83 Correct 8 ms 41628 KB Output is correct
84 Correct 10 ms 43868 KB Output is correct
85 Correct 87 ms 57000 KB Output is correct
86 Correct 10 ms 43864 KB Output is correct
87 Correct 55 ms 51796 KB Output is correct
88 Correct 9 ms 41560 KB Output is correct
89 Correct 51 ms 48464 KB Output is correct
90 Correct 71 ms 53240 KB Output is correct
91 Correct 60 ms 51232 KB Output is correct
92 Correct 60 ms 55168 KB Output is correct
93 Correct 10 ms 43868 KB Output is correct
94 Correct 8 ms 41620 KB Output is correct
95 Correct 57 ms 52760 KB Output is correct
96 Correct 71 ms 58448 KB Output is correct
97 Correct 10 ms 41820 KB Output is correct
98 Correct 9 ms 41560 KB Output is correct
99 Correct 93 ms 57540 KB Output is correct
100 Correct 9 ms 43868 KB Output is correct
101 Correct 50 ms 49216 KB Output is correct
102 Correct 75 ms 57072 KB Output is correct
103 Correct 9 ms 43868 KB Output is correct
104 Correct 10 ms 43868 KB Output is correct
105 Correct 10 ms 43864 KB Output is correct
106 Correct 9 ms 43868 KB Output is correct
107 Correct 10 ms 43868 KB Output is correct
108 Correct 589 ms 110092 KB Output is correct
109 Correct 446 ms 104024 KB Output is correct
110 Correct 450 ms 126428 KB Output is correct
111 Correct 483 ms 120176 KB Output is correct
112 Correct 527 ms 106504 KB Output is correct
113 Correct 352 ms 84140 KB Output is correct
114 Correct 436 ms 85588 KB Output is correct
115 Correct 462 ms 85668 KB Output is correct
116 Correct 750 ms 153244 KB Output is correct
117 Correct 444 ms 75856 KB Output is correct
118 Correct 424 ms 85672 KB Output is correct
119 Correct 480 ms 76116 KB Output is correct
120 Correct 511 ms 107636 KB Output is correct
121 Correct 451 ms 75844 KB Output is correct
122 Correct 777 ms 170044 KB Output is correct
123 Correct 370 ms 76980 KB Output is correct
124 Correct 517 ms 73204 KB Output is correct