微软编程之美初赛第一场第二题树
2014-04-19 22:20
260 查看
第二题一直wa,无语了。
思路写一下吧,
1.先用图的方式保存这个树 比如2的父节点为1则d[1][2]=1;d[i][j]表示j的父节点为i。
2.计算每个节点的深度
3.对树进行操作
不知道错在哪里,求指导啊。
思路写一下吧,
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(); } }
不知道错在哪里,求指导啊。
相关文章推荐
- 2014微软编程之美初赛第一场第二题 树
- 微软2014编程之美初赛第一场——题目2 : 树
- 微软2014编程之美初赛第一场——题目2 : 树
- 微软编程之美初赛第一场第一题焦距
- 2014年微软编程之美初赛第一场 第二题 树
- 微软2014编程之美初赛第一场——题目3 : 活动中心
- hihocoder 微软编程之美2015 初赛 第一场 (树算法 + 暴力思想 + 搜索思想)
- 微软2014编程之美初赛第一场——题目3 : 活动中心
- 2014微软编程之美初赛第一场第二题 树
- 微软2014编程之美初赛第一场——题目1 : 焦距
- 2014 微软 编程之美 初赛 第一场 题解
- 微软编程之美-初赛
- 竞价-2013编程之美初赛第一场第一题
- 编程之美初赛第一场 焦距
- HDU 4508 湫湫系列故事——减肥记I (2013腾讯编程马拉松初赛第一场)
- 编程之美初赛第一场 树
- 编程之美初赛第一场 题目3 : 活动中心
- 编程之美初赛第一场 树
- HDU 4508 湫湫系列故事——减肥记I (2013腾讯编程马拉松初赛第一场)
- 编程之美 2014 初赛第一场