"Bzoj" 3319: Black and White tree (and check set + special tricks/-tree chain split + line tree)

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=3319

I think it is a template problem to review the next hld ....... ...........

Then nlg^2n was taken by the tle ......... .............

Then look at the Qaq,,, this ...

God-Problem practice ... I'll write again later ...

HLD of Tle:

#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }const int N=1e6+10;int ihead[n], cnt;struct dat {int Next, to, ID }e[n<<1];void Add (int u, int v, int id) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].id=id;e[++cnt]. NEXT=IHEAD[V]; ihead[v]=cnt; E[cnt].to=u; E[cnt].id=id;} int n, Son[n], top[n], fa[n], sz[n], root, id[n], tot, ed[n], dep[n];void dfs1 (int x, int f) {sz[x]=1; int y; fa[x]=f; dep [X]=DEP[F]+1;RDM (x, i) if (f!= (y=e[i].to)) {dfs1 (y, x); Sz[x]+=sz[y];if (Sz[y]>sz[son[x]]) son[x]=y;}} void dfs2 (int x, int tp) {TOP[X]=TP; Id[x]=++tot;if (Son[x]) DFS2 (son[x], TP), RDM (x, i) if (E[i].to!=fa[x] && e[i].t O!=SON[X]) dfs2 (e[i].to, e[i].to);} void geted (int x, int t) {ED[ID[X]]=T;RDM (x, i) if (E[i].to!=fa[x]) geted (e[i].to, e[i].id);} #define LC x<<1#define RC X<<1|1#define Lson L, Mid, Lc#define Rson mid+1, R, Rc#define mid (L+r) >>1stru CT T {int mx; bool tag;} T[n<<2];void upd (int x, int r) {T[x].tag=1;t[x].mx=max (t[x].mx, R);} void pushdown (int x, int mid, int r) {if (T[x].tag) t[x].tag=0, UPD (LC, mid), UPD (RC, r);} void pushup (int x) {T[x].mx=max (T[LC].mx, t[rc].mx); }void Update (int L, int R, int l=1, int r=tot, int x=1) {if (l<=l && r<=r) {upd (x, R); return;} int Mid=mid;pushdown (x, Mid, R), if (l<=mid) Update (L, R, Lson), if (mid<r) Update (L, R, Rson);p ushup (x);} int ask (int L, int R, int l=1, int r=tot, int x=1) {if (l<=l && r<=r) return t[x].mx;int Mid=mid, ret=0;pushd Own (x, Mid, R), if (L<=mid) Ret=max (ret, Ask (L, R, Lson)), if (Mid<r) Ret=max (ret, Ask (L, R, Rson)); return ret;} void ask (int x) {int fx=top[x], pos=0;while (fx!=root) {pos=ask (Id[fx], id[x]), if (pos!=0) BREAK;X=FA[FX]; fx=top[x];} if (pos==0 && x!=root) pos=ask (Id[top[x], id[x]);p rintf ("%d\n", Ed[pos]);} void change (int x, int y) {int fx=top[x], fy=top[y];while (fx!=fy) {if (Dep[fx]<dep[fy]) {swap (FX, FY), swap (x, y);} Update (ID[FX], id[x]); X=FA[FX]; FX=TOP[X];} if (x==y) return;if (Dep[x]<dep[y]) swap (x, y); Update (id[y]+1, id[x]);} int main () {read (n); int m=getint (); For1 (i, 1, n-1) {int u=getint (), V=getint (), Add (U, V, i);}ROOT=1;DFS1 (root, 0); DFS2 (root, root); geted (root, 0), while (m--) {int Cc=getint (), if (cc==1) Ask (Getint ()), else {int u=getint (), V=getint (), Change (U, v);}} return 0;}

  

Description

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

Input


The first row of two number n,m represents the number of points and operands, respectively.
Next n? 1 lines, 2 numbers per line u,v. Represents a U to v edge.
Next m line, each behaves in the following format:


1 v indicates the first operation


2 v U represents the second operation



Output

For each query, output the corresponding answer. If not present, output 0.

Sample INPUT5 4
1 2
1 3
2 4
2 5
1 2
2 2 3
1 3
1 4

Sample Output0
2
1
HINT





For 100% data: n,m<=10^6

Source

"Bzoj" 3319: Black and White tree (and check set + special tricks/-tree chain split + line tree)

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.