QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#657#301471#7895. Graph Partitioning 2chen_zhe___jk__Failed.2024-06-09 00:15:282024-06-09 00:15:28

Details

Extra Test:

Accepted
time: 144ms
memory: 29940kb

input:

3
100000 300
99999 100000
99998 100000
99997 100000
99996 100000
99995 100000
99994 100000
99993 100000
99992 100000
99991 100000
99990 100000
99989 100000
99988 100000
99987 100000
99986 100000
99985 100000
99984 100000
99983 100000
99982 100000
99981 100000
99980 100000
99979 100000
99978 100000
9...

output:

0
0
0

result:

ok 3 lines

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#301471#7895. Graph Partitioning 2__jk__AC ✓2801ms723556kbC++141.4kb2024-01-09 22:41:322024-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();
}