Description
针对计算机系本科课程,根据课程之间的依赖关系(如离散数学应在数据结构之前开设)制定课程安排计划。
Input
第一行为样例组数T。每组样例第一行为课程数量n(1 <= n <= 5000),以下n行每行表示一门课程名称。接下来为关系数量m(1 <= m <= 10000),每一行有两个课程名称a、b,表示a课程要开设在b课程前面。(输入保证无环)
Output
每组样例第一行见输出,以下n行每行输出一个课程名称,描述拓扑排序后的课程表。如果课程优先级相同,则优先输出课程名称字典序小的课程。
Sample Input
1
6
Math
Chinese
English
Sports
Music
Computer
4
Chinese English
English Computer
Math Computer
Music Computer
Sample Output
Case 1:
Chinese English
Math Music
Computer Sports
HINT
Append Code
析:拓扑排序,先把所有的科目都排序,按照字典序排,然后再编号,在拓扑排序时,编号小的优先,可用优先队列实现,其他的就很简单了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e3 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int hr[]= {-2, -2, -1, -1, 1, 1, 2, 2};
const int hc[]= {-1, 1, -2, 2, -2, 2, -1, 1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int to, next;
};
Node a[10005<<1];
int head[maxn];
map<string, int> mp;
string s[maxn];
int cnt, in[maxn], ans[maxn];
void add(int u, int v){
a[cnt].to = v;
a[cnt].next = head[u];
head[u] = cnt++;
}
int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d", &n);
mp.clear();
char str[20];
for(int i = 0; i < n; ++i){
scanf("%s", str);
s[i] = str;
head[i] = -1;
in[i] = 0;
}
sort(s, s+n);
for(int i = 0; i < n; ++i) mp[s[i]] = i;
scanf("%d", &m);
cnt = 0;
for(int i = 0; i < m; ++i){
scanf("%s", str);
int u = mp[str];
scanf("%s", str);
int v = mp[str];
add(u, v);
++in[v];
}
priority_queue<int, vector<int>, greater<int> >pq;
for(int i = 0; i < n; ++i) if(!in[i]) pq.push(i);
int tot = 0;
while(!pq.empty()){
int x = pq.top(); pq.pop();
ans[tot++] = x;
for(int i = head[x]; ~i; i = a[i].next){
int t = a[i].to;
--in[t];
if(!in[t]) pq.push(t);
}
}
printf("Case %d:
", kase);
for(int i = 0; i < n; ++i)
printf("%s
", s[ans[i]].c_str());
}
return 0;
}