您的位置:首页 > 理论基础 > 计算机网络

codeforces 269C Flawed Flow(网络流)

2013-07-06 20:12 232 查看
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 andn 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 aito 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.

题目大意:给出一张网络流构成的图,给出每对点之间的流量,求流的方向。

思路:直接套算法求网络流必须超时,注意到每个点的流入=流出,而流入+流出可以从给的数据中求出,那么流入等于总流量的一半,利用拓扑排序的思路即可在O(n+m)的时间内求出解。

#include <cstdio>
#include <cctype>
#include <stack>

const int MAXN = 200010;

int n, m, ecnt;
int a[MAXN], b[MAXN], inflow[MAXN], outflow[MAXN], direct[MAXN];
int head[MAXN], to[MAXN*2], next[MAXN*2], c[MAXN*2], from[MAXN*2];

inline int readint(){
char c = getchar();
while(!isdigit(c)) c = getchar();
int x = 0;
while(isdigit(c)){
x = x * 10 + c - '0';
c = getchar();
}
return x;
}

inline void addEdge(int &u, int &v, int &i){
int x = readint();
outflow[u] += x; outflow[v] += x;
to[ecnt] = v; c[ecnt] = x; from[ecnt] = i;
next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; c[ecnt] = x; from[ecnt] = i;
next[ecnt] = head[v]; head[v] = ecnt++;
}

void makeDirect(){
std::stack<int> st;
st.push(1); outflow[1] = 0;
while(!st.empty()){
int u = st.top(); st.pop();
for(int p = head[u]; p; p = next[p]){
int &v = to[p];
if(inflow[v] == outflow[v] && v != n) continue;
inflow[v] += c[p]; outflow[v] -= c[p];
if(inflow[v] == outflow[v] && v != n) st.push(v);
int x = from[p];
if(v == a[x]) direct[x] = 1;
}
}
}

int main(){
scanf("%d%d",&n,&m);
ecnt = 1;
for(int i = 0; i < m; ++i){
a[i] = readint();
b[i] = readint();
addEdge(a[i], b[i], i);
}
makeDirect();
for(int i = 0; i < m; ++i) printf("%d\n", direct[i]);
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: