HDU 3849 By Recognizing These Guys, We Find Social Networks Useful

pid=3849" target="_blank" style="">题目链接

题意:说白了就是求一个无向图的桥

思路:字符串hash掉,然后双连通。要注意特判一下假设不是一个连通块。那么答案是0

代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
using namespace std; const int N = 10005;
const int M = 200005;
int t, n, m;
map<string, int> hash;
char A[20], B[20]; struct Edge {
int u, v, id;
bool iscut;
Edge() {}
Edge(int u, int v, int id) {
this->u = u;
this->v = v;
this->id = id;
this->iscut = false;
}
} edge[M]; int en, first[N], next[M], hn;
char name[N][20]; void init() {
en = hn = 0;
memset(first, -1, sizeof(first));
hash.clear();
} int get(char *str) {
if (!hash.count(str)) {
strcpy(name[hn], str);
hash[str] = hn++;
}
return hash[str];
} void add_edge(int u, int v, int id) {
edge[en] = Edge(u, v, id);
next[en] = first[u];
first[u] = en++;
} int pre[N], dfn[N], dfs_clock, ans = 0; void dfs_cut(int u, int f) {
pre[u] = dfn[u] = ++dfs_clock;
for (int i = first[u]; i + 1; i = next[i]) {
if (edge[i].id == f) continue;
int v = edge[i].v;
if (!pre[v]) {
dfs_cut(v, edge[i].id);
dfn[u] = min(dfn[u], dfn[v]);
if (dfn[v] > pre[u]) {
ans++;
edge[i].iscut = edge[i^1].iscut = true;
}
} else dfn[u] = min(dfn[u], pre[v]);
}
} void find_cut() {
memset(pre, 0, sizeof(pre));
for (int i = 0; i < n; i++)
if (!pre[i]) dfs_cut(i, -1);
} int parent[N]; int find(int x) {
return parent[x] == x ? x : parent[x] = find(parent[x]);
}
int main() {
scanf("%d", &t);
while (t--) {
init();
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) parent[i] = i;
int cnt = n;
for (int i = 0; i < m; i++) {
scanf("%s%s", A, B);
int u = get(A), v = get(B);
add_edge(u, v, i);
add_edge(v, u, i);
int pu = find(u), pv = find(v);
if (pu != pv) {
cnt--;
parent[pu] = pv;
}
}
if (cnt > 1) {
printf("0\n");
continue;
}
ans = 0;
find_cut();
printf("%d\n", ans);
for (int i = 0; i < en; i += 2)
if (edge[i].iscut)
printf("%s %s\n", name[edge[i].u], name[edge[i].v]);
}
return 0;
}

HDU 3849 By Recognizing These Guys, We Find Social Networks Useful(双连通)的更多相关文章

  1. HDU 3849 By Recognizing These Guys, We Find Social Networks Useful

    By Recognizing These Guys, We Find Social Networks Useful Time Limit: 1000ms Memory Limit: 65536KB T ...

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

  3. hdu3849-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)     ...

  4. HDU3849-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)     ...

  5. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  6. HDU 4005 The war(双连通好题)

    HDU 4005 The war pid=4005" target="_blank" style="">题目链接 题意:给一个连通的无向图.每条 ...

  7. hdu 3849 (双联通求桥)

    一道简单的双联通求桥的题目,,数据时字符串,,map用的不熟练啊,,,,,,,,,,,,, #include <iostream> #include <cstring> #in ...

  8. hdu 3394(点双连通)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3394 思路:题目的意思是要求无向图中的冲突边和不需要边的条数,如果一个块中有多个环,则该块中的每条边都 ...

  9. hdu 4005(边双连通)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 思路:首先考虑边双连通分量,如果我们将双连通分量中的边删除,显然我们无法得到非连通图,因此要缩点 ...

随机推荐

  1. RUP---统一软件开发过程

    更详细的见:http://www.ibm.com/developerworks/cn/rational/r-rupbp/ 本文引用:http://baike.baidu.com/view/223583 ...

  2. Godaddy ssl续费更新问题总结

    之前客户在Godaddy 上购买的ssl证书过期了,但客户续费后打开https时却提示证书过期了 进行Godaddy 后台看到证书确实是过期的 但在账户里也确实看到ssl续费成功了 猜想可能是ssl续 ...

  3. 关于bootstrap-datetimepicker.js不支持IE8的解决办法

    if (!Array.prototype.indexOf) Array.prototype.indexOf = function (elt /*, from*/) { var len = this.l ...

  4. WIN7使用技巧 让电脑变身WiFi热点,让手机、笔记本共享上网,如何开启无线网络,共享无线网络

    将win7电脑变身WIFI热点,让手机.笔记本共享上网 用win7建立无线局域网,可以共享上网可以局域网游戏. 开启windows 7的隐藏功能:虚拟WIFI和SoftAP(即虚拟无线AP),就可以让 ...

  5. 87. 再谈变体型Variant

    在85. BASIC和LotusScript中的Variant一文中.我提到了BASIC风格的语言中的变体型Variant.由于下述种种原因.在LotusScript中常常要用到变体型. 1.     ...

  6. activemq无法启动且后台管理界面进不去的解决办法

    从官网下载了一个最新的activemq,目前最新版本是5.14.5 我下载的是windows版本,通过执行%activemq home%/bin/win64/InstallService.bat,可以 ...

  7. 霍纳法则(Horner Rule)介绍及C语言实现

    参考自:http://flynoi.blog.hexun.com/31272178_d.html 霍纳法则简介 假设有n+2个实数a0,a1,-,an,和x的序列,要对多项式Pn(x)= anxn+a ...

  8. JDK5.0 特性 监控与管理虚拟机

    来自:http://www.cnblogs.com/taven/archive/2011/12/17/2291465.html import java.lang.management.ClassLoa ...

  9. Android OpenGL ES(四)----调整屏幕的宽高比

    1.宽高比问题 我们如今相当熟悉这样一个事实,在OpenGL里,我们要渲染的一切物体都要映射到X轴和Y轴上[-1,1]的范围内,对于Z轴也一样.这个范围内的坐标被称为归一化设备坐标,其独立于屏幕实际尺 ...

  10. Socket实现服务器与客户端的交互

       连接过程:   根据连接启动的方式以及本地套接字要连接的目标,套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认. (1)服务器监听:是服务器端套接字并不定位具体的客户端套接 ...