-
Notifications
You must be signed in to change notification settings - Fork 156
/
1706 - School Excursion.cpp
66 lines (62 loc) · 1.35 KB
/
1706 - School Excursion.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Problem Name: School Excursion
Problem Link: https://cses.fi/problemset/task/1706
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mxN = 1e5+5;
int c = 0;
vector<int> adj[mxN];
bool vis[mxN];
void dfs(int s){
if (vis[s]) return;
vis[s]=1; c++;
for (auto i: adj[s]) dfs(i);
}
int dp[mxN];
map<int,int> mp;
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m; cin>>n>>m;
for (int i = 0; i < m; i++) {
int x,y; cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int i = 0; i < n; i++) {
if (!vis[i+1]) {
c = 0;
dfs(i+1);
mp[c]++;
}
}
vector<int> v;
for (auto [i, c]: mp) {
int x = 0;
while(1<<x < c) {
v.push_back((1<<x)*i);
c -= 1<<x;
x++;
}
if (c)
v.push_back(c*i);
}
m = v.size();
dp[0] = 1;
for (int i = 0; i < m; i++) {
for (int j = n; j >= 1; j--) {
if (j >= v[i]) {
dp[j] = max(dp[j-v[i]], dp[j]);
}
}
}
for (int i = 1; i <= n; i++)
cout<<dp[i];
}