zoukankan      html  css  js  c++  java
  • HDU-4635 Strongly connected 强连通,缩点

      题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635

      题意:给一个简单有向图(无重边,无自环),要你加最多的边,使得图还是简单有向图。。。

      先判断图是否强连通。如果不是强连通的,那么缩点。我们的目的是加最多的边,那么最后的图中,肯定两个集合,这两个集合都是强联通的,一个集合到一个集合只有单向边。我们先让图是满图,然后通过删边来求的:有n*(n-1)条边,然后删掉已有的边m,然后还有删掉两个集合的边n1*(n-n1),n1为其中一个集合的顶点个数,因为这里是单向边。那么答案就是ans=n*(n-1)-m-n1*(n-n1),我们要使ans最大,那么n1*(n-n1)就要越小,则n1最小,就是缩点后一个点的情况,枚举下就行了。。。  n1*(n-n1)为二次凸函数,然后枚举找n1*(n-n1)的最小值就可以了。我直接找 n1最小居然过了><,数据真弱。。。

      1 //STATUS:C++_AC_46MS_3488KB
      2 #include <functional>
      3 #include <algorithm>
      4 #include <iostream>
      5 //#include <ext/rope>
      6 #include <fstream>
      7 #include <sstream>
      8 #include <iomanip>
      9 #include <numeric>
     10 #include <cstring>
     11 #include <cassert>
     12 #include <cstdio>
     13 #include <string>
     14 #include <vector>
     15 #include <bitset>
     16 #include <queue>
     17 #include <stack>
     18 #include <cmath>
     19 #include <ctime>
     20 #include <list>
     21 #include <set>
     22 #include <map>
     23 using namespace std;
     24 //#pragma comment(linker,"/STACK:102400000,102400000")
     25 //using namespace __gnu_cxx;
     26 //define
     27 #define pii pair<int,int>
     28 #define mem(a,b) memset(a,b,sizeof(a))
     29 #define lson l,mid,rt<<1
     30 #define rson mid+1,r,rt<<1|1
     31 #define PI acos(-1.0)
     32 //typedef
     33 typedef __int64 LL;
     34 typedef unsigned __int64 ULL;
     35 //const
     36 const int N=100010;
     37 const int INF=0x3f3f3f3f;
     38 const int MOD=10007,STA=8000010;
     39 const LL LNF=1LL<<60;
     40 const double EPS=1e-8;
     41 const double OO=1e15;
     42 const int dx[4]={-1,0,1,0};
     43 const int dy[4]={0,1,0,-1};
     44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     45 //Daily Use ...
     46 inline int sign(double x){return (x>EPS)-(x<-EPS);}
     47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
     48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
     49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
     50 template<class T> inline T Min(T a,T b){return a<b?a:b;}
     51 template<class T> inline T Max(T a,T b){return a>b?a:b;}
     52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
     53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
     54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
     55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
     56 //End
     57 
     58 struct Edge{
     59     int u,v;
     60 }e[N];
     61 int first[N],next[N],pre[N],sccno[N],low[N];
     62 int n,mt,dfs_clock,scnt;
     63 stack<int> s;
     64 
     65 int cntn[N],in[N],out[N];
     66 int T,m;
     67 
     68 void adde(int a,int b)
     69 {
     70     e[mt].u=a;e[mt].v=b;
     71     next[mt]=first[a],first[a]=mt++;
     72 }
     73 
     74 void dfs(int u)
     75 {
     76     int i,j,v;
     77     pre[u]=low[u]=++dfs_clock;
     78     s.push(u);
     79     for(i=first[u];i!=-1;i=next[i]){
     80         v=e[i].v;
     81         if(!pre[v]){
     82             dfs(v);
     83             low[u]=Min(low[u],low[v]);
     84         }
     85         else if(!sccno[v]){    //反向边更新
     86             low[u]=Min(low[u],low[v]);
     87         }
     88     }
     89     if(low[u]==pre[u]){   //存在强连通分量
     90         int x=-1;
     91         scnt++;
     92         while(x!=u){
     93             x=s.top();s.pop();
     94             sccno[x]=scnt;
     95         }
     96     }
     97 }
     98 
     99 void find_scc()
    100 {
    101     int i;
    102     mem(pre,0);mem(sccno,0);
    103     scnt=dfs_clock=0;
    104     for(i=1;i<=n;i++){
    105         if(!pre[i])dfs(i);
    106     }
    107 }
    108 
    109 int main(){
    110  //   freopen("in.txt","r",stdin);
    111     int ca=1,i,j,a,b,cnt;
    112     LL ans;
    113     scanf("%d",&T);
    114     while(T--)
    115     {
    116         scanf("%d%d",&n,&m);
    117         mem(first,-1);mt=0;
    118         for(i=0;i<m;i++){
    119             scanf("%d%d",&a,&b);
    120             adde(a,b);
    121         }
    122 
    123         find_scc();
    124         printf("Case %d: ",ca++);
    125         if(scnt==1){
    126             printf("-1
    ");
    127             continue;
    128         }
    129         mem(cntn,0);
    130         mem(in,0);mem(out,0);
    131         for(i=1;i<=n;i++)cntn[sccno[i]]++;
    132         for(i=0;i<mt;i++){
    133             if(sccno[e[i].u]!=sccno[e[i].v]){
    134                 in[sccno[e[i].v]]++;
    135                 out[sccno[e[i].u]]++;
    136             }
    137         }
    138         ans=0;
    139         int low=INF;
    140         for(i=1;i<=scnt;i++){
    141             if(in[i]==0 || out[i]==0){
    142                 low=Min(low,cntn[i]);
    143             }
    144         }
    145         ans+=(LL)(n-1)*n-(LL)low*(n-low)-(LL)m;
    146 
    147         printf("%I64d
    ",ans);
    148     }
    149     return 0;
    150 }
  • 相关阅读:
    Kotlin 学习笔记(一)
    Android 中调用本地命令
    Android 6.0 中的 Wifi 连接
    Android 中的广播机制
    面向对象之继承和多态
    2016 年秋季助教总结
    2016 年年志
    C 语言学习 第12次作业总结
    程序员编程艺术第三十六~三十七章、搜索智能提示suggestion,附近点搜索
    程序员编程艺术第三十四~三十五章:格子取数问题,完美洗牌算法
  • 原文地址:https://www.cnblogs.com/zhsl/p/3231632.html
Copyright © 2011-2022 走看看