QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#621513 | #2443. Dense Subgraph | Cheek_support | AC ✓ | 201ms | 19144kb | C++14 | 4.1kb | 2024-10-08 15:04:14 | 2024-10-08 15:04:15 |
Judging History
answer
#pragma GCC optimizeA("Ofast")
#pragma GCC optimizeA("inline")
#pragma GCC optimizeA("unroll-loops")
#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define rep2(i,j,k) for(int i=j;i>=k;i--)
#define mod 1000000007
#define DEBUG_VAR(x) { cerr << "* "#x" = " << x << endl; }
using namespace std;
template<typename T> void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T> void chkmax(T &x,T y){x=x>y?x:y;}
template<typename T> void read(T &num){
char c=getchar();T f=1;num=0;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){num=(num<<3)+(num<<1)+(c^48);c=getchar();}
num*=f;
}
template<typename T> T inc(T x,T y){return (x+y>=mod)?(x+y-mod):(x+y);}
template<typename T> T dec(T x,T y){return (x-y<0)?(x-y+mod):(x-y);}
int mul(int x,int y){return (1ll*x*y)%mod;}
const int N=35000;
int a[N+10];
vector<int>ver[N+10];
bool vis[N+10];
int dp[N+10][1<<5];
int w[N+10][1<<5];
int F[1<<6],G[1<<6];
int Num(int x,int i){
return ((x>>(i-1))&1);
}
void dfs(int u,int fr){
vis[u]=1;
dp[u][0]=1;w[u][0]=0;
dp[u][1]=1;w[u][1]=a[u];
int cnt=1;
for(auto x:ver[u]){
if(vis[x])continue;
dfs(x,u);
if(!fr)continue;
cnt++;
int L=(int)ver[x].size();
rep(i,0,(1<<cnt)-1)F[i]=G[i]=0;
rep(i,0,(1<<(cnt-1))-1){
rep(j,0,(1<<L)-1){
if(!Num(i,cnt-1)){
F[i<<1|Num(j,L)]=inc(F[i<<1|Num(j,L)],mul(dp[u][i],dp[x][j]));
G[i<<1|Num(j,L)]=0;
}else if(!Num(j,L)){
F[i<<1]=inc(F[i<<1],mul(dp[u][i],dp[x][j]));
G[i<<1]=w[u][i];
}else{
if(w[u][i]+w[x][j]>0){
;
}else{
F[i<<1|1]=inc(F[i<<1|1],mul(dp[u][i],dp[x][j]));
G[i<<1|1]=w[u][i]+max(0,a[x]);
}
}
}
}
rep(i,0,(1<<cnt)-1){
dp[u][i]=F[i];
w[u][i]=G[i];
}
}
return;
}
int main()
{
// freopen("1.in", "r", stdin);
int T;
read(T);
while(T--){
int n,x;
read(n);read(x);
rep(i,1,n){
vis[i]=0;
ver[i].clear();
}
rep(i,1,n){
read(a[i]);
a[i]-=x;
}
rep(i,1,n-1){
int u,v;
read(u);read(v);
ver[u].push_back(v);
ver[v].push_back(u);
}
int ans=1;
rep(i,1,n){
if(vis[i])continue;
dfs(i,0);
dp[i][0]=1;w[i][0]=0;
dp[i][1]=1;w[i][1]=a[i];
int cnt=1;
for(auto x:ver[i]){
cnt++;
int L=(int)ver[x].size();
rep(j,0,(1<<cnt)-1)F[j]=G[j]=0;
rep(j,0,(1<<(cnt-1))-1){
rep(k,0,(1<<L)-1){
if(!Num(j,cnt-1)){
F[j<<1|Num(k,L)]=inc(F[j<<1|Num(k,L)],mul(dp[i][j],dp[x][k]));
G[j<<1|Num(k,L)]=0;
}else if(!Num(k,L)){
F[j<<1]=inc(F[j<<1],mul(dp[i][j],dp[x][k]));
G[j<<1]=w[i][j];
}else{
if(w[i][j]+w[x][k]>0){
;
}else{
F[j<<1|1]=inc(F[j<<1|1],mul(dp[i][j],dp[x][k]));
G[j<<1|1]=w[i][j]+max(0,a[x]);
}
}
}
}
if(x==ver[i].back())continue;
rep(j,0,(1<<cnt)-1){
dp[i][j]=F[j];
w[i][j]=G[j];
}
}
int sum=0;
rep(j,0,(1<<cnt)-1){
sum=inc(sum,F[j]);
}
ans=mul(ans,sum);
}
cout<<ans<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 201ms
memory: 19144kb
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