Description
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed.Input
There are multiple test cases. The first line contains an integer T, indicating the number of test cases. Each test case begins with a line containing two integers, 1 <= n <= 100000 and 1 <= m <= 100000. n is the number of tasks (numbered from 1 to n) and m is the number of direct precedence relations between tasks. After this, there will be m lines with two integers i and j, representing the fact that task i must be executed before task j. It is guaranteed that no task needs to be executed before itself either directly or indirectly.Output
For each test case, print a line with n integers representing the tasks in a possible order of execution. To separate them, print exactly one space after each integer. If there are multiple solutions, output the smallest one by lexical order.Sample Input
1
5 5
3 4
4 1
3 2
2 4
5 3
Sample Output
5 3 2 4 1
根据题目意思,很容易就发现该题考查的是拓扑排序。
拓扑排序的基本算法:
1.找到入度为0的结点,将其删除;
2.以被删顶点为头的弧也顺带删除。
重复以上两个步骤直至所有结点都被删除或者是不存在入度为0的点,则拓扑排序结束。
要进行拓扑排序,首先要存储图。由于结点可以达到100000个,建立邻接矩阵和邻接表显然不现实,会超出内存限制。所以我考虑的方法是用一个multimap容器来存储结点间的关系,用isExist的数组来确定结点是否存在,用in_degree的数组来记录每个结点的入度。
我的代码如下:
# include <iostream>
# include <map>
# include <set>
using namespace std;
int main(void) {
int T;
cin >> T;
while(T--) {
int m, n;
cin >> n >> m;
int isExist[n+1];
int in_degree[n+1];
for (int i = 0; i < n+1; i++) {
isExist[i] = 1;
in_degree[i] = 0;
}
multimap<int, int> relation;
for (int i = 0; i < m; i++) {
int point1, point2;
cin >> point1 >> point2;
relation.insert(make_pair(point1, point2));
in_degree[point2]++;
}
set<int> myset;
int flag = 0;
for (int i = 1; i < n+1; i++) {
if (isExist[i] == 1 && in_degree[i] == 0) {
myset.insert(i);
flag = 1;
}
}
if (flag != 1) {
return 0;
}
while(!myset.empty()) {
set<int>::iterator it = myset.begin();
cout << *it << " ";
multimap<int, int>::iterator m = relation.find(*it);
for (int i = 0; i != relation.count(*it); i++, m++) {
in_degree[m->second]--;
}
isExist[*it] = 0;
myset.erase(it);
bool flag = 0;
for (int i = 1; i < n+1; i++) {
if (isExist[i] == 1 && in_degree[i] == 0) {
myset.insert(i);
flag = 1;
}
}
if (flag != 1) break;
}
}
return 0;
}
然而交上去,超时了!!!
在保留map容器的情况下,我尽我最大的努力对代码进行了优化:
1.把输入输出分别换成C语言中的scanf与printf;
2.把下面这段循环并入它上面的循环,不影响功能;
bool flag = 0;
for (int i = 1; i < n+1; i++) {
if (isExist[i] == 1 && in_degree[i] == 0) {
myset.insert(i);
flag = 1;
}
}
if (flag != 1) break;
提交后还是超时!!!
仔细看我的程序,唯一用时最多的就是map容器的查找。看来要想程序不超时,只能换掉map容器了。那么有什么容器能使得我方便查找到某个点指向的所有点呢?灵机一动,可以用一个set数组!这样查找起来效率就更高了。
于是再次改进了代码:
# include <iostream>
# include <cstring>
# include <cstdio>
# include <map>
# include <set>
# define N 100002
using namespace std;
int main(void) {
int T;
scanf("%d", &T);
while(T--) {
multiset<int> pArr[N];
int m, n;
scanf("%d%d", &n, &m);
int isExist[n+1];
int in_degree[n+1];
for (int i = 0; i < n+1; i++) {
isExist[i] = 1;
in_degree[i] = 0;
}
for (int i = 0; i < m; i++) {
int point1, point2;
scanf("%d%d", &point1, &point2);
pArr[point1].insert(point2);
in_degree[point2]++;
}
set<int> myset;
int flag = 0;
for (int i = 1; i < n+1; i++) {
if (isExist[i] == 1 && in_degree[i] == 0) {
myset.insert(i);
flag = 1;
}
}
if (flag != 1) {
return 0;
}
while(!myset.empty()) {
set<int>::iterator it = myset.begin();
printf("%d ", *it);
for (multiset<int>::iterator it1 = pArr[*it].begin(); it1 != pArr[*it].end(); it1++) {
in_degree[*it1]--;
if (in_degree[*it1] == 0) {
myset.insert(*it1);
}
}
isExist[*it] = 0;
myset.erase(it);
}
printf("\n");
}
return 0;
}
哈哈,终于过啦~
以上内容皆为本人观点,欢迎大家提出批评和指导,我们一起探讨!