QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#620110#2443. Dense SubgraphAnotherDayofSun#AC ✓370ms16816kbC++202.8kb2024-10-07 16:40:422024-10-07 16:41:02

Judging History

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

  • [2024-10-07 16:41:02]
  • 评测
  • 测评结果:AC
  • 用时:370ms
  • 内存:16816kb
  • [2024-10-07 16:40:42]
  • 提交

answer

#include<bits/stdc++.h>
#define For(i,j,k) for(int i=(j);i<=(k);i++)
#define foR(i,j,k) for(int i=(j);i>=(k);i--)

#define vi vector<int>
#define pb push_back
#define pii pair<int,int>
#define mkp make_pair
#define SZ(x) ((int)x.size())
// #define int long long
using namespace std;
const int P=1e9+7; 
int add(int x,int y) {return (x+y)%P; }
int sub(int x,int y) {return (x-y+P)%P; }
int mul(int x,int y) {return 1ll*x*y%P; }
void addmod(int &x,int y) {x=add(x,y); }
int read() {int x; scanf("%d",&x); return x; }
const int MN=40005+5; 
int n,pr,a[MN],fl[MN][2][16]; 
vi e[MN]; 
void dfs1(int u,int fa) {
    for(auto v:e[u]) {
        if(v==fa) continue; 
        dfs1(v,u); 
    }
    vi g; 
    for(auto v:e[u]) if(v!=fa) g.pb(v); 
    e[u]=g; int l=SZ(e[u]); 
    For(st,0,(1<<l)-1) {
        int sum=a[u]; 
        For(i,0,l-1) {
            if(st>>i&1) sum+=a[e[u][i]]; 
        }
        if(sum+a[fa]<=0) fl[u][1][st]=1; 
        if(sum<=0||st==0) fl[u][0][st]=1; 
    }
    For(st,0,(1<<l)-1) {
        if(!fl[u][0][st]) {
            For(i,0,l-1) fl[u][0][st|(1<<i)]=fl[u][1][st|(1<<i)]=0; 
        }
        if(!fl[u][1][st]) {
            For(i,0,l-1) fl[u][1][st|(1<<i)]=0; 
        }
    }
}
int f[MN][3]; // 0 : none, 1 : self, 2 : self + fa  
void dfs2(int u) {
    for(auto v:e[u]) {
        dfs2(v); 
    }
    int c[16],d[16]; memset(c,0,sizeof(c)); c[0]=1; 
    int noc=1; 
    int l=SZ(e[u]); 
    For(i,0,l-1) {
        int v=e[u][i]; 
        noc=mul(noc,add(f[v][0],f[v][1])); 
        memcpy(d,c,sizeof(d)); 
        memset(c,0,sizeof(c));
        For(st,0,(1<<l)-1) {
            if(!(st>>i&1)) {
                int nst=st^(1<<i); 
                c[nst]=add(c[nst],mul(d[st],f[v][2])); 
            }
            c[st]=add(c[st],mul(d[st],f[v][0])); 
            
        }
    }
    f[u][0]=noc; 
    For(st,0,(1<<l)-1) {
        if(fl[u][1][st]) f[u][2]=add(f[u][2],c[st]); 
        if(fl[u][0][st]) f[u][1]=add(f[u][1],c[st]); 
    }
}
void works() {
    n=read(),pr=read();
    For(i,1,n) e[i].clear(); 
    For(i,1,n) memset(fl[i],0,sizeof(fl[i])); 
    For(i,1,n) f[i][0]=f[i][1]=f[i][2]=0; 
    For(i,1,n) a[i]=read()-pr; 
    For(i,1,n-1) {
        int u=read(),v=read(); 
        e[u].pb(v),e[v].pb(u); 
    }
    int rt=0; 
    For(i,1,n) if(SZ(e[i])==1) rt=i; 
    dfs1(rt,0); dfs2(rt); 
    // cerr<<rt<<endl;
    // For(u,1,n) cerr<<u<<' '<<f[u][0]<<' '<<f[u][1]<<' '<<f[u][2]<<endl;
    int u=e[rt][0]; 
    int ans=add(mul(f[u][0],2),add(f[u][1],f[u][2]));
    cout<<ans<<endl; 

    
}
signed main() {
    #ifdef wasa855
        freopen("a.in","r",stdin); 
        freopen("a.out","w",stdout); 
    #endif
    int T=read();
    while(T--) {
        works(); 
    }
    return 0; 
}

详细

Test #1:

score: 100
Accepted
time: 370ms
memory: 16816kb

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