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

Need help! TLE...

Posted by okioki007 at 2008-08-25 00:08:14 on Problem 3321
#include <iostream>
using namespace std;

typedef struct node{
	bool next;
	int parent;
	int apple;
} node;

node appletree[100001];
int sum[100001];
int n, k;

int main(){
	cin >> n;
	int a, b;
	for(int i = 0; i < n-1; i++){
		scanf("%d %d", &a, &b);
		appletree[a].apple = 1;
		sum[a] = 1;
		appletree[a].next = true;
		appletree[b].apple = 1;
		appletree[b].parent = a;
		sum[b] = 1;
		appletree[b].next = false;
	}
	for(int i = 1; i <= n; i++){
		if(!appletree[i].next){
			int prev = i;
			int next = appletree[i].parent;
			while(1){
				sum[next] += sum[prev];
				if(next == 1) break;
				prev = next;
				next = appletree[next].parent;
			}
		}
	}
	cin >> k;
	char state;
	int x;
	for(int i = 0; i < k; i++){
		cin >> state >> x;
		if(state == 'Q'){
			cout << sum[x] << endl;
		}
		else if(state == 'C'){
			if(appletree[x].apple == 1){
				appletree[x].apple = 0;
				sum[x]--;
				int prev = x;
				int next = appletree[x].parent;
				while(1){
					sum[next]--;
					if(next == 1) break;
					prev = next;
					next = appletree[next].parent;
				}
			}
			else{ // if(appletree[x].apple == 0)
				appletree[x].apple = 1;
				sum[x]++;
				int prev = x;
				int next = appletree[x].parent;
				while(1){
					sum[next]++;
					if(next == 1) break;
					prev = next;
					next = appletree[next].parent;
				}
			}
		}
	}

	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