QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#656#301471#7895. Graph Partitioning 2chen_zhe___jk__Failed.2024-06-09 00:13:422024-06-09 00:13:42

Details

Extra Test:

Accepted
time: 123ms
memory: 29508kb

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

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();
}