日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces 486D D. Valid Sets

發布時間:2025/5/22 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces 486D D. Valid Sets 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://codeforces.com/contest/486/problem/D

題意:給定一棵樹,點上有權值,以及d,要求有多少種聯通塊滿足最大值減最小值小于等于d。

思路:枚舉i作為最大的點權,然后dfs樹規一下,就能得出以這個點為最大值的方案數,因為有權值相等的點,所以我們規定一下,只能從標號小的拓展到標號大的,就不會重復了。

1 #include<algorithm> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<iostream> 6 #define ll long long 7 const ll Mod=1000000007; 8 int tot,go[200005],first[200005],next[200005],a[200005],d,n; 9 ll f[200005]; 10 int read(){ 11 int t=0,f=1;char ch=getchar(); 12 while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} 13 while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();} 14 return t*f; 15 } 16 void insert(int x,int y){ 17 tot++; 18 go[tot]=y; 19 next[tot]=first[x]; 20 first[x]=tot; 21 } 22 void add(int x,int y){ 23 insert(x,y);insert(y,x); 24 } 25 void dfs(int x,int fa,int fi){ 26 f[x]=1; 27 for (int i=first[x];i;i=next[i]){ 28 int pur=go[i]; 29 if (pur==fa) continue; 30 if (a[pur]>a[fi]) continue; 31 if (a[fi]-d>a[pur]) continue; 32 if (a[fi]==a[pur]&&fi>pur) continue; 33 dfs(pur,x,fi); 34 f[x]*=(1LL+f[pur]); 35 f[x]%=Mod; 36 } 37 } 38 int main(){ 39 d=read();n=read(); 40 for (int i=1;i<=n;i++) 41 a[i]=read(); 42 for (int i=1;i<n;i++){ 43 int x=read(),y=read(); 44 add(x,y); 45 } 46 ll ans=0; 47 for (int i=1;i<=n;i++){ 48 for (int j=1;j<=n;j++) f[j]=0; 49 dfs(i,0,i); 50 ans=(ans+f[i])%Mod; 51 } 52 printf("%I64d\n",ans); 53 }

?

轉載于:https://www.cnblogs.com/qzqzgfy/p/5608635.html

總結

以上是生活随笔為你收集整理的Codeforces 486D D. Valid Sets的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。