求一个图中割点数目(模板题)
主要是输入的细节,处理好回车结束符和最末尾的0就好了
#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
using namespace std;
const int maxn=1010;
const int maxm=10010;
struct Edge{
int to,nxt;
bool cut;
}edge[maxm];
int head[maxn],tot;
int Low[maxn],DFN[maxn],Stack[maxn];
int Index,top,block;
bool Instack[maxn];
bool cut[maxn];
int add_block[maxn];
int bridge;
void addedge(int u,int v){
edge[tot].to=v;
edge[tot].nxt=head[u];
edge[tot].cut=false;
head[u]=tot++;
}
void Tarjan(int u,int pre){
int v;
Low[u]=DFN[u]=++Index;
Stack[top++]=u;
Instack[u]=true;
int son=0;
int pre_cnt=0;
for(int i=head[u];i!=-1;i=edge[i].nxt){
v=edge[i].to;
if (v==pre&&pre_cnt==0){
pre_cnt++;
continue;
}
if (!DFN[v]){
son++;
Tarjan(v,u);
if (Low[u]>Low[v]) Low[u]=Low[v];
if (Low[v]>DFN[u]){
bridge++;
edge[i].cut=true;
edge[i^1].cut=true;
}
if (u!=pre&&Low[v]>=DFN[u]){
cut[u]=true;
add_block[u]++;
}
}
else if (Low[u]>DFN[v])
Low[u]=DFN[v];
}
if (u==pre&&son>1) cut[u]=true;
if (u==pre) add_block[u]=son-1;
Instack[u]=false;
top--;
}
void init(){
memset(DFN,0,sizeof(DFN));
memset(Instack,0,sizeof(Instack));
memset(add_block,0,sizeof(add_block));
memset(cut,false,sizeof(cut));
memset(head,-1,sizeof(head));
Index=top=0;tot=0;
}
int main(){
//freopen("input.txt","r",stdin);
int u,v,n;
while(scanf("%d",&n),n){
init();
while(scanf("%d",&u),u){
while(getchar()!='\n'){
scanf("%d",&v);
addedge(u,v);
addedge(v,u);
}
}
int cnt=0;
for(int i=1;i<=n;i++)
if (!DFN[i]){
Tarjan(i,i);
cnt++;
}
int num=0;
for(int i=1;i<=n;i++)
if (cut[i]) num++;
printf("%d\n",num);
}
return 0;
}