hdu 4612 Warm up(双连通缩点+树直径,4级)

Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 955    Accepted Submission(s): 193

Problem Description
  N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.   If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system. People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.   Note that there could be more than one channel between two planets.
 
Input
  The input contains multiple cases.   Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.   (2<=N<=200000, 1<=M<=1000000)   Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.   A line with two integers '0' terminates the input.
 
Output
  For each case, output the minimal number of bridges after building a new channel in a line.
 
Sample Input
4 4 1 2 1 3 1 4 2 3 0 0
 
Sample Output
0
 
Source
 
Recommend
zhuyuanchen520

思路:多校时,一看就知道是双连通缩点+树直径,可是双连通原先的点一直有个地方没处理好,就一直没动它了。

注意:有重边环,自身环之类的。

  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include<iostream>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<vector>
  6 #include<algorithm>
  7 #define FOR(i,n) for(int i=0;i<n;++i)
  8 #define clr(f,z) memset(f,z,sizeof(f))
  9 using namespace std;
 10 const int mm=2e5+9;
 11 const int nn=2e6+9;
 12 int head[mm];
 13 class node
 14 {
 15   public:int v,next;bool vis,again;
 16 }e[nn];
 17 class dot
 18 {
 19   public:int u,v;
 20   bool operator<(const dot&x)const
 21   {
 22     if(u^x.u)return u<x.u;
 23     return v<x.v;
 24   }
 25 }f[nn];
 26 int dfn[mm],stak[mm],top,edge;
 27 int n,m,bcc_no,e_to[mm],brige,dfs_clock;
 28 void data()
 29 {
 30   clr(head,-1);edge=0;
 31 }
 32 void add(int u,int v,bool z)
 33 { e[edge].vis=0;e[edge].again=z;
 34   e[edge].v=v;e[edge].next=head[u];head[u]=edge++;
 35 }
 36 int tarjan(int u,int fa,bool yes)
 37 {
 38   int v,lowv,lowu;
 39   lowu=dfn[u]=++dfs_clock;
 40   stak[top++]=u;
 41   for(int i=head[u];~i;i=e[i].next)
 42   {
 43     v=e[i].v;
 44     if(v==fa&&!yes)continue;
 45     if(!dfn[v])
 46     {
 47       lowv=tarjan(v,u,e[i].again);
 48       if(lowv>dfn[u])
 49       {e[i].vis=e[i^1].vis=1;
 50         ++brige;
 51       }
 52       lowu=min(lowv,lowu);
 53     }
 54     else if(!e_to[v]&&dfn[v]<lowu)
 55       lowu=dfn[v];
 56   }
 57   if(lowu==dfn[u])
 58   {
 59     ++bcc_no;
 60         do
 61         { v=stak[--top];
 62           e_to[v]=bcc_no;
 63 
 64         }while(v!=u);
 65   }
 66   return lowu;
 67 }
 68 vector<int>g[mm];
 69 int dep[mm];
 70 void dfs(int u)
 71 { int v;
 72   FOR(i,g[u].size())
 73   {
 74     v=g[u][i];
 75     if(dep[v]!=-1)continue;
 76     dep[v]=dep[u]+1;
 77     dfs(v);
 78   }
 79 }
 80 void find_bcc()
 81 {
 82   clr(dfn,0);dfs_clock=0;
 83   clr(e_to,0);bcc_no=0;top=0;brige=0;
 84   //for(int i=1;i<=n;++i)
 85    // if(!dfn[i])
 86     tarjan(1,-1,0);
 87   FOR(i,n+1)g[i].clear();
 88   for(int i=1;i<=n;++i)
 89   for(int j=head[i];~j;j=e[j].next)
 90   if(e[j].vis)
 91   {
 92     g[e_to[i]].push_back(e_to[e[j].v]);
 93   }
 94   clr(dep,-1);
 95   dep[1]=0;
 96   dfs(1);int z=1;
 97   for(int i=1;i<=bcc_no;++i)
 98     if(dep[i]>dep[z])z=i;
 99   clr(dep,-1);dep[z]=0;dfs(z);
100   int ans=0;
101   for(int i=1;i<=bcc_no;++i)
102     ans=max(ans,dep[i]);
103   //puts("+++");
104  // for(int i=1;i<=n;++i)
105    // printf("%d %d\n",i,e_to[i]);
106  // printf("bcc=%d dep=%d\n",bcc_no,ans);
107   printf("%d\n",bcc_no-ans-1);
108 }
109 int main()
110 { int a,b;
111   while(~scanf("%d%d",&n,&m))
112   { if(n==0&&m==0)break;
113     data();
114     FOR(i,m)
115     {
116       scanf("%d%d",&a,&b);
117       if(a>b)a^=b,b^=a,a^=b;
118       f[i].u=a;f[i].v=b;
119       ///add(a,b);add(b,a);
120     }
121     sort(f,f+m);
122     FOR(i,m)
123     if(i==0||f[i].u!=f[i-1].u||f[i].v!=f[i-1].v)
124     {
125       if(i<m-1&&(f[i].u==f[i+1].u&&f[i].v==f[i+1].v))
126         add(f[i].u,f[i].v,1),add(f[i].v,f[i].u,1);
127       else
128       {
129         add(f[i].u,f[i].v,0);add(f[i].v,f[i].u,0);
130       }
131     }
132     find_bcc();
133   }
134   return 0;
135 }
136 /*
137 6 6
138 1 2
139 2 4
140 2 5
141 6 5
142 6 5
143 2 3
144 
145 5 5
146 1 2
147 1 3
148 1 4
149 1 5
150 1 2
151 */
View Code

 

 

 

posted @ 2013-07-26 18:51  剑不飞  阅读(211)  评论(0编辑  收藏  举报