记录编号 351231 评测结果 AAAATTTTTT
题目名称 新型武器 最终得分 40
用户昵称 GravatarsrO cwm Orz 是否通过 未通过
代码语言 C++ 运行时间 6.005 s
提交时间 2016-11-16 12:14:46 内存使用 5.95 MiB
显示代码纯文本
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<queue>
  5. #include<vector>
  6. #include<cstring>
  7. using namespace std;
  8.  
  9. const int maxn = 300003;
  10. vector<int> G[maxn];
  11. int w[maxn];
  12. int depth[maxn];
  13. bool inq[maxn],vis[maxn];
  14. int tot;
  15. int n,q;
  16.  
  17. void bfs(){
  18. queue<int> q;
  19. q.push(1);inq[1]=1;
  20. while(!q.empty()){
  21. int u = q.front(); q.pop(); inq[u]=0;
  22. for(int i = 0; i < G[u].size(); i++){
  23. int to = G[u][i];
  24. if(!inq[to] && !depth[to]){
  25. depth[to] = depth[u]+1;//printf("%d %d\n",to,depth[to]);
  26. q.push(to);
  27. inq[to]=1;
  28. }
  29. }
  30. }
  31. depth[1]=0;
  32. }
  33.  
  34. int sum;
  35. int ndep;
  36.  
  37. void dfs(int fr){
  38. if(depth[fr] == ndep){
  39. sum += w[fr];
  40. return;
  41. }
  42. if(depth[fr] > ndep)return;
  43. for(int i = 0; i < G[fr].size(); i++){
  44. int to = G[fr][i];
  45. dfs(to);
  46. }
  47. }
  48. int main(){
  49. #ifndef DEBUG
  50. string FileName="newweapon";
  51. freopen((FileName+".in").c_str(),"r",stdin);
  52. freopen((FileName+".out").c_str(),"w",stdout);
  53. #endif
  54. scanf("%d",&n);
  55. for(int i = 1; i <= n; i++)scanf("%d",&w[i]);
  56. for(int i = 1; i <= n-1; i++){
  57. int u,v;
  58. scanf("%d%d",&u,&v);
  59. G[u].push_back(v);
  60. }
  61. bfs();
  62. scanf("%d",&q);
  63. for(int i = 0; i < q; i++){
  64. int a,b,c;
  65. scanf("%d%d%d",&a,&b,&c);
  66. if(a == 1){
  67. w[b] = c;
  68. }else{
  69. sum = 0;
  70. ndep = c+depth[b];
  71. dfs(b);
  72. printf("%d\n",sum);
  73. }
  74. }
  75. }
  76.