[AGC010E] rearranging [topological sorting + heap]

Source: Internet
Author: User

Surface

Transmission Door

Ideas

First, one obvious conclusion is that the relative order of any two coprime in Alice's adjusted sequence cannot be changed

Then we can take this nature as a breach.

We have an edge between two non-coprime points (yes, it's a graph proposition!!!). ), then a topological order for each of the Unicom blocks will be the way that this block eventually becomes Bob.

Obviously, Alice can be greedy to operate, so that the smaller the topological order, the more forward

Then we sort all the numbers from small to large, then starting from the start of DFS, each time DFS starts from the smallest start, gets a $dag$

And then put all the $dag$ topological sort of the queue into a heap, each time the smallest, is Bob get the sequence of the current this bit

(equivalent to doing topological sorting in parallel)

Code

Give it to the whole truth.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cassert> #include <cmath> #include <queue> #define LL long longusing namespace Std;inline int read () {int re=    0,flag=1;char Ch=getchar (); while (ch> ' 9 ' | |        ch< ' 0 ') {if (ch== '-') flag=-1;    Ch=getchar ();    } while (ch>= ' 0 ' &&ch<= ' 9 ') re= (re<<1) + (re<<3) +ch-' 0 ', Ch=getchar (); return Re*flag;} int n,g[2010][2010],a[2010],vis[2010],in[2010];int first[2010],cnte;struct edge{int to,next;} e[10010];inline void Add (int u,int v) {e[++cnte]= (edge) {v,first[u]};first[u]=cnte;in[v]++;}    int gcd (int x,int y) {if (y==0) return x; return gcd (y,x%y);}    void Dfs (int u) {vis[u]=1;int i;    for (i=1;i<=n;i++) {if (!vis[i]&&!g[u][i]) Add (u,i), DFS (i); }}struct gaygayquan{int val,id;}; inline bool operator < (Gaygayquan X,gaygayquan b) {return x.val<b.val;} Gaygayquan MP (int l,int R) {return (Gaygayquan) {L,r};}    Priority_queue<gaygayquan>q;int Main () {memset (first,-1,sizeof (first));    N=read (); int I,j,v;gaygayquan Gay;    for (i=1;i<=n;i++) a[i]=read ();    Sort (a+1,a+n+1);        for (i=1;i<=n;i++) {for (j=1;j<i;j++) {if (GCD (A[i],a[j]) ==1) g[i][j]=g[j][i]=1;    }} for (i=1;i<=n;i++) if (!vis[i]) DFS (i);    for (i=1;i<=n;i++) if (!in[i]) Q.push (MP (A[i],i));        while (!q.empty ()) {gay=q.top (); Q.pop ();        printf ("%d", gay.val);            for (I=first[gay.id];~i;i=e[i].next) {v=e[i].to;in[v]--;        if (in[v]==0) Q.push (MP (A[V],V)); }    }}

[agc010e] rearranging [topological sorting + heap]

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.