Network UVA - 315 kuangbin带你飞 专题九 连通图 图论 tarjan

版权声明: https://blog.csdn.net/weixin_40959045/article/details/80715841

题意

求割点

tarjan 模板题

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX_V 110
#define MAX_E 10010
#define mem(a) memset((a),0,sizeof(a))
struct edge{
  int to;
  int next;
};
edge es[MAX_E];
int head[MAX_V],len;
int V;
int dfn[MAX_V],low[MAX_V];
int ord;
int instack[MAX_V];
int vcut[MAX_V];
int root;
void addedge(int from,int to)
{
  es[len].to = to;
  es[len].next = head[from];
  head[from] = len++;
}
void tarjan(int u,int fa){
  dfn[u] = low[u] = ++ord;
  instack[u] = 1;
  int cnt =0;
  for (int i = head[u];i != -1;i = es[i].next){
    int v = es[i].to;
    if (v == fa){
      continue;
    }
    if (dfn[v] == -1){
      tarjan(v,u);
      cnt++;
      if (low[u] > low[v]){
        low[u] = low[v];
      }
      if (root == u && cnt > 1){
        vcut[u] = 1;
      } else if (root != u && low[v] >= dfn[u]){
        vcut[u] = 1;
      }
    } else if (instack[v]){
      if(dfn[v] < low[u]){
        low[u] = dfn[v];
      }||
    }
  }
}
void init(){
  len = ord = 0;
  mem(low);
  mem(instack);
  mem(vcut);
  memset(dfn,-1,sizeof dfn);
  memset(head,-1,sizeof head);
}
void solve(){
  root = 1;
  tarjan(1,-1);
  int ans = 0;
  for (int i = 1;i<=V;i++){
    if (vcut[i]) ans++;
  }
  printf("%d\n",ans);
}
int main()
{
  int u,v;
  while(~scanf("%d",&V),V){
    init();
    while (scanf("%d", &u), u)  {
      while (getchar() != '\n')  {
        scanf("%d", &v);
        addedge (u, v);
        addedge (v, u);
      }
    }
    solve();

  }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40959045/article/details/80715841