Topological Sorting

Time limit: 1.0 second
Memory limit: 64 MB
Michael wants to win the world championship in programming and decided to study N subjects (for convenience we will number these subjects from 1 to N).
Michael has worked out a study plan for this purpose. But it turned out
that certain subjects may be studied only after others. So, Michael’s
coach analyzed all subjects and prepared a list of M limitations in the form “si ui” (1 ≤ si, uiN; i = 1, 2, …, M), which means that subject si must be studied before subject ui.
Your task is to verify if the order of subjects being studied is correct.
Remark.
It may appear that it’s impossible to find the correct order of
subjects within the given limitations. In this case any subject order
worked out by Michael is incorrect.
Limitations
1 ≤ N ≤ 1000; 0 ≤ M ≤ 100000.

Input

The first line contains two integers N and M (N is the number of the subjects, M is the number of the limitations). The next M lines contain pairs si, ui, which describe the order of subjects: subject si must be studied before ui. Further there is a sequence of N unique numbers ranging from 1 to N — the proposed study plan.

Output

Output
a single word “YES” or “NO”. “YES” means that the proposed order is
correct and has no contradictions with the given limitations. “NO” means
that the order is incorrect.

Samples

input output
5 6
1 3
1 4
3 5
5 2
4 2
1 2
1 3 4 5 2
YES
5 6
1 3
1 4
3 5
5 2
4 2
1 2
1 2 4 5 3
NO
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 10000000
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
int in[N],vis[N],w[N][N];
int n,m,k;
vector<int>vec[N]; int main()
{
int a,b;
bool flag=true;
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d",&a,&b);
if(!w[a][b]){
w[a][b]=;
vec[a].push_back(b);
}
}
for(int i=;i<=n;i++){
scanf("%d",&in[i]);
}
for(int i=;i<=n;i++){
int cnt=;
for(int j=i+;j<=n;j++){
if(w[in[i]][in[j]])cnt++;
}
if(cnt!=vec[in[i]].size())flag=false; }
if(flag)printf("YES\n");
else printf("NO\n");
return ;
}

URAL(timus) 1280 Topological Sorting(模拟)的更多相关文章

  1. hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  2. Lintcode: Topological Sorting

    Given an directed graph, a topological order of the graph nodes is defined as follow: For each direc ...

  3. Topological Sorting

    Topological sorting/ ordering is a linear ordering of its vertices such that for every directed edge ...

  4. Union - Find 、 Adjacency list 、 Topological sorting Template

    Find Function Optimization: After Path compression: int find(int x){ return root[x] == x ? x : (root ...

  5. 拓扑排序(Topological Sorting)

    一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...

  6. Topological Sorting拓扑排序

    定义: Topological Sorting is a method of arranging the vertices in a directed acyclic graph (DAG有向无环图) ...

  7. Course Schedule课程表12(用Topological Sorting)

    [抄题]: 现在你总共有 n 门课需要选,记为 0 到 n - 1.一些课程在修之前需要先修另外的一些课程,比如要学习课程 0 你需要先学习课程 1 ,表示为[0,1]给定n门课以及他们的先决条件,判 ...

  8. hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  9. hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

    DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

随机推荐

  1. android Xutils dbutils 注解

    xUtils DbUtils 关于实体类注解 汇总 RockyZhang 发布于 1年前,共有 0 条评论 先来官方demo DbUtils db = DbUtils.create(this);    ...

  2. 【SQL查询日志】查看数据库历史查询记录

    --关键字:cross apply & outer apply --最后更新:2011-10-20 作者:Ronli--更新链接:http://www.cnblogs.com/ronli/ar ...

  3. Allegro PCB SI (2)

    整理一下在电研院学的si (虽然彩超的si在频率15Mhz以上后,si是失真的.昨晚遇到孔大哥也是这样说的,板级仿真,要layout过硬,然后找到合适的top test point) Allegro ...

  4. WPF如何实现类似iPhone界面切换的效果(转载)

    WPF如何实现类似iPhone界面切换的效果 (version .1) 转自:http://blog.csdn.net/fallincloud/article/details/6968764 在论坛上 ...

  5. Ubuntu 14.10 下Ganglia监控Spark集群

    由于Licene的限制,没有放到默认的build里面,所以在官方网站下载的二进制文件中并不包含Gangla模块,如果需要使用,需要自己编译.在使用Maven编译Spark的时候,我们可以加上-Pspa ...

  6. AppCan相关网站

    AppCan文档中心: http://doc.appcan.cn/#!/guide/handbook AppCan官网: http://www.appcan.cn/index.html

  7. Python学习路程day4

    迭代器&生成器 迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退. ...

  8. hdu 2083

    ps:N个数中,中位数是最小距离...第一次WA是因为排序之后最小和最大相加除2...应该是找他们的中位数,而不是中间数. 代码: #include "stdio.h" #incl ...

  9. Interview----用最快的方法计算 Fibonacci 数

    输入 n, 用最快的方法求该 Fibocacci 数列的第 n 项. 方法1: 递归,非常慢 方法2: 迭代,因此计算 f[1] , f[2], f[3] ,,,, 复杂度 O(N) 方法3: 采用以 ...

  10. 禁止VMware用户在系统里删除网卡的操作的方法

    点击选项-常规-配置参数,如下图所示:   8)点击“添加行”,在新的栏目中,左栏输入:devices.hotplug,右边栏输入:false