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. 安装Oracle之后解决掉的问题分享

    TNS-03505: 无法解析名称                                                            在测试tnsping的时候始终显示这么个问题. ...

  2. ps aux命令解析

    auxa 显示所有与终端相关的进程,由终端发起的.x 显示所有与终端无关的进程.u 显示用户导向的用户列表.VSZ 虚拟内存集,进程占用的虚拟内存空间RSS 物理内存集,进程战用实际物理内存空间.S ...

  3. 解决WordPress 页面无法评论的问题

    最近在使用WordPress制作一个企业网站,因为是企业网站所以文章和页面都不需要评论功能,因此在主题里禁用掉了评论功能 //禁用页面和文章的评论功能//add_filter('the_posts', ...

  4. php中对MYSQL操作之预处理技术(1)数据库dml操作语句

    <?php //预处理技术 //创建一个mysqli对象 $mysqli = new MySQLi("主机名","mysqlusername"." ...

  5. [Android系列—] 2. Android 项目文件夹结构与用户界面的创建

    前言 在 [Android系列-] 1. Android 开发环境搭建与Hello World 这一篇中介绍了怎样高速搭建Android开发环境, 并成功了建立一个没有不论什么代码更改的 Androi ...

  6. Install Identity management Database

    Install Identity management Database         Installing Oracle Fusion Applications > Setting up I ...

  7. 记dynamic的一个小坑 -- RuntimeBinderException:“object”未包括“xxx”的定义

    创建一个控制台程序和一个类库, 在控制台创建一个匿名对象.然后再在类库中訪问它.代码例如以下: namespace ConsoleApplication1 { class Program { stat ...

  8. 修改字段结构之GP工具

    即然有这个需求,就有人这样做.有人写了GP工具直接来重命名字段名和字段别名.工具及源码下载链接为:http://www.t00y.com/file/90123888 加载到ToolBox中后,可直接运 ...

  9. Ipad也怕冷?!

    今天,说一Ipad充不了电,我想才没买好久,这么快电池就坏了呀.难道买到歪货了? 它的表现是充电线一接上去,电池指示有反应,也有"闪电"标志,就是充不进去电.本来想打客服的,还是先 ...

  10. MyEclipse开发Rest服务入门

    MyEclipse支持Rest服务,开发起来非常方便,下面我就举一个计算机的例子: 实现功能:加.减.乘.除: 效果如下: Rest服务要点:每个服务或任何东西都有一个URI: 步骤1:创建Web S ...