题意:大坑,不像普通的反向拓扑,反向建图后求出拓扑序列, 这个题的题目要求不是输出拓扑序列,而是输出球的每个球的重量的次序。
比如数据
4 1
4 1
输出的不应该是4 1 2 3而是2 3 4 1,也就是这个拓扑序按照1 2 3 4这四个排他们的次序,1在第二位,2在第三位,3在第四位,4在第一位,所以输出2 3 4 1。但样例数据能过,估计故意找的这种样例数据,坑着我这种看题不认真,英语不好的人。
解题思路:反向建图,拓扑排序, 排序的时候队列中每次弹出序号大的数,记录它的重量的次序,输出次序即可
数据结构选用的是链式向前星。
Description
Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that:
- No two balls share the same label.
- The labeling satisfies several constrains like "The ball labeled with a is lighter than the one labeled with b".
Can you help windy to find a solution?
Input
The first line of input is the number of test case. The first line of each test case contains two integers, N (1 ≤ N ≤ 200) and M (0 ≤ M ≤ 40,000). The next M line each contain two integers a and b indicating the ball labeled with a must be lighter than the one labeled with b. (1 ≤ a, b ≤ N) There is a blank line before each test case.
Output
For each test case output on a single line the balls' weights from label 1 to label N. If several solutions exist, you should output the one with the smallest weight for label 1, then with the smallest weight for label 2, then with the smallest weight for label 3 and so on... If no solution exists, output -1 instead.
Sample Input
5 4 0 4 1 1 1 4 2 1 2 2 1 4 1 2 1 4 1 3 2
Sample Output
1 2 3 4 -1 -1 2 1 3 4 1 3 2 4
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
int buf[10];
//整型变量快速输入输出函数
inline int readint()
{
char c = getchar();
while(!isdigit(c)) c = getchar();
int x = 0;
while(isdigit(c))
{
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
inline void writeint(int i)
{
int p = 0;
if(i == 0) p++;
else while(i)
{
buf[p++] = i % 10;
i /= 10;
}
for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
//
#define MAX_N 205
#define MAX_M 40005
const int INF = 0x3f3f3f3f;
int n , m;
priority_queue<int, vector<int>, less<int> > que;
int top;
int indegree[MAX_N];
int w[MAX_N];
int head[MAX_N];
struct node
{
int to, next;
} edge[MAX_M];
int init()
{
while(!que.empty()) que.pop();
memset(head, -1, sizeof(head));
memset(indegree, 0, sizeof(indegree));
top = 0;
}
int add_edge(int u, int v)
{
edge[top].to = v;
edge[top].next = head[u];
head[u] = top++;
}
void topo_sort()
{
int cnt = n;
for(int i = 1 ; i <= n ; i++)
{
if(indegree[i] == 0)
{
que.push(i);
}
}
while(!que.empty())
{
int p = que.top();
que.pop();
w[p] = cnt--;
for(int k = head[p]; k != -1 ; k = edge[k].next)
{
indegree[edge[k].to]--;
if(indegree[edge[k].to] == 0)
{
que.push(edge[k].to);
}
}
}
if( cnt > 0 )
{
cout<<-1<<endl;
}
else
{
for(int i = 1 ; i <= n ; i++)
{
printf("%d%c", w[i], i == n ? '\n' : ' ');
}
}
return;
}
int main()
{
// init();
int t;
t =readint();
while(t--)
{
init();
n = readint();
m = readint();
for(int i = 0 ; i < m ; i++)
{
int u = readint();
int v = readint();
add_edge(v, u);
indegree[u]++;
}
topo_sort();
}
return 0;
}