HDU 4729 An Easy Problem for Elfness(树上主席树+LCA+二分)
2016-08-23 21:14
477 查看
题目大意:给你一棵树,每条边有一个容量。然后m个询问,每个询问是互相独立的,给你两个点S, T,一个预算K,
建一条容量为1的新边的费用A(可以建在任一两个节点之间,包括S,T),将某一条现有的边容量扩大1的费用B。
问从S到T在预算允许的情况下最大流是多少。
这个分两种情况来讨论最优解:
1.如果A≤B,显然新建不会比扩展差,可以建立kA这么多条边。
2.如果A>B,这是有两种情况可能最优;第一种是新建然后对新建的边进行扩展;第二种则是对原路径上的边进行扩展;最后取两者最优即可。
这里第二种最多可以扩展的次数是kB.
那么我们就二分答案and,如果路径长度l∗mid−∑ai>kB,显然只能是ans<mid,
否则就是ans≥mid,∑ai就是用主席树+LCA的事儿了。
建一条容量为1的新边的费用A(可以建在任一两个节点之间,包括S,T),将某一条现有的边容量扩大1的费用B。
问从S到T在预算允许的情况下最大流是多少。
这个分两种情况来讨论最优解:
1.如果A≤B,显然新建不会比扩展差,可以建立kA这么多条边。
2.如果A>B,这是有两种情况可能最优;第一种是新建然后对新建的边进行扩展;第二种则是对原路径上的边进行扩展;最后取两者最优即可。
这里第二种最多可以扩展的次数是kB.
那么我们就二分答案and,如果路径长度l∗mid−∑ai>kB,显然只能是ans<mid,
否则就是ans≥mid,∑ai就是用主席树+LCA的事儿了。
/***************************************** Author :Crazy_AC(JamesQi) Time :2016 File Name : *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <climits> using namespace std; #define MEM(x,y) memset(x, y,sizeof x) #define pk push_back #define lson rt << 1 #define rson rt << 1 | 1 #define bug cout << "BUG HERE\n" #define debug(x) cout << #x << " = " << x << endl #define ALL(v) (v).begin(), (v).end() #define lowbit(x) ((x)&(-x)) #define Unique(x) sort(ALL(x)); (x).resize(unique(ALL(x)) - (x).begin()) #define BitOne(x) __builtin_popcount(x) #define showtime printf("time = %.15f\n",clock() / (double)CLOCKS_PER_SEC) #define Rep(i, l, r) for (int i = l;i <= r;++i) #define Rrep(i, r, l) for (int i = r;i >= l;--i) typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> ii; typedef pair<ii,int> iii; const double eps = 1e-8; const double pi = 4 * atan(1); const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int nCase = 0; //?????0??? int dcmp(double x){if (fabs(x) < eps) return 0;return x < 0?-1:1;} template<class T> inline bool read(T &n){ T x = 0, tmp = 1; char c = getchar(); while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar(); if(c == EOF) return false; if(c == '-') c = getchar(), tmp = -1; while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar(); n = x*tmp; return true; } template <class T> inline void write(T n){ if(n < 0){putchar('-');n = -n;} int len = 0,data[20]; while(n){data[len++] = n%10;n /= 10;} if(!len) data[len++] = 0; while(len--) putchar(data[len]+48); } LL QMOD(LL x, LL k) { LL res = 1LL; while(k) {if (k & 1) res = res * x % MOD;k >>= 1;x = x * x % MOD;} return res; } int n, m; const int maxn = 1e5 + 123; int head[maxn], nxt[maxn*2], pnt[maxn*2], cap[maxn*2], ecnt; inline void addedge(int u,int v,int c) { pnt[ecnt] = v, cap[ecnt] = c, nxt[ecnt] = head[u], head[u] = ecnt++; pnt[ecnt] = u, cap[ecnt] = c, nxt[ecnt] = head[v], head[v] = ecnt++; } struct Query { int s, t, k, a, b; void read() { scanf("%d%d%d%d%d", &s, &t, &k, &a, &b); } }Q[maxn]; struct _LCA { const static int LOGN = 20; int dep[maxn], fa[LOGN][maxn]; void dfs(int u,int f, int depth) {/*u = 1, f = -1, depth = 0*/ fa[0][u] = f, dep[u] = depth; for (int i = head[u];~i;i = nxt[i]) { int v = pnt[i]; if (v == f) continue; dfs(v, u, depth + 1); } } void build(int n) { for (int k = 0;k < LOGN;++k) { for (int u = 1;u <= n;++u) { if (fa[k][u] == -1) fa[k + 1][u] = -1; else fa[k + 1][u] = fa[k][fa[k][u]]; } } } int upslop(int u,int p) { for (int k = 0;k < LOGN;++k) if ((p>>k) & 1) u = fa[k][u]; return u; } int LCA(int u,int v) { if (dep[u] < dep[v]) swap(u, v); u = upslop(u, dep[u] - dep[v]); if (u == v) return u; for (int k = LOGN - 1;k >= 0;--k) if (fa[k][u] != fa[k][v]) {u = fa[k][u], v = fa[k][v];} return fa[0][u]; } }lca; int root[maxn], ls[maxn*30], rs[maxn*30], tot; LL sum[maxn*30], _cnt[maxn*20]; void build(int &rt, int l, int r) { rt = ++tot; sum[rt] = 0; _cnt[rt] = 0; if (l == r) return ; int mid = (l + r) >> 1; build(ls[rt], l, mid); build(rs[rt], mid + 1, r); } void updata(int last,int &rt, int l, int r,int pos, int val) { rt = ++tot; ls[rt] = ls[last], rs[rt] = rs[last]; sum[rt] = sum[last] + 1, _cnt[rt] = _cnt[last] + val; if (l == r) return ; int mid = (l + r) >> 1; if (pos <= mid) updata(ls[last], ls[rt], l, mid, pos, val); else updata(rs[last], rs[rt], mid + 1, r, pos, val); } void dfs_build(int u,int f, int v) { updata(root[f], root[u], 0, 10000, v, v); for (int i = head[u];~i;i = nxt[i]) { if (pnt[i] == f) continue; dfs_build(pnt[i], u, cap[i]); } } int Query_Kth (int x, int y, int lca, int l, int r, int k) { while(l < r) { int mid = (l + r) >> 1; /*[l, mid]*/ int temp = sum[ls[x]] + sum[ls[y]] - 2*sum[ls[lca]]; // printf("[l = %d, r = %d, temp = %d]\n", l, r, temp); // printf("sum_lx = %lld, sum_ly = %lld, sum_l_lca = %lld\n", sum[ls[x]], sum[ls[y]], sum[ls[lca]]); if (k <= temp) { x = ls[x], y = ls[y], lca = ls[lca]; r = mid; }else { k -= temp; x = rs[x], y = rs[y], lca = rs[lca]; l = mid + 1; } } return l; } int Query(int x, int y,int lca, int l, int r,int cap) { int ret = 0; LL cnt = 0, res = 0; while(l < r) { int mid = (l + r) >> 1; /*[l, mid]*/ LL temp_sum = sum[ls[x]] + sum[ls[y]] - 2LL*sum[ls[lca]]; LL temp_cnt = _cnt[ls[x]] + _cnt[ls[y]] - 2LL*_cnt[ls[lca]]; if ((temp_sum + res)*mid - (temp_cnt + cnt) > cap) { x = ls[x], y = ls[y], lca = ls[lca], r = mid; }else { ret = mid; x = rs[x], y = rs[y], lca = rs[lca]; l = mid + 1; res += temp_sum; cnt += temp_cnt; } } return ret; } int main(int argc, const char * argv[]) { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); // ios::sync_with_stdio(false); // cout.sync_with_stdio(false); // cin.sync_with_stdio(false); // cout << (1 << 19) << endl; int kase;cin >> kase; while(kase--) { memset(head, -1, sizeof head), ecnt = tot = 0; scanf("%d%d", &n, &m); for (int i = 1, u, v, c;i < n;++i) { scanf("%d%d%d", &u, &v, &c); addedge(u, v, c); } lca.dfs(1, -1, 0); lca.build(n); Rep(i, 1, m) Q[i].read(); build(root[0], 0, 10000); dfs_build(1, 0, 0);/*u, fa*/ printf("Case #%d:\n", ++nCase); Rep(i, 1, m) { // printf("[s = %d, t = %d, lca = %d]\n", Q[i].s, Q[i].t, lca.LCA(Q[i].s, Q[i].t)); LL ans = (LL)Query_Kth(root[Q[i].s], root[Q[i].t], root[lca.LCA(Q[i].s, Q[i].t)], 0, 10000, 1); // debug(ans); if (Q[i].a < Q[i].b) ans += Q[i].k / Q[i].a; else { if (Q[i].k >= Q[i].a) ans += (Q[i].k - Q[i].a) / Q[i].b + 1; // debug(ans); ans = max(ans, (LL)Query(root[Q[i].s], root[Q[i].t], root[lca.LCA(Q[i].s, Q[i].t)], 0, 10000, Q[i].k / Q[i].b)); } printf("%lld\n", ans); } } // showtime; return 0; }
相关文章推荐
- HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)
- HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)
- 【树链剖分】 HDU 4729 An Easy Problem for Elfness 二分
- HDU 4729 An Easy Problem for Elfness 主席树
- HDU 4729 An Easy Problem for Elfness(树链剖分 + 二分)
- HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)
- 数据结构(主席树):HDU 4729 An Easy Problem for Elfness
- 【HDU】4729 An Easy Problem for Elfness 可持久化线段树——主席树
- 【主席树】 HDOJ 4729 An Easy Problem for Elfness
- hdu 4729 An Easy Problem for Elfness,函数式线段树
- 【HDOJ】4729 An Easy Problem for Elfness
- [HDU4729]An Easy Problem for Elfness
- An Easy Problem for Elfness
- hdu-An Easy Problem
- hdu 2601 An easy problem(中)
- HDU-2601 An easy problem
- hdu——An Easy Problem
- HDU 2055 An easy problem
- HDU 2601 An easy problem
- hdu 2601 An easy problem