Sightseeing tour

 

Description

The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visited exactly once. The bus should also start and end at the same junction. As in any city, the streets are either one-way or two-way, traffic rules that must be obeyed by the tour bus. Help the executive board and determine if it's possible to construct a sightseeing tour under these constraints.

Input

On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and s, 1 <= m <= 200,1 <= s <= 1000 being the number of junctions and streets, respectively. The following s lines contain the streets. Each street is described with three integers, xi, yi, and di, 1 <= xi,yi <= m, 0 <= di <= 1, where xi and yi are the junctions connected by a street. If di=1, then the street is a one-way street (going from xi to yi), otherwise it's a two-way street. You may assume that there exists a junction from where all other junctions can be reached.

Output

For each scenario, output one line containing the text "possible" or "impossible", whether or not it's possible to construct a sightseeing tour.

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<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f,MAXN=;
int in[MAXN],out[MAXN],m,s;
struct Edge
{
int from,to,cap,flow;
Edge(){}
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
};
struct dinic
{
int s,t;
vector<Edge>edges;
vector<int>G[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void init()
{
for(int i=;i<=m+;i++)G[i].clear();
edges.clear();
memset(in,,sizeof(in));
memset(out,,sizeof(out));
}
void addedge(int from,int to,int cap)
{
edges.push_back((Edge){from,to,cap,});
edges.push_back((Edge){to,from,,});
int m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool bfs()
{
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
d[e.to]=d[x]+;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t||a==)return a;
int flow=,f;
for(int& i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>)
{
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==)break;
}
}
return flow;
}
int maxflow(int s,int t)
{
this->s=s,this->t=t;
int flow=;
while(bfs())
{
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}dc;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
dc.init();
scanf("%d%d",&m,&s);
for(int i=;i<s;i++)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
out[u]++,in[v]++;
if(d==)dc.addedge(u,v,);
}
bool ok=;
int sum=;
for(int i=;i<=m;i++)
if((in[i]-out[i])%==)
{
ok=;
break;
}
if(!ok){puts("impossible");continue;}
for(int i=;i<=m;i++)
{
if(in[i]<out[i])
dc.addedge(,i,(out[i]-in[i])/);
else if(in[i]>out[i])
{
dc.addedge(i,m+,(in[i]-out[i])/);
sum+=(in[i]-out[i])/;
}
}
puts(sum==dc.maxflow(,m+)?"possible":"impossible");
}
return ;
}

最新文章

  1. Spring快速入门
  2. Silverlight动态生成控件实例
  3. ubuntu 14.04 下找不到命令,路径出错
  4. 好玩的代码之C++实现CPU满载
  5. SQL Server附加数据库文件出错
  6. EmguCV学习——简单使用
  7. Android 2D绘图初步
  8. 通信协议:HTTP、TCP、UDP(转)
  9. Navicat for MySQL11--使用经验
  10. Android Studio 错误 Duplicate files copied in APK META-INF/LICENSE.txt解决方案
  11. bzoj 1064 假面舞会 图论??+dfs
  12. [Swift]LeetCode265.粉刷房子 II $ Paint House II
  13. C# 多线程学习笔记 - 3
  14. mvc EF框架中,加载外键对象序列化对象时报错 序列化类型为XX的对象时检测到循环引用
  15. ORA-00444: background process DBRM failed while starting
  16. Grails 第一课
  17. Windbg解决系统蓝屏
  18. Nginx使用(配置开机启动)
  19. MacBook PRO蓝牙无法搜索设备
  20. Codeforces Round #295 (Div. 2)C - DNA Alignment 数学题

热门文章

  1. Android学习四:数据库操作
  2. import logging 导入记录日志包
  3. List remove注意点
  4. zookeeper系列之通信模型(转)
  5. 短作业优先调度算法(SJF)
  6. NeHe OpenGL教程 第四十六课:全屏反走样
  7. 关于http协议详解
  8. sudo执行脚本找不到环境变量
  9. Xcode常用的快捷键有哪些?
  10. 利用spring、cxf编写并发布webservice