QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#300101#7895. Graph Partitioning 2defyers#WA 197ms13516kbC++172.5kb2024-01-07 17:51:542024-01-07 17:51:54

Judging History

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

  • [2024-01-07 17:51:54]
  • 评测
  • 测评结果:WA
  • 用时:197ms
  • 内存:13516kb
  • [2024-01-07 17:51:54]
  • 提交

answer

#include <bits/stdc++.h>
#define all(a) (a).begin(), (a).end()
#define sz(a) (int) (a).size()
#define forn(i, n) for(int i = 0; i < (n); i++)
#define int long long
using namespace std;

typedef long long ll;

const int N = 1e5 + 11;

#ifdef LOCAL
const int B = 10;
#else
const int B = 400;
#endif
const int MOD = 998244353;
int dp[N][B], sz[N];
vector<int> G[N], ch[N];

void dfs0(int v, int pa = -1){
    sz[v] = 1;
    for(auto u : G[v]){
        if(u == pa) continue;
        ch[v].push_back(u); dfs0(u, v);
        sz[v] += sz[u];
    }
}

int S, n, k;

int dp3[B];
void mrg(int s1, int s2, int* dp1, int* dp2){
    fill(dp3, dp3 + B, 0);
    int s = s1 + s2;
    int ps = s % (k + 2);
    int ps1 = s1 % (k + 2);
    int ps2 = s2 % (k + 2);
    for(int i = 0; i <= min(B - 1, s1); i++){
        for(int j = 0; j <= min(B - 1, s2); j++){
            int r1 = (ps1 + i) % (k + 2);
            int r2 = (ps2 + j) % (k + 2);
            int r = r1 + r2;
            if(r > k + 1) continue;
            dp3[(r - ps + (k + 2)) % (k + 2)] += dp1[i] * dp2[j] % MOD;
        }
    }
    for(int i = 0; i < B; i++) dp1[i] = dp3[i] % MOD;
}

int calc_pos(int v, int x){
    return ((x - sz[v] % (k + 2)) + (k + 2)) % (k + 2);
}

void chadd(int& x, int y){
    x += y; if(x >= MOD) x -= MOD;
}

void dfs(int v){
    dp[v][0] = 1; dp[v][1] = k == 1;
    int csz = 1;
    for(auto u : ch[v]){
        dfs(u);
        mrg(csz, sz[u], dp[v], dp[u]);
        csz += sz[u];
    }
    int p0 = calc_pos(v, 0);
    int p1 = calc_pos(v, k);
    int p2 = calc_pos(v, k + 1);
    // cout << p0 << ' ' << p1 << ' ' << p2 << '\n';
    if(p0 < B && p1 < B) chadd(dp[v][p0], dp[v][p1]);
    if(p0 < B && p2 < B) chadd(dp[v][p0], dp[v][p2]);
    // cout << "Node " << v << ": ";
    // for(int i = 0; i < B; i++){
    //     cout << dp[v][i] << ' ';
    // }
    // cout << '\n';
}

void solve(){
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        fill(dp[i], dp[i] + B, 0);
    }
    for(int i = 1; i <= n; i++) G[i].clear(), ch[i].clear();
    for(int i = 0; i < n - 1; i++){
        int a, b; cin >> a >> b;
        G[a].push_back(b); G[b].push_back(a);
    }
    S = min(B, n / B) + 5;
    dfs0(1);
    dfs(1);
    int pos = calc_pos(1, 0);
    if(pos > B){
        cout << 0 << '\n';
    }else{
        cout << dp[1][pos] << '\n';
    }
}

int32_t main(){
    cin.tie(0)->sync_with_stdio(false);
    int t; cin >> t;
    while(t--){
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: -100
Wrong Answer
time: 197ms
memory: 13516kb

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
0
0
1
0
1
0
0
0
1
0
1
0
0
1
0
0
0
0
0
0
1
6
1
1
2
2
0
0
0
1
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
0
0
1
16
0
3
0
0
1
0
2
0
0
0
0
0
0
0
0
0
1
0
1
1
1
0
0
0
0
1
0
0
0
1
0
1
0
0
1
1
0
0
0
1
0
0
0
10
0
2
0
0
0
1
0
0
0
0
0
0
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
1
1
1
0
0
0
1
0
0
0
1
1
0
1
1
0
...

result:

wrong answer 3rd lines differ - expected: '112', found: '0'