QOJ.ac
QOJ
ID | 提交记录ID | 题目 | Hacker | Owner | 结果 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|
#654 | #301471 | #7895. Graph Partitioning 2 | chen_zhe_ | __jk__ | Failed. | 2024-06-09 00:12:51 | 2024-06-09 00:12:51 |
详细
Extra Test:
Accepted
time: 79ms
memory: 28516kb
input:
3 100000 300 99999 100000 99998 100000 99997 99999 99996 99999 99995 99998 99994 99998 99993 99997 99992 99997 99991 99996 99990 99996 99989 99995 99988 99995 99987 99994 99986 99994 99985 99993 99984 99993 99983 99992 99982 99992 99981 99991 99980 99991 99979 99990 99978 99990 99977 99989 99976 999...
output:
0 0 0
result:
ok 3 lines
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#301471 | #7895. Graph Partitioning 2 | __jk__ | AC ✓ | 2801ms | 723556kb | C++14 | 1.4kb | 2024-01-09 22:41:32 | 2024-01-09 22:41:32 |
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define double long double
#define x first
#define y second
#define pb push_back
using namespace std;
using pii=pair <int,int>;
using tii=pair <pii,int>;
using rii=pair <int,pii>;
using qii=pair <pii,pii>;
using namespace __gnu_pbds;
const int mod=998244353LL;
int n,k;
vector <int> g[100010];
unordered_map <int,int> dp[100010],tmp;
void dfs(int cur,int prv){
dp[cur][1]=1;
for (int i:g[cur]){
if (i==prv) continue;
dfs(i,cur);
tmp.clear();
for (auto j:dp[cur]){
for (auto l:dp[i]){
if (j.x+l.x<=k+1){
tmp[j.x+l.x]+=j.y*l.y;
tmp[j.x+l.x]%=mod;
}
}
}
dp[cur]=tmp;
}
if (dp[cur].find(k)!=dp[cur].end()) dp[cur][0]+=dp[cur][k];
if (dp[cur].find(k+1)!=dp[cur].end()) dp[cur][0]+=dp[cur][k+1];
if (dp[cur].find(0)!=dp[cur].end()) dp[cur][0]%=mod;
}
void solve(){
cin>>n>>k;
for (int i=1; i<=n; i++){
g[i].clear(); dp[i].clear();
}
for (int i=1; i<n; i++){
int u,v; cin>>u>>v;
g[u].pb(v); g[v].pb(u);
}
dfs(1,0);
cout<<dp[1][0]<<'\n';
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t=1;
cin>>t;
while (t--) solve();
}