QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#620267 | #2443. Dense Subgraph | WorldFinalEscaped# | AC ✓ | 335ms | 16472kb | C++14 | 2.4kb | 2024-10-07 17:15:13 | 2024-10-07 17:15:32 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
const int N=35005;
const int mod=1e9+7;
vector<int> adj[N];
int deg[N];
int a[N],n,X;
inline void addx(int &x,int y){if((x+=y)>=mod)x-=mod;}
// f[u]: not choose u
// g[u][st]: choose u, state = st
int f[N],g[N][16];
int good[N][16],coef[N];
void dfs(int u,int fa){
vector<int> son;
for(auto v:adj[u]){
if(v==fa)continue;
dfs(v,u);
son.push_back(v);
}
// not choose u
f[u]=1;
for(auto v:son){
int ret=f[v];
for(int j=0;j<16;j++)ret=(ret+g[v][j])%mod;
f[u]=1ll*f[u]*ret%mod;
for(int j=0;j<16;j++){
if(a[u]+good[v][j]<=0)
addx(coef[v],g[v][j]);
}
}
// choose u
int len=son.size();
for(int st=0;st<1<<len;st++){
int num=st?__builtin_popcount(st):0;
static int s[5];
int p=0,global_ways=1;
static int ttmp[5];
int _=a[u];
for(int i=0;i<len;i++){
if(st>>i&1){
s[p]=son[i];
ttmp[p]=a[son[i]];
p++;
_+=max(0,a[son[i]]);
}else{
global_ways=1ll*global_ways*f[son[i]]%mod;
}
}
good[u][st]=_;
sort(ttmp,ttmp+p,greater<int>());
bool global_ok=1;
int global_cur=a[u];
for(int i=0;i<p;i++){
global_cur+=ttmp[i];
if(global_cur>0)global_ok=0;
}
if(!global_ok)continue;
int ways=1;
for(int i=0;i<p;i++)
ways=1ll*ways*coef[s[i]]%mod;
addx(g[u][st],1ll*ways*global_ways%mod);
}
}
void sc(){
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
memset(good,0,sizeof(good));
memset(coef,0,sizeof(coef));
scanf("%d%d",&n,&X);
for(int i=1;i<=n;i++)adj[i].clear(),deg[i]=0;
for(int i=1;i<=n;i++)scanf("%d",&a[i]),a[i]-=X;
for(int i=1;i<n;i++){
int u,v;scanf("%d%d",&u,&v);
adj[u].push_back(v),adj[v].push_back(u);
deg[u]++,deg[v]++;
}
int root=0;
for(int i=1;i<=n;i++)
if(deg[i]==1)root=i;
dfs(root,0);
int ans=f[root];
for(int st=0;st<16;st++)addx(ans,g[root][st]);
printf("%d\n",ans);
}
int main(){
int T;scanf("%d",&T);
while(T--)sc();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 335ms
memory: 16472kb
input:
30 10 11086 10189 24947 2265 9138 27104 12453 15173 3048 30054 2382 8 1 1 4 5 10 10 4 3 5 2 10 9 7 6 10 7 1 15 9664 4127 24649 13571 8586 34629 8644 3157 33133 3713 32646 29412 8108 13583 21362 23735 14 9 7 1 15 12 10 15 2 6 3 11 9 1 1 11 6 12 4 10 13 15 8 15 12 11 5 3 20 29310 21738 9421 8412 4617 ...
output:
320 3312 1048576 60461799 663660496 831386303 528321417 945912389 820338255 58962594 643816787 354510769 532631871 280169661 533022884 475656636 892230988 381315031 40006857 376652471 288541404 546717513 976236489 615823216 419541488 840899550 546169620 634600456 825179565 169850560
result:
ok 30 lines