poj1144Network (求割点模板题)
题意:给出一个无向图,求出割点的个数
code:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #define maxn 100005 using namespace std; vector<int> G[maxn]; int dfn[maxn],vis[maxn],low[maxn]; bool isGedian[maxn]; //标记该点是否是割点 int fa[maxn]; //父亲节点 int depth; int root; int ans; void init(){ memset(isGedian,0,sizeof(isGedian)); memset(fa,0,sizeof(fa)); memset(vis,0,sizeof(vis)); for(int i = 0;i < maxn;i ++){ G[i].clear(); } } void dfs(int cur,int depth){ int cnt = 0; //记录当前节点的孩子个数 low[cur] = dfn[cur] = depth; vis[cur] = true; for(int i = 0;i < G[cur].size();i ++){ int v = G[cur][i]; if(!vis[v] && v != fa[cur]){ fa[v] = cur; dfs(v,depth+1); cnt ++; low[cur] = min(low[cur],low[v]); if(low[v] >= dfn[cur] && root!= cur){ if(!isGedian[cur]){ ans ++; } isGedian[cur] = true; } } else{ low[cur] = min(low[cur],dfn[v]); } } if(root == cur && cnt > 1){ if(!isGedian[cur]){ ans ++; } isGedian[cur] = true; } } int main() { int n,a,b; while(cin >> n && n){ ans = 0; init(); while(cin >> a && a){ while(getchar() != '\n') { cin >> b; G[a].push_back(b); G[b].push_back(a); } } root = 1; dfs(1,0); cout << ans << endl; } return 0; }
posted on 2016-07-30 17:38 Tob's_the_top 阅读(161) 评论(0) 编辑 收藏 举报