ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#204052 | #3576. 切断 | drdilyor | 100 | 575ms | 118760kb | C++11 | 1.1kb | 2024-04-06 10:43:24 | 2024-04-06 12:01:46 |
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
vector<int> e[5005];
int dp[5005][5005];
int sz[5005];
const int mod=1e9+7;
int tmp[5005];
//i 子树挖掉大小为 j 的连通块,剩下的所有方案乘起来总数.
bool vis[5005];
void dfs(int x,int f){
vis[x]=1;
int sp=0;
for(auto v:e[x]){
if(v==f)continue;
sp++;
}
if(!sp){
sz[x]=1;
dp[x][0]=1;
dp[x][1]=1;
return;
}
sz[x]=1;
dp[x][1]=1;
for(auto v:e[x]){
if(v==f)continue;
dfs(v,x);
for(int i=0;i<=sz[x]+sz[v];i++)tmp[i]=0;
for(int i=0;i<=sz[v];i++){
for(int j=1;j<=sz[x];j++){
tmp[i+j]+=dp[v][i]*dp[x][j]%mod;
tmp[i+j]%=mod;
}
}
for(int i=0;i<=sz[x]+sz[v];i++)dp[x][i]=tmp[i];
sz[x]+=sz[v];
}
for(int i=1;i<=sz[x];i++)dp[x][0]+=dp[x][i]*i%mod,dp[x][0]%=mod;
}
signed main(){
cin>>n;
if(n==1){cout<<1<<"\n";return 0;}
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
e[u].push_back(v),e[v].push_back(u);
}
dfs(1,0);
int res=0;
for(int i=1;i<=n;i++)res+=dp[1][i]*i%mod;
cout<<res%mod<<"\n";
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 0ms
memory: 1464kb
input:
20 15 5 5 6 10 19 14 13 8 18 10 9 14 16 7 1 2 3 15 4 10 11 20 12 10 2 16 8 6 10 5 7 17 14 18 15 12 17
output:
61370916
result:
ok single line: '61370916'
Test #2:
score: 10
Accepted
time: 1ms
memory: 1464kb
input:
20 11 10 11 12 11 5 17 16 20 2 11 17 10 1 10 7 11 9 17 6 10 3 20 4 20 18 11 19 11 20 11 13 11 8 11 1...
output:
18251776
result:
ok single line: '18251776'
Test #3:
score: 10
Accepted
time: 0ms
memory: 1460kb
input:
20 1 10 4 16 11 6 1 7 15 20 18 1 3 17 12 13 15 4 17 18 4 12 7 2 20 9 20 14 15 19 18 8 13 5 8 11 1 15
output:
55139484
result:
ok single line: '55139484'
Test #4:
score: 10
Accepted
time: 123ms
memory: 118760kb
input:
5000 3050 3051 4061 4062 4370 4371 4171 4172 903 904 2007 2008 2918 2919 3291 3292 830 831 4151 4152...
output:
271496360
result:
ok single line: '271496360'
Test #5:
score: 10
Accepted
time: 114ms
memory: 118760kb
input:
4999 3630 3631 1827 1828 3648 3649 1229 1230 1374 1375 1245 1246 792 793 444 445 4000 4001 2896 2897...
output:
518769292
result:
ok single line: '518769292'
Test #6:
score: 10
Accepted
time: 112ms
memory: 118616kb
input:
4998 974 975 3903 3904 1751 1752 265 266 4493 4494 3590 3591 4017 4018 1629 1630 1870 1871 1498 1499...
output:
284811509
result:
ok single line: '284811509'
Test #7:
score: 10
Accepted
time: 47ms
memory: 25112kb
input:
5000 2758 10 2328 2521 2538 463 2385 4796 3308 4909 4851 2080 4680 3286 4314 3357 3543 1142 298 96 4...
output:
547641490
result:
ok single line: '547641490'
Test #8:
score: 10
Accepted
time: 61ms
memory: 21872kb
input:
4999 4528 888 2264 3674 3044 2369 4622 1556 1835 4871 3616 2116 2455 491 4528 393 1012 4311 2085 460...
output:
482768283
result:
ok single line: '482768283'
Test #9:
score: 10
Accepted
time: 54ms
memory: 27748kb
input:
4998 142 4938 2708 1818 4075 184 1021 3860 2015 1843 4079 460 3224 1188 2590 3056 3277 1753 301 4183...
output:
259434992
result:
ok single line: '259434992'
Test #10:
score: 10
Accepted
time: 63ms
memory: 21800kb
input:
5000 1047 2361 1047 4410 2998 4165 1047 393 2762 3337 2998 959 2998 2232 1047 4613 1047 1528 2998 40...
output:
579455619
result:
ok single line: '579455619'
Extra Test:
score: 0
Extra Test Passed