| ||||||||||
| 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 | |||||||||
Need help! TLE...#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator