bzoj4538: [Hnoi2016] Network

Source: Internet
Author: User
Tags int size
Topic links

bzoj4538 Topic description Description

A simple network system can be described as a tree without roots. Each node is a server. The data line connecting the server to the server is considered a tree edge. When two servers interact with the data, the data passes through all the servers on the path connecting the two servers (both servers themselves). Because this path is unique, data cannot interact when a server on the path fails and does not function properly. In addition, each data interaction request has an important degree, and the more important the request clearly needs to get the higher priority right. Now, you, as an administrator of a network system, monitor the running state of the entire system. The operation of the system is also very simple, at every moment, only one of the following three types of events can occur:
1. A new request for data interaction occurs between the two servers;
2. A data interaction end request;
3. A server fails. The system will fix it immediately after any failure occurs. That is, after the time of failure, the server is still normal. However, when the server fails, the data interaction requests that need to go through the server are still affected.
Your task is to maintain the maximum number of critical degrees in an unaffected request each time a failure occurs. Note that if a data interaction request has ended, it will not be included in the scope of the request that is not affected. Input

The first line, two positive integer n,m, describes the number of servers and events, respectively. The server number starts at 1, so that the number of n servers is in turn a,..., N. Next n-1 line, two positive integers per line u,v, describes a tree edge. The U and V are the server's number. Next M-line, according to the moment of occurrence
Each event is described, i.e. the line I (i=1,2,3,..., m) describes the events that occur at moment I. The first number of each line describes the event type, a total of 3 types:
(1) If type=0, then there are three positive integer a,b,v, indicating that there is an important data interaction request between Server A and B;
(2) If type=1, then there is a positive integer t, which indicates the end of the data interaction request that occurs at the moment T (that is, the event of the first T);
(3) If type=2, then there is a positive integer x, indicating that server x has failed at this point. For each event of type 2, it is a single query that asks "What is the maximum value of the number of critical degrees in a request that is not affected when server x fails." ”
Note that there may be a situation where the server itself interacts with the data itself. Output

For each type=2 event, the server failed event, the output is an integer that describes the maximum value of the critical degree in the request that was not affected. If there are no requests at this time, or if all requests are affected, then output-1. Sample Input

13 23
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
6 10
6 11
7 12
7 13
2 1
0 8 13 3
0 9 12 5
2 9
2 8
2 2
0 10 12 1
2 2
1 3
2 7
2 1
0 9 5 6
2 4
2 5
1 7
0 9 12 4
0 10 5 7
2 1
2 4
2 12
1 2
2 5
2 3 Sample Output

-1
3
5
-1
1
-1
1
1
3
6
7
7
4
6 HINT

The tree given by the sample is as follows:

Explain some of these queries; The following explanation uses (A,B;T,V) to represent a request for an important degree of V between Server A and B that occurs at t time:
For the first query (at moment 1), there is no request at this time, output-1.
For the fourth inquiry (at moment 6), there are two interactions (8,13;2,3), (9,12;3,5), all queries go through server 2nd, output-1.
For the fifth inquiry (at moment 8), there are three interactions (8,13;2,3), (9,12;3,5), (10,12;7,1), and only the interaction (10,12;7,1) is not 2nd server, so the output of its importance 1.
For the last query (at moment 23), there are three interactions (9,5;12,6), (9,12;16,4), (10,5;17,7). When server 3rd fails, only the interaction (9,5;12,6) does not go through the 3rd number server, so output 6. Solving

We consider tree dissection, maintaining a heap on a segment tree, and preserving the weights of paths that do not pass through that interval.
For a path we will extract the interval from the segment tree, and in the case of the segment tree, modify the interval. That is, it is inserted or deleted in the heap.
For a single query, it is only possible to find the maximum heap top weight value in the segment that contains the point.

There is also a way to consider the two-point answer, if the side is greater than the answer to the question, then the small two points otherwise toward the large two points. So we just maintain the intersection of the paths. Using RMQ to find the LCA, O (1) can be used to find the intersection of two paths. It is possible to maintain a binary structure with a segment tree after discrete, and to record the intersection of paths on the corresponding nodes, the query can be answered within O (logn) O (log n).

Here is the code for the first approach:

#include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std;
const int n=100010;
    struct heap{priority_queue<int>a,b;
    void del (int x) {b.push (x);}
    void push (int x) {a.push (x);}
        int Top () {while (!b.empty () &&a.top () ==b.top ()) A.pop (), B.pop ();
        if (A.empty ()) return-1;
    return A.top ();
}}t[n*4];
Vector<pair<int,int> >stk; struct Edge{int X,nex;}
E[N*2];
int size[n],son[n],top[n],dep[n],dfn[n],fa[n],first[n];

int n,m,op,tp,x[n*2],y[n*2],z[n*2],tot,cnt;
    void Add (int x,int y) {e[++tot].x=y;
    E[TOT].NEX=FIRST[X];
First[x]=tot;
    } void dfs1 (int x,int y) {size[x]=1; fa[x]=y; dep[x]=dep[y]+1;
        for (int I=first[x];i;i=e[i].nex) if (e[i].x!=y) {DFS1 (e[i].x,x);
        if (size[son[x]]<size[e[i].x]) son[x]=e[i].x;
    Size[x]+=size[e[i].x];
    }} void dfs2 (int x,int y) {dfn[x]=++cnt; top[x]=y;
    if (Son[x]) DFS2 (son[x],y); for (int i=fIrst[x];i;i=e[i].nex) if (E[i].x!=fa[x]&&e[i].x!=son[x]) DFS2 (e[i].x,e[i].x);
    } void Modify (int k,int l,int r,int ql,int qr,int val,int flag) {if (QL&GT;QR) return; 
        if (ql==l&&qr==r) {if (flag) T[k].push (VAL);
        Else T[k].del (Val);
    Return
    } int mid= (L+R) >>1;
    if (l<=mid) modify (K*2,l,mid,ql,min (Qr,mid), Val,flag);
if (r>mid) modify (K*2+1,mid+1,r,max (MID+1,QL), Qr,val,flag);
    } void Updata (int x,int y,int val,int flag) {stk.clear ();
        while (Top[x]!=top[y]) {if (dep[top[x]]<dep[top[y])) swap (x, y);
        Stk.push_back (Make_pair (dfn[top[x]],dfn[x));
    X=FA[TOP[X]];
    } if (Dep[x]<dep[y]) swap (x, y);
    Stk.push_back (Make_pair (dfn[y],dfn[x));
    Sort (Stk.begin (), Stk.end ());
    X=1;
        for (int i=0;i< (int) stk.size (); i++) {modify (1,1,n,x,stk[i].first-1,val,flag);
    x=stk[i].second+1;
} modify (1,1,n,x,n,val,flag); } int query (int k,int l,int r,int x) {if (l==r)return T[k].top ();
    int mid= (L+R) >>1;
    if (X<=mid) return Max (T[k].top (), query (k*2,l,mid,x));
Return Max (T[k].top (), query (k*2+1,mid+1,r,x));
    } int main () {scanf ("%d%d", &n,&m);
        for (int i=1;i<n;i++) {scanf ("%d%d", &x[0],&y[0]); Add (x[0],y[0]);
    Add (y[0],x[0]); } DFS1 (1,0);
    DFS2 (a);
        for (int i=1;i<=m;i++) {scanf ("%d", &AMP;TP);
            if (tp==0) {scanf ("%d%d%d", &x[i],&y[i],&z[i]);
        Updata (x[i],y[i],z[i],1);
            } if (tp==1) {scanf ("%d", &x[i]);
        Updata (x[x[i]],y[x[i]],z[x[i]],0);
            } if (tp==2) {scanf ("%d", &x[i]);
        printf ("%d\n", Query (1,1,n,dfn[x[i]));
}} return 0; }

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.