codevs-Apple Tree

Source: Internet
Author: User

Long time no blog, but also for a long time did not brush CV, casually write a bad article warm hands ...

This question is a topic in a tree ... General we would think of a tree array or line segment tree (Blue Goose Violence I also porcelain, just think this time seems to explode

At first I didn't look at it carefully--I thought the root node was not 1 and then began to worry, thought to seek a wave of gravity (fog

The center of gravity of the method to listen to Czl, and then Zzs said to use what points to divide the treatment (did not learn, do not understand, difficult to fight, abandoned pits

Because qzz over this problem, so start hugging thigh--root is 1 ah ... (on the topic of not reading ...)

So I knew the root of the tree and I was just thinking about using the DFS sequence to turn around and then the bare trees array.

Probably like this ... It's a pretty quick run >_<

#include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <vector > #include <map> #include <algorithm>using namespace std; #define MAXN 100010#define ll Long Long#define Rep (i,l,r,step) for (int i=l;i<=r;i+=step) #define DOWN (i,l,r,step) for (int i=l;i>=r;i-=step) int N,Q,T[MAXN],L[MAXN ],r[maxn],cnt; BOOL F[maxn];//tree_array#define lowbit (x) x&-xint Add (int x,int c) {Rep (i,x,n,lowbit (i)) t[i]+=c;} int sum (int x) {int Ans=0;down (i,x,1,lowbit (i)) Ans+=t[i];return ans;} int edge,to[maxn*2],next[maxn*2],head[maxn];//adjacency table void Insert (int u,int v) {to[++edge]=v;next[edge]=head[u];head[u]= Edge;} Dfs sequence void dfs (int rt,int fa) {l[rt]=++cnt;add (l[rt],1); for (int i=head[rt];i;i=next[i]) {if (TO[I]!=FA) DFS (TO[I],RT);} r[rt]=cnt;} int main () {memset (f,1,sizeof (f)), scanf ("%d", &n), Rep (i,1,n-1,1) {int x,y;scanf ("%d%d", &x,&y); Insert (x, y); insert (y,x);} DFS (1,0); scanf ("%d", &q), while (q--) {char op[10];int x;scanf ("%s%d", op,&x), if (op[0]== ' Q ') printf ("%d\n", SUM (R[x])-sum (l[x]-1)), Else{if (F[x]) {f[x]=0;add (l[x],-1);} Else{f[x]=1;add (l[x],1);}}} return 0;}

codevs-Apple 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.