日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ - 3177 Redundant Paths(边双缩点)

發布時間:2024/4/11 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ - 3177 Redundant Paths(边双缩点) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個由n個點和m條邊構成的無向圖,現在問至少添加幾條邊,才能使得任意兩點之間都能由至少兩條不同的路到達

題目分析:既然要讓任意兩點之間至少由兩條道路連接,換句話說也就是任意兩點都必須在一個環中出現,那么首先考慮有多少個點之間目前只有一條道路,也就是求一下當前的圖中有多少個橋,求出橋后我們就可以繼續利用邊雙縮點,這樣每一個點代表的集合中任意兩個點肯定都是能找到相應的環的,而由橋當邊建立的新圖中,我們該怎么處理呢,為了滿足題目的要求,我們可以先求一下有多少個度為1的節點,這些度為1的節點才需要連成環,首選肯定是讓這些度為1的集合兩兩建邊,這樣能在新增加的邊數盡量少的情況下滿足題意要求,所以設ans為度為1的節點個數,那么答案就是ans+1>>1了

最后看這個題的討論區中說有重邊,我一開始還開了個map去判重,后來將判重去掉發現一樣能AC,可能是數據水了沒有掛重邊,順帶一提,這個題目和poj3352的代碼一模一樣,白嫖一個題目的感覺蠻不錯的

代碼:

#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e4+100;const int M=1e5+100;struct Egde {int to,next; }edge1[M],edge2[M];int head1[N],head2[N],low[N],dfn[N],c[N],num,cnt1,cnt2,dcc,n,m,du[N];bool bridge[M];void addedge1(int u,int v) {edge1[cnt1].to=v;edge1[cnt1].next=head1[u];head1[u]=cnt1++; }void addedge2(int u,int v) {edge2[cnt2].to=v;edge2[cnt2].next=head2[u];head2[u]=cnt2++; }void tarjan(int u,int in_edge) {dfn[u]=low[u]=++num;for(int i=head1[u];i!=-1;i=edge1[i].next){int v=edge1[i].to;if(!dfn[v]){tarjan(v,i);low[u]=min(low[u],low[v]);if(low[v]>dfn[u])bridge[i]=bridge[i^1]=true;}else if(i!=(in_edge^1))low[u]=min(low[u],dfn[v]);} }void dfs(int u) {c[u]=dcc;for(int i=head1[u];i!=-1;i=edge1[i].next){int v=edge1[i].to;if(c[v]||bridge[i])continue;dfs(v);} }void solve() {for(int i=1;i<=n;i++)//找橋 if(!dfn[i])tarjan(i,0);for(int i=1;i<=n;i++)//縮點 if(!c[i]){dcc++;dfs(i);} }void build()//縮點+連邊 {solve();for(int i=2;i<cnt1;i+=2){int u=edge1[i^1].to;int v=edge1[i].to;if(c[u]==c[v])continue;addedge2(c[u],c[v]);addedge2(c[v],c[u]);} }void init() {cnt1=2;cnt2=num=dcc=0;memset(head2,-1,sizeof(head2));memset(head1,-1,sizeof(head1));memset(low,0,sizeof(low));memset(dfn,0,sizeof(dfn));memset(bridge,false,sizeof(bridge));memset(c,0,sizeof(c));memset(du,0,sizeof(du)); }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);while(scanf("%d%d",&n,&m)!=EOF){init();while(m--){int u,v;scanf("%d%d",&u,&v);addedge1(u,v);addedge1(v,u);}build();for(int i=0;i<cnt2;i+=2){int u=edge2[i].to;int v=edge2[i^1].to;du[u]++;du[v]++;}int ans=0;for(int i=1;i<=dcc;i++)if(du[i]==1)ans++;printf("%d\n",ans+1>>1);}return 0; }

?

總結

以上是生活随笔為你收集整理的POJ - 3177 Redundant Paths(边双缩点)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。