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. Template Method 模板方法 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 解决方案

    because regular C functions work differently than the Windows API functions; their "calling con ...

  3. c#写扩展方法

    学习MVC时,学会了写扩展方法,用起来很方便. 01 using System; 02 using System.Collections.Generic; 03 using System.Linq; ...

  4. IDA远程调试so库JNI_Onload函数

    JNI_OnLoad函数大概功能就是在程序加载so的时候,会执行JNI_OnLoad函数,做一系列的准备工作.很多时候,程序猿们会将一些重要信息放在此函数中,而不是通过某种事件来重复触发.包括说将反调 ...

  5. ECMAScript5之Object学习笔记(一)

    随着IE的逐步追赶,目前到IE11已经能够很好的支持ECMAScript5标准了,其他的现代浏览器像firefox,chrome,opera就更不用说了. 再加上nodejs使得javascript在 ...

  6. Smack 结合 Openfire服务器,建立IM通信,发送聊天消息

    在文章开始,请你了解和熟悉openfire方面的相关知识,这样对你理解下面代码以及下面代码的用途有很好的了解.同时,你可能需要安装一个简单的CS聊天工具,来测试你的代码是否成功的在openfire服务 ...

  7. 第十一节,命名空间namespace

    1,命名空间的定义 命名空间可以把不同的方法分散到不同的文件去实现,如果你会objective-C,他的作用和里面的类目有异曲同工之妙.当然了也有很多不同的地方,首先要明白的是,命名空间并不是一个类, ...

  8. Hibernate(六)一对多映射(多对一)

    一.Hinbernate中持久化类的关联关系 在数据库中,表表之间是通过外键关联的,在程序中是要转化为持久化类也就是(JAVA Bean)来实例的. 但在Hibernater中持久化的之间的映射关系, ...

  9. vue 项目的开发流程

    1.$ node -v (检测node版本,node版本需要在 V4 以上) 2.全局安装vue $ npm install -g vue 3.安装脚手架 $ npm install -g vue-c ...

  10. 解决openssh TimeOut

    SSH Client:ServerAliveInterval 100 SSH server:ClientAliveInterval 30TCPKeepAlive yes ClientAliveCoun ...