HDU 3394 Railway
2015-09-07 19:14
543 查看
题意:给一个无向图。如果至少有两个环共用了一些边,那么这些边被认为是“冲突边”。如果一些边不在任何一个环中,这些边被认为是“多余边”。你要找出这个图中有多少“多余边”和“冲突边”然后输出条数。另外这图不一定是连通的,无重边和自环。
分析:冲突边:就是找一个点双连通分量,如果此连通分量边数>点数 ,则边数为ans。多余边:桥。
分析:冲突边:就是找一个点双连通分量,如果此连通分量边数>点数 ,则边数为ans。多余边:桥。
#include <iostream> #include <stdio.h> #include <math.h> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <string> #include <string.h> #include <map> #include <set> using namespace std; const int maxn=10100; vector<int>edge[maxn],List; int dfn[maxn],low[maxn]; int Stack[maxn],vis[maxn],inseq[maxn]; int cnt,top,time,len; int ans1,ans2,ans3=0; int fun() { int ans=0; memset(inseq,0,sizeof inseq); int len=List.size(); for(int i=0;i<len;i++) inseq[List[i]]=1; for(int i=0;i<len;i++) { int cur=List[i]; int sz=edge[cur].size(); for(int j=0;j<sz;j++) { if(inseq[edge[cur][j]]) ans++; } } ans/=2; if(ans>len) return ans; else return 0; } void tarjan(int u,int fa) { dfn[u]=low[u]=++time; vis[u]=1; Stack[++top]=u; cout<<u<<endl; int sz=edge[u].size(),v; for(int j=0;j<sz;j++) { v=edge[u][j]; if(v==fa)continue; if(!dfn[v]) { tarjan(v,u); low[u]=min(low[u],low[v]); if(dfn[u]<low[v])ans1++; if(dfn[u]<=low[v])//求点双连通对父亲节点没要求 如果一个连通块中没割点,那么把父亲节点作为割点 { List.clear(); ans3++; while(1)//不能写成while(Stack[top]!=u), { int x=Stack[top]; List.push_back(x); vis[x]=0; top--; if(x==v)break;//只弹到u的第一个后继节点 } List.push_back(u); ans2+=fun();//查找冲突边 } } else if(vis[v]==1&&v!=fa&&low[u]>dfn[v]) low[u]=dfn[v]; } } int main() { int n,m,i; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0)break; for(i=0;i<maxn;i++)edge[i].clear(); for( i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); edge[a].push_back(b); edge[b].push_back(a); } memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(vis,0,sizeof(vis)); top=0;time=0; ans1=ans2=0; for(i=0;i<n;i++) if(!dfn[i])tarjan(i,-1); printf("%d %d\n",ans1,ans2); } return 0; }
相关文章推荐
- 1079. Total Sales of Supply Chain (25)
- two sample ttest & paired ttst
- Aittit.attilax超级框架 api 规划大全
- 线上问题分析:The target server failed to respond(目标服务器返回失败)
- error C2440: “初始化”: 无法从“const char *”转换为“ATL::CStringT<BaseType,StringTraits>”
- ARC下强引用弱引用retainCount增减规律
- [9-5]Mail机制和任务计划at、batch、crontab知识梳理和课后练习
- MongoDB错误1_启动mongodb时遇到child process failed,existed with error number 1错误
- container_of分析
- hdu 5410 CRB and His Birthday 背包问题 2015 Multi-University Training Contest 10
- Release Notes for Contrail Release 2.20
- 生产者消费者之 wait / notify
- tair集群配置
- nonatomic, retain,weak,strong用法详解
- ORA-29861: 域索引标记为 LOADING/FAILED/UNUSABLE
- RabbitMQ消息队列(一): Detailed Introduction 详细介绍(转)
- Light oj 1021 Painful Bases(状压dp)
- NetworkOnMainThreadException异常解决办法
- UVA 818 Cutting Chains
- WPF ItemContainerGenerator.ContainerFromItem返回Null