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. C#创建数字证书并导出为pfx,并使用pfx进行非对称加解密

    本文源程序下载:http://download.csdn.net/source/2444494 我的项目当中,考虑到安全性,需要为每个客户端分发一个数字证书,同时使用数字证书中的公私钥来进行数据的加解 ...

  2. JS 中数组字符串索引和数值索引研究

    先来看一个问题: var array = []; array["a"] = "hello"; array["b"] = "worl ...

  3. CentOS上编译安装OpenCV-2.3.1与ffmpeg-2.1.2

    已測试环境: CentOS 6.3 32bit CentOS 6.5 64bit 以前在CentOS 6.3 32bit安装过OpenCV,參见CentOS 6.3中安装OpenCV2.3.1,现在换 ...

  4. Maven deploy Return code is: 400

    Maven deploy Return code is: 400 学习了:https://blog.csdn.net/running_snail_/article/details/19821777 H ...

  5. com.esotericsoftware.kryo.kryoexception java.util.ConcurentModificationException

    近期 有网友看我的"整合Kafka到Spark Streaming--代码演示样例和挑战"文章, 讲 kafka对象 放到 pool 并通过broadcast广播出去: 然后 在开 ...

  6. java创建线程的三种方式及其对比

    第一种方法:继承Thread类,重写run()方法,run()方法代表线程要执行的任务.第二种方法:实现Runnable接口,重写run()方法,run()方法代表线程要执行的任务.第三种方法:实现c ...

  7. h5 微场景

    兔展: http://www.rabbitpre.com/ 易企秀: http://www.eqxiu.com/site/show 云来: http://www.liveapp.cn/

  8. Gulp构建前端自动化工作流之:常用插件介绍及使用

    在对Gulp有了一个初步的了解之后,我们开始构建一个较为完整的Gulp开发环境. 本文主要分为6个段落: 1. 构建项目目录结构(Directory Structure Build) 2. 插件介绍及 ...

  9. MVC MVP MVVM 图解

    1.MVC (1)图解 解释: 视图(View):用户界面. 控制器(Controller):业务逻辑 模型(Model):数据保存 各部分之间的通信方式如下: View 传送指令到 Controll ...

  10. Linux see 网卡当前流量

    linux see网卡的当前流量 sar –n DEV  1 2  命令后面1 2 意思是:每一秒钟取1次值,取2次. DEV显示网络接口信息 -n参数很有用,他有6个不同的开关:DEV | EDEV ...