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. ElasticSearch 深度分页解决方案

    常见深度分页方式 from+size 另一种分页方式 scroll scroll + scan search_after 的方式 es 库 scroll search 的实现 常见深度分页方式 fro ...

  2. elasticsearch(七)java 搜索功能Search Request的介绍与使用

    目录 1,首先创建主搜索请求: 2,对主搜索请求进行参数设置 前端几节都是介绍的基于单个文档或着单个文档库的操作, 本节开始将介绍基于所有或指定的任何个数文档库的操作的api SearchReques ...

  3. spring mvc流程理解

    1.controller处理的终究就是一个结果,默认是modelandview对象,controller里自己随便调用service或者dao,终究都还是在controller里有返回值. 2.  在 ...

  4. 【SSH2框架(理论篇)】--SSH2 Vs 经典三层

     这几天一直在学习使用SSH2框架.对于框架本身的使用并非非常困难.相信经过多锻炼就行熟练的掌握框架的使用,让我匪夷所思的是在使用框架的时候感觉非常熟悉,好像在哪里用过似得. 就在某次查看代码的时 ...

  5. [Tailwind] Apply mobile-first Responsive Classes in Tailwind

    In this lesson, we take a look at tailwind's mobile-first CSS architecture and learn how to apply st ...

  6. Zookeeper源代码编译为Eclipseproject(win7下Ant编译)

    为了深入学习ZooKeeper源代码,首先就想到将其导入到Eclispe中,所以要先将其编译为Eclispeproject. 1.什么是Ant??? Apache Ant™ Apache Ant is ...

  7. SSAO + FXAA

    如今已经完毕了渲染器的屏幕空间环境光遮挡(SSAO)算法和FXAA高速反走样算法,等有时间就把当中的相关原理和当中遇到的问题进行总结发表.

  8. NYOJ 145 聪明的小珂

    /* 题目大意:求解和输入数的互质的数 解题思路:求解和 n 互质的最大数.从n/2開始找 关键点:GCD函数的使用 解题人:lingnichong 解题时间:2014-10-04 16:11:55 ...

  9. DISCUZ站点DIY后,导致DIY功能失效,无法在前台删除已创建的DIY功能解决的方法

    DISCUZ站点DIY后.导致DIY功能失效,无法在前台删除已创建的DIY功能解决的方法.这是一个常常会遇到的问题.在程序调试过程中常常的会遇到这种问题.这里提供一个自己常常使用的解决的方法,供遇到这 ...

  10. 关于wait notify notifyall的学习心得

    wait()能让同步的线程挂起并将锁抛出,sleep只能使线程“睡了“,线程的锁并不会抛出,所以sleep还可以作用于非同步的线程.notify与notifyall能将被挂起或睡着的线程唤醒,但并不是 ...