One-way traffic

Time Limit: 3000ms
Memory Limit: 131072KB

This problem will be judged on UVALive. Original ID: 2664
64-bit integer IO format: %lld      Java class name: Main

 
In a certain town there are n intersections connected by two- and one-way streets. The town is very modern so a lot of streets run through tunnels or viaducts. Of course it is possible to travel between any two intersections in both ways, i.e. it is possible to travel from an intersection a to an intersection b as well as from b to a without violating traffic rules. Because one-way streets are safer, it has been decided to create as much one-way traffic as possible. In order not to make too much confusion it has also been decided that the direction of traffic in already existing one-way streets should not be changed.

Your job is to create a new traffic system in the town. You have to determine the direction of traffic for as many two-way streets as possible and make sure that it is still possible to travel both ways between any two intersections.

Write a program that:

  • reads a description of the street system in the town from the standard input,
  • for each two-way street determines one direction of traffic or decides that the street must remain two-way,
  • writes the answer to the standard output.

Input

The first line of the input contains two integers n and m, where 2n2000 and n - 1mn(n - 1)/2. Integer n is the number of intersections in the town and integer m is the number of streets.

Each of the next m lines contains three integers ab and c, where 1an, 1bnab and c belongs to {1, 2}. If c = 1 then intersections a and b are connected by an one-way street from a to b. If c = 2 then intersections a and b are connected by a two-way street. There is at most one street connecting any two intersections.

Output

The output contains exactly the same number of lines as the number of two-way streets in the input. For each such street (in any order) the program should write three integers ab and c meaning, the new direction of the street from a to b (c = 1) or that the street connecting a and b remains two-way (c = 2). If there are more than one solution with maximal number of one-way streets then your program should output any of them but just one.

Sample Input

4 4
4 1 1
4 2 2
1 2 1
1 3 2

Sample Output

2 4 1
3 1 2

Source

 
解题:将混合图中的无向边重定向使其成为强连通
 
好吧 解释下 为什么遇到桥,要反向吧!。。。因为不反向,就不能返祖,这样就无法强联通了。。。强连通必须保证low[u]<=dfn[u].
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc{
int to,next;
bool cut,print,flag;
}e[maxn*maxn];
int head[maxn],tot,n,m;
void add(int u,int v,int flag){
e[tot].to = v;
e[tot].flag = flag;
e[tot].cut = false;
e[tot].print = flag == ;
e[tot].next = head[u];
head[u] = tot++;
}
int low[maxn],dfn[maxn],idx;
void tarjan(int u,int fa){
bool flag = false;
low[u] = dfn[u] = ++idx;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa && !flag){
flag = true;
continue;
}
if(!dfn[e[i].to]){
tarjan(e[i].to,u);
low[u] = min(low[u],low[e[i].to]);
if(low[e[i].to] > dfn[u]) e[i].cut = e[i^].cut = true;
}else low[u] = min(low[u],dfn[e[i].to]);
}
}
void dfs(int u,int fa){
low[u] = dfn[u] = ++idx;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || !e[i].flag) continue;
e[i].flag = true;
e[i^].flag = false;
if(!dfn[e[i].to]){
dfs(e[i].to,u);
low[u] = min(low[u],low[e[i].to]);
if(low[e[i].to] > dfn[u]){
e[i].flag = false;
e[i^].flag = true;
}
} else low[u] = min(low[u],dfn[e[i].to]);
}
}
int main(){
int u,v,t;
while(~scanf("%d%d",&n,&m)){
memset(head,-,sizeof head);
for(int i = tot = ; i < m; ++i){
scanf("%d%d%d",&u,&v,&t);
if(t == ){
add(u,v,);
add(v,u,);
}else{
add(u,v,);
add(v,u,);
}
}
idx = ;
memset(dfn,,sizeof dfn);
tarjan(,-);
idx = ;
memset(dfn,,sizeof dfn);
dfs(,-);
for(int i = ; i < tot; i += )
if(e[i].print){
if(e[i].cut) printf("%d %d 2\n",e[i^].to,e[i].to);
else if(e[i].flag) printf("%d %d 1\n",e[i^].to,e[i].to);
else printf("%d %d 1\n",e[i].to,e[i^].to);
}
}
return ;
}

