大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #incl…
树上莫队就是把莫队搬到树上-利用欧拉序乱搞.. 子树自然是普通莫队轻松解决了 链上的话 只能用树上莫队了吧.. 考虑多种情况 [X=LCA(X,Y)] [Y=LCA(X,Y)] else void dfs(int u) { sz[u] = 1 ; rev[st[u] = ++ cnt] = u ; for(int i = 0 ; i < G[u].size() ; i ++) { int v = G[u][i] ; if(v == fa[u]) { continue ; } fa[v] = u…
链接 https://vjudge.net/problem/SPOJ-COT2 https://www.luogu.org/problemnew/show/SP10707 思路 dfs欧拉序转化为普通莫队(并不算树上莫队,不过也可做) 好神仙啊,原来欧拉序是可以求任意两点的点,不过要加lca. 代码 #include <bits/stdc++.h> using namespace std; const int N=2e5+7; int read() { int x=0,f=1;char s=g…
题目大意:有一棵$n$个节点的树,第$i$个点有一个颜色$C_i$,$m$组询问,每次问$x->y$的路径上有多少种颜色 题解:树上莫队,把树按欧拉序展开成一条链,令第$i$个节点第一次出现在序列中为$in_i$,第二次为$out_i$,每一个询问就是看$in_x->in_y$中只出现一次的节点的颜色,但发现如果$x$不为$x,y$的$lca$的话$lca$不会被计入答案,特判一下就行 卡点:1$\sim$2.数组未开大 3.$tarjan$求$lca$时加询问加错 4.为先加入第一个点导致答…
题意:求一条链 \((u,v)\) 上不同的颜色数. 我们可以求出树的出栈入栈序(or 括号序?我也不确定). 图(from attack) 然后有一个很优美的性质: 设点 \(u\) 的入栈时间为 \(dfn[u]\) ,出栈时间为 \(low[u]\) 设两个点 \(u,v\) 满足 \(dfn[u]<dfn[v]\) 若 \(u\) 为 \(v\) 的 \(lca\),那么我们只需查询 \([dfn[u],dfn[v]]\) 中只出现一次的点有多少不同的颜色. 若 \(u\) , \(v\…
COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight. We will ask you to perform the following operation: u v : ask for how many different integers that represent…
COT2 - Count on a tree II You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight. We will ask you to perform the following operation: u v : ask for how many different integers that represent the we…
COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight. We will ask you to perform the following operation: u v : ask for how many…
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<algorithm> #include<queue> #include<set> #include<map>…
题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的,  受益匪浅.. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #i…