Labeling Balls POJ3687 "topological sort reverse build edge" "adjacency table"

Source: Internet
Author: User

http://poj.org/problem?id=3687


Description

Windy have n balls of distinct weights from 1 unit to N units. Now the he tries to the label them with 1 to N in such a by that:

    1. No. Balls share the same label.
    2. The labeling satisfies several constrains like "the ball labeled with a are lighter than the one labeled with B ".

Can windy to find a solution?

Input

The first line of input was the number of the test case. The first line of all test case contains-integers, n (1≤ n ≤200) and m (0≤ m ≤ 40,000). The next M line each contain, integers a and B indicating the ball labeled with a must is lighter than the one labeled with b. (1≤ A, bN) There is a blank line before each test case.

Output

For each test, the output on a, 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

54 04 11 14 21 22 14 12 14 13 2

Sample Output

1 2 3 4-1-12 1 3 41 3 2 4


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

I did not understand this sentence at first, always WA,

If this sort of topology exists, the current number of such sequences is renumbered, the lightest is 1, then the 2,3,4,5 ... That is, the original ranking order, through this request after sorting, output he is now the first number. However, I directly output the sequencing sequence, and the pit is the same as the given example.


#include <cstdio> #include <cstring> #include <queue> #define N 44000using namespace std;struct Node { int To;int Next; Arr[n];int head[n];int indegree[n];int res[n];int w[n];int n;int topo () {int i,j,k;priority_queue<int>q;//large first-out queue for (J=1;J&LT;=N;++J) {if (indegree[j]==0) {Q.push (j);}} K=0;while (!q.empty ()) {int u=q.top (); Q.pop (); Res[k++]=u;for (j=head[u];j!=-1;j=arr[j].next) {int v=arr[j].to; Indegree[v]--;if (indegree[v]==0) Q.push (v);}} return k;} int main () {int t,m;int i,j;int a,b;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m); Memset (indegree,0, sizeof (Indegree)); Memset (Head,-1,sizeof (head)); for (i=1;i<=m;++i) {scanf ("%d%d", &a,&b); arr[i].next= head[b];//reverse build head[b]=i;arr[i].to=a;indegree[a]++;} int K=topo ();            if (k<n) printf (" -1\n"), else {for (int i=0;i<k;i++)//re-numbering w[res[i]]=n-i from heavy to light;            printf ("%d", w[1]);            for (int i=2;i<=n;i++) printf ("%d", w[i]); Puts ("");//for (i=k-1;i>0;--i)//printf ("%d", Res[i]);//printf ("%d\n ", Res[0]);}} return 0;} /*25 310-8ans:2 4 5 3 1 Reverse build 5 1 6 2 7 8 3 4 9 10 output -1*/without a heavy edge


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

Labeling Balls POJ3687 "topological sort reverse build edge" "adjacency table"

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.