uva 10765 (求去掉每一个一个点 可以得到联通块的个数 )

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
	int to,next;
}e[100000];
int head[10000],pre[10000],low[10000],d[10000],ans[10000];
int cnt,dfs_clock;
bool cmp(int a,int b){
	if(ans[a]==ans[b])
	return a<b;
	else
	return ans[a]>ans[b];
}
void add_edge(int from,int to){
	e[cnt].to=to;
	e[cnt].next=head[from];
	head[from]=cnt++;
}
int dfs(int u,int v){
	low[u]=pre[u]=++dfs_clock;
	int num=0;
	for(int i=head[u];i!=-1;i=e[i].next){
		int to=e[i].to;
		if(to==v)
		continue;
		if(!pre[to]){
			num++;
			int t=dfs(to,u);
			low[u]=min(low[u],t);
			if(low[to]>=pre[u])
			ans[u]++;
		}
		else if(pre[to]<pre[u] && to != v){
			low[u]=min(low[u],pre[to]);
		}
	}
	if(v < 0 && num == 1) ans[u]=1;
	return low[u];
}
int main(){
	int n,m,a,b;
    while(scanf("%d%d",&n,&m) && (n+m)){
    	memset(head,-1,sizeof(head));
    	cnt=0;
    	while(scanf("%d%d",&a,&b) && (a!=-1 || b!=-1)){
    		add_edge(a,b);
    		add_edge(b,a);
		}
		dfs_clock=0;

		memset(pre,0,sizeof(pre));
		for(int i=0;i<n;i++){
			d[i]=i;
			ans[i]=1;
		}
		dfs(0,-1);
		sort(d,d+n,cmp);
		for(int i=0;i<m;i++)
		printf("%d %d\n",d[i],ans[d[i]]);
		printf("\n");
	}     	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值