1144 - Network

我觉得我还是别学点双了。
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define rep(i,j,k) for(i=j;i<=k;++i)
#define per(i,j,k) for(i=j;i>=k;--i)
#define G getchar()
#define LL long long
#define pii pair
#define mkp make_pair
#define X first
#define Y second
const int N=105;
int n,ans;bool flg[N];
int he[N],ne[N<<1],to[N<<1],tot;
int dfn[N],low[N],id;bool ic[N];
void add(int x,int y){
	to[++tot]=y;ne[tot]=he[x];he[x]=tot;
}
void tarjan(int x,int fa){
	dfn[x]=low[x]=++id;
	int i,y,cnt=0;
	for(i=he[x];i;i=ne[i])if((y=to[i])!=fa){
		if(!dfn[y]){
			tarjan(y,x);low[x]=min(low[x],low[y]);
			if(low[y]>dfn[x]){
				if(x>1)ic[x]=1;
				++cnt;
			}
			
		}
		else low[x]=min(low[x],dfn[y]);
	}
	if(x==1)ic[1]=cnt>1;
}
int main(){
	int x,y,i;
	while(1){
		scanf("%d",&n);
		if(!n)break;
		rep(i,1,n)he[i]=flg[i]=ic[i]=dfn[i]=0;
		tot=1;id=ans=0;
		while(1){
			scanf("%d",&x);
			if(!x)break;
			while(getchar()!='\n'){
				scanf("%d",&y);
				add(x,y);add(y,x);
			}
		}
		tarjan(1,0);
		rep(i,1,n)ans+=ic[i];
		printf("%d\n",ans);
	}
	return 0;
}

你可能感兴趣的:(*unsolved,POJ)