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 ;
}

POJ 1637 Sightseeing tour (混合图欧拉回路)的更多相关文章

  1. POJ 1637 Sightseeing tour ★混合图欧拉回路

    [题目大意]混合图欧拉回路(1 <= N <= 200, 1 <= M <= 1000) [建模方法] 把该图的无向边随便定向,计算每个点的入度和出度.如果有某个点出入度之差为 ...

  2. poj 1637 Sightseeing tour 混合图欧拉回路 最大流 建图

    题目链接 题意 给定一个混合图,里面既有有向边也有无向边.问该图中是否存在一条路径,经过每条边恰好一次. 思路 从欧拉回路说起 首先回顾有向图欧拉回路的充要条件:\(\forall v\in G, d ...

  3. POJ 1637 Sightseeing tour(混合图的欧拉回路)

    题目链接 建个图,套个模板. #include <cstdio> #include <cstring> #include <iostream> #include & ...

  4. poj1637 Sightseeing tour(混合图欧拉回路)

    题目链接 题意 给出一个混合图(有无向边,也有有向边),问能否通过确定无向边的方向,使得该图形成欧拉回路. 思路 这是一道混合图欧拉回路的模板题. 一张图要满足有欧拉回路,必须满足每个点的度数为偶数. ...

  5. POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)

                                                                Sightseeing tour Time Limit: 1000MS   Me ...

  6. POJ 1637 Sightseeing tour 建图+网络流

    题意: 给定一个混合图,所谓混合图就是图中既有单向边也有双向边,现在求这样的图是否存在欧拉回路. 分析: 存在欧拉回路的有向图,必须满足[入度==出度],现在,有些边已经被定向,所以我们直接记录度数即 ...

  7. poj 1637 Sightseeing tour —— 最大流+欧拉回路

    题目:http://poj.org/problem?id=1637 建图很妙: 先给无向边随便定向,这样会有一些点的入度不等于出度: 如果入度和出度的差值不是偶数,也就是说这个点的总度数是奇数,那么一 ...

  8. poj1637 Sightseeing tour 混合图欧拉回路判定

    传送门 第一次做这种题, 尽管ac了但是完全不知道为什么这么做. 题目就是给一些边, 有向边与无向边混合, 问你是否存在欧拉回路. 做法是先对每个点求入度和出度, 如果一条边是无向边, 就随便指定一个 ...

  9. poj 1637 Sightseeing tour——最大流+欧拉回路

    题目:http://poj.org/problem?id=1637 先给无向边随便定向,如果一个点的入度大于出度,就从源点向它连 ( 入度 - 出度 / 2 ) 容量的边,意为需要流出去这么多:流出去 ...

随机推荐

  1. C#委拖小例子

    委托具有以下属性: 委托类似于 C++ 函数指针,但它们是类型安全的. 委托允许将方法作为参数进行传递. 委托可用于定义回调方法. 委托可以链接在一起:例如,可以对一个事件调用多个方法. 方法不必与委 ...

  2. unity, 顶点对齐

    按住v键,选中物体的一个顶点,可以对齐到其它物体的某个顶点上. 参考https://docs.unity3d.com/Manual/PositioningGameObjects.html

  3. MySQL物理文件组成

    日志文件 错误日志:Error Log 错误日志记录了MySQL运行过程中所有较为严重的警告和错误信息,以及MySQL Server每次启动和关闭的详细信息.在默认情况下,系统记录错误日志的功能是关闭 ...

  4. pt-fifo-split使用

    percona-toolkit系列-pt-find http://blog.itpub.net/23249684/viewspace-1354308/ 在<mysql插入/更新数据>这篇文 ...

  5. HA(High available)--Heartbeat高可用性集群(双机热备)菜鸟入门级

    HA(High available)--Heartbeat高可用性集群(双机热备)   1.理解:两台服务器A和B ,当A提供服务,B闲置待命,当A服务宕机,会自动切换至B机器继续提供服务.当主机恢复 ...

  6. Google Tensorflow 源码编译(一):Protobuf<v3.0.0-alpha-3>

    这几天终于把tensorflow安装上了,中间遇到过不少的问题,这里记录下来.供大家想源码安装的参考. 安装环境:POWER8处理器,Docker容器Ubuntu14.04镜像. Build Prot ...

  7. ruby的加密方法整理(des rsa加密 加签)

    # coding:utf-8require 'openssl'require 'base64'#des加密并且base64编码def des_encrypt des_key, des_text des ...

  8. CSAPP(前言)

    很久之前就听过有过CSAPP这本书的传闻了,今天终于决定上手这本神作:既然是神作,就要仔细拜读一下,今天看了一下前言部分还真的令人耳目一新,单单是前言部分就让我学习到几个新的知识点: 1.c和Java ...

  9. Linux卷配置管理

    [root@linux ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/sda3 19G 11G 6.5G 63% /tmpfs 995 ...

  10. Codeforces 723d [暴力dfs]

    /* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给n,m和k,n和m为所给矩阵的高和宽.k是要求最多剩下的湖的数量. 在所给的矩阵中,*代表陆地,.代表水. 湖的定义是一片连续的水(上下左右四 ...