1 #include<algorithm>
2 #include<iostream>
3 #include<cstring>
4 #include<cstdio>
5 #include<queue>
6 //by NeighThorn
7 #define inf 0x3f3f3f3f
8 #define M 60000*3
9 using namespace std;
10 //大鹏一日同风起,扶摇直上九万里
11
12 const int maxn=4000+5,maxm=60000*2+5;
13
14 int n,m,S,T,cnt,id[maxm],hd[maxn],to[maxm],fl[maxm],nxt[maxm],vis[maxn],pos[maxn],flag[maxm],ans1[maxm],ans2[maxm];
15
16 int C,tim,tail,mp[maxn],dfn[maxn],low[maxn],stk[maxn],instk[maxn];
17
18 inline void add(int s,int x,int y,int l){
19 fl[cnt]=s;id[cnt]=l;to[cnt]=y;nxt[cnt]=hd[x];flag[cnt]=1;hd[x]=cnt++;
20 fl[cnt]=0;id[cnt]=M;to[cnt]=x;nxt[cnt]=hd[y];flag[cnt]=0;hd[y]=cnt++;
21 }
22
23 inline bool bfs(void){
24 memset(pos,-1,sizeof(pos));
25 int head=0,tail=0,q[maxn];
26 q[0]=S,pos[S]=0;
27 while(head<=tail){
28 int top=q[head++];
29 for(int i=hd[top];i!=-1;i=nxt[i])
30 if(pos[to[i]]==-1&&fl[i])
31 pos[to[i]]=pos[top]+1,q[++tail]=to[i];
32 }
33 return pos[T]!=-1;
34 }
35
36 inline int find(int v,int f){
37 if(v==T)
38 return f;
39 int res=0,t;
40 for(int i=hd[v];i!=-1&&f>res;i=nxt[i])
41 if(pos[to[i]]==pos[v]+1&&fl[i])
42 t=find(to[i],min(f-res,fl[i])),fl[i]-=t,fl[i^1]+=t,res+=t;
43 if(!res)
44 pos[v]=-1;
45 return res;
46 }
47
48 inline void dinic(void){
49 while(bfs())
50 while(find(S,inf));
51 }
52
53 inline void tarjan(int root){
54 dfn[root]=low[root]=++tim;stk[++tail]=root;instk[root]=1;
55 for(int i=hd[root];i!=-1;i=nxt[i])
56 if(fl[i]){
57 if(dfn[to[i]]==0)
58 tarjan(to[i]),low[root]=min(low[root],low[to[i]]);
59 else if(instk[to[i]])
60 low[root]=min(low[root],dfn[to[i]]);
61 }
62 if(low[root]==dfn[root]){
63 int tmp;C++;
64 do{
65 tmp=stk[tail--];instk[tmp]=0;mp[tmp]=C;
66 }while(tmp!=root);
67 }
68 }
69
70 signed main(void){
71 memset(hd,-1,sizeof(hd));
72 memset(vis,0,sizeof(vis));
73 memset(low,0,sizeof(low));
74 memset(dfn,0,sizeof(dfn));
75 memset(instk,0,sizeof(instk));
76 scanf("%d%d%d%d",&n,&m,&S,&T);
77 for(int i=1,s,x,y;i<=m;i++)
78 scanf("%d%d%d",&x,&y,&s),add(s,x,y,i);
79 dinic();C=tim=tail=0;
80 for(int i=1;i<=n;i++)
81 if(!dfn[i])
82 tarjan(i);
83 for(int i=1;i<=n;i++)
84 for(int j=hd[i];j!=-1;j=nxt[j])
85 if(flag[j]&&fl[j]==0)
86 ans1[j]=(mp[i]==mp[to[j]]?0:1),ans2[j]=((mp[i]==mp[S]&&mp[to[j]]==mp[T])?1:0);
87 for(int i=0;i<cnt;i+=2)
88 printf("%d %d\n",ans1[i],ans2[i]);
89 return 0;
90 }//Cap ou pas cap. Cap.