【HDOJ】5154 Harry and Magical Computer
拓扑排序。
/* 5154 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <vector>
using namespace std; #define MAXN 105 vector<int> vec[MAXN];
vector<int>::iterator iter;
int cnt[MAXN];
int n, m; void init() {
int i; memset(cnt, , sizeof(cnt));
for (i=; i<=n; ++i)
vec[i].clear();
} bool topSort() {
int i, j, k, tmp;
int c = ;
queue<int> Q; for (i=; i<=n; ++i) {
if (cnt[i] == ) {
Q.push(i);
}
} while (!Q.empty()) {
++c;
i = Q.front();
Q.pop();
for (iter=vec[i].begin(); iter!=vec[i].end(); ++iter) {
if (--cnt[*iter] == ) {
Q.push(*iter);
}
}
} if (c < n)
return false;
else
return true;
} int main() {
int i, j, k;
bool flag; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif while (scanf("%d %d", &n, &m) != EOF) {
init();
for (i=; i<m; ++i) {
scanf("%d %d", &j, &k);
vec[j].push_back(k);
++cnt[k];
}
flag = topSort();
if (flag)
printf("YES\n");
else
printf("NO\n");
} return ;
}
【HDOJ】5154 Harry and Magical Computer的更多相关文章
- hdu 5154 Harry and Magical Computer
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5154 Harry and Magical Computer Description In reward ...
- hdu 5154 Harry and Magical Computer 拓扑排序
Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- (简单) HDU 5154 Harry and Magical Computer,图论。
Description In reward of being yearly outstanding magic student, Harry gets a magical computer. When ...
- HDU 5154 Harry and Magical Computer bfs
Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
随机推荐
- ZOJ 1301 The New Villa (BFS + 状态压缩)
题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=10),有一天晚上他回家时发现所有的灯(除了他出发的房间)都是关闭的,而他想回卧室去休息 ...
- Codeforces 112B-Petya and Square(实现)
B. Petya and Square time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- HDU 3018 Ant Trip
九野的博客,转载请注明出处: http://blog.csdn.net/acmmmm/article/details/10858065 题意:n个点m条边的无向图,求用几笔可以把所有边画完(画过的边 ...
- java中-静态代码块、构造代码块、构造方法的联系
例如该题: 1 class Fu{ static { System.out.println("这是父类静态代码块"); } { System.out.println("这 ...
- HttpClient4.0
****************************HttpClient4.0用法***************************** 1.初始化HttpParams,设置组件参数 //Ht ...
- MySQL存储过程(一)
1.1 CREATE PROCEDURE (创建) CREATE PROCEDURE存储过程名 (参数列表) BEGIN SQL语句代码块 END 注意: 由括号包围的参数列必须总是存在.如果没有参 ...
- 在ssh框架中注解方式需要注意的几个问题
1.注解方式的时候 Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 set ,get ...
- (转)Call to undefined function mb_convert_encoding()
需要先enable mbstring 扩展库 在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉mb_convert_encoding 可以指定多种输入编 ...
- Android - Layout时发生'Unfortunately xxx has stopped'
概述 我在进行LinearLayout和TableLayout的嵌套布局的时候,发生题的错误.如下布局xml代码: <LinearLayout xmlns:android="http: ...
- 【转】 iOS开发数据库篇—SQLite简单介绍
开始学SQLite啦, 原文: http://www.cnblogs.com/wendingding/p/3868893.html iOS开发数据库篇—SQLite简单介绍 一.离线缓存 在项目开发中 ...