hdu 3849 (双联通求桥)
一道简单的双联通求桥的题目,,数据时字符串,,map用的不熟练啊,,,,,,,,,,,,,
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <string>
#include <algorithm>
#define N 10001
using namespace std;
int head[N],num,dfs[N],low[N],n,m,idx,bridge[N],bum;
struct edge
{
int st,ed,next;
}E[N*20];
void addedge(int x,int y)
{
E[num].st=x;
E[num].ed=y;
E[num].next=head[x];
head[x]=num++;
}
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
void Tarjan(int u,int father)
{
int i,v;
low[u]=dfs[u]=idx++;
for(i=head[u];i!=-1;i=E[i].next)
{
v=E[i].ed;
if(v==father)continue;
if(dfs[v]==-1)
{
Tarjan(v,u);
low[u]=low[u]>low[v]?low[v]:low[u];
if(low[v]>dfs[u])//桥
{
bridge[bum++]=i;
}
}
else low[u]=low[u]>dfs[v]?dfs[v]:low[u];
}
}
int main()
{
int i,x,y,j,t,sum;
char str[2][200];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
num=0;sum=1;
map<string,int>mp;
map<int,string>mmp;
for(i=0;i<m;i++)
{
scanf("%s%s",str[0],str[1]);
if(mp[str[0]]==0)
{
mp[str[0]]=sum;
mmp[sum]=str[0];
sum++;
}
if(mp[str[1]]==0)
{
mp[str[1]]=sum;
mmp[sum]=str[1];
sum++;
}
x=mp[str[0]];
y=mp[str[1]];
addedge(x,y);
addedge(y,x);
}
memset(dfs,-1,sizeof(dfs));
idx=0;
bum=0;
Tarjan(1,-1);
for(i=1;i<=n;i++)
{
if(dfs[i]==-1)
break;
}
if(i<=n){printf("0\n");continue;}
printf("%d\n",bum);
qsort(bridge,bum,sizeof(bridge[0]),cmp);
for(j=0;j<bum;j++)
{
i=bridge[j];
i=(i/2*2);
x=E[i].st;
y=E[i].ed;
if(x!=y)
printf("%s %s\n",mmp[E[i].st].c_str(),mmp[E[i].ed].c_str());
}
}
return 0;
}
hdu 3849 (双联通求桥)的更多相关文章
- hdu 4738 (双联通求桥)
2013 ACM/ICPC Asia Regional Hangzhou Online 题目大意:有n个岛,曹操在一些岛之间建了一些桥,每个桥上有一些士兵把守,周瑜只有一个炸弹只能炸掉一个桥,炸弹需要 ...
- HDU3394 Railway —— 点双联通分量 + 桥(割边)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3394 Railway Time Limit: 2000/1000 MS (Java/Others) ...
- 【HDU4612】 双连通分量求桥
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 题目大意:给你一个无向图,问你加一条边后最少还剩下多少多少割边. 解题思路:好水的一道模板题.先 ...
- hdu 4612 (双联通+树形DP)
加一条边后最少还有多少个桥,先Tarjan双联通缩点, 然后建树,求出树的直径,在直径起点终点加一条边去的桥最多, #pragma comment(linker, "/STACK:10240 ...
- hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- hdu 2242双联通分量+树形dp
/*先求出双联通缩点,然后进行树形dp*/ #include<stdio.h> #include<string.h> #include<math.h> #defin ...
- hdu 4612 双联通缩点+树形dp
#pragma comment(linker,"/STACK:102400000,102400000")//总是爆栈加上这个就么么哒了 #include<stdio.h> ...
- hdoj 3849 By Recognizing These Guys, We Find Social Networks Useful【双连通分量求桥&&输出桥&&字符串处理】
By Recognizing These Guys, We Find Social Networks Useful Time Limit: 2000/1000 MS (Java/Others) ...
- zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...
随机推荐
- C++ 出现bug :二位数组的操作运算,求非对角线的元素的和
编写一个通用程序,求出二位数组(行数和列数必须相等)的非对角线的元素之和,试建立类MATRIX完成上述功能 #include<iostream> using namespace std; ...
- LeetCode题解——4Sum
题目: 给定一个数组,找出其中和为0的所有4个数组合,每个组合内的4个数非递降. 解法: ①先排序,然后利用4个指针,前两个遍历,后两个在第二个指针之后的部分里夹逼,时间O(N3). ②或者利用一个哈 ...
- 复制表的sql语句
1.sqlserver 原表存在:insert into a select * from b 原表不存在:select * into a from b 2.mysql.oracle 原表存在:inse ...
- [学习笔记] Web设计过程中该做和不该做的
原文网址: http://www.javascriptstyle.com/the-dos-and-donts-of-web-design -该做的: QR代码QR代码即快速响应代码,这是矩阵条形码的一 ...
- svn跳过某个目录
svn up --set-depth exclude dir2 http://stackoverflow.com/questions/1439176/svn-can-you-remove-direct ...
- How do I use SOCKS proxy in delphi?
TCP====== For Indy 8.0 In Delphi, do the following: IdTCPClient1.SocksInfo.Host := [the DNS name of ...
- Android 添加系统服务
原创文章,转载请注明出处:http://blog.csdn.net/t5721654/article/details/7480696 Android系统本身提供了很多系统服务,如WindowManag ...
- C++问题-无法打开某个自定义源文件
问题经过:需要做一个工具,是在某个产品的基础上做的,所以要来了同事的代码.用VS打开后,提示如下问题.1>c1xx : fatal error C1083: 无法打开源文件:“..\..\GUX ...
- PC问题-VMware Workstation出现“文件锁定失败”
问题现象:电脑关机时挂起VMware Workstation后,第二天运行VMware Workstation出现“文件锁定失败”. 问题原因:在WIN的目录中有*.LCK文件,此文件是用来锁定当前虚 ...
- Spring EL Lists, Maps example
In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way ...