Advertisement
Guest User

Untitled

a guest
May 18th, 2014
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. //#include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <set>
  5. #include <map>
  6. #include <cstring>
  7. #include <string>
  8. #include <cmath>
  9. #include <cassert>
  10. #include <ctime>
  11. #include <algorithm>
  12. #include <sstream>
  13. #include <list>
  14. #include <queue>
  15. #include <deque>
  16. #include <stack>
  17. #include <cstdlib>
  18. #include <cstdio>
  19. #include <iterator>
  20. #include <functional>
  21. #include <bitset>
  22. #define mp make_pair
  23. #define pb push_back
  24.  
  25. #define ws ws_i_love_std
  26.  
  27. #ifdef LOCAL
  28. #define eprintf(...) fprintf(stderr,__VA_ARGS__)
  29. #else
  30. #define eprintf(...)
  31. #endif
  32.  
  33. #define TIMESTAMP(x) eprintf("["#x"] Time : %.3lf s.\n", clock()*1.0/CLOCKS_PER_SEC)
  34. #define TIMESTAMPf(x,...) eprintf("[" x "] Time : %.3lf s.\n", __VA_ARGS__, clock()*1.0/CLOCKS_PER_SEC)
  35.  
  36. #if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
  37.     #define LLD "%I64d"
  38. #else
  39.     #define LLD "%lld"
  40. #endif
  41.  
  42. using namespace std;
  43.  
  44. #define TASKNAME "E"
  45.  
  46. #ifdef LOCAL
  47. static struct __timestamper {
  48.     string what;
  49.     __timestamper(const char* what) : what(what){};
  50.     __timestamper(const string& what) : what(what){};
  51.     ~__timestamper(){
  52.         TIMESTAMPf("%s", what.data());
  53.     }
  54. } __TIMESTAMPER("end");
  55. #else
  56. struct __timestamper {};
  57. #endif
  58.  
  59. typedef long long ll;
  60. typedef long double ld;
  61.  
  62. const int MAXN = 5e5;
  63.  
  64. vector<int> g[MAXN];
  65. vector<int> l[MAXN];
  66. vector<int> r[MAXN];
  67. int sz[MAXN];
  68. int n,m;
  69.  
  70. struct item {
  71.     int w, l, r;
  72.     item(int w, int l, int r):w(w), l(l), r(r){};
  73. };
  74.  
  75. vector<item> ws;
  76.  
  77.  
  78. int dfs(int v, int p){
  79.     sz[v] = 1;
  80.     for (int i = 0; i < (int)g[v].size(); i++) {
  81.         int to = g[v][i];
  82.         if (to == p) continue;
  83.         int tmp = dfs(to, v);
  84.         ws.pb(item(min(m + 1LL , tmp * 2LL * (n - tmp)), l[v][i], r[v][i]));
  85.         sz[v] += tmp;
  86.     }
  87.     return sz[v];
  88. }
  89.  
  90. const int MOD = 1000000007;
  91.  
  92. void solve(){
  93.     for (int i = 0; i < n; i++)
  94.         g[i].clear(), l[i].clear(), r[i].clear();
  95.     ws.clear();
  96.     for (int i = 0; i < n - 1; i++) {
  97.         int a, b, l, r;
  98.         scanf("%d %d %d %d",&a,&b,&l,&r);
  99.         --a, --b;
  100.         g[a].pb(b), ::l[a].pb(l), ::r[a].pb(r);
  101.         g[b].pb(a), ::l[b].pb(l), ::r[b].pb(r);
  102.     }
  103.  
  104.     dfs(0, -1);
  105.     int tot = 0;
  106.     for (int i = 0; i < (int)ws.size(); i++) {
  107.         tot = min(m + 1LL, tot + ws[i].w * 1LL * ws[i].l);
  108. //          eprintf("%d %d %d\n", ws[i].w, ws[i].l, ws[i].r);
  109.     }
  110.     eprintf("%d\n", tot);
  111.     if (tot > m) {
  112.         printf("0\n");
  113.         return;
  114.     }
  115.  
  116. //      eprintf("\n\n");
  117.  
  118.     vector<int> dp(m+1);
  119.     dp[0] = 1;
  120.     int minv, maxv;
  121.     minv = maxv = 0;
  122.     for (int i = 0; i < (int)ws.size(); i++) {
  123.         maxv = min(m * 1LL, maxv + ws[i].w * 1LL* ws[i].r);
  124.         vector<int> sums(m+1);
  125.         for (int j = minv; j <= maxv; j++){
  126.             int lf = max(-1LL, j - (ws[i].r + 1) * 1LL * ws[i].w);
  127.             int rg = max(-1LL, j - ws[i].l * 1LL * ws[i].w);
  128.             sums[j] = (j < ws[i].w ? 0 : sums[j - ws[i].w]) + dp[j];
  129.             if (sums[j] >= MOD) sums[j] -= MOD;
  130.             dp[j] = (rg == -1 ? 0 : sums[rg]) - (lf == -1 ? 0 : sums[lf]);
  131.             if (dp[j] < 0) dp[j] += MOD;
  132.         }
  133.         minv += ws[i].w * ws[i].l;
  134.     }
  135.     printf("%d", dp[m]);
  136.  
  137. }
  138.  
  139.  
  140. int main(){
  141.   #ifdef LOCAL
  142.     assert(freopen(TASKNAME".in","r",stdin));
  143.     assert(freopen(TASKNAME".out","w",stdout));
  144.   #endif
  145.  
  146.     while (scanf("%d %d",&n,&m) == 2)
  147.         solve();
  148.      
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement