poj 1438--One-way Traffic(边的双连通)
给定一个图,并给定边,a b c(c==1||c==2) 表示ab之间有c条边 求把尽可能多的有向边定向变成强联通图。
先把图当做无向图,加边时记录是否有边,dfs的时候不要把本没有的边用到!因为这个错了好多次。。。。然后就简单了,记录桥就可以了。
/**************************************************
Problem: 1438 User: G_lory
Memory: 5312K Time: 657MS
Language: C++ Result: Accepted
**************************************************/
#include <cstdio>
#include <cstring>
#include <iostream>
#define pk printf("KKK!\n"); using namespace std; const int N = 2005;
const int M = N * N; struct Edge {
int from, to, next;
int flag; // 1代表单向边 0代表没边 2代表双向边
int cut;
} edge[M];
int cnt_edge;
int head[N];
void add_edge(int u, int v, int c)
{
edge[cnt_edge].from = u;
edge[cnt_edge].to = v;
edge[cnt_edge].next = head[u];
edge[cnt_edge].flag = c;
edge[cnt_edge].cut = 0;
head[u] = cnt_edge++;
} int dfn[N]; int idx;
int low[N]; int n, m; void tarjan(int u, int pre)
{
dfn[u] = low[u] = ++idx;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].flag == 0) continue;
if (edge[i].cut == 0)
{
edge[i].cut = 1;
edge[i ^ 1].cut = -1;
}
if (v == pre) continue; if (!dfn[v])
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (dfn[u] < low[v])
{
edge[i].cut = 2;
edge[i ^ 1].cut = -1;
}
}
else low[u] = min(low[u], dfn[v]);
}
} void init()
{
idx = cnt_edge = 0;
memset(dfn, 0, sizeof dfn);
memset(head, -1, sizeof head);
} void solve()
{
for (int i = 0; i < cnt_edge; ++i)
if (edge[i].flag == 2 && (edge[i].cut == 1 || edge[i].cut == 2))
printf("%d %d %d\n", edge[i].from, edge[i].to, edge[i].cut);
} int main()
{
//freopen("in.txt", "r", stdin);
while (~scanf("%d%d", &n, &m))
{
if (n == 0 && m == 0) break;
int u, v, c;
init();
for (int i = 0; i < m; ++i)
{
scanf("%d%d%d", &u, &v, &c);
add_edge(u, v, c);
if (c == 1) c = 0;
add_edge(v, u, c);
}
tarjan(1, -1);
solve();
}
return 0;
}
poj 1438--One-way Traffic(边的双连通)的更多相关文章
- poj 3177 Redundant Paths(tarjan边双连通)
题目链接:http://poj.org/problem?id=3177 题意:求最少加几条边使得没对点都有至少两条路互通. 题解:边双连通顾名思义,可以先求一下连通块显然连通块里的点都是双连通的,然后 ...
- poj 3694 Network 边双连通+LCA
题目链接:http://poj.org/problem?id=3694 题意:n个点,m条边,给你一个连通图,然后有Q次操作,每次加入一条边(A,B),加入边后,问当前还有多少桥,输出桥的个数. 解题 ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- POJ 3177 Redundant Paths (边双连通+缩点)
<题目链接> <转载于 >>> > 题目大意: 有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新 ...
- POJ - 3177 Redundant Paths (边双连通缩点)
题意:在一张图中最少可以添加几条边,使其中任意两点间都有两条不重复的路径(路径中任意一条边都不同). 分析:问题就是最少添加几条边,使其成为边双连通图.可以先将图中所有边双连通分量缩点,之后得到的就是 ...
- POJ 3177 边双连通求连通量度的问题
这道题的总体思路就是找到连通量让它能够看作一个集合,然后找这个集合的度,度数为1的连通量为k,那么需要添加(k+1)/2条边才可以保证边双连通 这里因为一个连通量中low[]大小是相同的,所以我们用a ...
- 算法笔记_150:图论之双连通及桥的应用(Java)
目录 1 问题描述 2 解决方案 1 问题描述 Description In order to get from one of the F (1 <= F <= 5,000) graz ...
- POJ3352 Road Construction Tarjan+边双连通
题目链接:http://poj.org/problem?id=3352 题目要求求出无向图中最少需要多少边能够使得该图边双连通. 在图G中,如果任意两个点之间有两条边不重复的路径,称为“边双连通”,去 ...
- poj1515--Street Directions(边的双连通)
给一个无向图,要求变成强连通的有向图,需要保留哪些边. 边的双连通,对于桥保留两条边,其他的只保留一条边.求双连通的过程中记录保留边. /******************************* ...
- poj3177--Redundant Paths(边的双连通)
有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路.两条独立的路是指:没有公共边的路,但可以 ...
随机推荐
- extern "C"的作用
一.概述 在C语言的头文件中,经常可以看到如下的代码,那这个是什么作用呢? #ifdef __cplusplus extern "C" { #endif /*...*/ #ifde ...
- 视图--bai
/*视图的必要性 create view population_all_view as select xxxx 详细信息 from qgck where rownum<500 -- sql语句不 ...
- delphi xe5 android 控制蓝牙
本文部分内容摘自: http://www.pclviewer.com/android/用以下代码中的接口实现控制蓝牙的开.关及详细信息 unit Androidapi.JNI.BluetoothAda ...
- uva 1476 - Error Curves
对x的坐标三分: #include<cstdio> #include<algorithm> #define maxn 10009 using namespace std; do ...
- 关于.net那点事儿
.NET是什么? .NET是开发“托管”软件的平台. 传统环境和.NET环境区别: 传统环境——先将源代码编译为包含机器代码的可执行文件,然后由操作系统加载和执行可执行文件. .NET环境——编译器首 ...
- Linux内核学习笔记: uid之ruid,euid,suid
转自: http://www.linuxidc.com/Linux/2011-09/43194.htm 看UNIX相关的书时经常能遇到这几个概念,但一直没有好好去理清这几个概念,以致对这几个概念一直一 ...
- LinearLayout按下(pressed)或获取焦点(focused)时背景设置不同颜色或图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id=&qu ...
- in on at 总结
in,on,at的时间用法和地点用法 一.in, on, at的时间用法 ①固定短语: in the morning/afternoon/evening在早晨/下午/傍晚, at noon/night ...
- IPv6 tutorial – Part 7: Zone ID and unique local IPv6 unicast addresses
The zone ID is used to distinguish ambiguous link-local and site-local addresses. Unique local IPv6 ...
- Linq中的常用方法
System.Linq System.Linq.Enumerable 类 Range Repeat Reverse Select Where Sum Zip Aggregate Count Firs ...