萌新求助,最后2个点一直RE
查看原帖
萌新求助,最后2个点一直RE
158846
xixike楼主2021/7/27 12:58
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>

#define N 100010

using namespace std;

struct node{
	int v, nxt;
}edge[N << 2];
int head[N], tot;
int n, m;
int nex[N], last[N], t[N], vis[N], cov[N];
int cnt;
queue <int> q;

void add(int x, int y){
	edge[++tot].v = y;
	edge[tot].nxt = head[x];
	head[x] = tot;
}

void prework(){
	nex[0] = 1;
	for(int i = 1; i < n; i++){
		last[i + 1] = i;
		nex[i] = i + 1;
	}
}
void del(int x){
	nex[last[x]] = nex[x];
	last[nex[x]] = last[x];
}

int main(){
	scanf("%d%d", &n, &m);
	int x, y;
	for(int i = 1; i <= m; i++){
		scanf("%d%d", &x, &y);
		add(x, y);
		add(y, x);
	}
	prework();
	for(int i = 1; i <= n ; i++){
		if(!vis[i]){
			t[++cnt] = 1;
			vis[i] = 1;
			q.push(i);
			del(i);
			while(!q.empty()){
				int x = q.front();
				q.pop();
				for(int j = head[x]; j; j = edge[j].nxt)
					if(!vis[edge[j].v])
						cov[edge[j].v] = 1;
				for(int j = nex[0]; j; j = nex[j]){
					if(!cov[j]){
						vis[j] = 1;
						t[cnt]++;
						del(j);
						q.push(j);
					}else cov[j] = 0;
				}
			}
		}
	}
	sort(t + 1, t + 1 + cnt);
	printf("%d\n", cnt);
	for(int i = 1; i <= cnt; i++)
		printf("%d ", t[i]);
	printf("\n");
	return 0;
}
2021/7/27 12:58
加载中...