1. 程式人生 > >專題訓練之雙連通

專題訓練之雙連通

sin 註意 數根 display 枚舉 int clear AI spl

橋和割點例題+講解:hihocoder1183 http://hihocoder.com/problemset/problem/1183

技術分享圖片
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<vector>
  5 #include<set>
  6 using namespace std;
  7 const int maxn=1005;
  8 const int maxm=200010;
  9 struct edge{
 10     int
to,nxt; 11 bool cut; 12 }edge[maxm*2]; 13 int head[maxn],tot; 14 int low[maxn],dfn[maxn]; 15 int index,n,bridge; 16 set<int>st; 17 bool cut[maxn]; 18 19 void addedge(int u,int v) 20 { 21 edge[tot].to=v; 22 edge[tot].nxt=head[u]; 23 edge[tot].cut=false; 24 head[u]=tot++;
25 } 26 27 void tarjan(int u,int pre) 28 { 29 low[u]=dfn[u]=++index; 30 int son=0; 31 for ( int i=head[u];i!=-1;i=edge[i].nxt ) { 32 int v=edge[i].to; 33 if ( v==pre ) continue; 34 if ( !dfn[v] ) { 35 son++; 36 tarjan(v,u); 37 low[u]=min(low[u],low[v]);
38 if ( low[v]>dfn[u] ) { 39 bridge++; 40 edge[i].cut=true; 41 edge[i^1].cut=true; 42 } 43 if ( low[v]>=dfn[u] && u!=pre ) { 44 st.insert(u); 45 cut[u]=true; 46 } 47 } 48 else if ( low[u]>dfn[v] ) low[u]=dfn[v]; 49 } 50 if ( u==pre && son>1 ) { 51 cut[u]=true; 52 st.insert(u); 53 } 54 } 55 56 void solve() 57 { 58 memset(low,0,sizeof(low)); 59 memset(dfn,0,sizeof(dfn)); 60 memset(cut,false,sizeof(cut)); 61 index=bridge=0; 62 st.clear(); 63 for ( int i=1;i<=n;i++ ) { 64 if ( !dfn[i] ) tarjan(i,i); 65 } 66 set<int>::iterator it; 67 if ( st.size()==0 ) printf("Null\n"); 68 else { 69 for ( it=st.begin();it!=st.end();it++ ) { 70 if ( it!=st.begin() ) printf(" "); 71 printf("%d",*it); 72 } 73 printf("\n"); 74 } 75 vector<pair<int,int> >ans; 76 for ( int i=1;i<=n;i++ ) { 77 for ( int j=head[i];j!=-1;j=edge[j].nxt ) { 78 if ( edge[j].cut && edge[j].to>i ) ans.push_back(make_pair(i,edge[j].to)); 79 } 80 } 81 sort(ans.begin(),ans.end()); 82 for ( int i=0;i<ans.size();i++ ) { 83 printf("%d %d\n",ans[i].first,ans[i].second); 84 } 85 } 86 87 void init() 88 { 89 tot=0; 90 memset(head,-1,sizeof(head)); 91 } 92 93 int main() 94 { 95 int m,i,j,k,x,y,z; 96 while ( scanf("%d%d",&n,&m)!=EOF ) { 97 init(); 98 while ( m-- ) { 99 scanf("%d%d",&x,&y); 100 addedge(x,y); 101 addedge(y,x); 102 } 103 solve(); 104 } 105 }
橋和割點模板

1.(POJ2117)http://poj.org/problem?id=2117 (求連通塊數量)

題意:去掉一個點使得有更多的雙連通,求最多有多少雙連通

分析:添加數組add_block[],當u為割點時則add_block[u]++,最後逐一枚舉要去掉的點。特別註意對於數根來說add_block[u]=son-1

