1280. Topological Sorting

博客围绕1280. Topological Sorting展开,包含输入、输出及示例等内容,还给出了转载来源。主要聚焦于拓扑排序这一信息技术相关问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1280. 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, ui  N; 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

inputoutput
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
Problem Author: © Sergey G. Volchenkov, 2003(volchenkov@yandex.ru); Vladimir N. Pinaev, 2003(vpinaev@mail.ru; http://www.pic200x.chat.ru); Michael Y. Kopachev, 2003 (mkopachev@krista.ru). Problem Source: 2003-2004 ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk, October 15-16, 2003
************************************************************************************************
拓扑排序(用链表)
************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cctype>
 6 #include<cstdio>
 7 #include<stack>
 8 #include<queue>
 9 #include<vector>
10 using namespace std;
11 const int maxn=10005;
12 int ind[maxn];
13 struct node
14 {
15     int ve;
16     struct node *next;
17     node()
18     {
19         ve=0;
20         next=NULL;
21     }
22 };
23 node *edge[maxn];
24 void insert(int x,int y)//链表的链接
25  {
26      node *temp;
27      temp=new node(); 
28      temp->ve=y;
29      temp->next=edge[x];
30      edge[x]=temp;
31      ind[y]++;
32 
33  }
34 int main()
35 {
36     int i,j,be,en,n,m,s;
37     memset(ind,0,sizeof(ind));
38     cin>>n>>m;
39     for(i=1;i<=m;i++)
40      {
41          cin>>be>>en;
42          insert(be,en);
43      }
44     for(i=1;i<=n;i++)
45      {
46          cin>>s;
47          if(ind[s]==0)
48           {
49               while(edge[s])
50                {
51                    ind[edge[s]->ve]--;
52                    edge[s]=edge[s]->next;
53                }
54           }
55          else
56            {
57                 cout<<"NO"<<endl;
58                 return 0;
59            }
60      }
61      cout<<"YES"<<endl;
62      return 0;
63 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3242306.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值