QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#305214 | #7895. Graph Partitioning 2 | nvujica | WA | 15ms | 8556kb | C++14 | 2.3kb | 2024-01-14 21:38:05 | 2024-01-14 21:38:05 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
const int maxn = 1e5 + 10, mod = 998244353;
int t, n, k;
vector <int> v[maxn];
vector <int> dp[maxn];
vector <int> dp2;
int siz[maxn];
int add(int a, int b){
return (a + b) % mod;
}
int mul(int a, int b){
return ((ll)a * b) % mod;
}
void rek(int x, int p){
// cout << x << endl;
siz[x] = 1;
dp[x].push_back(1);
int cnt = 0, cnt2 = 0;
for(int l = 0; l < v[x].size(); l++){
int x2 = v[x][l];
if(x2 == p) continue;
rek(x2, x);
while(dp2.size() < k + 1 && dp2.size() < dp[x].size() + dp[x2].size() - 1) dp2.push_back(0);
for(int i = 0; i < dp[x].size(); i++){
for(int j = 0; j < dp[x2].size(); j++){
if((siz[x2] + j) % (k + 1) == 0) cnt2 = add(cnt2, mul(cnt, dp[x2][j]));
int sum = (siz[x] + i) % (k + 1) + (siz[x2] + j) % (k + 1);
if(sum > k + 1) continue;
if(sum == k + 1){
cnt2 = add(cnt2, mul(dp[x][i], dp[x2][j]));
continue;
}
// if(!i && !j) cout << x << ' ' << x2 << ' ' << dp[x][0] << ' ' << dp[x2][0] << endl;
dp2[(i + j) % (k + 1)] = add(dp2[(i + j) % (k + 1)], mul(dp[x][i], dp[x2][j]));
}
}
dp[x] = dp2;
cnt = cnt2;
dp2.clear();
siz[x] += siz[x2];
}
// cout << x << ":\n";
// for(int i = 0; i < dp[x].size(); i++){
// cout << (siz[x] + i) % (k + 1) << ' ' << dp[x][i] << endl;
// }
if(siz[x] % (k + 1) + dp[x].size() > k){
if(siz[x] % (k + 1) + dp[x].size() == k + 1) dp[x].push_back(0);
dp[x][(k + 1 - siz[x] % (k + 1)) % (k + 1)] = add(dp[x][(k + 1 - siz[x] % (k + 1)) % (k + 1)], dp[x][k - siz[x] % (k + 1)]);
}
if(siz[x] % (k + 1) + dp[x].size() > k + 1 || siz[x] % (k + 1) == 0){
dp[x][(k + 1 - siz[x] % (k + 1)) % (k + 1)] = add(dp[x][(k + 1 - siz[x] % (k + 1)) % (k + 1)], cnt);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while(t--){
cin >> n >> k;
for(int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
rek(1, 0);
cout << dp[1][(k + 1 - n % (k + 1)) % (k + 1)] << "\n";
for(int i = 1; i <= n; i++){
v[i].clear();
dp[i].clear();
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 8308kb
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: 15ms
memory: 8556kb
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 407 2 1 55 2 2 0 0 6 0 1 0 6 1 0 786 0 0 0 13429 1 7 6 1 10 2 0 4613 0 1 1 0 0 1 1 0 0 873 189747 3 3 80490 0 0 1 0 4464 1 21772 133956 3 1455 0 1 0 138 0 0 0 0 0 6 0 0 0 1 0 1 1 1 1393 0 0 0 1 0 0 1 3 0 1 1 0 1 1 1 1 2 2 0 0 0 79 0 4 0 1 0 1 8175 1 8052 0 0 471 0 1 4 0 6 0 1 2 0 0 1 0 1 12 0 1 ...
result:
wrong answer 3rd lines differ - expected: '112', found: '407'