UVALive 2664 One-way traffic的更多相关文章

  1. 【暑假】[实用数据结构]UVAlive 3135 Argus

    UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

  2. UVALive 5412 Street Directions

    Street Directions Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. ...

  3. Linux下按程序查实时流量 network traffic

    实然看到下载速度多达几M/s,但实际上并没有什么占用带宽的进程. 相查看每个程序占用的网络流量, 但系统自带的 System Monitor 只能查看全局的流量, 不能具体看某个程序的...... k ...

  4. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  5. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  6. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  7. Windows Azure Traffic Manager (5) Traffic Manager Overview

    <Windows Azure Platform 系列文章目录> 笔者默默地看了一下之前写的Traffic Manager内容,已经差不多是3年前的文章了.现在Azure Traffic M ...

  8. Windows Azure Traffic Manager (6) 使用Traffic Manager,实现本地应用+云端应用的高可用

    <Windows Azure Platform 系列文章目录> 注意:本文介绍的是使用国内由世纪互联运维的Azure China服务. 以前的Traffic Manager,背后的Serv ...

  9. 流量工程 traffic engineering (TE)

    什么是流量工程 流量工程是指根据各种数据业务流量的特性选取传输路径的处理过程.流量工程用于平衡网络中的不同交换机.路由器以及链路之间的负载. [编辑] 流量工程的内容 流量工程在复杂的网络环境中,控制 ...

随机推荐

  1. [Design]Ppt处理大段文字

    可以用标签类的东西 时间轴

  2. 用户体验之如何优化你的APP

    用户体验,速度为王,来几个优化APP“速度”的建议. 1.后台执行 毋庸多言,已是通常做法. 一般在执行下载任务时让其在后台运营,让用户有精力去做别的事情. 后端加载 2.提前显示 客户端与WEB的数 ...

  3. 【MongoDB】The basic operation of Index in MongoDB

    In the past four blogs, we attached importance to the index, including description and comparison wi ...

  4. wpf获取webbroswer的两个方法

    //跳转前的地址 private void WebBrowser_BeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref ...

  5. node generator 模仿co

    exports.run = function(fn ){ return function(onDone){ function thunk(tfn , ctx){ return function(sql ...

  6. CALayer初认识

    CALayer :CA就是coreAnimation 核心动画 它是同时支持 Mac OS 和 iOS系统的 所有的核心动画都是通过CALayer来实现的 UIView本身是不具备显示功能的 是它内部 ...

  7. Core篇——初探Core的Http请求管道&&Middleware

    目录: 1.Core 处理HTTP请求流程 2.中间件(Middleware)&&处理流程 3.创建自定义中间件&&模拟Core的请求管道 Core 处理HTTP请求流 ...

  8. BZOJ2440: [中山市选2011]完全平方数(莫比乌斯+容斥原理)

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4920  Solved: 2389[Submit][Sta ...

  9. 详解循环神经网络(Recurrent Neural Network)

    本文结构: 模型 训练算法 基于 RNN 的语言模型例子 代码实现 1. 模型 和全连接网络的区别 更细致到向量级的连接图 为什么循环神经网络可以往前看任意多个输入值 循环神经网络种类繁多,今天只看最 ...

  10. 像素点的Hessian矩阵

    最近开始学习图像处理相关知识,碰到对像素点求黑塞矩阵查了资料才搞懂. 给定一个图像f(x,y)上的一点(x,y).其黑塞矩阵如下: 因为导数的公式是f'(x)=(f(x+dx)-f(x))/dx在数字 ...