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)
什么是流量工程 流量工程是指根据各种数据业务流量的特性选取传输路径的处理过程.流量工程用于平衡网络中的不同交换机.路由器以及链路之间的负载. [编辑] 流量工程的内容 流量工程在复杂的网络环境中,控制 ...
随机推荐
- [Linux]第一部分-认识Linux及Linux主机规划与安装
ctrl + alt + f1~f6 切换六个终端ctrl + alt + f7 图形化界面 startx 开启x-window桌面 ls -al /root 列出root目录 date日期 +%y/ ...
- HDU 4767
昨晚苦恼了一晚,因为即将大三了,必须要准备实习什么的事了.一般都会去公司实习吧,但是看看自己的简历,实在拿不出手,因为大一大二都在搞ACM(虽然真正搞的只有大二一年),但却没有什么成绩,又不愿意做项目 ...
- spring4和hibernate4.0.0的整合
1.在myeclipse以下创建一个javaproject或者webproject,我创建的时webproject,用的myeclipse2013 2.导入spring的依赖包 3.导入hiberna ...
- Nginx 做系统的前端反向proxy
Nginx是一款很优秀的基于event的webserver.吞吐量大.占用资源少,只是文档就很让人郁闷了,免费的Nginx和收费的Nginx+的文档共用一份,配置完之后才发现免费的Nginx启动某些命 ...
- Android通过Intent.ACTION_CLOSE_SYSTEM_DIALOGS监听Home按键消息
Android对屏幕下方经常使用的四个按键消息处理是不一致的: 1.搜索按键的消息在onKeyDown或者onKeyUp中接收: 2.菜单按键的消息在onCreateOptionsMenu.onKey ...
- Linux下安装PHP7.1并做关联APACHE处理
1.复制php压缩包到/usr/local/src 2.解压 tar -zxvf php-7.1.2.tar.gz 3.编译安装(请先安装apache和mysql) ./configure --pre ...
- swift-判断是否已获得相机、相册权限
// 相机权限 func isRightCamera() -> Bool { let authStatus = AVCaptureDevice.authorizationStatus(forMe ...
- 2017-3-3 leetcod 1 35 448
ACM退役了,接下来是考研的准备,刷刷leetcode保证不会生手,也算是调剂生活,初步计划是每天三题吧,希望可以坚持下去. 打算按照专题来做,先是Array....本来以为特别水,结果.... == ...
- mybatis的sql语句导致索引失效,使得查询超时
mybaitis书写sql需要特别注意where条件中的语句,否则将会导致索引失效,使得查询总是超时.如下语句会出现导致索引失效的情况: with test1 as (select count(C_F ...
- js判断传入时间和当前时间大小
//判断时间是否过期 function judgeTime(time){ var strtime = time.replace("/-/g", "/");//时 ...