首页 > 代码库 > [POJ2117]Electricity(连通分量,割点)

[POJ2117]Electricity(连通分量,割点)

题目链接:http://poj.org/problem?id=2117

题意:求去掉割点后的最大连通分支个数。

kuangbin的板子。

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <iomanip>
  4 #include <cstring>
  5 #include <climits>
  6 #include <complex>
  7 #include <cassert>
  8 #include <cstdio>
  9 #include <bitset>
 10 #include <vector>
 11 #include <deque>
 12 #include <queue>
 13 #include <stack>
 14 #include <ctime>
 15 #include <set>
 16 #include <map>
 17 #include <cmath>
 18 using namespace std;
 19 
 20 const int maxn = 10050;
 21 typedef struct Edge {
 22     int u;
 23     int v;
 24     int next;
 25     Edge() { next = -1; }
 26 }Edge;
 27 
 28 int head[maxn], ecnt;
 29 Edge edge[maxn<<2];
 30 int n, m;
 31 int del;
 32 
 33 int dindex;
 34 int dfn[maxn], low[maxn];
 35 int stk[maxn], top;
 36 int belong[maxn];
 37 bool instk[maxn];
 38 
 39 void init() {
 40     memset(edge, 0, sizeof(edge));
 41     memset(head, -1, sizeof(head));
 42     memset(instk, 0, sizeof(instk));
 43     memset(dfn, 0, sizeof(dfn));
 44     memset(low, 0, sizeof(low));
 45     memset(belong, 0, sizeof(belong));
 46     ecnt = top = dindex = 0;
 47 }
 48 
 49 void adde(int uu, int vv) {
 50     edge[ecnt].u = uu;
 51     edge[ecnt].v = vv;
 52     edge[ecnt].next = head[uu];
 53     head[uu] = ecnt++;
 54 }
 55 
 56 void tarjan(int u, int pre) {
 57     int v;
 58     dfn[u] = low[u] = ++dindex;
 59     stk[top++] = u;
 60     instk[u] = 1;
 61     int son = 0;
 62     for(int i = head[u]; ~i; i=edge[i].next) {
 63         v = edge[i].v;
 64         if(v == pre) continue;
 65         // if(v == del || v == pre) continue;
 66         if(!dfn[v]) {
 67             son++;
 68             tarjan(v, u);
 69             low[u] = min(low[u], low[v]);
 70             if(u != pre && low[v] >= dfn[u]) belong[u]++;
 71         }
 72         else low[u] = min(low[u], dfn[v]);
 73     }
 74     if(u == pre) belong[u] = son - 1;
 75     instk[u] = 0;
 76     top--;
 77 }
 78 
 79 
 80 int main() {
 81     // freopen("in", "r", stdin);
 82     int u, v;
 83     while(~scanf("%d%d",&n,&m) && n + m) {
 84         init();
 85         for(int i = 0; i < m; i++) {
 86             scanf("%d %d", &u, &v);
 87             u++; v++;
 88             adde(u, v); adde(v, u);
 89         }
 90         int ret = 0;
 91         // for(int i = 1; i <= n; i++) {
 92             // del = i;
 93             memset(instk, 0, sizeof(instk));
 94             memset(dfn, 0, sizeof(dfn));
 95             memset(belong, 0, sizeof(belong));
 96             top = dindex = 0;
 97             int cnt = 0;
 98             for(int j = 1; j <= n; j++) {
 99                 // if(j == del) continue;
100                 if(!dfn[j]) {
101                     tarjan(j, j);
102                     cnt++;
103                 }
104             }
105             for(int j = 1; j <= n; j++) {
106                 ret = max(ret, cnt+belong[j]);
107             }
108         // }
109         printf("%d\n", ret);
110     }
111     return 0;
112 }

 

[POJ2117]Electricity(连通分量,割点)