比赛 |
20161116 |
评测结果 |
AAAATTTTTT |
题目名称 |
新型武器 |
最终得分 |
40 |
用户昵称 |
srO cwm Orz |
运行时间 |
6.857 s |
代码语言 |
C++ |
内存使用 |
7.75 MiB |
提交时间 |
2016-11-16 12:08:58 |
显示代码纯文本
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 300003;
vector<int> G[maxn];
int w[maxn];
int depth[maxn];
bool inq[maxn],vis[maxn];
int tot;
int n,q;
void bfs(){
queue<int> q;
q.push(1);inq[1]=1;
while(!q.empty()){
int u = q.front(); q.pop(); inq[u]=0;
for(int i = 0; i < G[u].size(); i++){
int to = G[u][i];
if(!inq[to] && !depth[to]){
depth[to] = depth[u]+1;//printf("%d %d\n",to,depth[to]);
q.push(to);
inq[to]=1;
}
}
}
depth[1]=0;
}
int Query(int fa,int dep){
memset(vis,0,sizeof(vis));
memset(inq,0,sizeof(inq));
int ans(0);
queue<int> q;
q.push(fa);inq[fa]=1;
while(!q.empty()){
int u = q.front();
q.pop();
inq[u]=0;
if(depth[u] > dep)continue;
for(int i = 0; i < G[u].size(); i++){
int to = G[u][i];
if(vis[to])continue;
if(depth[to] == dep && !vis[to]) ans += depth[to],vis[to]=1;//,printf("ans=%d,to=%d,depth[to]=%d\n",ans,to,depth[to]);
else{
q.push(to);
inq[to]=1;
//printf("ans=%d,to=%d,depth[to]=%d\n",ans,to,depth[to]);
}
}
}
return ans;
}
int sum;
int ndep;
int num[maxn];
void dfs(int fr){
if(depth[fr] == ndep){
sum += w[fr];
// num[fr]++;
// printf("fr=%d\n",fr);
return;
}
if(depth[fr] > ndep)return;
for(int i = 0; i < G[fr].size(); i++){
int to = G[fr][i];
if(!vis[to]){
vis[to]=1;
dfs(to);
// vis[to]=0;
}
}
}
int main(){
#ifndef DEBUG
string FileName="newweapon";
freopen((FileName+".in").c_str(),"r",stdin);
freopen((FileName+".out").c_str(),"w",stdout);
#endif
scanf("%d",&n);
for(int i = 1; i <= n; i++)scanf("%d",&w[i]);
for(int i = 1; i <= n-1; i++){
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
// G[v].push_back(u);
}
bfs();
scanf("%d",&q);
for(int i = 0; i < q; i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a == 1){
w[b] = c;
}else{
sum = 0;
memset(vis,0,sizeof(vis));memset(num,0,sizeof(num));
ndep = c+depth[b];
dfs(b);
printf("%d\n",sum);
}
}
}