HDU [P3849]
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]的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- dijkstra算法优先队列
d[i] 是起点到 I 节点的最短距离 void Dijkstra(int s) { priority_queue<P, vector<P>, greater<P> &g ...
- mysql 5.7 编译安装脚本。
此脚本尽量运行在centos 服务器上面,用于编译安装mysql 5.7 将此脚本和相应的软件 都放到/usr/local/src 目录下面 由于不能上传附件 所以需要把cmake-3.9.6.ta ...
- IP代理池之验证是否有效
IP代理池之验证是否有效 把proxy pool项目跑起来,但也不知道这些ip怎么用,爬虫的时候是否用代理去爬取,下面通过一个例子来看看. 代码如下: import requests PROXY_PO ...
- (ADO.NET)关于C#中“配置”sqlite问题
配置打引号,只是因为觉得只是一些小问题,在此记录一下,第一次遇到还真有点手足无措,昨天到今天~终于可以开始放肆的写sqlite了. 好,第一个问题是引用已下载的system.data.sqlite.d ...
- mysql不能登陆
前些天还正常运行的mysql,不知怎么就不能登陆了.错误提示为 :ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (1 ...
- [原]sencha touch之NavigationView
还是直接上代码,都是基本的几个容器控件,没什么还说的 Ext.application({ name:'itkingApp', launch:function(){ var view =Ext.crea ...
- C语言用一维数组打印杨辉三角(原:无意中想到)
本贴地址 ] = { }; a[] = , a[] = ; int i, j,m; ; i <= ; i++) //2-11 输出10行 { ; j > ; j--) //关键在这句,倒着 ...
- 2612. [FHZOI 2017]被窃的项链
2612. [FHZOI 2017]被窃的项链 ★★★ 输入文件:stolen_necklace.in 输出文件:stolen_necklace.out 简单对比时间限制:1 s 内存 ...
- 1568: [JSOI2008]Blue Mary开公司(超哥线段树)
1568: [JSOI2008]Blue Mary开公司 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 1198 Solved: 418 Descr ...
- 高亮T4模板
http://t4-editor.tangible-engineering.com/Download_T4Editor_Plus_ModelingTools.html