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. UVa11235 RMQ

    input 1<=n,q<=100000 升序序列a1 a2 a3 ... an -100000<=ai<=100000 q行i j 1<=i,j<=n 输入结束标 ...

  2. linux的学习系列 10---vi

    Linux下的文本编辑器有很多种,vi 是最常用的,也是各版本Linux的标配.注意,vi 仅仅是一个文本编辑器,可以给字符着色,可以自动补全,但是不像 Windows 下的 word 有排版功能. ...

  3. sizeof和strlen

    1.char *str="0123456789"; 这个变量是存在静态区域的,是delete不了的,是内存自动分配的,可以用strlen(str)得到其长度,不能用sizeof. ...

  4. lucene中Field.Index,Field.Store的一些设置

    lucene在doc.add(new Field("content",curArt.getContent(),Field.Store.NO,Field.Index.TOKENIZE ...

  5. Linux学习 -- 启动管理

    1 CentOS 6.x启动管理 系统运行级别 运行级别命令 #runlevel   查看级别 #init 运行级别  改变级别 系统默认运行级别 配置文件 /etc/inittab id:3:ini ...

  6. 动态规划3-------poj1050

    首先还是对题目的意思进行说明,给出一个矩阵的数,然后求出一个子矩阵这个子矩阵包含的数的和是最大的.   首先对于题目进行转化,利用一个数组add进行存放临时数据,第一行存放原来数据的第一行,第二行存放 ...

  7. SVM阅读资料

    1,Andrew Ng机器学习公开课笔记 -- 支持向量机 2,http://blog.pluskid.org/?page_id=683 3,支持向量机SVM(一) 4,机器学习中的算法(2)-支持向 ...

  8. SQL Server 日志传送[转载]

    http://jimshu.blog.51cto.com/3171847/590413 SQL Server 2012 日志传送 一.准备: 数据库为完全恢复模式,并事先做一次完全备份. 共享一个文件 ...

  9. jquery获取页面相关尺寸

    $(windows).width();获取页面可视宽度 $(windows).height();获取页面可视高度 $(document).height();获取页面内容的总高度 $(document) ...

  10. java输出日期时间

    Calendar类下方法 c.add(Calendar.YEAR,4);//加4年 c.add(Calendar.MONTH,-1);// 月份减1 c.set(2012,2,23); //把时间设置 ...