269C - Flawed Flow - CodeForces Solution


constructive algorithms flows graphs greedy *2100

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int N=2e5;
const int M=5e5;
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
int n,m,ans[M+5],sum[N+5];
int head[N+5],ver[M+5],nxt[M+5],edge[M+5],tot=1;
void addedge(int x,int y,int z){
	ver[++tot]=y;edge[tot]=z;
	nxt[tot]=head[x];head[x]=tot;
}
queue<int>que;
void bfs(){
	que.push(1);
	while(!que.empty()){
		int x=que.front();que.pop();
		for(int i=head[x];i!=-1;i=nxt[i]){
			if(ans[i] || ans[i^1])continue;
			int y=ver[i];
			ans[i]=1;
			sum[y]-=edge[i];
			if(sum[y]==0 && y!=n)que.push(y);
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	memset(head,-1,sizeof(head));
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y,z;cin>>x>>y>>z;
		addedge(x,y,z);
		addedge(y,x,z);
		sum[x]+=z,sum[y]+=z;
	}
	for(int i=1;i<=n;i++)sum[i]/=2;
	bfs();
	for(int i=1;i<=m;i++)
		if(ans[i<<1|1])cout<<"1\n";
		else cout<<"0\n";
	return 0;
}
				   		 				  	 			 					


Comments

Submit
0 Comments
More Questions

46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory