題意:就是給你一些點,某些點之間有邊。求有多少個點是割點。
思路:模板題目了,直接用無向圖求個點模板就可以ac。需要注意的是輸入,輸入有點麻煩。以換行結尾可以寫成while(getchar() != '\n'),其他沒什麼難度了。
代碼:
[cpp]
#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
using namespace std;
#define CLR(arr,val) memset(arr,val,sizeof(arr))
const int N = 110;
int low[N],dfn[N],vis[N],numblock[N];
vector<int> vv[N];
int numcnt,timeorder,numpoint,numson;
void init(){
CLR(low,0);
CLR(dfn,0);
CLR(vis,0);
CLR(vv,0);
CLR(numblock,0);
numson = 0;
numcnt = 0;
timeorder = 0;
}
int min(int a,int b){
return a < b ? a : b;
}
void dfs(int x){
timeorder++;
low[x] = dfn[x] = timeorder;
vis[x] = 1;
for(int i = 0; i < vv[x].size(); ++i){
int y = vv[x][i];
if(!vis[y]){
dfs(y);
low[x] = min(low[x],low[y]);
if(low[y] >= dfn[x] && x != 1){
numblock[x]++;
}
else if(x == 1)
numson++;
}
else
low[x] = min(low[x],dfn[y]);
}
}
int main(){
//freopen("1.txt","r",stdin);
while(scanf("%d",&numpoint) && numpoint){
init();
int x,y;
char ch;
while(scanf("%d",&x) && x){
while(getchar() != '\n'){
scanf("%d",&y);
vv[x].push_back(y);
vv[y].push_back(x);
}
}
dfs(1);
for(int i = 1; i <= numpoint; ++i)
if(numblock[i])
numcnt++;
if(numson > 1)
numcnt++;
printf("%d\n",numcnt);
}
return 0;
}