Computer HDU - 2196 树形dp
A school bought the first computer some time ago(so this computer’s id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information.
Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
Input
Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.
Output
For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).
Sample Input
5
1 1
2 1
3 1
1 1
Sample Output
3
2
3
4
4
思路:二次扫描与换跟,对于这种以某一个点为跟,就能求出这个点的正确答案的题,基本都可以通过两次dfs从n*n优化到n,保存一下子树前二大,这题就出来了.
#include<stdio.h>
#include<cstring>
const int mod=1e9+7;
typedef long long ll;
long long dp[20][10][10],dp1[20][10][10],dp2[20][10][10];
int a[20];
ll p[20];
ll dfs(int pos,int fjia,int fhong,bool limit,ll &ss,ll &dd,ll &cc){
if(pos==-1){
dd=0; ss=0;
if(fjia!=0&&fhong!=0) cc=1;
else cc=0;
return dd;
}
if(!limit&&dp2[pos][fjia][fhong]!=-1){
ss=dp1[pos][fjia][fhong];
dd=dp2[pos][fjia][fhong];
cc=dp[pos][fjia][fhong];
return dp2[pos][fjia][fhong];
}
int up=limit?a[pos]:9;
ss=0,dd=0,cc=0;
ll s,d,c;
for(int i=0;i<=up;i++){
if(i==7) continue;
dfs(pos-1,(fjia+i)%7,(fhong*10+i)%7,limit&&up==i,s,d,c);
ll k=i*p[pos]%mod;
cc=(cc+c)%mod;
ss=(((c*k%mod)+s)%mod+ss)%mod;
dd=((c*k%mod*k%mod)%mod+d%mod+2*k*s%mod+dd%mod)%mod;
}
if(!limit){
dp1[pos][fjia][fhong]=ss;
dp[pos][fjia][fhong]=cc;
dp2[pos][fjia][fhong]=dd;
}
return dd;
}
ll solve(ll x){
int pos=0;
while(x){
a[pos++]=x%10;
x/=10;
}
ll s,d,c;
return dfs(pos-1,0,0,true,s,d,c);
}
int main(){
int t;
memset(dp2,-1,sizeof(dp2));
scanf("%d",&t);
p[0]=1;
for(int i=1;i<20;i++)
p[i]=p[i-1]*10%mod;
while(t--){
ll x,y;
scanf("%lld%lld",&x,&y);
printf("%lld\n",((solve(y)-solve(x-1))%mod+mod)%mod);
}
return 0;
}