UVALive 6044(双连通分量的应用)
2013-09-14 09:57
309 查看
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34902
思路:首先是双连通缩点,然后就是搜索一下,搜索时要跳过连通分量的点的个数>=2的点,最后的答案是n*(n-1)/2.
View Code
思路:首先是双连通缩点,然后就是搜索一下,搜索时要跳过连通分量的点的个数>=2的点,最后的答案是n*(n-1)/2.
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stack> #include<vector> using namespace std; #define MAXN 11111 #define MAXM 444444 struct Edge{ int v,next; }edge[MAXM]; int n,m,NE,cnt,_count; int head[MAXN]; void Insert(int u,int v) { edge[NE].v=v; edge[NE].next=head[u]; head[u]=NE++; } int low[MAXN],dfn[MAXN]; int color[MAXN]; bool mark[MAXN]; stack<int>S; void Tarjan(int u,int father) { int flag=0; low[u]=dfn[u]=++cnt; mark[u]=true; S.push(u); for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(v==father&&!flag){ flag=1;continue; } if(dfn[v]==0){ Tarjan(v,u); low[u]=min(low[u],low[v]); }else if(mark[v]){ low[u]=min(low[u],dfn[v]); } } if(low[u]==dfn[u]){ _count++; int x=S.top(); if(x==u)S.pop(); else { do{ x=S.top(); S.pop(); mark[x]=false; color[x]=_count; }while(x!=u); } } } int ans; void dfs(int u,int father) { color[u]=1; _count++; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(v==father)continue; if(color[v])continue; dfs(v,u); } } int main() { int _case,u,v,t=1; scanf("%d",&_case); while(_case--){ scanf("%d%d",&n,&m); NE=0; memset(head,-1,sizeof(head)); while(m--){ scanf("%d%d",&u,&v); Insert(u,v); Insert(v,u); } cnt=_count=0; memset(dfn,0,sizeof(dfn)); memset(color,0,sizeof(color)); for(int i=1;i<=n;i++){ if(dfn[i]==0)Tarjan(i,-1); } ans=0; for(int i=1;i<=n;i++){ if(color[i]==0){ _count=0; dfs(i,-1); ans+=_count*(_count-1)/2; } } printf("Case #%d: %d\n",t++,ans); } return 0; }
View Code
相关文章推荐
- UVALive 6044(双连通分量的应用)
- UVAlive 6044 - Unique Path(边双连通分量)
- UVAlive 5135 Mining Your Own Business [点双连通分量] [求割顶]
- UVALive 5135 Mining Your Own Business(点双连通分量)
- UVALive 3026 period(周期) kmp算法的应用
- UVALive - 3523 Knights of the Round Table(无向图的双连通分量)
- UVALive - 7454 Parentheses 栈应用
- uvalive 6955 Finding Lines rand()应用
- 指南 第三章 例题3 UVALive 3135 Argus(优先队列的应用)
- UVALive-5135 Mining Your Own Business (无向图的双连通分量)
- uvalive 5135 Mining Your Own Business(双连通分量)
- uvalive 6955 Finding Lines rand()应用
- UVAlive 3523 Knights of the Round Table [点双连通分量] [Tarjan]
- 最小生成树应用uvalive5713
- UVALive - 5135 Mining Your Own Business(【无向图双连通分量】+【超级思维】)
- 指南 第三章 例题6 UVALive 3027 Corporative Network(并查集的应用)
- UVALive - 5135 Mining Your Own Business(双连通分量)
- UVALive - 3523 Knights of the Round Table(【点双连通分量】+【二分图判定】)
- UVALive - 5135 Mining Your Own Business 双连通分量
- 点双连通分量——究极版。非主流的版本。。uvalive5135