hdu - 2667 Proving Equivalences(強連通)

http://acm.hdu.edu.cn/showproblem.php?pid=2767php

求至少添加多少條邊才能變成強連通份量.統計入度爲0的點和出度爲0的點,取最大值便可.ios

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <vector>
  5 #include <cstring>
  6 #include <algorithm>
  7 #include <string>
  8 #include <set>
  9 #include <functional>
 10 #include <numeric>
 11 #include <sstream>
 12 #include <stack>
 13 #include <map>
 14 #include <queue>
 15 
 16 #define CL(arr, val)    memset(arr, val, sizeof(arr))
 17 
 18 #define ll long long
 19 #define inf 0x7f7f7f7f
 20 #define lc l,m,rt<<1
 21 #define rc m + 1,r,rt<<1|1
 22 #define pi acos(-1.0)
 23 
 24 #define L(x)    (x) << 1
 25 #define R(x)    (x) << 1 | 1
 26 #define MID(l, r)   (l + r) >> 1
 27 #define Min(x, y)   (x) < (y) ? (x) : (y)
 28 #define Max(x, y)   (x) < (y) ? (y) : (x)
 29 #define E(x)        (1 << (x))
 30 #define iabs(x)     (x) < 0 ? -(x) : (x)
 31 #define OUT(x)  printf("%I64d\n", x)
 32 #define lowbit(x)   (x)&(-x)
 33 #define Read()  freopen("a.txt", "r", stdin)
 34 #define Write() freopen("dout.txt", "w", stdout);
 35 
 36 using namespace std;
 37 #define N 20100
 38 //N爲最大點數
 39 #define M 50100
 40 //M爲最大邊數
 41 int n, m;//n m 爲點數和邊數
 42 
 43 struct Edge{
 44     int from, to, nex;
 45     bool sign;//是否爲橋
 46 }edge[M<<1];
 47 int head[N], edgenum;
 48 void add(int u, int v){//邊的起點和終點
 49     Edge E={u, v, head[u], false};
 50     edge[edgenum] = E;
 51     head[u] = edgenum++;
 52 }
 53 
 54 int DFN[N], Low[N], Stack[N], top, Time; //Low[u]是點集{u點及以u點爲根的子樹} 中(全部反向弧)能指向的(離根最近的祖先v) 的DFN[v]值(即v點時間戳)
 55 int taj;//連通分支標號,從1開始
 56 int Belong[N];//Belong[i] 表示i點屬於的連通分支
 57 bool Instack[N];
 58 vector<int> bcc[N]; //標號從1開始
 59 
 60 void tarjan(int u ,int fa){
 61     DFN[u] = Low[u] = ++ Time ;
 62     Stack[top ++ ] = u ;
 63     Instack[u] = 1 ;
 64 
 65     for (int i = head[u] ; ~i ; i = edge[i].nex ){
 66         int v = edge[i].to ;
 67         if(DFN[v] == -1)
 68         {
 69             tarjan(v , u) ;
 70             Low[u] = min(Low[u] ,Low[v]) ;
 71             if(DFN[u] < Low[v])
 72             {
 73                 edge[i].sign = 1;//爲割橋
 74             }
 75         }
 76         else if(Instack[v]) Low[u] = min(Low[u] ,DFN[v]) ;
 77     }
 78     if(Low[u] == DFN[u]){
 79         int now;
 80         taj ++ ; bcc[taj].clear();
 81         do{
 82             now = Stack[-- top] ;
 83             Instack[now] = 0 ;
 84             Belong [now] = taj ;
 85             bcc[taj].push_back(now);
 86         }while(now != u) ;
 87     }
 88 }
 89 
 90 void tarjan_init(int all){
 91     memset(DFN, -1, sizeof(DFN));
 92     memset(Instack, 0, sizeof(Instack));
 93     top = Time = taj = 0;
 94     for(int i=1;i<=all;i++)if(DFN[i]==-1 )tarjan(i, i); //注意開始點標!!!
 95 }
 96 vector<int>G[N];
 97 int du[N];
 98 void suodian(){
 99     memset(du, 0, sizeof(du));
100     for(int i = 1; i <= taj; i++)G[i].clear();
101     for(int i = 0; i < edgenum; i++){
102         int u = Belong[edge[i].from], v = Belong[edge[i].to];
103         if(u!=v)
104         {
105             G[u].push_back(v), du[v]++;
106            // printf("%d %d\n",u,v);
107         }
108     }
109 }
110 void init(){memset(head, -1, sizeof(head)); edgenum=0;}
111 int main()
112 {
113     //Read();
114     int t,a,b;
115     scanf("%d",&t);
116     while(t--)
117     {
118         scanf("%d%d",&n,&m);
119         init();
120         for(int i=0;i<m;i++)
121         {
122             scanf("%d%d",&a,&b);
123             add(a,b);
124         }
125         tarjan_init(n);
126         suodian();
127         int x=0,y=0;
128         for(int i=1;i<=taj;i++)
129         {
130             if(du[i]==0) x++; //出度爲0點的個數
131             if(G[i].size()==0) y++;
132         }
133         //printf("%d\n",j);
134         if(taj==1) printf("0\n");
135         else
136         printf("%d\n",max(x,y));
137     }
138     return 0;
139 }
相關文章
相關標籤/搜索