[Title] topological sorting

Topological sorting

P3243 [HNOI2015] prepared dishes: the first note title in claim 1 as far forward surface, then 2 as far forward, then. . .
In fact, greedy, then Canada needs to build anti map root stack to run, reason: We start with the last point may become one of the biggest challenges out (certainly not inferior), and then put all of his successor node is inserted, this time we then take the biggest; we can take a point is equivalent to the maximum point, as far as possible after taking a smaller point, so we can put less points as possible to put forward.

inline void main() {
    T=g(); while(T--) { cnt=tot=0;
        priority_queue<int> q;
        memset(fir,0,sizeof fir),memset(in,0,sizeof in);
        n=g(),m=g(); for(R i=1,u,v;i<=m;++i) u=g(),v=g(),add(v,u);
        for(R i=1;i<=n;++i) if(!in[i]) q.push(i);
        while(q.size()) { R u=q.top(); q.pop(); ans[++tot]=u;
            for(R i=fir[u];i;i=nxt[i]) { R v=vr[i];
                if(--in[v]==0) q.push(v);
            }
        } if(tot<n) {puts("Impossible!"); continue;}
        for(R i=n;i;--i) printf("%d ",ans[i]); puts("");
    }
}

P1954 [NOI2010] air traffic control: on a similar question. Note that the first question is still not being built map, because there may be something like this:

So you put the \ (k_i = 2 \) is the point goo out. .
Therefore, we will build FIG trans: trans construction because the panels into position on the topology sequence \ (pos_i> n--K_i \) , and we have taken the minimum certainly correct. (OTZ)
so that we can solve the first question.
Then, for the second question, we have to run for each point while \ (\ mathcal {O} ( mlogn) \) determination. Specifically, running anti map, we can find each point into the latest time to go, that is the first time in the original graph.

inline int solve(int x) {//返回x点最早的时间。
    R tot=0; while(q.size()) q.pop();
    memcpy(in,mem,sizeof mem);
    for(R i=1;i<=n;++i) if(i!=x&&!in[i]) q.push(mp(c[i],i));
    while(q.size()) { R w=q.top().first,u=q.top().second; q.pop();
        if(w<n-tot) return n-tot;
        for(R i=fir[u];i;i=nxt[i]) { R v=vr[i];
            --in[v]; if(!in[v]&&x!=v) q.push(mp(c[v],v));
        }
        ++tot;
    } return n-tot;
}

P3588 [POI2015] PUS: good questions, segment tree optimization built FIG. + Topology DP or differential constraint (almost), similar to the differential constraint model, \ (A <B \ rightarrow A \ Leq B-. 1 \ rightarrow B \) to \ (a \) even a weight value \ (- 1 \) edges of the shortest run similar to DP. Summary or think about before, we found that the right side is non-positive, and 0 no edge to form a ring, so if there is some negative ring ring original problem has no solution. So we can take DP topological sort to work it out. \ (D [V] = min (D [V], D [U] W + [I]) \) , we do not all values are located to determine the \ (1E9 \) (upper bound title claims) according to the equation DP, we ran out of each point must be a maximum value (after the minimum edge), we found that if the determined value is updated to the smaller, is no solution. Otherwise solvable, the direct output \ (d \) array can be.

P5603 board games and small C: greedy, topological sorting.
First we have been asked to remove the smallest. The second question for the current point, all extensions can be extended to smaller point.

const int N=500010;
int n,m,ans1,ans2,cnt,mx;
int vr[N],nxt[N],fir[N],mem[N],in[N],s[N],tot; 
priority_queue<int> p;
priority_queue<int,vector<int>,greater<int> > q;
inline void add(int u,int v) {vr[++cnt]=v,nxt[cnt]=fir[u],fir[u]=cnt;}
inline void main() {
    n=g(),m=g(); for(R i=1,u,v;i<=m;++i) u=g(),v=g(),add(u,v),++mem[v];
    memcpy(in,mem,sizeof mem);
    for(R i=1;i<=n;++i) if(!in[i]) q.push(i); 
    while(q.size()) { R u=q.top(); q.pop();
        if(u>mx) ++ans1,mx=u;
        for(R i=fir[u];i;i=nxt[i]) { R v=vr[i];
            if(--in[v]==0) q.push(v);
        }
    } memcpy(in,mem,sizeof mem),mx=0;
    for(R i=1;i<=n;++i) if(!in[i]) p.push(i); 
    while(p.size()) { R u=p.top();
        if(u>mx) ++ans2,mx=u;
        tot=0; while(p.size()) s[++tot]=p.top(),p.pop();
        for(R i=1;i<=tot;++i) { R u=s[i]; 
            for(R i=fir[u];i;i=nxt[i]) { R v=vr[i];
                if(--in[v]==0) {
                    if(v>mx) p.push(v);
                    else s[++tot]=v;
                }
            }
        }
    } printf("%d\n%d\n",ans1,ans2);
}

Guess you like

Origin www.cnblogs.com/Jackpei/p/11837179.html
Recommended