dfs:

 /*
dfs
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<math.h>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int maxn = ;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-; int vis[ maxn ];
struct Edge{
int u,next;
}edge[ maxn<< ];
int cnt,head[ maxn ];
void init(){
memset( vis,-,sizeof( vis ) );
cnt = ;
memset( head,-,sizeof( head ) );
}
void addedge( int a,int b ){
edge[ cnt ].u = b;
edge[ cnt ].next = head[ a ];
head[ a ] = cnt++;
} bool flag;
int ans;
void dfs( int cur ){
if( flag ) return ;
vis[ cur ] = ;
for( int i=head[ cur ];i!=-;i=edge[i].next ){
int nxt = edge[i].u;
if(nxt == ans){
flag = true;
return;
}
if(vis[ nxt ] == )continue;
if( flag )return;
dfs( nxt );
}
} int main(){
int T;
scanf("%d",&T);
int Case = ;
while( T-- ){
int n,m;
scanf("%d%d",&n,&m);
int edge_a,edge_b;
scanf("%d%d",&edge_a,&edge_b);
int x,y;
init();
while( edge_a-- ){
scanf("%d%d",&x,&y);
x++,y++;
y += n;
addedge( x,y );
}
while( edge_b-- ){
scanf("%d%d",&x,&y);
x++,y++;
x += n;
addedge( x,y );
}
flag = false; for( int i=;i<=n;i++ ){
memset(vis,,sizeof(vis));
ans = i;
dfs(i);
if(flag)break;
/*
if( flag==false&&vis[i]==-1 ){
dfs( i,-1 );
}
*/
}
printf("Case %d: ",Case ++ );
if( flag ) printf("Possible\n");
else printf("Impossible\n");
}
return ;
}

FZU-1924+判断环/DFS/BFS的更多相关文章

  1. FZU 1924——死锁——————【topo判环】

    死锁 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pr ...

  2. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  3. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  4. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  5. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  6. Floyd判断环算法总结

    Floyd判断环算法 全名Floyd’s cycle detection Algorithm, 又叫龟兔赛跑算法(Floyd's Tortoise and Hare),常用于链表.数组转化成链表的题目 ...

  7. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  8. HDU 2647 Reward(拓扑排序+判断环+分层)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...

  9. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

随机推荐

  1. stl中的map数据类型

    1.1 STL map 1.1.1 背景 关联容器使用键(key)来存储访问读取元素,而顺序容器则通过元素在容器中的位置存储和访问元素. 常见的顺序容器有:vector.list.deque.stac ...

  2. PHP学习笔记 - 进阶篇(5)

    PHP学习笔记 - 进阶篇(5) 正则表达式 什么叫正则表达式 正则表达式是对字符串进行操作的一种逻辑公式,就是用一些特定的字符组合成一个规则字符串,称之为正则匹配模式. $p = '/apple/' ...

  3. 前端性能优化工具--DOM Monster

    当我们开发web应用的时候,性能是一个永远不能回避的问题.其实对于DOM的性能调试也是一个不可或缺的过程.使用DOM monster你只需要添加到你的”书签中“,在任何需要调试的页面点击这个书签,它就 ...

  4. Libcurl笔记二

    一: multi与easy接口的不同处The multi interface offers several abilities that the easy interface doesn't. The ...

  5. C++ sizeof操作符的用法和strlen函数的区别

    摘要:本人首先介绍了C++中sizeof操作符的用法和注意事项,其次对比了和strlen的区别和使用,方便大家在写代码的时候查阅,和面试.笔试的时候复习. 目录: sizeof的用法: sizeof和 ...

  6. Poj 2840 Big Clock

    1.Link: http://poj.org/problem?id=2840 2.Content: Big Clock Time Limit: 1000MS   Memory Limit: 13107 ...

  7. linux开机启动增加tomcat启动项

    需求:开发环境(linux)重启后,每次需手动启动相关应用较为繁琐,如设置为开机自动启动则可减少此工作量. google下,参考了以下博文较好解决了问题: 1. 简单说明 Centos下设置程序开机自 ...

  8. C#,PHP对应加密函数

    require_once "JunDes.php"; $jDes=new JunDes(); echo $jDes->encode('98765'); //echo $jDe ...

  9. [Linux]学习笔记(3)-uname的用法

    uname的用法如下: uname –a[--all]:输出全部信息 [root@linuxforlijiaman ~]# uname -a Linux linuxforlijiaman -.el6. ...

  10. PHP中CURL技术模拟登陆抓取网站信息,用与微信公众平台成绩查询

    伴随微信的红火,微信公众平台成为许多开发者的下一个目标.笔者本身对于这种新鲜事物没有如此多的吸引力.但是最近有朋友帮忙开发微信公众平台中一个成绩查询的功能.于是便在空余时间研究了一番. 主要的实现步骤 ...