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
题目大意:给出序列,判断是否为拓扑序列。拓扑序列:对序列中任意两个顶点Vi,Vj,在有向图中有一条从Vi到Vj的路径,则在序列中Vi必排在Vj之前。
拓扑排序:前提:有向图。从图中找一个没有入度的点,即没有边指向它。输出此点,把此点和以此点为起点的边删除即把此点指向的点的入度-1,重复操作直到遍历全部点。若此过程中,出现入度不为0的点,输出点形成的序列就不是拓扑序列。若最后输出的节点数目小于有向图中全部顶点的数目,说明有回路。
拓扑序列:若在有向图中,a点指向b点,则在拓扑序列中,a点一定在b点之前。吾代码思路就是如此。柳的思路是做了一遍拓扑排序。
吾:代码一遍过
#include <iostream>
#include <vector>
using namespace std;
struct node
{//为了方便保存那m条边
int a, b;
};
int n, m, k;
vector<node> v;
int main()
{
//freopen("in.txt", "r", stdin);
cin >> n >> m;
v.resize(m);
for (int i = 0; i < m; i++)
{
cin >> v[i].a >> v[i].b;
}
cin >> k;
bool outputed = false;//为了格式需要
for (int i = 0; i < k; i++)
{
vector<int> seq(1006);//注意这里的大小,虽然这次意识到了,没出错但还是要注意
for (int j = 0; j < n; j++)
{
int t;
cin >> t;
seq[t] = j;
}
for (int j = 0; j < v.size(); j++)
{
if (seq[v[j].a]>seq[v[j].b])
{
if (outputed==false)
{//为了格式需要,没有输出过,前面就不加空格;
//输出过了前面加个空格,这样直接输出就不用放在ans数组里统一输出了。
cout << i;
outputed = true;
}
else
{
cout << " " << i;
}
break;
}
}
}
return 0;
}
二刷:柳:
注意:判断的时候如果不是事先将要判断的一行值全部读入,而是读一个判断一下的情况,不要得到判断结果就中途break,这样会导致,这次判断的这行值读了一半,下次判断读入的时候会接着上次那一半继续读入,这样肯定会错误。
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 1006;
int main()
{
//freopen("in.txt", "r", stdin);
int n,m,k;
cin >> n>>m;
//int in[maxn] = { 0 };
vector<int> in(maxn, 0);
vector<int> v[maxn];
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
v[a].push_back(b);
in[b]++;
}
cin >> k;
int flag = 0;
for (int i = 0; i < k; i++)
{
int flag1 = 1;
//vector<int> tin(in,in+n+1); //对应int [maxn]形式,
vector<int> tin(in);//对应vector<int> in的写法,两种写法都一样都一样过。
vector<int> q;//为了验证下面的break出错的原因,直接break检验的值没有全部读入,被当做下一个判断的值,这次判断的值没全读,下次判断的时候接着上次的一半读入肯定出错。
for (int j = 0; j < n; j++)
{
int temp;
cin >> temp;
q.push_back(temp);
}
for (int j = 0; j < n; j++)
{
int a;
//cin >> a;
a = q[j];
if (tin[a]!=0)
{
flag1 = 0;
break; //why?:果然如上面的猜想,验证成功,实现把要判断的这一行值读完,这里break就不会有错误了。
}
for (auto it : v[a])
{
tin[it]--;
}
}
if (flag1==1)
{
continue;
}
printf("%s%d", flag == 0 ? "" : " ", i);
flag = 1;
}
return 0;
}