QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#626399#7895. Graph Partitioning 2MiniLongWA 281ms798576kbC++175.0kb2024-10-10 07:56:122024-10-10 07:56:13

Judging History

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

  • [2024-10-10 07:56:13]
  • 评测
  • 测评结果:WA
  • 用时:281ms
  • 内存:798576kb
  • [2024-10-10 07:56:12]
  • 提交

answer

#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
    #ifdef ONLINE_JUDGE
    char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
    #define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
    #else
    #define get() getchar()
    #endif
    template<typename T> inline void read(T &t){
        T x = 0, f = 1;
        char c = getchar();
        while(!isdigit(c)){
            if(c == '-') f = -f;
            c = getchar();
        }
        while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
        t = x * f;
    }
    template<typename T, typename ... Args> inline void read(T &t, Args&... args){
        read(t);
        read(args...);
    }
    template<typename T> void write(T t){
        if(t < 0) putchar('-'), t = -t;
        if(t >= 10) write(t / 10);
        putchar(t % 10 + '0');
    }
    template<typename T, typename ... Args> void write(T t, Args... args){
        write(t), putchar(' '), write(args...);
    }
    template<typename T> void writeln(T t){
        write(t);
        puts("");
    }
    template<typename T> void writes(T t){
        write(t), putchar(' ');
    }
    #undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
    const ll mod = 998244353;
    ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
    void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
    void add(ll &x, ll y){x = (x + y) % mod;}
    void mul(ll &x, ll y){x = x * y % mod;}
    ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
    ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
    ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 1e5 + 5, B = 300, M = 505;
int n, k, siz[N];
vector<int> G[N];
namespace solution1{
    ll f[N][M], g[M];
    void dfs(int u, int fa){
        f[u][1] = 1, siz[u] = 1;
        for(auto &v : G[u]){
            if(v == fa) continue;
            dfs(v, u);
            _rep(i, 1, min(siz[u], k + 1)) _rep(j, 0, min(siz[v], k + 1 - i)){
                add(g[i + j], f[u][i] * f[v][j] % mod);
            }
            siz[u] += siz[v];
            _rep(i, 0, min(siz[u], k + 1)) f[u][i] = g[i], g[i] = 0;
        }
        add(f[u][0], f[u][k] + f[u][k + 1]), f[u][k + 1] = 0;
    }
    void solve(){
        dfs(1, 0);
        writeln(f[1][0]);
        _rep(i, 1, n) _rep(j, 0, k + 1) f[i][j] = 0; 
        _rep(i, 1, n) G[i].clear(), siz[i] = 0;
    }
}
namespace solution2{
    ll f[N][M][2], g[M][2];
    void dfs(int u, int fa){
        f[u][0][0] = 1, siz[u] = 1;
        for(auto &v : G[u]){
            if(v == fa) continue;
            dfs(v, u);
            _rep(i, 0, siz[u] / k) _rep(j, 0, siz[v] / k){
                _rep(p, 0, 1) _rep(q, 0, 1){
                    int x = (siz[u] - i * k) / (k + 1) - p, y = (siz[v] - j * k) / (k + 1) - q;
                    x = siz[u] - (k + 1) * x - i * k, y = siz[v] - (k + 1) * y - j * k;
                    if(x){
                        if(!p && !q && x + y == k + 1){
                            add(g[i + j][1], f[u][i][p] * f[v][j][q]);
                        }
                        if(p){
                            if(q || !y) add(g[i + j][1], f[u][i][p] * f[v][j][q]);
                        }
                        if(!p){
                            if(q) add(g[i + j][0], f[u][i][p] * f[v][j][q]);
                            if(!q && x + y <= k){
                                add(g[i + j][0], f[u][i][p] * f[v][j][q]);
                            }
                        }
                    }
                }
            }
            siz[u] += siz[v];
            _rep(i, 0, siz[u] / k) _rep(p, 0, 1) f[u][i][p] = g[i][p], g[i][p] = 0;
        }
        _rep(i, 0, siz[u] / k) if((siz[u] - i * k) % (k + 1) == k) add(f[u][i + 1][0], f[u][i][0]); 
    }
    void solve(){
        dfs(1, 0);
        ll ans = 0;
        _rep(i, 0, n){
            _rep(j, 0, 1){
                if(j || (n - i * k) % (k + 1) == 0) add(ans, f[1][i][j]);
            }
        }
        writeln(ans);
        _rep(i, 1, n) _rep(j, 0, n / k) _rep(p, 0, 1) f[i][j][p] = 0; 
        _rep(i, 1, n) G[i].clear(), siz[i] = 0;
    }
}
int main(){
    multitest(){
        read(n, k);
        _rep(i, 2, n){
            int u, v; read(u, v);
            G[u].pb(v), G[v].pb(u);
        }
        if(k <= B) solution1::solve();
        else{
            solution2::solve();
        }
    }    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9712kb

input:

2
8 2
1 2
3 1
4 6
3 5
2 4
8 5
5 7
4 3
1 2
1 3
2 4

output:

2
1

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 22ms
memory: 20424kb

input:

5550
13 4
10 3
9 1
10 8
3 11
8 5
10 7
9 6
13 5
9 7
2 7
5 12
4 8
8 2
4 1
3 4
7 8
2 5
6 7
4 8
2 3
11 1
11 10
1 4
9 10
8 4
3 6
5 7
6 1
10 2
11 7
11 1
17 2
14 16
13 15
17 3
15 11
1 6
13 2
13 17
4 8
14 10
8 14
14 5
9 12
14 2
12 17
17 6
15 7
14 6
2 14
2 13
2 4
8 4
3 11
7 3
14 1
11 9
13 3
5 10
6 8
3 10
14 ...

output:

0
3
112
0
1
0
1
0
0
0
1
0
1
0
0
1
0
140
0
0
0
814
1
6
1
1
2
2
0
612
0
1
0
0
0
1
1
0
0
121
4536
0
0
1718
0
0
1
0
444
1
1908
1813
3
74
0
1
0
46
0
0
0
0
0
0
0
0
0
1
0
1
1
1
239
0
0
0
1
0
0
0
1
0
1
0
0
1
1
0
0
0
1
0
0
0
48
0
2
0
0
0
1
364
0
206
0
0
76
0
1
0
0
2
0
1
2
0
0
1
0
0
4
0
1
1
0
0
1
1
1
0
0
1
1
...

result:

ok 5550 lines

Test #3:

score: 0
Accepted
time: 281ms
memory: 404928kb

input:

3
99990 259
23374 69108
82204 51691
8142 67119
48537 97966
51333 44408
33147 68485
21698 86824
15746 58746
78761 86975
58449 61819
69001 68714
25787 2257
25378 14067
64899 68906
29853 31359
75920 85420
76072 11728
63836 55505
43671 98920
77281 25176
40936 66517
61029 61440
66908 52300
92101 59742
69...

output:

259200
247
207766300

result:

ok 3 lines

Test #4:

score: -100
Wrong Answer
time: 267ms
memory: 798576kb

input:

3
99822 332
11587 83046
63424 60675
63423 73718
74622 40130
5110 26562
28361 80899
30886 70318
8708 11068
34855 96504
7904 75735
31904 42745
87892 55105
82374 81319
77407 82147
91475 12343
13470 95329
58766 95716
83232 44156
75907 92437
69785 93598
47857 33018
62668 31394
24238 72675
98254 43583
180...

output:

315881302
4505040
185631156

result:

wrong answer 1st lines differ - expected: '315881300', found: '315881302'