tarjan 求 无向图的割边 (桥)

边 (x,y) 是桥当且仅当, 对于 x 的子节点 y ,low[x] < dfn[y]

对于连向父节点的边要特殊处理

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <string>
#pragma comment(linker, "/STACK:16777216")
#include <map>
using namespace std;
const int MAXN = 10005, MAXM = 200005;
int head[MAXN], nume, n, m, dfn[MAXN], low[MAXN], ind, cnt, T, tot;
map <string, int> Hash;
string s[MAXN];
bool f[MAXM];
struct edge{
int to, nxt;
}e[MAXM];
void adde(int from, int to) {
e[++nume].to = to;
e[nume].nxt = head[from];
head[from] = nume;
}
void tarjan(int u, int id) {
dfn[u] = low[u] = ++ind;
for(int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if(!dfn[v]) {
tarjan(v, i);
low[u] = min(low[u], low[v]);
if(low[v] > dfn[u]) {
f[i] = f[((i - 1) ^ 1) + 1] = 1;
}
}else if(i != ((id - 1) ^ 1) + 1) low[u] = min(low[u], dfn[v]);
}
}
int main() {
cin >> T;
while(T--) {
cin >> n >> m;
memset(head, 0, sizeof(head));
memset(f, 0, sizeof(f));
nume = ind = tot = cnt = 0;
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
Hash.clear();
for(int i = 1; i <= m; i++) {
string s1, s2;
cin >> s1 >> s2;
int u = Hash[s1];
if(!u) u = ++tot, s[u] = s1;
Hash[s1] = u;
int v = Hash[s2];
if(!v) v = ++tot; s[v] = s2;
Hash[s2] = v;
adde(u, v); adde(v, u);
}
tarjan(1, 0);
int fff = 1;
for(int i = 1; i <= tot; i++) if(!dfn[i]) {fff = 0;break;}
if(!fff) {
printf("0\n"); continue;
}
for(int i = 1; i <= nume; i++) if(f[i]) cnt++;
cnt /= 2;
cout << cnt << endl;
for(int i = 1; i < nume; i += 2) {
if(f[i]) cout << s[e[((i - 1) ^ 1) + 1].to] << " " << s[e[i].to] << endl;
}
}
}

HDU [P3849]的更多相关文章

  1. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  4. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  9. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

随机推荐

  1. Linux下C程序内存泄露检测

    在linux下些C语言程序,最大的问题就是没有一个好的编程IDE,当然想kdevelop等工具都相当的强大,但我还是习惯使用kdevelop工具,由于没有一个习惯的编程IDE,内存检测也就成了在lin ...

  2. iOS多播Delegate类——GCDMulticastDelegate用法小结

    iOS中通常的delegate模式只能有一个被委托的对象,这样当需要有多个被委托的对象时,实现起来就略为麻烦,在开源库XMPPFramework中提供了一个GCDMulticastDelegate类, ...

  3. JS MarcoTasks MicroTasks

    JS MarcoTasks MicroTasks 在JS的event loop中,有两种任务队列microtasks和macrotasks microtasks process.nextTick Pr ...

  4. 洛谷P3374树状数组1

    下有彩蛋(from https://www.cnblogs.com/wuwangchuxin0924/p/5921130.html)树状数组的blog写的最好的是这位//https://www.cnb ...

  5. 20181229(守护进程,互斥锁,IPC,生产者和消费者模型)

    一.守护进程 守护进程:一个进程B守护另一个进程A,当被守护的进程A结束,进程B也就结束了.(不一定同生,但会同死) 两个特点: ①守护进程会在主进程代码执行结束后就终止 ②守护进程内无法再开启子进程 ...

  6. Spark MLlib(下)--机器学习库SparkMLlib实战

    1.MLlib实例 1.1 聚类实例 1.1.1 算法说明 聚类(Cluster analysis)有时也被翻译为簇类,其核心任务是:将一组目标object划分为若干个簇,每个簇之间的object尽可 ...

  7. 如何排查Java内存泄漏?看完我给跪了!

    没有经验的程序员经常认为Java的自动垃圾回收完全使他们免于担心内存管理.这是一个常见的误解:虽然垃圾收集器做得很好,但即使是最好的程序员也完全有可能成为严重破坏内存泄漏的牺牲品.让我解释一下. 当不 ...

  8. mysql 外连接的时候,条件在on后面和条件在where后面的区别

    最近使用mysql的时候碰到一个问题:当一个表外联另一个表的时候,将一些查询条件放在on后面和放在where后面不太一样: 学生分数表stuscore: 当查询语句如下(查询语句1): SELECT ...

  9. Python 内置函数isinstance

    isinstance 用来判断对象的类型 isinstance(对象,类型/类型集合) 如果属于 返回True 不属于返回 False >>> a = 1 >>> ...

  10. python字符串内置用法,择选重要