Codeforces Gym 101142 G gangsters in Central city (Lca+dfs sequence + Tree array +set)

Source: Internet
Author: User

Test instructions

The root node of the tree is the water source, numbered 1. The parent node of the point given number 2, 3, 4, ..., N. It is known that only leaf nodes are houses.

There are Q operations, each operation can be one of the following:

    1. + v, indicating that the house numbered V was occupied by gangsters.
    2. - v, indicating that the gangster exits the house numbered v.

All the original houses were not gangsters. For each change, the need to remove the fewest edges, so that all the houses have no gangsters to connect with the water, and on this basis, the number of affected ordinary houses are required to be the least.

Exercises

First, the subtree of the root node of the tree is categorized, so the most deleted edges are actually the number of subtrees.

For each subtree, if the number of ordinary houses affected is required to be the least, then it is actually the LCA of all the gangsters ' houses.

For this LCA, you can use the DFS sequence, and the LCA of the node with the smallest DFS sequence and the largest node is the LCA of all nodes (this can be maintained with set)

Then use a tree-like array to maintain how many ordinary houses are affected.

#include <iostream>#include<cstdio>#include<vector>#include<Set>#defineFi first#defineSe Secondusing namespaceStd;typedef pair<int,int>PII;Const intMAXN = 1e5 + -;intc[maxn*8], f[maxn][2];intDEEP[MAXN], p[maxn][ -], Col[maxn];vector<int>G[MAXN];Set<PII> s[maxn][2];intn, x, tot, q;Charstr[Ten]; PII ans;voidModify (intXints) {     for(; x <=2*n; x + = x& (-X)) c[x] + =s;}intQuery (inty) {    if(Y <=0)return 0; intAns =0;  for(intx = y; X X-= x& (-X)) ans + =C[x]; returnans;}intQueryintXintY) {returnQuery (y)-Query (X-1); }intLcaintUintv) {    if(Deep[u] >Deep[v]) Swap (U, v);  for(inti = -; I >=0; i--)if(Deep[p[v][i]] >= deep[u]) v =P[v][i]; if(U = = v)returnu;  for(inti = -; I >=0; i--)if(P[v][i]! = P[u][i]) u = p[u][i], V =P[v][i]; returnp[u][0];}voidDfsintXintFaintDintLab) {p[x][0] =FA; DEEP[X]=D; COL[X]=Lab; f[x][0] = ++tot;  for(Auto to:g[x]) {if(to = = FA)Continue; DFS (to, X, D+1, Lab); } f[x][1] = ++tot; if(g[x].size () = =0) {Modify (f[x][0],1); Modify (f[x][1],1); }}voidLca_pre () { for(intj =1; J <= -; J + +)         for(inti =1; I <= N; i++) P[i][j]= p[p[i][j-1]][j-1];}intMain () {Freopen ("gangsters.in","R", stdin); Freopen ("Gangsters.out","W", stdout); CIN>>n>>Q;  for(inti =1; I < n; i++) {scanf ("%d", &x); G[x].push_back (i+1); }    intColn = g[1].size ();  for(inti =0; I < g[1].size (); i++) DFS (g[1][i],1,1, i+1);    Lca_pre (); intu, V, UV;  while(q--) {cin>>str; if(str[0] =='+') {scanf ("%d", &x); if(s[col[x]][0].size () = =0) ans.fi++; if(s[col[x]][0].size () >0) {u= (*s[col[x]][0].begin ()). SE; v = (*--s[col[x]][1].end ()). SE; UV=LCA (U, v); if(G[uv].size ()! =0) ans.se-= Query (f[uv][0], f[uv][1])/2; } s[col[x]][0].insert ({f[x][0], x}); s[col[x]][1].insert ({f[x][1], x}); Modify (f[x][0], -1); Modify (f[x][1], -1); U= (*s[col[x]][0].begin ()). SE; v = (*--s[col[x]][1].end ()). SE; UV=LCA (U, v); if(G[uv].size ()! =0) ans.se + = query (f[uv][0], f[uv][1])/2; printf ("%d%d\n", ans.fi, ans.se); } Else{scanf ("%d", &x); if(s[col[x]][0].size () = =1) ans.fi--; U= (*s[col[x]][0].begin ()). SE, v = (*--s[col[x]][1].end ()). SE; UV=LCA (U, v); if(G[uv].size ()! =0) ans.se-= Query (f[uv][0], f[uv][1])/2; s[col[x]][0].erase ({f[x][0], x}); s[col[x]][1].erase ({f[x][1], x}); Modify (f[x][0],1); Modify (f[x][1],1); if(s[col[x]][0].size () >0) {u= (*s[col[x]][0].begin ()). SE, v = (*--s[col[x]][1].end ()). SE; UV=LCA (U, v); if(G[uv].size ()! =0) ans.se + = query (f[uv][0], f[uv][1])/2; } printf ("%d%d\n", ans.fi, ans.se); }    }    return 0;}

Codeforces Gym 101142 G gangsters in Central city (Lca+dfs sequence + Tree array +set)

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.