Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5792 | Accepted: 2357 |
Description
Input
Output
Sample Input
4 5 8 2 1 0 1 3 0 4 1 1 1 5 0 5 4 1 3 4 0 4 2 1 2 2 0 4 4 1 2 1 2 3 0 3 4 0 1 4 1 3 3 1 2 0 2 3 0 3 2 0 3 4 1 2 0 2 3 1 1 2 0 3 2 0
Sample Output
possible impossible impossible possible
#include
#include
#include
#include
#include
#include
using namespace std;
#define MAXN 255
#define INF 0xFFFFFF
struct edge
{
int to,c,next;
};
struct edge2
{
int u,v,ty;
};
edge e[99999];
edge2 ee[99999];
int deg[MAXN];
int que[MAXN*100],dis[MAXN],pre[MAXN];
int head[MAXN],head2[MAXN];
int st,ed,maxflow;
int en,n,m;
void add(int a,int b,int c)
{
e[en].to=b;
e[en].c=c;
e[en].next=head[a];
head[a]=en++;
e[en].to=a;
e[en].c=0;
e[en].next=head[b];
head[b]=en++;
}
bool bfs()
{
memset(dis,-1,sizeof(dis));
que[0]=st,dis[st]=1;
int t=1,f=0;
while(f0)
add(i,ed,deg[i]);
else
{
add(st,i,abs(deg[i]));
temp+=deg[i];
}
}
}
for(int i=1;i<=m;i++)
if(!ee[i].ty) add(ee[i].u,ee[i].v,1);
return temp;
}
void solve()
{
int a,b,c;
scanf("%d%d",&n,&m);
memset(deg,0,sizeof(deg));
st=0,ed=1+n;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
deg[a]--;
deg[b]++;
ee[i].u=a;
ee[i].v=b;
ee[i].ty=c;
}
for(int i=1;i<=n;i++)
{
if(deg[i]%2!=0)
{
printf("impossible\n");
return;
}
deg[i]/=2;
}
int fuflow=abs(build());
maxflow=0;
while(bfs()) dfs();
if(maxflow==fuflow)
printf("possible\n");
else
printf("impossible\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--) solve();
return 0;
}