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. fopen打开文件失败的问题

    fopen打开带中文路径或含中文名称的文件失败. 解决这个问题有两个方法:一是改用_wfopen,这个函数接受两个宽字符类型,函数原型如下: FILE* _wfopen(const wchar_t* ...

  2. read指令使用方法

    read命令用于从标准输入中读取输入单行,并将读取的单行根据IFS变量分裂成多个字段,并将分割后的字段分别赋值给指定的变量列表var_name.第一个字段分配给第一个变量var_name1,第二个字段 ...

  3. inotifywait实时监控文件目录

    一.inotify简介 inotify 是一种强大的.细粒度的.异步文件系统监控机制,它满足各种各样的文件监控需要,可以监控文件系统的访问属性.读写属性.权限属性.创建删除.移动等操作,也可以监控文件 ...

  4. ATM-lib-common

    import logging.configfrom conf import settingsfrom core import src def get_logger(name): logging.con ...

  5. 使用U盘给笔记本重做系统

    **一.戴尔 Vostro 14 3000 Series **1. 开机时快速按F12进入BIOS界面 **2. 按照下图进行一系列的处理,把U盘被设置为第一启动项 **3. 插入U盘后进入老毛桃PE ...

  6. spark入门: wordcount-java

    wordcount-java: pom.xml文件如下: <dependencies> <dependency> <groupId>junit</groupI ...

  7. configParser模块详谈

    前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser configPars ...

  8. django_mysql_配置

    配置 1. 安装Pymysql pip install PyMySQL 然后在项目同名_init__添加 from pymysql import install_as_MySQLdb install_ ...

  9. C++构造函数使用的多种方法

    // classes and uniform initialization #include <iostream> using namespace std; class Circle { ...

  10. 动态规划:HDU1059-Dividing(多重背包问题的二进制优化)

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...