网络流相关(拓扑)CodeForces 269C:Flawed Flow
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet — it calculates the maximum flow in an undirected graph. The graph consists of n vertices and m edges. Vertices are numbered from 1 to n. Vertices 1 and n being the source and the sink respectively.
However, his max-flow algorithm seems to have a little flaw — it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow.
More formally. You are given an undirected graph. For each it's undirected edge (ai, bi) you are given the flow volume ci. You should direct all edges in such way that the following conditions hold:
- for each vertex v (1 < v < n), sum of ci of incoming edges is equal to the sum of ci of outcoming edges;
- vertex with number 1 has no incoming edges;
- the obtained directed graph does not have cycles.
Input
The first line of input contains two space-separated integers n and m (2 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105), the number of vertices and edges in the graph. The following m lines contain three space-separated integers ai, bi and ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 104), which means that there is an undirected edge from ai to bi with flow volume ci.
It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists.
Output
Output m lines, each containing one integer di, which should be 0 if the direction of the i-th edge is ai → bi (the flow goes from vertex ai to vertex bi) and should be 1 otherwise. The edges are numbered from 1 to m in the order they are given in the input.
If there are several solutions you can print any of them.
Sample Input
3 3
3 2 10
1 2 10
3 1 5
1
0
1
4 5
1 2 10
1 3 10
2 3 5
4 2 15
3 4 5
0
0
1
1
0
可以发现这里有拓扑性质,可以直接做,O(N)复杂度。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N=,M=;
int cnt=,fir[N],nxt[M],to[M],cap[M];
int n,m,in[N],vis[N],ans[N];queue<int>q;
void addedge(int a,int b,int c){
nxt[++cnt]=fir[a];
to[fir[a]=cnt]=b;
cap[cnt]=c;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=,a,b,c;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);addedge(b,a,c);
in[a]+=c;in[b]+=c;
}
for(int i=;i<n;i++)in[i]/=;
q.push();in[]=;vis[]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=fir[x];i;i=nxt[i])
if(!vis[to[i]]){
in[to[i]]-=cap[i];
ans[i/]=i%;
if(in[to[i]]==){
q.push(to[i]);
vis[to[i]]=;
}
}
}
for(int i=;i<=m;i++)
printf("%d\n",ans[i]);
return ;
}
网络流相关(拓扑)CodeForces 269C:Flawed Flow的更多相关文章
- codeforces 269C Flawed Flow(网络流)
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious prog ...
- CodeForces - 269C Flawed Flow
http://codeforces.com/problemset/problem/269/C 题目大意: 给定一个边没有定向的无法增广的残量网络且1是源点,n是汇点,给定每条边中的流. 让你把所有边 ...
- Codeforces 269C Flawed Flow (看题解)
我好菜啊啊啊.. 循环以下操作 1.从队列中取出一个顶点, 把哪些没有用过的边全部用当前方向. 2.看有没有点的入度和 == 出度和, 如果有将当前的点加入队列. 现在有一个问题就是, 有没有可能队列 ...
- Codeforces 270E Flawed Flow 网络流问题
题意:给出一些边,给出边的容量.让你为所有边确定一个方向使得流量最大. 题目不用求最大流, 而是求每条边的流向,这题是考察网络流的基本规律. 若某图有最大,则有与源点相连的边必然都是流出的,与汇点相连 ...
- [bzoj1565][NOI2009]植物大战僵尸_网络流_拓扑排序
植物大战僵尸 bzoj1565 题目大意:给你一张网格图,上面种着一些植物.你从网格的最右侧开始进攻.每个植物可以对僵尸提供能量或者消耗僵尸的能量.每个植物可以保护一个特定网格内的植物,如果一个植物被 ...
- 网络流相关知识点以及题目//POJ1273 POJ 3436 POJ2112 POJ 1149
首先来认识一下网络流中最大流的问题 给定一个有向图G=(V,E),把图中的边看做成管道,边权看做成每根管道能通过的最大流量(容量),给定源点s和汇点t,在源点有一个水源,在汇点有一个蓄水池,问s-t的 ...
- Oracle Spatial 中的弧段及弧相关拓扑错误
1.报告说明 此报告用于验证下列问题: ORACLE SPATIAL 0.05m的最小拓扑容差值是否可以被修改 原始数据通过ARCGIS入库数据精度是否有损失 修改ORACLE SPATIAL图层的最 ...
- @codeforces - 708D@ Incorrect Flow
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个有源点与汇点的图 G,并对于每一条边 (u, v) 给定 ...
- [模板] 网络流相关/最大流ISAP/费用流zkw
最大流/ISAP 话说ISAP是真快...(大多数情况)吊打dinic,而且还好写... 大概思路就是: 在dinic的基础上, 动态修改层数, 如果终点层数 \(>\) 点数, break. ...
随机推荐
- memcached 安装
安装 memcached 需要 三部1,下载 memcached 放到php目录将php_memcached.dll 放到php的ext 目录 2,打开管理员命令,将memcached 拖拉到命令中, ...
- ssh 无密码登录远程服务器
在讲下文之前,我都默许大家都已经生成了自己的ssh公钥和密钥,在自己的~/.ssh 目录下面,如果没有,请使用 ssh-keygen -t rsa -C "你的邮箱" 命令生成 1 ...
- 让Sql语句区分大小写
除非你使用 LIKE 来比较字符串,否则MySQL的WHERE子句的字符串比较是不区分大小写的. 你可以使用 BINARY 关键字来设定WHERE子句的字符串比较是区分大小写的. SELECT * f ...
- yieId浅谈
例子:在不使用yieId时,通常我们都会采取先遍历再把元素加到新的List中 using (var reader = SqlHelper.ExecuteReader("")) { ...
- hibernate使用sql语句查询实体时,要写上addEntity
abDAO.getSession().createSQLQuery(hql).addEntity(对象.class).list(); 参考http://blog.csdn.net/vacblog/ar ...
- ios专题 - 安全
iOS通过以下几种机制来保全整个系统的安全性: 一:系统结构 所有iOS设备中,系统与硬件都高度集成,从系统启动.系统更新.应用的安装.应用的运行时等多个方面来保全系统的安全,具体包括: 1:所有iO ...
- CSS3 文本装饰
浏览器对CSS3文本特性的支持情况,如下表所示: 浏览器 text-shadow text-overflow word-wrap hyphens Opera 9.5+ 9+.带前缀-o- 10.5+ ...
- Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)
一, GNU工具链简介: (1)编译代码步骤: 预处理 -> 编译 -> 汇编 -> 链接: 预处理:去掉注释,进行宏替换,头文件包含等工作: gcc -E test.c -o te ...
- [设计模式]解释器(Interpreter)之大胆向MM示爱吧
为方便读者,本文已添加至索引: 设计模式 学习笔记索引 写在前面 “我刚写了个小程序,需要你来参与下.”我把MM叫到我的电脑旁,“来把下面这条命令打进去,这是个练习打(Pian)符(ni)号(de)的 ...
- python模块学习 hashlib
一.hashlib概述 涉及加密服务:14. Cryptographic Services 其中 hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法借口,如SHA1.SHA224.SHA2 ...