[10.27 In-school test] [Delete heap + expand row]

Source: Internet
Author: User

Solution

To find the longest path for a directed graph, you can think of topology sequence transfer. Both sides of the forward and backward processes the maximum value of each vertex from the start point and the end point. The maximum path length of each edge can be measured.

The problem is how to calculate the impact of deleting each vertex?

After the topology is sorted, you can find that deleting the vertices at the back of the number of layers will affect the front, because if you want to calculate the longest path of the front edge, you cannot determine the path through this point, therefore, you can only delete vertices from front to back in the topological order.

Let's talk about the practice here. Maintain a large root heap and store the longest paths currently enumerated. First, push the maximum value from each vertex to the heap. To delete a vertex from each enumeration, delete the path that has an impact on the previous vertex, update the answer, and add it to the heap for the subsequent path. After completing all the vertices, you can.

However, the heap cannot directly Delete the specified element. Therefore, you must delete the heap Qaq.

In fact, there are two heaps. One is to maintain the current heap, and the other is to maintain the elements to be deleted. Each deletion adds elements to the second heap. The actual operations are implemented during the removal process, if the two heap top elements are the same, delete them.

% $ JZY $ daxie code novelty, I am waiting for the average person to look up only %

Code

#include<bits/stdc++.h>#define RG registerusing namespace std;inline char nc(){    static char buf[100000],*p1=buf,*p2=buf;    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline void read(int &x) {    x = 0; int t = 1; char ch = nc();    while(ch > ‘9‘ || ch < ‘0‘) { if(ch == ‘-‘)    t = -1; ch = nc(); }    while(ch >= ‘0‘ && ch <= ‘9‘) { x = x * 10 + ch - ‘0‘; ch = nc(); }    x *= t;}struct Node {    int v, nex;} Edge[500005], Edge_rz[500005];int stot_rz = 0, h_rz[100005], stot, h[100005];void add(int u, int v) {    Edge[++stot] = (Node) {v, h[u]};    h[u] = stot;    Edge_rz[++stot_rz] = (Node) {u, h_rz[v]};    h_rz[v] = stot_rz;}class Heap {    private:        priority_queue < int > q1;        priority_queue < int > q2;    public:        inline void init() {            while(!q1.empty())    q1.pop();            while(!q2.empty())    q2.pop();        }        inline void push(int x) {            q1.push(x);        }        inline void del(int x) {            q2.push(x);        }        inline int top() {            while(!q2.empty() && q1.top() == q2.top()) {                q1.pop();    q2.pop();            }            return q1.top();        }} Q;int in[100005], q[100005], st[100005], ed[100005], ans, id, n, m;int main() {    freopen("johnny.in", "r", stdin);    freopen("johnny.out", "w", stdout);    int T;    read(T);    while(T --) {        memset(h, 0, sizeof(h));        memset(h_rz, 0, sizeof(h_rz));        stot = 0, stot_rz = 0; int t = 0, hi = 1;        memset(q, 0, sizeof(q));        memset(in, 0, sizeof(in));        memset(st, 0, sizeof(st));        memset(ed, 0, sizeof(ed));        ans = 0x3f3f3f3f, id = 0;        Q.init();        read(n);    read(m);        while(m --) {            int u, v;            read(u);    read(v);            add(u, v);            in[v] ++;        }        for(int i = 1; i <= n; i ++)            if(!in[i])    q[++t] = i;        while(hi <= t) {            int u = q[hi ++];            for(RG int i = h[u]; i; i = Edge[i].nex) {                int v = Edge[i].v;                if((-- in[v]) == 0)    q[++t] = v;            }        }        for(RG int i = 1; i <= n; i ++) {            int u = q[i];            for(RG int j = h[u]; j; j = Edge[j].nex) {                int v = Edge[j].v;                st[v] = max(st[v], st[u] + 1);            }        }        for(RG int i = n; i >= 1; i --) {            int u = q[i];            for(RG int j = h_rz[u]; j; j = Edge_rz[j].nex) {                int v = Edge_rz[j].v;                ed[v] = max(ed[v], ed[u] + 1);            }        }        for(int i = 1; i <= n; i ++)    Q.push(ed[i]);        for(RG int i = 1; i <= n; i ++) {            int u = q[i];            for(int j = h_rz[u]; j; j = Edge_rz[j].nex) {                int v = Edge_rz[j].v;                Q.del(st[v] + ed[u] + 1);            }            Q.del(ed[u]);            int now = Q.top();            if(now < ans)    ans = now, id = u;            else if(now == ans)    id = min(id, u);            for(RG int i = h[u]; i; i = Edge[i].nex) {                int v = Edge[i].v;                Q.push(st[u] + ed[v] + 1);            }            Q.push(st[u]);        }        printf("%d %d\n", id, ans);    }    return 0;}

 

[10.27 In-school test] [Delete heap + expand row]

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.