您的位置:首页 > 编程语言

微软编程之美初赛第一场第二题树

2014-04-19 22:20 260 查看
第二题一直wa,无语了。

思路写一下吧,

1.先用图的方式保存这个树 比如2的父节点为1则d[1][2]=1;d[i][j]表示j的父节点为i。

2.计算每个节点的深度

3.对树进行操作

#include<iostream>
#include<string.h>
#define  N 1001
using namespace std;
int d

;
int depth
;
long long w
;
int n;
void calucatedepth(){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(d[i][j])
depth[j]=1+depth[i];
}
}
}
void operatree(int u,int l,int r,int delta){
if(depth[u]>r)
return;
if(depth[u]>=l&&depth[u]<=r){
w[u]+=delta;
}
for(int i=1;i<=n;i++)
if(d[u][i])
operatree(i,l,r,delta);

}
void printhash(){
long mod=1000000007;
long magic=12347;
long long Hs=0;
for(int i=1;i<=n;i++){
Hs=(Hs*magic+w[i])%mod;
}
cout<<Hs<<endl;
}
int main(){
int T;
cin>>T;
for(int Case=1;Case<=T;Case++){
memset(d,0x0,sizeof(d));
memset(depth,0x0,sizeof(depth));
memset(w,0x0,sizeof(w));
cin>>n;
int temp,Q,u,l,r,delta;
depth[1]=1;
for(int i=2;i<=n;i++){
cin>>temp;
d[temp][i]=1;
}
calucatedepth();
cin>>Q;
while(Q--){
cin>>u>>l>>r>>delta;
operatree(u,l,r,delta);
}
cout<<"Case "<<Case<<": ";
printhash();
}
}


不知道错在哪里,求指导啊。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: