Bzoj 3319: Black and white trees

Source: Internet
Author: User

Test instructions

Given a tree, the color of the edges is black or white, and the original is all white. Maintain two operations:


1. Query the label of the first black edge on the root path.
2. Set the color of all edges on the U to v path to black.

Notice: The root node of this tree is 1

Exercises

10^6 data range, tree profile is too much.

A better way?

First it is easy to think that each side only the first time is stained when the time is valid, so can refer to Bzoj 2054: crazy steamed bread a problem with and check set maintenance up the first white point. Thus each point is guaranteed to be dyed only once and the complexity is O (n * k).

And then what?

I think of an approach that dist[i] represents the depth of the current point up to the nearest black edge before the I moment. Updated in the order of DFS, each access to a point, put greater than equal (this point with its father's edge is dyed black moment) of the moment of the dist to this point and its father's edge of the depth, ah feel very reliable, the interval modified to open a tree can be persisted line.

Orz Towerlight told me it's dist is obviously monotonous, so casually with a monotonous stack to engage in, I said the monotonous stack how to persist ah, he said this does not need to be persistent as long as you can backtrack on it, ah, I said that how can be retrospective ah, he said you are weak ah, even [Noi20   14] No tickets have been written. Then I thought about it as if it is really possible to do, write a monotonous stack when the elements of the stack is not deleted, but to open something to refer to the past, and then the original point to save the place on the line!!

Well, this method should be OK.

Here's a nice way to label the solution:

In fact, as the pretreatment of the use and check the set backwards sweep over the past is enough! Pretreatment is a white tree dyed black, this time a black tree dyed white can be. FAT2 maintain the current point up to the first black dot, by the time backwards sweep, the first (black moment) equals the current time of the Dianran white, and then ask is find (x),, it is so simple,

Implementation is also easy:

#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include < Cstring> #define MAXN 1000005using namespace Std;int N, M, HEAD[MAXN], CHANGE[MAXN], FAT[MAXN], EE, DEEP[MAXN], Numm[max N], FAT2[MAXN], FIRST[MAXN], LA[MAXN], cnt[maxn];struct edge{int to, Next, num;} EDGE[MAXN * 2];inline void Addedge (int x, int y, int id) {edge[++ ee].to = y; edge[ee].next = head[x]; head[x] = EE; edge[e E].num = ID;}         void Dfs (int x, int fatt) {fat[x] = Fatt;    DEEP[X] = Deep[fatt] + 1; for (int i = head[x]; i =-1; i = edge[i].next) if (edge[i].to! = Fatt) numm[edge[i].to] = Edge[i].num, DFS (edge[i].t o, x);} inline int get (int x) {return x = = Fat2[x]? x:fat2[x] = Get (Fat2[x]);}        void color (int x, int y, int tt) {x = Get (x), y = Get (y);        while (x! = y) {if (Deep[x] < deep[y]) swap (x, y);        if (first[x]! = m + 1) x = fat2[x];    else fat2[x] = fat2[fat[x]], first[x] = TT, x = fat[x]; }}int Main () {scanf ("%d%d", &n, &m);    Memset (Head,-1, sizeof (head));    Memset (LA,-1, sizeof (LA));        for (int i = 1; i < n; i + +) {int x, y; scanf ("%d%d", &x, &y); Addedge (x, y, i);        Addedge (y, x, i);    } for (int i = 1; I <= n; i + +) fat2[i] = i, first[i] = m + 1;    DFS (1, 1);        for (int i = 1; I <= m; i + +) {int x, y; scanf ("%d", &x);            if (x = = 2) {scanf ("%d%d", &x, &y);        Color (x, y, i);    }else scanf ("%d", &la[i]);    } for (int i = 1; I <= n; i + +) + + cnt[first[i]];    for (int i = 1; I <= m + 1; i + +) Cnt[i] + = cnt[i-1];    for (int i = 1; I <= n; i + +) Change[cnt[first[i]]--] = i;    for (int i = 1; I <= n; i + +) fat2[i] = i;    int all = n, u;        for (int i = m + 1; i; I-) {if (la[i]! = 1) La[i] = Numm[get (La[i]);        else for (; all && first[u = Change[all]] = = I, all--) fat2[u] = Fat2[fat[u]; } for (int i = 1; I <= m; i + +) if (la[i]! =-1) printf ("%d\n", La[i]); return 0;}

Bzoj 3319: Black and white trees

Related Article

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.