Hdu4857 escape [topological sorting]

Source: Internet
Author: User

Escape Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others) Total submission (s): 902 accepted submission (s): 246

The bad problem description happened, and now everyone is busy escaping. However, the escape channel is very narrow, and everyone can only line up.

There are now n people, ranging from 1 to n. At the same time, there are some strange constraints, each of which is like: A must be before B.
At the same time, society is unequal, and these people are rich and poor. 1: richest, 2: Second: rich, and so on. Rich people bribe their owners, so they have some benefits.

The owner can now arrange the order of queues for everyone. Due to the benefits, he wants to make the first day as high as possible. If there are many situations at this time, let the second day as high as possible, if there are many other cases, let the 3rd be as high as possible, and so on.

Then you need to arrange the order of the people. We are sure there is a solution.
The first line of input is an integer T (1 <=t <= 5), indicating the number of test data.
For each test data, the first row has two integers, n (1 <= n <= 30000) and M (1 <= m <= 100000 ), represents the number of people and the number of constraints respectively.

Then, in the m row, each row has two integers A and B, indicating that there is a constraint that A must be before B. A and B must be different.
For each test data, output queues are separated by spaces.
Sample Input
15 103 51 42 51 23 41 42 31 53 51 2
 
Sample output
1 2 3 4 5
A set of data provided by DWAYNE:

Input: 13 13 1 answer: 3 1 2 instead of 2 3 1

Wa code: Forward topology

#include <cstdio>#include <cstring>#include <queue>#define maxn 100002using namespace std;int head[maxn], indegree[maxn], ans[maxn];struct Node{    int to, next;} map[maxn];void topoSort(int n){    priority_queue<int, vector<int>, greater<int> > Q;    int i, u, id = 1;    for(i = 1; i <= n; ++i)        if(!indegree[i]) Q.push(i);    while(!Q.empty()){        ans[id++] = u = Q.top(); Q.pop();        for(i = head[u]; i != -1; i = map[i].next)            if(!--indegree[map[i].to]) Q.push(map[i].to);            }        for(i = 1; i <= n; ++i)        if(i != n) printf("%d ", ans[i]);        else printf("%d\n", ans[i]);}int main(){    int t, n, m, a, b, i;    scanf("%d", &t);    while(t--){        memset(indegree, 0, sizeof(indegree));        memset(head, -1, sizeof(head));        scanf("%d%d", &n, &m);        for(i = 0; i < m; ++i){            scanf("%d%d", &a, &b);            map[i].to = b;            map[i].next = head[a];            head[a] = i;            ++indegree[b];        }        topoSort(n);    }    return 0;}


AC code: reverse Topology

#include <cstdio>#include <cstring>#include <queue>#define maxn 100002using namespace std;int head[maxn], indegree[maxn], ans[maxn];struct Node{    int to, next;} map[maxn];void topoSort(int n){    priority_queue<int> Q;    int i, u, id = 1;    for(i = 1; i <= n; ++i)        if(!indegree[i]) Q.push(i);    while(!Q.empty()){        ans[id++] = u = Q.top(); Q.pop();        for(i = head[u]; i != -1; i = map[i].next)            if(!--indegree[map[i].to]) Q.push(map[i].to);            }        for(i = n; i >= 1; --i)        if(i != 1) printf("%d ", ans[i]);        else printf("%d\n", ans[i]);}int main(){    int t, n, m, a, b, i;    scanf("%d", &t);    while(t--){        memset(indegree, 0, sizeof(indegree));        memset(head, -1, sizeof(head));        scanf("%d%d", &n, &m);        for(i = 0; i < m; ++i){            scanf("%d%d", &a, &b);            map[i].to = a;            map[i].next = head[b];            head[b] = i;            ++indegree[a];        }        topoSort(n);    }    return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.