hdu - 2667 Proving Equivalences(强连通)

时间:2022-09-26 09:48:05

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

求至少添加多少条边才能变成强连通分量.统计入度为0的点和出度为0的点,取最大值即可.

  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 }