POJ 1144 Network 无向图求割点
题意:就是给你一些点,某些点之间有边。求有多少个点是割点。
思路:模板题目了,直接用无向图求个点模板就可以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;
}
补充:软件开发 , C++ ,