Network UVA - 315
题意&&思路:
就是求割点。
AC
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <sstream>
#define mst(x,a) memset(x,a,sizeof(x))
#define fzhead EDGE(int _to, int _next)
#define fzbody to(_to), next(_next)
#define For(i,x,y) for(int i=(x); i<=(y); i++)
using namespace std;
const string lim="0";
const int maxm=1e4+10;
const int maxn=100+10;
int n;
struct EDGE{
int to,next;
EDGE(){}
fzhead:fzbody{}
}e[maxm];
int dfs_clock,bcc_cnt,root,tot;
int head[maxn],vis[maxn],low[maxn],dfn[maxn],cut[maxn];
stack<int>s;
void init(){
root = tot = dfs_clock = bcc_cnt = 0;
mst(head,-1);
mst(vis,0);
mst(low,0);
mst(dfn,0);
mst(cut,0);
while(!s.empty())s.pop();
}
void add(int bg, int to){
e[tot]=EDGE(to,head[bg]);
head[bg]=tot++;
}
void tarjan(int u){
vis[u]=1;
low[u]=dfn[u]=++dfs_clock;
int flag=0;
s.push(u);
for(int i=head[u]; i!=-1; i=e[i].next){
int v=e[i].to;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u]&&(u!=root||++flag>1))cut[u]=1;
}else low[u]=min(low[u],dfn[v]);
}
///这里就不用求强联通块了
}
int main()
{
while(cin>>n,n){
getchar();
init(); tot=2; root=1;
string line;
while(getline(cin,line)&&line!=lim){
stringstream ss(line);
int u,v;
ss>>u;
///cout<<"u----"<<u<<endl;
while(ss>>v){
///cout<<"v----"<<v<<endl;
///cout<<v<<endl;//add()
add(u,v);
add(v,u);
}
}
For(i,1,n){
if(!dfn[i])dfs_clock=0,root=i,tarjan(i);
}
int ans=0;
For(i,1,n)if(cut[i])ans++;
// cout<<"ans-------";
cout<<ans<<endl;
}
return 0;
}
/*
5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
*/