# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
236394 | 2020-06-01T19:12:49 Z | Charis02 | Mergers (JOI19_mergers) | C++14 | 3000 ms | 22248 KB |
#include<iostream> #include<stdio.h> #include<vector> #include<cmath> #include<queue> #include<string.h> #include<map> #include<set> #include<algorithm> #define ll long long #define pi pair < ll,ll > #define mp(a,b) make_pair(a,b) #define mid (low+high)/2 #define rep(i,a,b) for(int i = a;i < b;i++) #define N 300004 #define INF 1e9+7 using namespace std; struct dsu{ ll par[N]; ll sz[N]; ll col[N]; ll get(ll x) { return (par[x] == x) ? x : get(par[x]); } void join(ll x,ll y) { ll fx=get(x); ll fy = get(y); if(fx==fy) return; if(sz[fx]<sz[fy]) swap(fx,fy); sz[fx]+=sz[fy]; par[fy]=fx; } ll get_col(ll x) { return col[get(x)]; } }; ll n,k,state[N],answer; ll ar[N]; vector < ll > graph[N]; ll cnt = 1; dsu comp; vector < ll > fin[N]; ll deg[N]; bool check(ll cur,ll par,ll col) { bool res = false; if(state[cur] == col) res = true; rep(i,0,graph[cur].size()) { ll v =graph[cur][i]; if(v==par) continue; if(check(v,cur,col)) { comp.join(cur,v); res=true; } } return res; } void process_paths() { rep(i,1,n+1) { if(ar[state[i]] == 0) ar[state[i]] = cnt++; check(i,i,state[i]); } rep(i,1,n+1) { if(comp.get_col(i) == 0) comp.col[comp.get(i)] = cnt++; ar[i] = comp.get_col(i); } rep(i,1,n+1) { rep(j,0,graph[i].size()) { ll v = graph[i][j]; if(ar[i] != ar[v]) { deg[ar[i]]++; fin[ar[i]].push_back(ar[v]); } } } ll leaves = 0; rep(i,1,cnt) { if(deg[i] == 1) leaves++; } answer = (leaves+1)/2; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; rep(i,0,n-1) { ll a,b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } rep(i,1,n+1) { comp.par[i]=i; comp.sz[i]=1; cin >> state[i]; } process_paths(); cout << answer << endl; return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 12 ms | 14464 KB | Output is correct |
2 | Correct | 12 ms | 14464 KB | Output is correct |
3 | Correct | 12 ms | 14464 KB | Output is correct |
4 | Correct | 12 ms | 14464 KB | Output is correct |
5 | Correct | 13 ms | 14464 KB | Output is correct |
6 | Correct | 19 ms | 14464 KB | Output is correct |
7 | Correct | 12 ms | 14464 KB | Output is correct |
8 | Correct | 12 ms | 14464 KB | Output is correct |
9 | Correct | 12 ms | 14464 KB | Output is correct |
10 | Correct | 12 ms | 14464 KB | Output is correct |
11 | Correct | 12 ms | 14464 KB | Output is correct |
12 | Correct | 12 ms | 14592 KB | Output is correct |
13 | Correct | 12 ms | 14464 KB | Output is correct |
14 | Correct | 12 ms | 14464 KB | Output is correct |
15 | Correct | 12 ms | 14464 KB | Output is correct |
16 | Correct | 12 ms | 14464 KB | Output is correct |
17 | Correct | 12 ms | 14464 KB | Output is correct |
18 | Correct | 12 ms | 14464 KB | Output is correct |
19 | Correct | 12 ms | 14464 KB | Output is correct |
20 | Correct | 12 ms | 14464 KB | Output is correct |
21 | Correct | 12 ms | 14464 KB | Output is correct |
22 | Correct | 12 ms | 14464 KB | Output is correct |
23 | Correct | 12 ms | 14464 KB | Output is correct |
24 | Correct | 12 ms | 14464 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 12 ms | 14464 KB | Output is correct |
2 | Correct | 12 ms | 14464 KB | Output is correct |
3 | Correct | 12 ms | 14464 KB | Output is correct |
4 | Correct | 12 ms | 14464 KB | Output is correct |
5 | Correct | 13 ms | 14464 KB | Output is correct |
6 | Correct | 19 ms | 14464 KB | Output is correct |
7 | Correct | 12 ms | 14464 KB | Output is correct |
8 | Correct | 12 ms | 14464 KB | Output is correct |
9 | Correct | 12 ms | 14464 KB | Output is correct |
10 | Correct | 12 ms | 14464 KB | Output is correct |
11 | Correct | 12 ms | 14464 KB | Output is correct |
12 | Correct | 12 ms | 14592 KB | Output is correct |
13 | Correct | 12 ms | 14464 KB | Output is correct |
14 | Correct | 12 ms | 14464 KB | Output is correct |
15 | Correct | 12 ms | 14464 KB | Output is correct |
16 | Correct | 12 ms | 14464 KB | Output is correct |
17 | Correct | 12 ms | 14464 KB | Output is correct |
18 | Correct | 12 ms | 14464 KB | Output is correct |
19 | Correct | 12 ms | 14464 KB | Output is correct |
20 | Correct | 12 ms | 14464 KB | Output is correct |
21 | Correct | 12 ms | 14464 KB | Output is correct |
22 | Correct | 12 ms | 14464 KB | Output is correct |
23 | Correct | 12 ms | 14464 KB | Output is correct |
24 | Correct | 12 ms | 14464 KB | Output is correct |
25 | Correct | 13 ms | 14464 KB | Output is correct |
26 | Correct | 178 ms | 14840 KB | Output is correct |
27 | Correct | 216 ms | 14720 KB | Output is correct |
28 | Correct | 172 ms | 14968 KB | Output is correct |
29 | Correct | 160 ms | 14968 KB | Output is correct |
30 | Correct | 167 ms | 14720 KB | Output is correct |
31 | Correct | 12 ms | 14464 KB | Output is correct |
32 | Correct | 222 ms | 14976 KB | Output is correct |
33 | Correct | 12 ms | 14464 KB | Output is correct |
34 | Correct | 71 ms | 14720 KB | Output is correct |
35 | Correct | 161 ms | 14896 KB | Output is correct |
36 | Correct | 167 ms | 14732 KB | Output is correct |
37 | Correct | 166 ms | 14756 KB | Output is correct |
38 | Correct | 12 ms | 14464 KB | Output is correct |
39 | Correct | 160 ms | 14720 KB | Output is correct |
40 | Correct | 275 ms | 14720 KB | Output is correct |
41 | Correct | 180 ms | 14720 KB | Output is correct |
42 | Correct | 71 ms | 14840 KB | Output is correct |
43 | Correct | 237 ms | 14968 KB | Output is correct |
44 | Correct | 12 ms | 14464 KB | Output is correct |
45 | Correct | 211 ms | 14808 KB | Output is correct |
46 | Correct | 163 ms | 14720 KB | Output is correct |
47 | Correct | 12 ms | 14464 KB | Output is correct |
48 | Correct | 173 ms | 14720 KB | Output is correct |
49 | Correct | 71 ms | 14968 KB | Output is correct |
50 | Correct | 172 ms | 14968 KB | Output is correct |
51 | Correct | 183 ms | 14848 KB | Output is correct |
52 | Correct | 72 ms | 14720 KB | Output is correct |
53 | Correct | 70 ms | 14720 KB | Output is correct |
54 | Correct | 189 ms | 14720 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 12 ms | 14464 KB | Output is correct |
2 | Correct | 12 ms | 14464 KB | Output is correct |
3 | Correct | 12 ms | 14464 KB | Output is correct |
4 | Correct | 12 ms | 14464 KB | Output is correct |
5 | Correct | 13 ms | 14464 KB | Output is correct |
6 | Correct | 19 ms | 14464 KB | Output is correct |
7 | Correct | 12 ms | 14464 KB | Output is correct |
8 | Correct | 12 ms | 14464 KB | Output is correct |
9 | Correct | 12 ms | 14464 KB | Output is correct |
10 | Correct | 12 ms | 14464 KB | Output is correct |
11 | Correct | 12 ms | 14464 KB | Output is correct |
12 | Correct | 12 ms | 14592 KB | Output is correct |
13 | Correct | 12 ms | 14464 KB | Output is correct |
14 | Correct | 12 ms | 14464 KB | Output is correct |
15 | Correct | 12 ms | 14464 KB | Output is correct |
16 | Correct | 12 ms | 14464 KB | Output is correct |
17 | Correct | 12 ms | 14464 KB | Output is correct |
18 | Correct | 12 ms | 14464 KB | Output is correct |
19 | Correct | 12 ms | 14464 KB | Output is correct |
20 | Correct | 12 ms | 14464 KB | Output is correct |
21 | Correct | 12 ms | 14464 KB | Output is correct |
22 | Correct | 12 ms | 14464 KB | Output is correct |
23 | Correct | 12 ms | 14464 KB | Output is correct |
24 | Correct | 12 ms | 14464 KB | Output is correct |
25 | Correct | 12 ms | 14464 KB | Output is correct |
26 | Execution timed out | 3078 ms | 22124 KB | Time limit exceeded |
27 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Execution timed out | 3067 ms | 22248 KB | Time limit exceeded |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 12 ms | 14464 KB | Output is correct |
2 | Correct | 12 ms | 14464 KB | Output is correct |
3 | Correct | 12 ms | 14464 KB | Output is correct |
4 | Correct | 12 ms | 14464 KB | Output is correct |
5 | Correct | 13 ms | 14464 KB | Output is correct |
6 | Correct | 19 ms | 14464 KB | Output is correct |
7 | Correct | 12 ms | 14464 KB | Output is correct |
8 | Correct | 12 ms | 14464 KB | Output is correct |
9 | Correct | 12 ms | 14464 KB | Output is correct |
10 | Correct | 12 ms | 14464 KB | Output is correct |
11 | Correct | 12 ms | 14464 KB | Output is correct |
12 | Correct | 12 ms | 14592 KB | Output is correct |
13 | Correct | 12 ms | 14464 KB | Output is correct |
14 | Correct | 12 ms | 14464 KB | Output is correct |
15 | Correct | 12 ms | 14464 KB | Output is correct |
16 | Correct | 12 ms | 14464 KB | Output is correct |
17 | Correct | 12 ms | 14464 KB | Output is correct |
18 | Correct | 12 ms | 14464 KB | Output is correct |
19 | Correct | 12 ms | 14464 KB | Output is correct |
20 | Correct | 12 ms | 14464 KB | Output is correct |
21 | Correct | 12 ms | 14464 KB | Output is correct |
22 | Correct | 12 ms | 14464 KB | Output is correct |
23 | Correct | 12 ms | 14464 KB | Output is correct |
24 | Correct | 12 ms | 14464 KB | Output is correct |
25 | Correct | 13 ms | 14464 KB | Output is correct |
26 | Correct | 178 ms | 14840 KB | Output is correct |
27 | Correct | 216 ms | 14720 KB | Output is correct |
28 | Correct | 172 ms | 14968 KB | Output is correct |
29 | Correct | 160 ms | 14968 KB | Output is correct |
30 | Correct | 167 ms | 14720 KB | Output is correct |
31 | Correct | 12 ms | 14464 KB | Output is correct |
32 | Correct | 222 ms | 14976 KB | Output is correct |
33 | Correct | 12 ms | 14464 KB | Output is correct |
34 | Correct | 71 ms | 14720 KB | Output is correct |
35 | Correct | 161 ms | 14896 KB | Output is correct |
36 | Correct | 167 ms | 14732 KB | Output is correct |
37 | Correct | 166 ms | 14756 KB | Output is correct |
38 | Correct | 12 ms | 14464 KB | Output is correct |
39 | Correct | 160 ms | 14720 KB | Output is correct |
40 | Correct | 275 ms | 14720 KB | Output is correct |
41 | Correct | 180 ms | 14720 KB | Output is correct |
42 | Correct | 71 ms | 14840 KB | Output is correct |
43 | Correct | 237 ms | 14968 KB | Output is correct |
44 | Correct | 12 ms | 14464 KB | Output is correct |
45 | Correct | 211 ms | 14808 KB | Output is correct |
46 | Correct | 163 ms | 14720 KB | Output is correct |
47 | Correct | 12 ms | 14464 KB | Output is correct |
48 | Correct | 173 ms | 14720 KB | Output is correct |
49 | Correct | 71 ms | 14968 KB | Output is correct |
50 | Correct | 172 ms | 14968 KB | Output is correct |
51 | Correct | 183 ms | 14848 KB | Output is correct |
52 | Correct | 72 ms | 14720 KB | Output is correct |
53 | Correct | 70 ms | 14720 KB | Output is correct |
54 | Correct | 189 ms | 14720 KB | Output is correct |
55 | Correct | 12 ms | 14464 KB | Output is correct |
56 | Execution timed out | 3078 ms | 22124 KB | Time limit exceeded |
57 | Halted | 0 ms | 0 KB | - |