UVALive 2664 One-way traffic
One-way traffic
This problem will be judged on UVALive. Original ID: 2664
64-bit integer IO format: %lld Java class name: Main
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
n
2000 and n - 1
m
n(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 a, b and c, where 1
a
n, 1
b
n, a
b 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
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
#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的更多相关文章
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
- UVALive 5412 Street Directions
Street Directions Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. ...
- Linux下按程序查实时流量 network traffic
实然看到下载速度多达几M/s,但实际上并没有什么占用带宽的进程. 相查看每个程序占用的网络流量, 但系统自带的 System Monitor 只能查看全局的流量, 不能具体看某个程序的...... k ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- Windows Azure Traffic Manager (5) Traffic Manager Overview
<Windows Azure Platform 系列文章目录> 笔者默默地看了一下之前写的Traffic Manager内容,已经差不多是3年前的文章了.现在Azure Traffic M ...
- Windows Azure Traffic Manager (6) 使用Traffic Manager,实现本地应用+云端应用的高可用
<Windows Azure Platform 系列文章目录> 注意:本文介绍的是使用国内由世纪互联运维的Azure China服务. 以前的Traffic Manager,背后的Serv ...
- 流量工程 traffic engineering (TE)
什么是流量工程 流量工程是指根据各种数据业务流量的特性选取传输路径的处理过程.流量工程用于平衡网络中的不同交换机.路由器以及链路之间的负载. [编辑] 流量工程的内容 流量工程在复杂的网络环境中,控制 ...
随机推荐
- ASP.Net MVC – What are the uses of Display, DisplayName, DisplayFormat and ScaffoldColumn attributes
http://www.codeproject.com/Articles/775220/ASP-Net-MVC-What-are-the-uses-of-Display-DisplayNa?utm_so ...
- 《大话操作系统——做坚实的project实践派》(2)
<大话操作系统--做坚实的project实践派>(2)
- 四、基于HTTPS协议的12306抢票软件设计与实现--水平DNS并发查询分享
一.基于HTTPS协议的12306抢票软件设计与实现--实现效果 二.基于HTTPS协议的12306抢票软件设计与实现--相关接口以及数据格式 三.基于HTTPS协议的12306抢票软件设计与实现-- ...
- MYSQL Training: MySQL I
让以admin身份登录.源代码: 非常easy的注入 在username输入 admin' OR '1'='1 OK.
- bzoj1786: [Ahoi2008]Pair 配对&&1831: [AHOI2008]逆序对
一个自以为很对的东西,我们往-1放的数肯定是不增的. 然后就预处理一下,假如i这个位置放j会多多少逆序对. DP一下,我的复杂度应该是O(n*m^2)的,然而你随便搞都能省掉一个m吧,我算了算好像可以 ...
- sql server 数据库展开变慢
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/99bbcb47-d4b5-4ec0-9e91-b1a23a655844/ssms-2 ...
- SQLSERVER 链接服务器
1. 执行代码 EXEC sp_addlinkedserver @server='XLZFSqlServer', --链接服务器别名 @srvproduct='', @provider='SQLOLE ...
- 今天,你Try-Catch了吗?
引言: 高级语言中的异常处理很好用,对于有可能出错的代码我们用Try-Catch包起来,就能保证系统健壮的运行了,但是你的Try-Catch用对了吗? 今天code review的时候,老板给我提了个 ...
- 尝试实现bootstrap3网格系统
这是一篇搁置了很久的博文,个人实现的关键代码如下: // 这是用sass实现的,只是大致实现了网格系统和offset的功能 $size_list: ( xs: 0, sm: 576, md: 992, ...
- 深入了解React组件重新渲染的条件和生命周期
React组件rerender的真正条件 当前组件的State中的属性改变时且当前组件的shouldcomponentupdate返回true,那么当前组件会rerender 组件的props中的任一 ...