QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#761414 | #7895. Graph Partitioning 2 | podys | WA | 88ms | 4788kb | C++23 | 4.8kb | 2024-11-18 22:41:39 | 2024-11-18 22:41:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld double
#define uint unsigned long long
#define endl '\n'
#define ll __int128
#define pii pair<int,int>
#define pa first
#define pb second
#define counts __builtin_popcountll
namespace OOO
{
char buf[1 << 20], *_now = buf, *_end = buf;
// #define getchar() (_now == _end && (_end = (_now = buf) + fread(buf, 1, 1 << 20, stdin), _now == _end) ? EOF : *_now++)
inline void fast_io()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
}
int fast_pow(int a,int b,int mod)
{
int ans=1;
a%=mod;
for(;b;b>>=1)
{
if(b&1)
{
ans=ans*a%mod;
}
a=a*a%mod;
}
return ans;
}
int get_int()
{
int x=0;
bool y=0;
char c=getchar();
for(;c<'0'||c>'9';c=getchar())
{
if(c=='-')
{
y=1;
}
}
for(;c>='0'&&c<='9';c=getchar())
{
x=(x<<3)+(x<<1)+c-'0';
}
return y?-x:x;
}
template<typename T> void out_int(T x)
{
if(x==0)
{
putchar('0');
return;
}
if(x<0)
{
putchar('-');
x=-x;
}
static int num[20],top;
top=0;
for(;x;x/=10)
{
num[top++]=x%10;
}
for(;top;)
{
putchar(num[--top]+'0');
}
}
int ni(int a,int mod)
{
return fast_pow(a,mod-2,mod);
}
int sgn(ld x)
{
static const ld eps=1e-12;
if(fabs(x)-eps<0)
{
return 0;
}
return x>0?1:-1;
}
}using namespace OOO;
const int mod=998244353;
int _(int x)
{
return (x%mod+mod)%mod;
}
int n,k;
vector<vector<int>> road;
void read()
{
cin>>n>>k;
road=vector<vector<int>>(n+1);
for(int w=1,a,b;w<n;w++)
{
cin>>a>>b;
road[a].push_back(b);
road[b].push_back(a);
}
}
namespace Solve_big
{
class node
{
public:
int size,k0_size,k1_size;
int fa;
vector<vector<int>> dp;
};
vector<node> th;
void dfs(int w,int fa)
{
th[w].fa=fa;
th[w].size=1;
th[w].k0_size=th[w].size/k;
th[w].k1_size=th[w].size/(k+1);
th[w].dp=vector<vector<int>>(th[w].k0_size+1,vector<int>(th[w].k1_size+1));
th[w].dp[0][0]=1;
for(auto to:road[w])
{
if(to==th[w].fa)
{
continue;
}
dfs(to,w);
th[w].size+=th[to].size;
th[w].k0_size+=th[to].k0_size;
th[w].k1_size+=th[to].k1_size;
vector<vector<int>> dp(th[w].k0_size+1,vector<int>(th[w].k1_size+1));
for(int s0=0;s0<=th[w].k0_size;s0++)
{
for(int ls0=max(s0-th[to].k0_size,0ll);ls0<=min(th[w].k0_size-th[to].k0_size,s0);ls0++)
{
for(int s1=0;s1<=th[w].k1_size;s1++)
{
for(int ls1=max(s1-th[to].k1_size,0ll);ls1<=min(th[w].k1_size-th[to].k1_size,s1);ls1++)
{
dp[s0][s1]=_(dp[s0][s1]+_(th[w].dp[ls0][ls1]*th[to].dp[s0-ls0][s1-ls1]));
}
}
}
}
th[w].dp=dp;
th[to].dp.clear();
}
vector<vector<int>> dp=vector<vector<int>>(th[w].size/k+1,vector<int>(th[w].size/(k+1)+1));
for(int s0=0;s0<=th[w].k0_size;s0++)
{
for(int s1=0;s1<=th[w].k1_size;s1++)
{
dp[s0][s1]=_(dp[s0][s1]+th[w].dp[s0][s1]);
int residue=th[w].size-s0*k-s1*(k+1);
if(residue>=k)
{
dp[s0+1][s1]=_(dp[s0+1][s1]+th[w].dp[s0][s1]);
}
if(residue>=k+1)
{
dp[s0][s1+1]=_(dp[s0][s1+1]+th[w].dp[s0][s1]);
}
}
}
th[w].dp=dp;
th[w].k0_size=th[w].size/k;
th[w].k1_size=th[w].size/(k+1);
}
int solve()
{
if(n%k>n/k)
{
return 0;
}
th=vector<node>(n+1);
dfs(1,0);
return th[1].dp[n/k-n%k][n%k];
}
}
namespace Solve_small
{
class node
{
public:
int size;
int fa;
vector<int> dp;
};
vector<node> th;
void dfs(int w,int fa)
{
th[w].fa=fa;
th[w].size=1;
th[w].dp=vector<int>(min(th[w].size,k+1)+1);
th[w].dp[1]=1;
for(auto to:road[w])
{
if(to==th[w].fa)
{
continue;
}
dfs(to,w);
th[w].size+=th[to].size;
vector<int> dp(min(th[w].size,k+1)+1);
for(int s=0;s<=min(th[w].size,k+1);s++)
{
for(int ls=max(s-min(th[to].size,k+1),0ll);ls<=min(th[w].size-min(th[to].size,k+1),s);ls++)
{
dp[s]=_(dp[s]+_(th[w].dp[ls]*th[to].dp[s-ls]));
}
}
th[w].dp=dp;
th[to].dp.clear();
}
th[w].dp[0]=_(th[w].dp[0]+(th[w].size>=k?th[w].dp[k]:0)+(th[w].size>=k+1?th[w].dp[k+1]:0));
}
int solve()
{
th=vector<node>(n+1);
dfs(1,0);
return th[1].dp[0];
}
}
signed main()
{
int t;
cin>>t;
for(;t--;)
{
read();
cout<<(k*k*k>n*n?Solve_big::solve():Solve_small::solve())<<endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3640kb
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: 88ms
memory: 4788kb
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:
93 154 304 0 3 0 1 100 6 2 410320023 3 1 0 2 1 0 633168181 0 993152682 2 1294 1 526777927 911976987 988059312 739451808 11 1 1339 0 1 3 1630063 0 1 1 0 3 376888484 156555526 0 4 852534330 0 0 720127394 6 15516 3 403639575 210748718 868614426 937111519 0 1 3 328118863 0 777349147 1 0 0 0 2 496575189 ...
result:
wrong answer 1st lines differ - expected: '0', found: '93'