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. 纯手写分页控件CSS+JS+SQL

    Asp.net中虽然用DataPager配合ListView可以实现分页显示,但是有时候由于开发环境等问题不能用到DataPager控件,那么自己手工写一个分页控件就很有必要了,当然,最重要的是通用性 ...

  2. Matlab与C/C++联合编程之Matlab以MEX方式调用C/C++代码(一)

    MEX文件是一种可在matlab环境中调用的C语言(或fortran)衍生程序,mex的编译结果实际上就是一个带输出函数mexFunction 的dll文件. 中文名 mex文件 外文名 MATLAB ...

  3. [网络技术][转]PPTP连接过程

    转自:http://blog.csdn.net/zhu_hit/article/details/5698958 在未来几天会总结一下PPTP的工作过程,分为以下3篇讲述. 1. PPTP连接过程: 2 ...

  4. 深入理解mybatis参数

    这个的话我是看的别人的文章,感觉很好: http://blog.csdn.net/isea533/article/details/44002219

  5. 记录一些容易忘记的属性 -- UIGestureRecognize手势

    //一个手势只能添加到一个view上面 //设置当前手势需要的点击次数    _tapRec.numberOfTapsRequired = 1;//(默认为1)    //设置当前需要几个手指同时点击 ...

  6. tableView中的“点击加载更多”点击不到

    假设当前的tableView是_tableView,则可以这样设置 _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0); 该属性用于设置当 ...

  7. 0125 多线程 继承Thread 练习

    //定义一个继承Thread类的类,并覆盖run()方法,在run()方法中每隔100毫秒打印一句话public class Csh extends Thread{ public void run() ...

  8. ACM2013.9.7

    acm还是要好好做的,还有好多的东西要学呢!不能不认真对待了,该玩够了!

  9. Python学习路程day5

    冒泡排序 将一个不规则的数组按从小到大的顺序进行排序 data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6] #第一次循环,最后一个数字不需要循环,因为最大值已经 ...

  10. python 安装操作 MySQL 数据库.

    以ubuntu和mysql为例 检查自己的机器上面有没有安装数据库 xpower@xpower-CW65S:~$ sudo service mysql start [sudo] xpower 的密码: ...