HTML & JS
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define f(i, x, y) for(int i = x; i < y; i++)
#define all(X) X.begin(), X.end()
#define int long long
#define key pair<int, int>
#define ff first
#define ss second
int mod = 1e9 + 7;
const int inf = 1e15;
using namespace std;

vector<int> g[200001];
set<int> s; int idx = 1; int res[200001];
vector<int> v;
int n, m; 
bool vis[200001], tvis[200001];

void dfs(int src)
{
    //cout<<src<<" ";
    vector<int> t;
    for(int i : g[src])
    {
        t.pb(i); vis[i] = 1;        
    }
    for(auto it = s.begin(); it != s.end();)
    {
        if(!vis[(*it)])
        {
            if(!tvis[(*it)])
            {
                tvis[(*it)] = 1; v.pb(*it);
            }
            s.erase(it++); 
        }
        else it++;
    }
    for(int i : t) vis[i] = 0;
    
}

main()
{
    cin>>n>>m;
    f(i, 1, m+1)
    {
        int s, d; cin>>s>>d; g[s].pb(d); g[d].pb(s);
    }
    memset(vis, 0, sizeof(vis));
    memset(tvis, 0, sizeof(tvis));
    f(i, 1, n+1) s.insert(i);
    f(i, 1, n+1)
    {
        if(s.find(i) != s.end())
        {
            dfs(i);
            f(j, 1, v.size())
                dfs(v[j]);
            for(int j : v) tvis[j] = 0;    
            res[idx] = v.size();
            v.clear(); idx++;    
        }
    }
    cout<<idx-1<<"\n";
    f(i, 1, idx) cout<<res[i]<<" ";
}

Generated URL:

Output:

1
6 
Report Bug