zoukankan      html  css  js  c++  java
  • POJ3352 Road Construction 双连通分量

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

      本题要求的就是最少添加多少条边可变无桥的连通图,和POJ3177一样,(度为1的边双连通分量的个数+1)/2。

      1 //STATUS:C++_AC_16MS_276KB
      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 //define
     25 #define pii pair<int,int>
     26 #define mem(a,b) memset(a,b,sizeof(a))
     27 #define lson l,mid,rt<<1
     28 #define rson mid+1,r,rt<<1|1
     29 #define PI acos(-1.0)
     30 //typedef
     31 typedef __int64 LL;
     32 typedef unsigned __int64 ULL;
     33 //const
     34 const int N=1010;
     35 const int INF=0x3f3f3f3f;
     36 const int MOD=100000,STA=8000010;
     37 const LL LNF=1LL<<60;
     38 const double EPS=1e-8;
     39 const double OO=1e15;
     40 const int dx[4]={-1,0,1,0};
     41 const int dy[4]={0,1,0,-1};
     42 //Daily Use ...
     43 inline int sign(double x){return (x>EPS)-(x<-EPS);}
     44 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
     45 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
     46 template<class T> inline T Min(T a,T b){return a<b?a:b;}
     47 template<class T> inline T Max(T a,T b){return a>b?a:b;}
     48 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
     49 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
     50 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
     51 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
     52 //End
     53 
     54 struct Edge{
     55     int u,v;
     56 }e[N*2];
     57 //bool iscut[N];
     58 int first[N],next[N*2],pre[N],low[N],bccno[N],cnt[N];
     59 int n,m,mt,bcnt,dfs_clock;
     60 stack<int> s;
     61 
     62 void adde(int a,int b)
     63 {
     64     e[mt].u=a;e[mt].v=b;
     65     next[mt]=first[a];first[a]=mt++;
     66     e[mt].u=b;e[mt].v=a;
     67     next[mt]=first[b];first[b]=mt++;
     68 }
     69 
     70 void dfs(int u,int fa)
     71 {
     72     int i,v;
     73     pre[u]=low[u]=++dfs_clock;
     74     s.push(u);
     75     for(i=first[u];i!=-1;i=next[i]){
     76         v=e[i].v;
     77         if(!pre[v]){
     78             dfs(v,u);
     79             low[u]=Min(low[u],low[v]);
     80           //  if(low[v]>pre[u])iscut[i]=true;
     81         }
     82         else if(v!=fa && pre[v]<pre[u]){
     83             low[u]=Min(low[u],pre[v]);
     84         }
     85     }
     86     if(low[u]==pre[u]){
     87         int x=-1;
     88         bcnt++;
     89         while(x!=u){
     90             x=s.top();s.pop();
     91             bccno[x]=bcnt;
     92         }
     93     }
     94 }
     95 
     96 void find_bcc()
     97 {
     98     int i;
     99     bcnt=dfs_clock=0;//mem(iscut,0);
    100     mem(pre,0);mem(bccno,0);
    101     for(i=1;i<=n;i++){
    102         if(!pre[i])dfs(i,-1);
    103     }
    104 }
    105 
    106 int main()
    107 {
    108  //   freopen("in.txt","r",stdin);
    109     int i,j,a,b,ans,t;
    110     while(~scanf("%d%d",&n,&m))
    111     {
    112         mt=0;
    113         mem(first,-1);
    114         while(m--){
    115             scanf("%d%d",&a,&b);
    116             adde(a,b);
    117         }
    118 
    119         find_bcc();
    120         ans=0;
    121         if(bcnt>1){
    122             mem(cnt,0);
    123             for(i=0;i<mt;i+=2){
    124                 if(bccno[e[i].u]!=bccno[e[i].v]){
    125                     cnt[bccno[e[i].u]]++;
    126                     cnt[bccno[e[i].v]]++;
    127                 }
    128             }
    129             for(i=1;i<=bcnt;i++){
    130                 if(cnt[i]<=1)ans++;
    131             }
    132         }
    133 
    134         printf("%d\n",(ans+1)/2);
    135     }
    136     return 0;
    137 }
  • 相关阅读:
    vs2012下如何调试带输入参数的程序
    BASH-数据流重导向
    VS在连接期间的一个错误的处理:转换到 COFF 期间失败: 文件无效或损坏
    vmware中NAT配置不能上网的一个解决方案
    linux下查找
    系统及用户的bash环境配置 学习笔记
    linux中控制台字体和背景颜色配置
    bash中变量的巧用
    vi 常用指令存档
    vim指令示意图
  • 原文地址:https://www.cnblogs.com/zhsl/p/3090457.html
Copyright © 2011-2022 走看看