[POI2007] Meg-megalopolis (tree-like array, DFS order)

Source: Internet
Author: User

Title Description


Solution

The exam was not carefully thought, the results only took the violent points ...
is actually a DFS sequence + tree-like array.

Let's turn it into a sequence with DFS and record their \ (siz\).
So every time we have one edge, we have an effect on the subtree.
It's good to maintain it in a tree array.

Code
#include <bits/stdc++.h>using namespace Std;const int maxn=250008;struct sj{int to; int next;}    A[maxn*2];int head[maxn],size;int n,m;void add1 (int x,int y) {a[++size].to=y;    A[SIZE].NEXT=HEAD[X]; Head[x]=size;}    int id[maxn],tot;int siz[maxn],ans[maxn];void DFS (int x) {id[x]=++tot;    Siz[x]=1;        for (int i=head[x];i;i=a[i].next) {int tt=a[i].to;            if (!siz[tt]) {ans[tt]=ans[x]+1;            DFS (TT);            SIZ[X]+=SIZ[TT]; }}}int c[maxn];int lowbit (int x) {return x& (-X);} void Add (int x,int z) {for (int i=x;i<=n;i+=lowbit (i)) c[i]+=z;}    int check (int x) {int sum=0;    for (int i=x;i>=1;i-=lowbit (i)) sum+=c[i]; return sum;}    int main () {scanf ("%d", &n);    for (int i=1;i<n;i++) {int x, Y, scanf ("%d%d", &x,&y), Add1 (x, y); Add1 (y,x);}    DFS (1);    for (int i=1;i<=n;i++) Add (id[i],0);    scanf ("%d", &m); for (int i=1;i<=m+n-1;i++) {int X,y;char ch; cin>>ch;        if (ch== ' A ') {scanf ("%d%d", &x,&y);            if (Id[x]<id[y]) swap (Y,X);            Add (id[x],1);        Add (id[x]+siz[x],-1);    } if (ch== ' W ') scanf ("%d", &x), Cout<<ans[x]-check (Id[x]) <<endl;  }}

[Poi2007]meg-megalopolis (tree array, DFS order)

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.