codeforces 593D(并查集 + LCA)
来源:互联网 发布:手机维修网络教学 编辑:程序博客网 时间:2024/05/22 06:56
题意不在描述.
首先我们注意到改变每个点的值时,只会变的更小,每次要除的数为1e18 , 由于log2(1e18) <= 60 .那么最多只需要除以至多60次大于等于2的数变回变成0,所以我们
只以任意一点为根建立有根树,并且在每次遍历一条链时进行压缩,当该路经值为1,应该被压缩,每次走到多余六十个点直接结束。
#include <bits/stdc++.h>#define fst first#define snd second#define ALL(a) a.begin(), a.end()#define clr(a, x) memset(a, x, sizeof a)#define rep(i,x) for(int i=0;i<(int)x;i++)#define rep1(i,x,y) for(int i=x;i<=(int)y;i++)#define LOGN 22typedef long long ll;using namespace std;const int N = 2e5 + 10000;vector<int> tree[N];int fa[N][LOGN],pa[N] , realpa[N];int depth[N];ll val[N*2] , ided[N];void dfs(int u, int p, int d) { depth[u] = d; pa[u] = p; realpa[u] = p; fa[u][0] = p; for (int i = 0; i < tree[u].size(); ++i) { if (tree[u][i] != p) dfs(tree[u][i], u, d + 1); }}int LCA(int u, int v) { if (depth[u] > depth[v]) swap(u, v); for (int i = 0; i < LOGN; ++i) { if (((depth[v] - depth[u]) >> i) & 1) v = fa[v][i]; } if (u == v) return u; for (int i = LOGN - 1; i >= 0; --i) { if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; } } return fa[u][0];}void predo(int n) { int root = 1; pa[root] = -1; dfs(root, -1, 0); depth[0] = -1; for (int j = 0; j + 1 < LOGN; ++j) { for (int i = 1; i <= n; ++i) { if (fa[i][j] < 0) fa[i][j + 1] = -1; else fa[i][j + 1] = fa[fa[i][j]][j]; } }}typedef pair<int,int> pii;map<pii,int> M;ll get_c(int u){ return val[ided[u]]; }int n,m;int find(int u){ if(realpa[pa[u]] == -1 || get_c(pa[u]) > 1) return pa[u]; else return pa[u] = find(pa[u]);}ll cal1(int a,int b,ll c){ int lca = LCA(a,b); int u = a , v = b; ll now = c; vector<ll> lt,rt; while(u != v){ if(depth[u] >= depth[v]){ int fau = find(u); if(depth[fau] < depth[lca]) fau = lca; if(get_c(u) > 1) lt.push_back(get_c(u)); u = fau; } else { int fav = find(v); if(depth[fav] < depth[lca]) fav = lca; if(get_c(v) > 1) rt.push_back(get_c(v)); v = fav; } if(lt.size() + rt.size() > 60) return 0; } if(rt.size()) reverse(ALL(rt)); rep(i,rt.size()) lt.push_back(rt[i]); for(int i=0;i<lt.size();i++){ now/=lt[i]; } return now;}void read(){ scanf("%d %d",&n,&m); rep(i,n-1){ int u,v; ll w; scanf("%d %d %I64d",&u,&v,&w); M[pii(u,v)] = M[pii(v,u)] = 1 + i; val[1+i] = w; tree[u].push_back(v); tree[v].push_back(u); } predo(n); rep1(i,2,n) ided[i] = M[pii(i,realpa[i])] ; rep1(i,1,m){ ll cmd , x ,y , z; scanf("%I64d %I64d %I64d",&cmd,&x,&y); if(cmd == 1) { scanf("%I64d",&z); printf("%I64d\n",cal1(x,y,z)); } else { val[x] = y; } }}int main(){ read(); return 0;}
0 0
- codeforces 593D(并查集 + LCA)
- codeforces 593 D. Happy Tree Party (LCA + 并查集)
- codeforces 593D Happy Tree Party Lca+并查集
- Summer Training day6 codeforces 593D LCA+并查集
- CodeForces 593D Happy Tree Party(树链剖分(边权) or LCA+并查集)
- codeforces #329 D. Happy Tree Party (LCA+并查集 || 树链剖分)
- Codeforces Round #329 (Div. 2)D LCA+并查集路径压缩
- Codeforces 366D 贪心+并查集
- Codeforces 371D. Vessels【并查集】
- codeforces 366D 并查集
- codeforces 691D 并查集
- [Codeforces 325 D. Reclamation]并查集
- Codeforces 766D 并查集
- codeforces 766D 并查集进阶
- Codeforces 776D 并查集
- CodeForces 25D(并查集)
- poj3728并查集+LCA
- 【Codeforces Round 329 (Div 2) D】【LCA+并查集路径压缩】Happy Tree Party 除上两点间路径全部权值
- dns nsswitch.conf
- Java Code To Byte Code
- Groovy基础——MetaClass详解
- iOS小技巧-UISwitch改变系统默认的颜色
- LeetCode Remove Duplicates from Sorted Array
- codeforces 593D(并查集 + LCA)
- 一致性哈希算法(consistent hashing)
- windows7下用命令操作mysql
- Ajax工作原理
- 红米手机上ResolveInfo loadIcon的背景问题
- base 64 加密 和 解密
- 【软工7】软件工程中的文档
- 配置tomcat,在访问时不需要加端口号和工程名
- Android.mk 文件语法详解