技術分享圖片
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<set>
 6 using namespace std;
 7 const int maxn=20005;
 8 const int maxm=100010;
 9 struct edge{
10     int to,nxt;
11     bool cut;
12 }edge[maxm*3];
13 int head[maxn],tot;
14 int low[maxn],dfn[maxn];
15 int index,n,bridge;
16 int add_block[maxn];
17 set<int>st;
18 bool cut[maxn];
19 
20 void addedge(int u,int v)
21 {
22     edge[tot].to=v;
23     edge[tot].nxt=head[u];
24     edge[tot].cut=false;
25     head[u]=tot++;
26 }
27 
28 void tarjan(int u,int pre)
29 {
30     low[u]=dfn[u]=++index;
31     int son=0;
32     for ( int i=head[u];i!=-1;i=edge[i].nxt ) {
33         int v=edge[i].to;
34         if ( v==pre ) continue;
35         if ( !dfn[v] ) {
36             son++;
37             tarjan(v,u);
38             low[u]=min(low[u],low[v]);
39             if ( low[v]>dfn[u] ) {
40                 bridge++;
41                 edge[i].cut=true;
42                 edge[i^1].cut=true;
43             }
44             if ( u!=pre && low[v]>=dfn[u] ) {
45                 st.insert(u);
46                 cut[u]=true;
47                 add_block[u]++;
48             }
49         }
50         else if ( low[u]>dfn[v] ) low[u]=dfn[v];
51     }
52     if ( u==pre && son>1 ) {
53         cut[u]=true;
54         st.insert(u);
55     }
56     if ( u==pre ) add_block[u]=son-1;
57 }
58 
59 void solve()
60 {
61     memset(low,0,sizeof(low));
62     memset(dfn,0,sizeof(dfn));
63     memset(cut,false,sizeof(cut));
64     memset(add_block,0,sizeof(add_block));
65     int cnt,ans;
66     index=bridge=cnt=ans=0;
67     for ( int i=1;i<=n;i++ ) {
68         if ( !dfn[i] ) {
69             tarjan(i,i);
70             cnt++;
71         }
72     }
73     for ( int i=1;i<=n;i++ ) ans=max(ans,cnt+add_block[i]);
74     printf("%d\n",ans);
75 }
76 
77 void init()
78 {
79     tot=0;
80     memset(head,-1,sizeof(head));
81     st.clear();
82 }
83 
84 int main()
85 {
86     int m,i,j,k,x,y,z;
87     while ( scanf("%d%d",&n,&m)!=EOF && (n+m) ) {
88         init();
89         while ( m-- ) {
90             scanf("%d%d",&x,&y);
91             x++;y++;
92             addedge(x,y);
93             addedge(y,x);
94         }
95         solve();
96     }
97 }
POJ2117

2.(POJ3117)http://poj.org/problem?id=3177 (構造邊雙連通)

題意:求添加多少條邊後在圖中的任意兩點都有兩條邊不重復的路徑

分析:邊雙連通,利用強連通分量中的寫法,把每個點對應的縮點後的點標記下來。最後構建新圖(即進行縮點,邊只存在原先為橋的邊)記錄入度(或者出度,選擇一個即可),最後入度為1的點即為葉子節點,對於一棵樹想要使其變成邊雙連通,所加的邊數=(葉子節點的個數+1)/2

技術分享圖片
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<vector>
  5 using namespace std;
  6 const int maxn=1005;
  7 const int maxm=200010;
  8 struct edge{
  9     int to,nxt;
 10     bool cut;
 11 }edge[maxm*2];
 12 int head[maxn],tot,n;
 13 int index,ebc_cnt,bridge,block,top;
 14 int dfn[maxn],low[maxn],belong[maxn],stack[maxn],du[maxn];
 15 bool vis[maxn];
 16 
 17 void addedge(int u,int v)
 18 {
 19     edge[tot].to=v;
 20     edge[tot].nxt=head[u];
 21     edge[tot].cut=false;
 22     head[u]=tot++;
 23 }
 24 
 25 void tarjan(int u,int pre)
 26 {
 27     low[u]=dfn[u]=++index;
 28     stack[top++]=u;
 29     vis[u]=true;
 30     for ( int i=head[u];i!=-1;i=edge[i].nxt ) {
 31         int v=edge[i].to;
 32         if ( v==pre ) continue;
 33         if ( !dfn[v] ) {
 34             tarjan(v,u);
 35             low[u]=min(low[u],low[v]);
 36             if ( low[v]>dfn[u] ) {
 37                 bridge++;
 38                 edge[i].cut=true;
 39                 edge[i^1].cut=true;
 40             }
 41         }
 42         else if ( low[u]>dfn[v] && vis[v] ) low[u]=dfn[v];
 43     }
 44     if ( low[u]==dfn[u] ) {
 45         block++;
 46         int v;
 47         do
 48         {
 49             v=stack[--top];
 50             vis[v]=true;
 51             belong[v]=block;
 52         }
 53         while ( v!=u );
 54     }
 55 }
 56 
 57 void solve()
 58 {
 59     memset(low,0,sizeof(low));
 60     memset(dfn,0,sizeof(dfn));
 61     memset(vis,false,sizeof(vis));
 62     index=bridge=block=top=0;
 63     for ( int i=1;i<=n;i++ ) {
 64         if ( !dfn[i] ) tarjan(i,i);
 65     }
 66     memset(du,0,sizeof(du));
 67     for ( int i=1;i<=n;i++ ) {
 68         for ( int j=head[i];j!=-1;j=edge[j].nxt ) {
 69             if ( edge[j].cut ) {
 70                 du[belong[i]]++;
 71             }
 72         }
 73     }
 74     int cnt=0;
 75     for ( int i=1;i<=block;i++ ) {
 76         if ( du[i]==1 ) cnt++;
 77     }
 78     printf("%d\n",(cnt+1)/2);
 79 }
 80 
 81 void init()
 82 {
 83     tot=0;
 84     memset(head,-1,sizeof(head));
 85 }
 86 
 87 int main()
 88 {
 89     int m,i,j,k,x,y,z;
 90     while ( scanf("%d%d",&n,&m)!=EOF ) {
 91         init();
 92         for ( i=1;i<=m;i++ ) {
 93             scanf("%d%d",&x,&y);
 94             addedge(x,y);
 95             addedge(y,x);
 96         }
 97         solve();
 98     }
 99     return 0;
100 }
POJ3117

專題訓練之雙連通