forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchool Excursion.cpp
More file actions
44 lines (36 loc) · 812 Bytes
/
School Excursion.cpp
File metadata and controls
44 lines (36 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5+1;
int N, M, a, b, ds[maxN];
multiset<int> sizes;
bitset<maxN> dp;
int find(int u){
if(ds[u] < 0) return u;
ds[u] = find(ds[u]);
return ds[u];
}
bool merge(int u, int v){
u = find(u); v = find(v);
if(u == v) return false;
if(ds[u] < ds[v]) swap(u, v);
ds[v] += ds[u];
ds[u] = v;
return true;
}
int main(){
scanf("%d %d", &N, &M);
fill(ds+1, ds+N+1, -1);
for(int i = 0; i < M; i++){
scanf("%d %d", &a, &b);
merge(a, b);
}
for(int i = 1; i <= N; i++)
if(find(i) == i)
sizes.insert(-ds[i]);
dp[0] = 1;
for(int sz : sizes)
dp |= (dp<<sz);
for(int i = 1; i <= N; i++)
printf("%d", dp[i] ? 1 : 0);
printf("\n");
}