Advertisement
FHVirus

TIOJ 1647 to debug

May 26th, 2021
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. // Knapsack DP is harder than FFT.
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
  5. #define ff first
  6. #define ss second
  7. #define pb emplace_back
  8. #define AI(x) begin(x),end(x)
  9. template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
  10. template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;}
  11. #ifdef OWO
  12. #define debug(args...) SDF(#args, args)
  13. #define OIU(args...) ostream& operator<<(ostream&O,args)
  14. #define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
  15. LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
  16. template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
  17. template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
  18. #else
  19. #pragma GCC optimize("Ofast")
  20. #define debug(...) ((void)0)
  21. #endif
  22.  
  23. const int N = 200002;
  24.  
  25. int n;
  26. ll k, ans;
  27. vector<pll> adj[N];
  28.  
  29. bool vis[N];
  30.  
  31. int siz[N];
  32. void getSize(int u, int p){
  33.     siz[u] = 1;
  34.     for(auto [v, _]: adj[u]){
  35.         if(v == p or vis[v]) continue;
  36.         getSize(v, u);
  37.         siz[u] += siz[v];
  38.     }
  39. }
  40. int getCen(int u, int p, int s){
  41.     for(auto [v, _]: adj[u]){
  42.         if(v == p or vis[v]) continue;
  43.         if(siz[v] * 2 > s) return getCen(v, u, s);
  44.     }
  45.     return u;
  46. }
  47.  
  48. vector<ll> tmp;
  49. ll sum[N], pmx[N];
  50. void treepre(int u, int p){
  51.     for(auto [v, w]: adj[u]){
  52.         if(v == p or vis[v]) continue;
  53.         sum[v] = sum[u] + w;
  54.         pmx[v] = max(pmx[u], sum[v]);
  55.         treepre(v, u);
  56.     }
  57. }
  58. void dfs(int u, int p){
  59.     tmp.pb(pmx[u]);
  60.     for(auto [v, w]: adj[u]){
  61.         if(v == p or vis[v]) continue;
  62.         dfs(v, u);
  63.     }
  64. }
  65. ll getAns(int u){
  66.     tmp.clear();
  67.     dfs(u, 0);
  68.     sort(AI(tmp));
  69.     ll ret = 0;
  70.     debug(u, tmp);
  71.     for(int i = 0; i < tmp.size(); ++i){
  72.         int j = lower_bound(begin(tmp) + i + 1, end(tmp), k - tmp[i]) - begin(tmp);
  73.         ret += tmp.size() - j;
  74.     }
  75.     debug(u, ret);
  76.     return ret;
  77. }
  78.  
  79. void cenDecomp(int u, int p){
  80.     getSize(u, p);
  81.  
  82.     int cen = getCen(u, p, siz[u]);
  83.     debug(cen);
  84.     sum[cen] = pmx[cen] = 0;
  85.     treepre(cen, 0);
  86.     ans += getAns(cen);
  87.  
  88.     vis[cen] = true;
  89.     for(auto [v, w]: adj[cen]) if(!vis[v]) ans -= getAns(v);
  90.     for(auto [v, _]: adj[cen]) if(!vis[v]) cenDecomp(v, cen);
  91. }
  92.  
  93. signed main(){
  94.     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  95.     cin >> n >> k;
  96.     for(int i = 1, u, v, c; i < n; ++i){
  97.         cin >> u >> v >> c;
  98.         adj[u].pb(v, c);
  99.         adj[v].pb(u, c);
  100.     }
  101.  
  102.     cenDecomp(1, 0);
  103.  
  104.     cout << ans;
  105.     return 0;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement