[cf825e] Minimal Labels (reverse build, topological sort)

Source: Internet
Author: User

Title Link: Http://codeforces.com/problemset/problem/825/E

Test instructions: Give a direction graph, the arrangement is the ordinal of each point, so that the order of the points corresponding to the sequence of topological order and the Order of the dictionary is minimal.

It is not possible to run the smallest topological order of the dictionary order directly, because that is to make sure that the dictionary order of the points is not the order of the dictionary, such as this data:

10 1
5 2

In turn, a point with a large entry level of 0 must be in the back, and this position is determined. However, a small number of degrees to 0 is not necessarily in the front, because this point may have a degree of not 0, but the point unrelated to this point in front, press test instructions this point is smaller than the current point ordinal.

Reverse build, run dictionary order the largest topological sort on the line.

1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intMAXN =100100;5 intN, M, CNT;6vector<int>G[MAXN];7 intRET[MAXN],inch[MAXN];8priority_queue<int>PQ;9 Ten voidinit () { OneCNT =N; AMemsetinch,0,sizeof(inch)); -      while(!pq.empty ()) Pq.pop (); -      for(inti =0; i < MAXN; i++) g[i].clear (); the } -  - signed Main () { -     //freopen ("in", "R", stdin); +     intu, v; -      while(~SCANF ("%d%d",&n,&m)) { + init (); A          for(inti =0; I < m; i++) { atscanf"%d%d",&u,&v); - g[v].push_back (u); -             inch[u]++; -         } -          for(inti =1; I <= N; i++) { -             if(!inch[i]) pq.push (i); in         } -          while(!Pq.empty ()) { to             intU =pq.top (); Pq.pop (); +Ret[u] = cnt--; -              for(Auto V:g[u]) { the                 inch[v]--; *                 if(inch[v] = =0) Pq.push (v); $             }Panax Notoginseng         } -          for(inti =1; I <= N; i++) { theprintf"%d%c", Ret[i], i==n?'\ n':' '); +         } A     } the     return 0; +}

[cf825e] Minimal Labels (reverse build, topological sort)

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.