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)
什么是流量工程 流量工程是指根据各种数据业务流量的特性选取传输路径的处理过程.流量工程用于平衡网络中的不同交换机.路由器以及链路之间的负载. [编辑] 流量工程的内容 流量工程在复杂的网络环境中,控制 ...
随机推荐
- javascript——从「最被误解的语言」到「最流行的语言」
JavaScript曾是"世界上最被误解的语言".由于它担负太多的特性.包含糟糕的交互和失败的设计,但随着Ajax的到来.JavaScript"从最受误解的编程语言演变为 ...
- json 与其他数据 格式转换及json学习新得
jsonobject var a={"a","A"} 通过json都对象能很轻松的操作json数据 jsonString var a=" ...
- webRequest
chrome.webRequest 描述: 使用 chrome.webRequest API 监控与分析流量,还可以实时地拦截.阻止或修改请求. 可用版本: 从 Chrome 17 开始支持. 权 ...
- Unity5.1 新的网络引擎UNET(七) UNET 单人游戏转换为多人
单人游戏转换为多人 孙广东 2015.7.12 本文档描写叙述将单人游戏转换为使用新的网络系统的多人游戏的步骤.这里描写叙述的过程是简化,对于一个真正的游戏事实上须要更高级别版本号的实际 ...
- 根据EXCEL模板填充数据
string OutFileName = typeName+"重点源达标率" + DateTime.Now.ToString("yyyy-MM-dd"); ...
- Oracle 11G R2 用exp无法导出空表解决方法
四. Oracle 10g以后增加了expdp和impdp工具,用此工具也可以导出空的表 oracleexpdp/impdp 用法详解 1) 创建逻辑目录,该命令不会在操作系统创建真正的目录,最好 ...
- DB-SQL-MySQL-杂项-调优:Mysql千万以上数据优化、SQL优化方法
ylbtech-DB-SQL-MySQL-杂项-调优:Mysql千万以上数据优化.SQL优化方法 1.返回顶部 1. 1,单库表别太多,一般保持在200以下为宜 2,尽量避免SQL中出现运算,例如se ...
- WEEX SDK集成到工程(Integrate to Android) #25
WEEX SDK集成到工程(Integrate to Android) #25 https://github.com/weexteam/article/issues/25
- Linux,Docker,Jenkins No such file or directory
你们先休息下,我先哭哭! 今天在做交接项目的bug修改的时候,在创建文件的时候报错 No such file or directory 然后跟着路径去linux中查看了该路径,但确实存在,并且权限都是 ...
- 原生js实现复选框
简单排版 <style> .box { display: flex; align-items: center; } #allSelect, p { width: 20px; height: ...