PAT甲级1146 Topological Order (25分) 拓扑排序
1146 Topological Order (25分)
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 1,000), the number of vertices in the graph, and M (≤ 10,000), the number of directed edges. Then M lines follow, each gives the start and the end vertices of an edge. The vertices are numbered from 1 to N. After the graph, there is another positive integer K (≤ 100). Then K lines of query follow, each gives a permutation of all the vertices. All the numbers in a line are separated by a space.
Output Specification:
Print in a line all the indices of queries which correspond to “NOT a topological order”. The indices start from zero. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line. It is graranteed that there is at least one answer.
Sample Input:
6 8
1 2
1 3
5 2
5 4
2 3
2 6
3 4
6 4
5
1 5 2 3 6 4
5 1 2 6 3 4
5 1 2 3 6 4
5 2 1 6 3 4
1 2 3 4 5 6
Sample Output:
3 4
方法1:
基本思路是拓补排序要求对于所有边u-v 则u在线性序列中出现在v之前
那么反过来判断是否满足拓扑排序,对于边u-v,在线性序列中u之前不能出现v
AC代码:
#include <iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
//基本思路是拓补排序要求对于所有边u-v 则u在线性序列中出现在v之前
//那么反过来判断是否满足拓扑排序,对于边u-v,在线性序列中u之前不能出现v
int N,M;//顶点是1 -N;
cin>>N>>M;
int ver[N+1]= {0};
vector<vector<int>> vec(N+1);//记录边
for(int i=0; i<M; i++)
{
int u,v;
cin>>u>>v;
vec[u].push_back(v);//就是u前面不能有v
}
vector<int> outPut;//用来记录输出结果的
int num;//需要判断的线性序列个数
cin>>num;
for(int q=0; q<num; q++)
{
int out=1;//先假设满足拓扑排序
int sn[N];
for(int i=0; i<N; i++)
{
cin>>sn[i];
}
int sign[N+1]= {0}; //记录之前出现的结点编号
for(int i=0; i<N&&out==1; i++)
{
int checkNum=sn[i];//需要判断的编号
if(i==0)
sign[checkNum]=1;
else
{
for(int p=0; p<vec[checkNum].size(); p++)
//把编号checkNum 之前所有不能出现的编号遍历一遍
{
//其实也就是对应边 checkNum->denyNum
int denyNum=vec[checkNum][p];
if(sign[denyNum]==1)//checkNum 之前出现了不能有的编号
{
out=0;
break;
}
}
if(out==1)
{
sign[checkNum]=1;
}
}
}
if(out==0)
outPut.push_back(q);
}
for(int i=0;i<outPut.size();i++){
if(i==0)
cout<<outPut[i];
else
cout<<' '<<outPut[i];
}
return 0;
}
方法二: 参考了柳神 https://blog.csdn.net/liuchuo/article/details/79805295 思路
我们可以从入度的角度考虑,因为这里是所有结点的线性序列,所以每个结点之前的所有结点对应边的末端结点 入度-1 后,所有结点的入度为0
比如B选项2之前 有 5 和1 结点,他们对应边的末端结点就是2 3 4 ,那么就把 2 3 4结点的入度-1,你会发现2结点的入度刚好为0了,而D结点的2结点入度为1 那么就不满足拓扑排序。
代码可以参考柳神的,状态不好,不想撸了。。。。
上一篇: 6-10 阶乘计算升级版 (20 分)
下一篇: 6-10 阶乘计算升级版 (20分)
推荐阅读
-
PAT (Advanced Level) Practice 1146 Topological Order (25分) (拓扑序列的判断)
-
PAT甲级--1146 Topological Order(25 分)【判断是否为拓扑序列】
-
【PAT甲级A1146】Topological Order (25分)(c++)
-
PAT甲级A1146 Topological Order(25 分)
-
PAT甲级1146 Topological Order (25分) 拓扑排序
-
【PAT甲级】1146 Topological Order(25 分)(拓扑排序)
-
PAT甲级1146 Topological Order (25分)|C++实现
-
PAT甲级——1146 Topological Order (25分)