Topological sorting + not a dictionary order precedence Arrangement (poj3687+hdu4857)

Source: Internet
Author: User

First, preface

In the past week, the end of the CCSP game, which has a problem card I 9 hours, all kinds of debugging can not complete the mediation to the problem, so the pain decided to start the problem, this is part of the plan. In fact, based on the wrong understanding I wrote a number of topological sorting + dictionary order algorithm, but the collective unity GG, and finally found that, in fact, the design is not strictly the smallest dictionary order, but "the smallest must be placed before the largest" this looks very similar but at a completely different view. And this is why, is trying to build GG but reverse build, with the big top heap to find the biggest idea is correct. This is actually equivalent to, "Looking for the maximum dictionary order and reverse output" of this process.

First look at a set of examples

1

3 1

3 1
For the reorganization sample, the constrained--3 must precede 1, because if the algorithm with the smallest dictionary order is going to be output, it will get 2 3 1. But this data clearly violates the problem of the order of the statute-"If there is a 1, can be in front of 2, then you must put 1 to 2 front, after that, if there is 2 can be placed in front of 3, you must put 2 before 3". We intuitively believe that this is equivalent to first putting all possible maximum values at the end, to ensure that there is no valid decimal place behind the large number. The correct approach is that 2 1 3 (reverse output is 3,1,2). This approach guarantees from metaphysics that the output is "ordered in accordance with the meaning of the topic".

second, ideas and related optimization

The simple idea is that the topological ordering process, by detecting whether there are new elements can be used as a queue at any time the element, if any, join the priority queue, if not, continue.

in general, using a dictionary-ordered output topology ordering is a simple matter. Comparing the code written by other people on the Internet, we can intuitively think of at least the following optimization methods:

    1. Use adjacency tables to store specific edge information instead of adjacency matrices. (It can be proved that the time expected to insert N edges using vectors as adjacency table should be O (n))
    2. Use the priority queue, Multiset to select the largest and smallest element from the collection
    3. Use the cnt[] array to record the number of times the point has been pointed, and then, in Topsort, determine whether the value is equal to zero by judging the corresponding element of the CNT array.
Third, the general AC code

POJ3687

#include <iostream>#include<stdio.h>#include<string.h>#include<Set>#include<vector>#include<queue>using namespacestd;Const Long Longmaxn=30233; Vector<int>G[MAXN];intCNT[MAXN];Long Longn,m;intVIS[MAXN];BOOLDfsintNow ) {Vis[now]=1; intlen=g[now].size ();  for(intI=0; i<len;++i) {intTar=G[now][i]; if(vis[tar]==1)return true; if(vis[tar]==0&&dfs (TAR))return true; }vis[now]=2; return false;}BOOLcheck_circle () {memset (Vis,0,sizeof(int) * (n+5));  for(intI=1; i<=n;++i) {if(vis[i]==0&&dfs (i))return true; }return false;}intANS[MAXN];voidTopsort () {priority_queue<int>P; intsumm=N;  for(intI=1; i<=n;++i) {if(cnt[i]==0) Q.push (i); }     while(!Q.empty ()) {        intnow=q.top (); Q.pop (); intlen=g[now].size (); Ans[now]=summ--;  for(intI=0; i<len;++i) {intTar=G[now][i]; Cnt[tar]--; if(cnt[tar]==0) Q.push (TAR); }    }}voidinit () {memset (CNT,0,sizeof(int) *n+233); CIN>>n>>m;  for(intI=0; i<=n;++i) {g[i].clear (); }     for(intI=0; i<m;++i) {intb; CIN>>a>>b;        G[b].push_back (a); Cnt[a]++; }    if(Check_circle ()) {cout<<"-1\n"; return ;    } topsort ();  for(intI=1; i<=n;++i) {cout<<ans[i]<<" "; }cout<<Endl;}intMain () {Cin.sync_with_stdio (false); intCA; CIN>>CA;  while(ca--) init (); return 0;}

HDU4857

#include <iostream>#include<stdio.h>#include<string.h>#include<Set>#include<vector>#include<queue>using namespacestd;Const Long Longmaxn=30233; Vector<int>G[MAXN];intCNT[MAXN];Long Longn,m;intVIS[MAXN];BOOLDfsintNow ) {Vis[now]=1; intlen=g[now].size ();  for(intI=0; i<len;++i) {intTar=G[now][i]; if(vis[tar]==1)return true; if(vis[tar]==0&&dfs (TAR))return true; }vis[now]=2; return false;}BOOLcheck_circle () {memset (Vis,0,sizeof(int) * (n+5));  for(intI=1; i<=n;++i) {if(vis[i]==0&&dfs (i))return true; }return false;}intANS[MAXN];voidTopsort () {priority_queue<int>Q; intsumm=N;  for(intI=1; i<=n;++i) {if(cnt[i]==0) Q.push (i); }     while(!Q.empty ()) {        intnow=q.top (); Q.pop (); intlen=g[now].size (); Ans[summ--]=Now ;  for(intI=0; i<len;++i) {intTar=G[now][i]; Cnt[tar]--; if(cnt[tar]==0) Q.push (TAR); }    }}voidinit () {memset (CNT,0,sizeof(int) *n+233); CIN>>n>>m;  for(intI=0; i<=n;++i) {g[i].clear (); }     for(intI=0; i<m;++i) {intb; CIN>>a>>b;        G[b].push_back (a); Cnt[a]++; }    if(Check_circle ()) {cout<<"-1\n"; return ;    } topsort ();  for(intI=1; i<n;++i) {cout<<ans[i]<<" "; }cout<<ans[n]<<Endl;}intMain () {Cin.sync_with_stdio (false); intCA; CIN>>CA;  while(ca--) init (); return 0;}

Topological sorting + not a dictionary order precedence Arrangement (poj3687+hdu4857)

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.