#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int read()
{
int x=0,f=1; char ch=getchar();
while (ch<‘0‘ || ch>‘9‘) {if (ch==‘-‘) f=-1; ch=getchar();}
while (ch>=‘0‘ && ch<=‘9‘) {x=x*10+ch-‘0‘; ch=getchar();}
return x*f;
}
//quick read
#define maxn 4000+100
#define maxm (60000+100)*2
struct data{int from,to,next,cap;}edge[maxm];
int head[maxn],cnt=1,cur[maxn];
void add(int u,int v,int w)
{cnt++;edge[cnt].to=v;edge[cnt].from=u;
edge[cnt].next=head[u];head[u]=cnt;edge[cnt].cap=w;}
void insert(int u,int v,int w){add(u,v,w);add(v,u,0);}
//add edge
int n,m,s,t;int dis[maxn],q[maxn];
bool bfs()
{
for (int i=1; i<=n; i++) dis[i]=-1;
int he=0,ta=1; q[0]=s; dis[s]=0;
while (he<ta)
{
int now=q[he++];
for (int i=head[now]; i; i=edge[i].next)
if (dis[edge[i].to]==-1 && edge[i].cap)
dis[edge[i].to]=dis[now]+1,q[ta++]=edge[i].to;
}
return dis[t]!=-1;
}
int dfs(int loc,int low)
{
if (loc==t) return low;
int w,used=0;
for (int i=cur[loc]; i; i=edge[i].next)
if (dis[edge[i].to]==dis[loc]+1)
{
w=dfs(edge[i].to,min(low-used,edge[i].cap));
edge[i].cap-=w;edge[i^1].cap+=w;
if (edge[i].cap) cur[loc]=i;
used+=w; if (used==low) return low;
}
if (!used) dis[loc]=-1;
return used;
}
#define inf 0x7fffffff
int dinic()
{
int tmp=0;
while (bfs())
{
for (int i=1; i<=n; i++) cur[i]=head[i];
tmp+=dfs(s,inf);
}
return tmp;
}
//MaxFlow Dinic
int stack[maxn],st,qcnt,tot;
int dfn[maxn],loww[maxn],belong[maxn],num[maxn];bool visit[maxn];
void Tarjan(int x)
{
dfn[x]=loww[x]=++tot; stack[++st]=x; visit[x]=1;
for (int i=head[x]; i; i=edge[i].next)
if (edge[i].cap)//根据题意,在Tarjan的时候需要进行此判断,否则会出问题
{
if (!dfn[edge[i].to])
{
Tarjan(edge[i].to);
if (loww[edge[i].to]<loww[x]) loww[x]=loww[edge[i].to];
}
else if(visit[edge[i].to] && dfn[edge[i].to]<loww[x])
loww[x]=dfn[edge[i].to];
}
if (dfn[x]==loww[x])
{
qcnt++;int u;
while (x!=u)
u=stack[st--],num[qcnt]++,
visit[u]=0,belong[u]=qcnt;
}
}
//Tarjan SCC
int main()
{
n=read(),m=read(),s=read(),t=read();
for (int u,v,c,i=1; i<=m; i++)
u=read(),v=read(),c=read(),insert(u,v,c);
int maxflow=dinic();
for (int i=1; i<=n; i++)
if (!dfn[i]) Tarjan(i);
for (int ans1,ans2,u,v,i=2; i<=cnt; i+=2)
{
u=edge[i].from,v=edge[i].to;
if (edge[i].cap) ans1=0,ans2=0;
else
{
if (belong[u]!=belong[v])
ans1=1; else ans1=0;
if (belong[u]==belong[s] && belong[v]==belong[t])
ans2=1; else ans2=0;
}
printf("%d %d\n",ans1,ans2);
}
return 0;
}
本来应该做的飞快,然而...自己脑残打错两个变量...坑了好久....SB错误毁一生啊!!