Description
  In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal with a process, it will work until the ending of the processes. One day the computer got n processes to deal with. We number the processes from 1 to n. However there are some dependencies between some processes. When there exists a dependencies (a, b), it means process b must be finished before process a. By knowing all the m dependencies, Harry wants to know if the computer can finish all the n processes.
 
  题目大致就是说对一个有向图判断是否有环。BC 25 的A题,这场比赛很幸运的爆零了,我觉得是因为A题不停的写错导致整个人都乱了,看来以后要注意,如果第一个题就乱了的话要注意调整,不能影响之后的发挥。
  这个题目的题解是用的floyd写的,我使用dfs写的,貌似复杂度低一点。
 
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; bool vis[];
bool rem[];
int n,m;
int first[];
int next[];
int bs[],be[]; bool dfs(int x)
{
int t=first[x]; while(t)
{
if(rem[be[t]]) //这里要注意先判断。
return ; if(vis[be[t]]==)
{
vis[be[t]]=;
rem[be[t]]=; if(dfs(be[t])==)
return ;
rem[be[t]]=;
} t=next[t]; //不然会无限循环。
} return ;
} bool getans()
{
for(int i=;i<=n;++i)
if(vis[i]==)
{
memset(rem,,sizeof(rem)); //这里要注意清零。
rem[i]=;
vis[i]=;
if(dfs(i)==)
return ;
} return ;
} int main()
{
int a,b;
int temp; while(~scanf("%d %d",&n,&m))
{
memset(vis,,sizeof(vis));
memset(first,,sizeof(first));
memset(next,,sizeof(next)); for(int i=;i<=m;++i)
{
scanf("%d %d",&a,&b);
bs[i]=b;
be[i]=a;
temp=first[b];
first[b]=i;
next[i]=temp;
} if(getans())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
} return ;
}

(简单) HDU 5154 Harry and Magical Computer,图论。的更多相关文章

  1. hdu 5154 Harry and Magical Computer

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5154 Harry and Magical Computer Description In reward ...

  2. hdu 5154 Harry and Magical Computer 拓扑排序

    Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  3. HDU 5154 Harry and Magical Computer bfs

    Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  4. HDU 5154 Harry and Magical Computer 有向图判环

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5154 题解: 有向图判环. 1.用dfs,正在访问的节点标记为-1,已经访问过的节点标记为1,没有访 ...

  5. 【HDOJ】5154 Harry and Magical Computer

    拓扑排序. /* 5154 */ #include <iostream> #include <cstdio> #include <cstring> #include ...

  6. hdu 5154 拓扑排序

    例题:hdu 5154 链接  http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思是第一行先给出n和m表示有n件事,m个关系,接下来输入m行,每行有 ...

  7. BC Harry and Magical Computer (拓扑排序)

    Harry and Magical Computer  Accepts: 350  Submissions: 1348  Time Limit: 2000/1000 MS (Java/Others) ...

  8. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  9. BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...

随机推荐

  1. CodeForces 709B Checkpoints 模拟

    题目大意:给出n个点的坐标,和你当前的坐标,求走过n-1个点的最短路程. 题目思路:走过n-1个点,为了使路程更短,那么不走的点只可能第一个点或最后一个点.模拟就行了,比较恶心. #include&l ...

  2. linux的学习系列 7---管道和过滤器

    有时候,我们可以把两个命令连起来使用,一个命令的输出作为另一个命令的输入,这就叫做管道.为了建立管道,需要在两个命令之间使用竖线(|)连接. 管道是Linux进程之间一种重要的通信机制:除了管道,还有 ...

  3. bos项目经验心得(1)

    ---恢复内容开始--- 一.搭建数据库环境: 1.在cmd窗口登录mysql数据库: mysql -uroot -proot   (mysql登录数据库的形式就是 mysql-u用户名 -p密码) ...

  4. 80x86的3种工作方式

    80x86中的32位CPU全面支持32位的数据.指令和寻址方式,提供了3种工作方式:是地址方式.保护方式和保护方式下的虚拟8086方式.在计算机上电或复位后,32位CPU首先初始化为是地址方式,再通过 ...

  5. HDU-1072 Nightmare (bfs+贪心)

    Nightmare Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  6. java super关键字

    子类用super操作被隐藏的成员变量和方法 Example5_7.java class Sum { int n; float f() { float sum=0; for(int i=1;i<= ...

  7. CSS3中的skew()属性

    刚开始接触CSS3的2D变换属性,就被这个skew()搞的一头雾水,不知道具体是怎么变化的! 研究了一会才发现,CSS3的斜切坐标系和数学中的坐标系完全不一样(设置斜切原点为左上角) <styl ...

  8. 帝国cms文章内容tags关键词设置调用方法以及tags静态化

    说实话帝国cms用起来真的不好找一些功能,就比如说帝国cms的tag标签调用.需要注意的是帝国CMS文章的关键词和tags标签并非一码事,关键词标签是设置文章的关键词的,是用来给搜索引擎说明本篇文章的 ...

  9. 使用maven开发过程中,pom报的一些错的解决方法

    1.  maven-resources-plugin prior to 2.4 is not supported by m2e. Use maven-  resources-plugin versio ...

  10. windows 系统注册dll文件

    使用管理员身份注册:命令提示符 管理员身份运行 32 位系统:regsvr32 %windir%\system32\jscript.dll 64 位系统:regsvr32 %windir%\SysWO ...