QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#620576#2443. Dense SubgraphLavender_FieldAC ✓281ms14024kbC++204.2kb2024-10-07 19:22:272024-10-07 19:22:28

Judging History

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

  • [2024-10-07 19:22:28]
  • 评测
  • 测评结果:AC
  • 用时:281ms
  • 内存:14024kb
  • [2024-10-07 19:22:27]
  • 提交

answer

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i = a; i <= b; ++i)
#define pb push_back
using namespace std;
inline int rd() {
    int r = 0; bool w = false; char ch = getchar();
    while( ch < '0' || ch > '9' ) w = !(ch^45), ch = getchar();
    while( ch >= '0' && ch <= '9' ) r = (r<<1) + (r<<3) + (ch^48), ch = getchar();
    return w ? -r : r;
}

#define MAXN 35000
const int mod = 1e9+7;
const int qpow3[] = {1, 3, 9, 27, 81, 243, 729};
int n, x;
int a[MAXN+5];
bool minimal_status[MAXN+5][32];
vector<int> to[MAXN+5], son[MAXN+5]; int fa[MAXN+5];
vector<int> constraint[MAXN+5];
void dft( int u ) {
    for(auto v: to[u]) if( fa[u] != v ) {
        fa[v] = u;
        son[u].pb(v);
        dft(v);
    }
}
inline void add( int u , int v ) {
    to[u].pb(v);
}
int f[MAXN+5], g[MAXN+5], h[MAXN+5];
bool chk( int u , int typ , int sta ) {
    if( typ == 0 ) return true;
    int tmpsta = sta;
    for(int j = 0; j < son[u].size(); ++j) {
        if( tmpsta % 3 == 2 ) return false;
        tmpsta /= 3;
    }
    if( typ == 1 ) {
        for(auto st: constraint[u]) {
            bool flag = false;
            for(int j = 0; j < son[u].size(); ++j) {
                if( (st>>j & 1) == 1 && sta / qpow3[j] % 3 == 0 ) {
                    flag = true;
                    break;
                }
            }
            if( flag == false ) return false;
        }
    } else {
        bool typ2_topflag = false;
        for(auto st: constraint[u]) {
            if( (st >> son[u].size()) & 1 ) {
                bool flag = false;
                for(int j = 0; j < son[u].size(); ++j) {
                    if( (st >> j & 1) == 1 && sta / qpow3[j] % 3 == 0 ) {
                        flag = true;
                        break;
                    }
                }
                if( flag == false ) typ2_topflag = true;
            } else {
                bool flag = false;
                for(int j = 0; j < son[u].size(); ++j) {
                    if( (st >> j & 1) == 1 && sta / qpow3[j] % 3 == 0 ) {
                        flag = true;
                        break;
                    }
                }
                if( flag == false ) return false;
            }
        }
        if( !typ2_topflag ) return false;
    }
    return true;
}
void dfs( int u ) {
    if( son[u].size() == 0 ) {
        f[u] = g[u] = 1;
        return;
    }
    for(auto v: son[u]) dfs(v);
    for(int s = 0; s < qpow3[son[u].size()]; ++s) {
        int mul = 1;
        int tmps = s;
        for(int j = 0; j < son[u].size(); ++j) {
            int v = son[u][j];
            mul = 1ll * mul * (tmps%3 == 0 ? f[v] : (tmps%3 == 1 ? g[v] : h[v])) % mod;
            tmps /= 3;
        }
        if( chk(u,0,s) ) f[u] = (f[u] + mul) % mod;
        if( chk(u,1,s) ) g[u] = (g[u] + mul) % mod;
        if( chk(u,2,s) ) h[u] = (h[u] + mul) % mod;
    }
    // printf("%d: %d %d %d\n", u, f[u], g[u], h[u]);
}   
int popcount( int x ) {
    int ret = 0;
    while( x ) ret += (x&1), x >>= 1;
    return ret;
}
void solve() {
    n = rd(), x = rd();
    FOR(i,1,n) a[i] = rd() - x;
    FOR(i,1,n-1) {
        int u = rd(), v = rd();
        add(u,v), add(v,u);
    }
    dft(1);
    FOR(i,1,n) {
        for(int s = 1; s < 1 << to[i].size(); ++s) {
            if( minimal_status[i][s] ) continue;
            int sum = a[i];
            if( i != 1 && ((s>>(to[i].size()-1)) & 1) ) sum += a[fa[i]];
            for(int j = 0; j < son[i].size(); ++j) {
                if( s>>j & 1 ) sum += a[son[i][j]];
            }
            if( sum > 0 ) {
                for(int S = s; S < 1 << to[i].size(); S = (S + 1) | s) {
                    minimal_status[i][S] = true;
                }
                if( i == 1 || ((s >> son[i].size()) & 1) == 0 || popcount(s) != 1 )
                    constraint[i].pb(s);
            }
        }
    }
    dfs(1);
    printf("%d\n", (f[1] + g[1]) % mod);
    FOR(i,1,n) to[i].clear(), son[i].clear(), fa[i] = 0, constraint[i].clear(), memset(minimal_status[i], 0, sizeof(minimal_status[0]));
    FOR(i,1,n) f[i] = g[i] = h[i] = 0;
    return;
}

int main() {
    int T = rd(); while( T-- ) solve();
    return 0;
} 

详细

Test #1:

score: 100
Accepted
time: 281ms
memory: 14024kb

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