一道简单的双联通求桥的题目,,数据时字符串,,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 (双联通求桥)的更多相关文章

  1. hdu 4738 (双联通求桥)

    2013 ACM/ICPC Asia Regional Hangzhou Online 题目大意:有n个岛,曹操在一些岛之间建了一些桥,每个桥上有一些士兵把守,周瑜只有一个炸弹只能炸掉一个桥,炸弹需要 ...

  2. HDU3394 Railway —— 点双联通分量 + 桥(割边)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3394 Railway Time Limit: 2000/1000 MS (Java/Others)   ...

  3. 【HDU4612】 双连通分量求桥

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 题目大意:给你一个无向图,问你加一条边后最少还剩下多少多少割边. 解题思路:好水的一道模板题.先 ...

  4. hdu 4612 (双联通+树形DP)

    加一条边后最少还有多少个桥,先Tarjan双联通缩点, 然后建树,求出树的直径,在直径起点终点加一条边去的桥最多, #pragma comment(linker, "/STACK:10240 ...

  5. hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  6. hdu 2242双联通分量+树形dp

    /*先求出双联通缩点,然后进行树形dp*/ #include<stdio.h> #include<string.h> #include<math.h> #defin ...

  7. hdu 4612 双联通缩点+树形dp

    #pragma comment(linker,"/STACK:102400000,102400000")//总是爆栈加上这个就么么哒了 #include<stdio.h> ...

  8. 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)     ...

  9. zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】

    Burning Bridges Time Limit: 5 Seconds      Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...

随机推荐

  1. const 常量数据,只读

    网上其他的博客地址:1 http://www.cnblogs.com/ronny/p/3672501.html 2 http://www.cnblogs.com/hellogiser/p/cplusp ...

  2. mvc JavaScriptResult的用法

    一.JavaScriptResult在MVC中的定义的代码片段   C# 代码   复制 public class JavaScriptResult : ActionResult { public o ...

  3. C#冒泡排序详解

    今天写一简单的冒泡排序,带有详细的中文注释,新手一定要看看! 因为这是找工作面试时经常 笔试 要考的题目. using System; using System.Collections.Generic ...

  4. SqlHelper 带详细中文注释

    using System; using System.Collections.Generic; using System.Linq; using System.Text; //对数据库进行操作引入命名 ...

  5. IO-同步,异步,阻塞,非阻塞,阅读摘要

    http://www.cnblogs.com/Fly-Wind/p/io.html http://blog.csdn.net/historyasamirror/article/details/5778 ...

  6. Lucene Query Term Weighting

    方法 public static Query TermWeighting(Query tquery,Map<String,Float>term2weight){ BooleanQuery ...

  7. Web Service学习之五:WSDL详解

    WSDL是Web Service定义文档,不同平台 不同语言实现Web Service遵循的共同协议 ,在解析XML时按照各自语言的特点解析成相应的具体类.方法.参数和数据类型. WSDL是一个XML ...

  8. 06 java中常量以及常量池

    1.举例说明 变量 常量 字面量 int a=10; float b=1.234f; String c="abc"; final long d=10L; a,b,c为变量,d为常量 ...

  9. 栈的简单应用 HDU 1022 http://acm.hdu.edu.cn/showproblem.php?pid=1022

    #include<stdio.h> #include<stack> #include<string.h> #define N 20 using namespace ...

  10. 用 JavaScript 修改样式元素

    利用 <style> 元素,我们可以在网页中嵌入样式表.如果需要动态增加 <style> 元素,似乎可以用如下的 JavaScript 代码: var style = docu ...