HDU 4857/bestcoder round#1 1001 (Reverse build)

Source: Internet
Author: User

This problem needs careful analysis of the topic, otherwise test instructions easy to understand the error, should pay attention to the following situation

As far as possible, the smallest row of the front, and then the small as far as possible to the front, and then go on

Such as

Input

1

3 1

3 1

Output

3 1 2

Analysis: We should let 1 as far as possible in front, and then as far as possible to make 2 rows of the front ... So the result of 2 3 1 is wrong



Idea: Topology sequencing (reverse build + queue)//To solve the above example, if we are to build a map, each time the selection of the minimum number of zero output can not meet the above case;

If we try to reverse-build, each time we choose the maximum number output of zero in degrees is just the reverse of the correct result (the inverse of the province and check set, the training of reverse thinking)


#include <vector> #include <queue> #include <cstdio> #include <cstring> #include <stack>    using namespace Std;const int max=30010;vector<int> g[max];struct node{int x; int y;}    Wd[100010];struct cmp1{bool Operator () (int &a,int &b) {return a<b;    }};p riority_queue<int,vector<int>,cmp1>pq1;int ind[max];int n,m;stack<int> st;int main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        for (int i=1;i<=n;++i) g[i].clear ();        memset (IND,0,SIZEOF (Ind));        int x, y;            for (int i=1;i<=m;++i) {scanf ("%d%d", &x,&y);            G[y].push_back (x);        ind[x]++;        } while (!pq1.empty ()) Pq1.pop ();        for (int i=1;i<=n;++i) {if (ind[i]==0) Pq1.push (i);        } while (!st.empty ()) St.pop ();        int tmp;    while (!pq1.empty ()) {tmp=pq1.top ();        Pq1.pop ();            St.push (TMP);            int len=g[tmp].size ();                for (int i=0;i<len;++i) {ind[g[tmp][i]]--;            if (ind[g[tmp][i]]==0) Pq1.push (G[tmp][i]);        }} tmp=st.top ();        St.pop ();            while (!st.empty ()) {printf ("%d", TMP);            Tmp=st.top ();        St.pop ();    } printf ("%d\n", TMP); } return 0;}

Harvesting: The training of Reverse thinking (reverse mapping, reverse processing, etc.)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4857/bestcoder round#1 1001 (Reverse build)

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.