Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这个是考虑多分枝的,也是WA

Posted by tsmrdk at 2011-07-19 21:29:53 on Problem 3321
In Reply To:data Posted by:jerryjiang at 2011-06-27 03:23:09
#include<stdio.h>
#include<vector>
using namespace std;
#define N 100003
typedef struct Node
{
	bool flag;
	int cnt,p;
	vector<int>child;
}Node;
Node Tree[N];
void inittree(int n)
{
	for(int i=0;i<=n;i++)
	{
		Tree[i].flag=false;
		Tree[i].cnt=Tree[i].p=0;		
	}
}
int buildtree(int i)
{
	if(i==0) return 0;
	Tree[i].flag=true;
	Tree[i].cnt=1;
	for(int j=0;j<Tree[i].child.size();j++)
	{
		Tree[i].cnt+=buildtree(Tree[i].child[j]);
	}
	return Tree[i].cnt;
}
void updatetree(int i,int d)
{
	while(i!=0)
	{
		Tree[i].cnt+=d;
		i=Tree[i].p;
	}	
}
int main()
{
	int n,i,u,v,tmp,m;
	char ch;
	//freopen("data.txt","r",stdin);
	scanf("%d",&n);
	inittree(n);
	for(i=1;i<n;i++)
	{
		scanf("%d%d",&u,&v);
		if(u>v) {tmp=u;u=v;v=tmp;}
		Tree[u].child.push_back(v);
		Tree[v].p=u;
	}	
	buildtree(1);	
	scanf("%d",&m);
	while(m--)
	{
		scanf(" %c%d",&ch,&i);
		if(ch=='C')
		{
			if(Tree[i].flag)
			{
				Tree[i].flag=false;
				updatetree(i,-1);
			}
			else
			{
				Tree[i].flag=true;
				updatetree(i,1);
			}
		}
		else if(ch=='Q')
		{
			printf("%d\n",Tree[i].cnt);
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator