Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
终于过了,给个代码参考。#include<iostream> using namespace std; int c[100005],sum[100005],N; int first[100005],last[100005]; int znum=1,fnum=1; struct Edge { int to; Edge *next; }; Edge *edge[100005],*temp; int lowbit(int x) { return x&(-x); } int Sum(int n) { int sum=0; while(n) { sum+=c[n]; n-=lowbit(n); } return sum; } void add(int n,int num) { while(n<=N) { c[n]+=num; n+=lowbit(n); } } void dfs(int id) { Edge *temp; first[id]=znum++; temp=edge[id]; for(;temp!=NULL;temp=temp->next) { fnum++; dfs(temp->to); } last[id]=fnum; } int main() { int i,u,v,qnum; char ch; scanf("%d",&N); for(i=1;i<=N-1;i++) { scanf("%d%d",&u,&v); temp=new Edge; temp->to=v;temp->next=NULL; if(edge[u]==NULL)edge[u]=temp; else { temp->next=edge[u]; edge[u]=temp; } add(i,1); } add(N,1); dfs(1); scanf("%d",&qnum); //for(i=1;i<=N;i++)cout<<first[i]<<" "<<last[i]<<endl; for(i=0;i<qnum;i++) { getchar(); scanf("%c%d",&ch,&u); if(ch=='C') { u=first[u]; if(Sum(u)-Sum(u-1)==1)add(u,-1); else add(u,1); } else printf("%d\n",Sum(last[u])-Sum(first[u]-1)); } //system("pause"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator