Submission #248665

# Submission time Handle Problem Language Result Execution time Memory
248665 2020-07-13T02:49:05 Z fivefourthreeone Mergers (JOI19_mergers) C++17
100 / 100
1465 ms 167928 KB
#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a); i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"debug"<<endl
 
using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 998244353;
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
const int NINF = 0x3f3f3f40;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0x3f3f3f3f3f3f3f40;
const int mxN = 500001;
vector<int> adj[mxN];
int n, k;
int state[mxN];
int high[mxN];
int depth[mxN];
int val[mxN];
int par[mxN];
bool flag[mxN];
int best[mxN];
int ans = 0;
int cnt =0;
ttgl edges[mxN];
int dsu[mxN];
int sz[mxN];
map<int, int> mp[mxN];
int find(int a){return dsu[a]==a ? a : dsu[a] = find(dsu[a]);}
void merge(int a, int b) {
    a = find(a);b = find(b);
    if(sz[b]<sz[a]) swap(a, b);
    dsu[a] = b;
    sz[b]+=sz[a];
}
void comb(int u, int v) {
    if(mp[u].size()<mp[v].size()) {
        swap(mp[u], mp[v]);   
    }
    for(auto p: mp[v]) {
        mp[u][p.first]+=p.second;
        if(mp[u][p.first]==state[p.first]) {
            mp[u].erase(p.first);
        }
    }
}
void dfs(int u, int p) {
    par[u] = p;
    mp[u][val[u]]++;
    for(int v: adj[u]) {
        if(v==p)continue;
        depth[v] =depth[u]+1;
        dfs(v, u);
        comb(u, v);
    }
    for(auto p: mp[u]) {
        if(mp[u][p.first]==state[p.first]) {
            mp[u].erase(p.first);
        }
    }
    if(!mp[u].empty()) {
        //cout<<u<<"\n";
        merge(val[u], val[p]);
    }
}
int deg[mxN];
int main()
{
    //freopen("filename.in", "r", stdin);
    //freopen("filename.out", "w", stdout);
    mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    cin>>n>>k;
    int a, b;
    owo(i, 0, n-1) {
        cin>>a>>b;
        a--; b--;
        edges[i] = {a, b};
        adj[a].senpai(b);
        adj[b].senpai(a);
    }
    owo(i, 0, n) {
        cin>>a;
        a--;
        val[i] = a;
        state[a]++;
    }
    owo(i, 0, k) {
        dsu[i] = i;
        sz[i] = 1;
        //cout<<state[i]<<"\n";
    }
    dfs(0, 0);
    for(auto p: edges) {
        if(find(val[p.first])!=find(val[p.second])) {
            deg[find(val[p.first])]++;
            deg[find(val[p.second])]++;
        }
    }
    owo(i, 0, k){
        if(deg[i]==1) ans++;
    }
    //cout<<ans<<"\n";
    cout<<(ans+1)/2<<"\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35584 KB Output is correct
2 Correct 22 ms 35584 KB Output is correct
3 Correct 22 ms 35584 KB Output is correct
4 Correct 25 ms 35584 KB Output is correct
5 Correct 25 ms 35712 KB Output is correct
6 Correct 23 ms 35584 KB Output is correct
7 Correct 22 ms 35584 KB Output is correct
8 Correct 24 ms 35584 KB Output is correct
9 Correct 22 ms 35712 KB Output is correct
10 Correct 21 ms 35584 KB Output is correct
11 Correct 25 ms 35584 KB Output is correct
12 Correct 22 ms 35584 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 21 ms 35584 KB Output is correct
15 Correct 25 ms 35584 KB Output is correct
16 Correct 21 ms 35584 KB Output is correct
17 Correct 24 ms 35584 KB Output is correct
18 Correct 21 ms 35584 KB Output is correct
19 Correct 21 ms 35584 KB Output is correct
20 Correct 21 ms 35712 KB Output is correct
21 Correct 25 ms 35584 KB Output is correct
22 Correct 22 ms 35584 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 25 ms 35584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35584 KB Output is correct
2 Correct 22 ms 35584 KB Output is correct
3 Correct 22 ms 35584 KB Output is correct
4 Correct 25 ms 35584 KB Output is correct
5 Correct 25 ms 35712 KB Output is correct
6 Correct 23 ms 35584 KB Output is correct
7 Correct 22 ms 35584 KB Output is correct
8 Correct 24 ms 35584 KB Output is correct
9 Correct 22 ms 35712 KB Output is correct
10 Correct 21 ms 35584 KB Output is correct
11 Correct 25 ms 35584 KB Output is correct
12 Correct 22 ms 35584 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 21 ms 35584 KB Output is correct
15 Correct 25 ms 35584 KB Output is correct
16 Correct 21 ms 35584 KB Output is correct
17 Correct 24 ms 35584 KB Output is correct
18 Correct 21 ms 35584 KB Output is correct
19 Correct 21 ms 35584 KB Output is correct
20 Correct 21 ms 35712 KB Output is correct
21 Correct 25 ms 35584 KB Output is correct
22 Correct 22 ms 35584 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 25 ms 35584 KB Output is correct
25 Correct 22 ms 35584 KB Output is correct
26 Correct 28 ms 35960 KB Output is correct
27 Correct 24 ms 35968 KB Output is correct
28 Correct 29 ms 36088 KB Output is correct
29 Correct 25 ms 35840 KB Output is correct
30 Correct 28 ms 36224 KB Output is correct
31 Correct 21 ms 35584 KB Output is correct
32 Correct 33 ms 36224 KB Output is correct
33 Correct 22 ms 35704 KB Output is correct
34 Correct 26 ms 35968 KB Output is correct
35 Correct 23 ms 35832 KB Output is correct
36 Correct 24 ms 35968 KB Output is correct
37 Correct 27 ms 36096 KB Output is correct
38 Correct 25 ms 35584 KB Output is correct
39 Correct 29 ms 36224 KB Output is correct
40 Correct 23 ms 36088 KB Output is correct
41 Correct 23 ms 35960 KB Output is correct
42 Correct 27 ms 35968 KB Output is correct
43 Correct 22 ms 36224 KB Output is correct
44 Correct 22 ms 35584 KB Output is correct
45 Correct 26 ms 36096 KB Output is correct
46 Correct 29 ms 36096 KB Output is correct
47 Correct 25 ms 35584 KB Output is correct
48 Correct 27 ms 35960 KB Output is correct
49 Correct 23 ms 35840 KB Output is correct
50 Correct 25 ms 35960 KB Output is correct
51 Correct 26 ms 35968 KB Output is correct
52 Correct 23 ms 35968 KB Output is correct
53 Correct 26 ms 35968 KB Output is correct
54 Correct 28 ms 36096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35584 KB Output is correct
2 Correct 22 ms 35584 KB Output is correct
3 Correct 22 ms 35584 KB Output is correct
4 Correct 25 ms 35584 KB Output is correct
5 Correct 25 ms 35712 KB Output is correct
6 Correct 23 ms 35584 KB Output is correct
7 Correct 22 ms 35584 KB Output is correct
8 Correct 24 ms 35584 KB Output is correct
9 Correct 22 ms 35712 KB Output is correct
10 Correct 21 ms 35584 KB Output is correct
11 Correct 25 ms 35584 KB Output is correct
12 Correct 22 ms 35584 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 21 ms 35584 KB Output is correct
15 Correct 25 ms 35584 KB Output is correct
16 Correct 21 ms 35584 KB Output is correct
17 Correct 24 ms 35584 KB Output is correct
18 Correct 21 ms 35584 KB Output is correct
19 Correct 21 ms 35584 KB Output is correct
20 Correct 21 ms 35712 KB Output is correct
21 Correct 25 ms 35584 KB Output is correct
22 Correct 22 ms 35584 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 25 ms 35584 KB Output is correct
25 Correct 21 ms 35584 KB Output is correct
26 Correct 98 ms 47220 KB Output is correct
27 Correct 160 ms 53624 KB Output is correct
28 Correct 23 ms 35968 KB Output is correct
29 Correct 21 ms 35584 KB Output is correct
30 Correct 21 ms 35584 KB Output is correct
31 Correct 123 ms 46968 KB Output is correct
32 Correct 26 ms 35968 KB Output is correct
33 Correct 200 ms 57208 KB Output is correct
34 Correct 162 ms 52344 KB Output is correct
35 Correct 25 ms 35968 KB Output is correct
36 Correct 125 ms 47992 KB Output is correct
37 Correct 26 ms 35968 KB Output is correct
38 Correct 23 ms 35968 KB Output is correct
39 Correct 102 ms 47216 KB Output is correct
40 Correct 23 ms 36216 KB Output is correct
41 Correct 115 ms 46840 KB Output is correct
42 Correct 169 ms 50296 KB Output is correct
43 Correct 25 ms 35584 KB Output is correct
44 Correct 152 ms 57336 KB Output is correct
45 Correct 168 ms 53368 KB Output is correct
46 Correct 24 ms 35968 KB Output is correct
47 Correct 27 ms 36096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 45804 KB Output is correct
2 Correct 102 ms 42608 KB Output is correct
3 Correct 32 ms 35964 KB Output is correct
4 Correct 24 ms 35968 KB Output is correct
5 Correct 22 ms 35584 KB Output is correct
6 Correct 21 ms 35584 KB Output is correct
7 Correct 26 ms 35960 KB Output is correct
8 Correct 157 ms 50808 KB Output is correct
9 Correct 23 ms 35968 KB Output is correct
10 Correct 106 ms 46968 KB Output is correct
11 Correct 25 ms 35584 KB Output is correct
12 Correct 110 ms 46968 KB Output is correct
13 Correct 128 ms 47480 KB Output is correct
14 Correct 115 ms 44152 KB Output is correct
15 Correct 100 ms 47216 KB Output is correct
16 Correct 26 ms 35968 KB Output is correct
17 Correct 21 ms 35584 KB Output is correct
18 Correct 116 ms 45436 KB Output is correct
19 Correct 124 ms 52088 KB Output is correct
20 Correct 27 ms 36088 KB Output is correct
21 Correct 24 ms 35584 KB Output is correct
22 Correct 138 ms 47344 KB Output is correct
23 Correct 26 ms 35968 KB Output is correct
24 Correct 111 ms 46712 KB Output is correct
25 Correct 137 ms 49528 KB Output is correct
26 Correct 23 ms 35840 KB Output is correct
27 Correct 23 ms 35968 KB Output is correct
28 Correct 26 ms 35968 KB Output is correct
29 Correct 24 ms 35968 KB Output is correct
30 Correct 27 ms 35960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35584 KB Output is correct
2 Correct 22 ms 35584 KB Output is correct
3 Correct 22 ms 35584 KB Output is correct
4 Correct 25 ms 35584 KB Output is correct
5 Correct 25 ms 35712 KB Output is correct
6 Correct 23 ms 35584 KB Output is correct
7 Correct 22 ms 35584 KB Output is correct
8 Correct 24 ms 35584 KB Output is correct
9 Correct 22 ms 35712 KB Output is correct
10 Correct 21 ms 35584 KB Output is correct
11 Correct 25 ms 35584 KB Output is correct
12 Correct 22 ms 35584 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 21 ms 35584 KB Output is correct
15 Correct 25 ms 35584 KB Output is correct
16 Correct 21 ms 35584 KB Output is correct
17 Correct 24 ms 35584 KB Output is correct
18 Correct 21 ms 35584 KB Output is correct
19 Correct 21 ms 35584 KB Output is correct
20 Correct 21 ms 35712 KB Output is correct
21 Correct 25 ms 35584 KB Output is correct
22 Correct 22 ms 35584 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 25 ms 35584 KB Output is correct
25 Correct 22 ms 35584 KB Output is correct
26 Correct 28 ms 35960 KB Output is correct
27 Correct 24 ms 35968 KB Output is correct
28 Correct 29 ms 36088 KB Output is correct
29 Correct 25 ms 35840 KB Output is correct
30 Correct 28 ms 36224 KB Output is correct
31 Correct 21 ms 35584 KB Output is correct
32 Correct 33 ms 36224 KB Output is correct
33 Correct 22 ms 35704 KB Output is correct
34 Correct 26 ms 35968 KB Output is correct
35 Correct 23 ms 35832 KB Output is correct
36 Correct 24 ms 35968 KB Output is correct
37 Correct 27 ms 36096 KB Output is correct
38 Correct 25 ms 35584 KB Output is correct
39 Correct 29 ms 36224 KB Output is correct
40 Correct 23 ms 36088 KB Output is correct
41 Correct 23 ms 35960 KB Output is correct
42 Correct 27 ms 35968 KB Output is correct
43 Correct 22 ms 36224 KB Output is correct
44 Correct 22 ms 35584 KB Output is correct
45 Correct 26 ms 36096 KB Output is correct
46 Correct 29 ms 36096 KB Output is correct
47 Correct 25 ms 35584 KB Output is correct
48 Correct 27 ms 35960 KB Output is correct
49 Correct 23 ms 35840 KB Output is correct
50 Correct 25 ms 35960 KB Output is correct
51 Correct 26 ms 35968 KB Output is correct
52 Correct 23 ms 35968 KB Output is correct
53 Correct 26 ms 35968 KB Output is correct
54 Correct 28 ms 36096 KB Output is correct
55 Correct 21 ms 35584 KB Output is correct
56 Correct 98 ms 47220 KB Output is correct
57 Correct 160 ms 53624 KB Output is correct
58 Correct 23 ms 35968 KB Output is correct
59 Correct 21 ms 35584 KB Output is correct
60 Correct 21 ms 35584 KB Output is correct
61 Correct 123 ms 46968 KB Output is correct
62 Correct 26 ms 35968 KB Output is correct
63 Correct 200 ms 57208 KB Output is correct
64 Correct 162 ms 52344 KB Output is correct
65 Correct 25 ms 35968 KB Output is correct
66 Correct 125 ms 47992 KB Output is correct
67 Correct 26 ms 35968 KB Output is correct
68 Correct 23 ms 35968 KB Output is correct
69 Correct 102 ms 47216 KB Output is correct
70 Correct 23 ms 36216 KB Output is correct
71 Correct 115 ms 46840 KB Output is correct
72 Correct 169 ms 50296 KB Output is correct
73 Correct 25 ms 35584 KB Output is correct
74 Correct 152 ms 57336 KB Output is correct
75 Correct 168 ms 53368 KB Output is correct
76 Correct 24 ms 35968 KB Output is correct
77 Correct 27 ms 36096 KB Output is correct
78 Correct 105 ms 45804 KB Output is correct
79 Correct 102 ms 42608 KB Output is correct
80 Correct 32 ms 35964 KB Output is correct
81 Correct 24 ms 35968 KB Output is correct
82 Correct 22 ms 35584 KB Output is correct
83 Correct 21 ms 35584 KB Output is correct
84 Correct 26 ms 35960 KB Output is correct
85 Correct 157 ms 50808 KB Output is correct
86 Correct 23 ms 35968 KB Output is correct
87 Correct 106 ms 46968 KB Output is correct
88 Correct 25 ms 35584 KB Output is correct
89 Correct 110 ms 46968 KB Output is correct
90 Correct 128 ms 47480 KB Output is correct
91 Correct 115 ms 44152 KB Output is correct
92 Correct 100 ms 47216 KB Output is correct
93 Correct 26 ms 35968 KB Output is correct
94 Correct 21 ms 35584 KB Output is correct
95 Correct 116 ms 45436 KB Output is correct
96 Correct 124 ms 52088 KB Output is correct
97 Correct 27 ms 36088 KB Output is correct
98 Correct 24 ms 35584 KB Output is correct
99 Correct 138 ms 47344 KB Output is correct
100 Correct 26 ms 35968 KB Output is correct
101 Correct 111 ms 46712 KB Output is correct
102 Correct 137 ms 49528 KB Output is correct
103 Correct 23 ms 35840 KB Output is correct
104 Correct 23 ms 35968 KB Output is correct
105 Correct 26 ms 35968 KB Output is correct
106 Correct 24 ms 35968 KB Output is correct
107 Correct 27 ms 35960 KB Output is correct
108 Correct 972 ms 97452 KB Output is correct
109 Correct 801 ms 118120 KB Output is correct
110 Correct 808 ms 133884 KB Output is correct
111 Correct 800 ms 126200 KB Output is correct
112 Correct 829 ms 109812 KB Output is correct
113 Correct 589 ms 84700 KB Output is correct
114 Correct 663 ms 92792 KB Output is correct
115 Correct 670 ms 92740 KB Output is correct
116 Correct 1211 ms 153544 KB Output is correct
117 Correct 708 ms 79500 KB Output is correct
118 Correct 702 ms 92492 KB Output is correct
119 Correct 712 ms 79480 KB Output is correct
120 Correct 809 ms 107976 KB Output is correct
121 Correct 740 ms 79608 KB Output is correct
122 Correct 1465 ms 167928 KB Output is correct
123 Correct 508 ms 80996 KB Output is correct
124 Correct 774 ms 86752 KB Output is correct