QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#620152#2443. Dense SubgraphWorldFinalEscaped#WA 326ms11324kbC++142.5kb2024-10-07 16:49:492024-10-07 16:50:06

Judging History

你现在查看的是最新测评结果

  • [2024-10-07 16:50:06]
  • 评测
  • 测评结果:WA
  • 用时:326ms
  • 内存:11324kb
  • [2024-10-07 16:49:49]
  • 提交

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;}

int dp[N][2][2];
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
    dp[u][0][0]=1;
    for(auto v:son){
        int ret=(1ll*dp[v][0][0]+dp[v][1][0]+dp[v][1][1])%mod;
        dp[u][0][0]=1ll*dp[u][0][0]*ret%mod;
    }
    // choose u
    int len=son.size();
    for(int st=0;st<1<<len;st++){
        int num=st?__builtin_popcount(st):0,type=num>0;
        // dp[u][1][type]
        if(type==0){
            dp[u][1][0]=1;
            for(int i=0;i<len;i++)
                dp[u][1][0]=1ll*dp[u][1][0]*dp[son[i]][0][0]%mod;
            continue;
        }
        static int s[5],p;
        p=0;
        int global_ways=1;
        for(int i=0;i<len;i++){
            if(st>>i&1){
                s[p++]=son[i];
            }else{
                global_ways=1ll*global_ways*dp[son[i]][0][0]%mod;
            }
        }
        for(int st2=0;st2<1<<p;st2++){
            static int tmp[5];
            for(int i=0;i<p;i++){
                if(st2>>i&1)tmp[i]=max(0,a[s[i]]);
                else tmp[i]=a[s[i]];
            }
            sort(tmp,tmp+p,greater<int>());
            int now=a[u]+tmp[0],ways=1,ok=1;
            if(now>0)continue;
            for(int i=1;i<p;i++){
                now+=tmp[i];
                if(now>0)ok=0;
            }
            if(ok){
                for(int i=0;i<p;i++){
                    if(st2>>i&1)ways=1ll*ways*dp[s[i]][1][1]%mod;
                    else ways=1ll*ways*dp[s[i]][1][0]%mod;
                }
                addx(dp[1][1][1],1ll*global_ways*ways%mod);
            }
        }
    }
}

void sc(){
    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=(1ll*dp[root][0][0]+dp[root][1][0]+dp[root][1][1])%mod;
    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: 0
Wrong Answer
time: 326ms
memory: 11324kb

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:

268
2592
13692320
94797734
292136229
890786284
346225695
867407123
395003025
366036457
833130304
539996150
881516818
532187860
430245992
779717995
650953496
463281392
297650501
217521288
816320105
983226635
652318430
711883737
664897674
399511648
86150680
933962909
312328876
968792718

result:

wrong answer 1st lines differ - expected: '320', found: '268'