HDU 5967 small R with mobile phone (dynamic tree)

Source: Internet
Author: User

"Topic link" http://acm.hdu.edu.cn/showproblem.php?pid=5967

"The main topic"

Give a picture of each point with only one forward edge, or no connection,
Requires that the query be in a situation where there is a change to an edge, and each point passes through the end that has a forward edge.
If it's a ring then output-1

Exercises

We maintain it with LCT, and mark the ring at the point where each root node is located,
Because the ring must be in the root node, otherwise it is just a direct parent node and not ring,
For the action of the Direct parent node, we see if the root of the tree has a tag,
If there is a need to reconstruct the root of the tree after Cutf,
If the parent node of the switch is its own child node, then it becomes a tree and marks the ring,

Code

#include <cstdio> #include <cstring> #include <algorithm> using namespace std;const int n=200010;    namespace link_cut_tree{int F[n],son[n][2],val[n],sum[n],tmp[n],xor[n];bool rev[n];        void Initialize () {memset (f,0,sizeof (f));        memset (son,0,sizeof (son));        Memset (Val,0,sizeof (Val));    memset (rev,0,sizeof (rev.)); } bool IsRoot (int x) {return!f[x]| |    Son[f[x]][0]!=x&&son[f[x]][1]!=x;}    void Rev1 (int x) {if (!x) Return;swap (son[x][0],son[x][1]); rev[x]^=1;}    void pb (int x) {if (rev[x]) Rev1 (son[x][0]), Rev1 (Son[x][1]), rev[x]=0;}        void rotate (int x) {int y=f[x],w=son[y][1]==x;        SON[Y][W]=SON[X][W^1];        if (son[x][w^1]) f[son[x][w^1]]=y;            if (F[y]) {int z=f[y];        if (son[z][0]==y) Son[z][0]=x;else if (son[z][1]==y) son[z][1]=x;    }f[x]=f[y];f[y]=x;son[x][w^1]=y;        } void Splay (int x) {int s=1,i=x,y;tmp[1]=i;        while (!isroot (i)) tmp[++s]=i=f[i];        while (s) PB (tmp[s--]); WhiLe (!isroot (x)) {y=f[x];            if (!isroot (y)) {if ((son[f[y]][0]==y) ^ (son[y][0]==x)) rotate (x); else rotate (y);}        Rotate (x);    }} void access (int x) {for (int y=0;x;y=x,x=f[x]) splay (x), son[x][1]=y;}    Query the root of the tree where x is located int root (int x) {access (x); splay (x); while (son[x][0]) X=son[x][0];return x;}    Causes X to become the root void makeroot (int x) {access (x); splay (x); Rev1 (x);}    Merges the X and Y-owned trees with void link (int x,int y) {makeroot (x); f[x]=y;access (x);}    Separates X from its parent node void Cutf (int x) {access (x); splay (x); f[son[x][0]]=0;son[x][0]=0;}    Cut the Edge x-y off void cut (int x,int y) {makeroot (x); Cutf (y);}    Query x-to-y chains and int ask (int x,int y) {makeroot (x); Access (y); splay (y); return sum[y];}    Computes the Xor and int xorsum x to y (int x,int y) {makeroot (x); Access (y); splay (y); return xor[y];}    Query node to root distance int query (int x) {access (x); splay (x); return sum[x];}    Changes the value of x to the y int change (int x,int y) {makeroot (x); val[x]=y;} Change the father of X to y int changef (int x,int y) {cutf (x); f[x]=y;}} int mark[n];voidLink (int x,int y) {using namespace Link_cut_tree;    int rx=root (x);    if (rx==x) mark[x]=0;    else Cutf (x);        if (Mark[rx]&&root (Mark[rx])!=rx) {CHANGEF (Rx,mark[rx]);    mark[rx]=0;    }if (Root (y) ==x) mark[x]=y; else Changef (x, y);}    int N,m,op,x;int Main () {scanf ("%d%d", &n,&m);    using namespace Link_cut_tree;    Initialize ();        for (int i=1;i<=n;i++) {scanf ("%d", &x);    if (x) Link (i,x);        } while (m--) {scanf ("%d%d", &op,&x);            if (op==2) {x=root (x);        printf ("%d\n", mark[x]==0?x:-1);            }else{int y;            scanf ("%d", &y);        Link (x, y); }}return 0;}

HDU 5967 small R with Mobile (Dynamic 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.