http://www.cogs.pro/cogs/problem/problem.php?pid=2274
★☆ 输入文件:heoi2016_tree.in
输出文件:heoi2016_tree.out
简单对比
时间限制:1 s 内存限制:128 MB
福利题?
1 #include <algorithm> 2 #include <iostream> 3 #include <cstdio> 4 5 using namespace std; 6 7 const int N(1e6+15); 8 int n,q,u,v; 9 10 int mark[N],dad[N]; 11 int Get_grandfa(int x,int y) 12 { 13 for(;!mark[x];) 14 x=dad[x]; 15 return x; 16 } 17 18 int main() 19 { 20 freopen("heoi2016_tree.in","r",stdin); 21 freopen("heoi2016_tree.out","w",stdout); 22 scanf("%d%d",&n,&q); 23 for(int i=1;i<n;i++) 24 scanf("%d%d",&u,&v),dad[v]=u; 25 mark[1]=1; 26 for(char op;q--;) 27 { 28 cin>>op>>v; 29 if(op==‘C‘) mark[v]=1; 30 else printf("%d\n",Get_grandfa(v,1)); 31 } 32 return 0; 33 }
时间: 2024-10-07 19:58:06