[SP10707]COT2 - Count on a tree II
题目大意:有一棵$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.为先加入第一个点导致答案多一
C++ Code:
#include <cstdio>
#include <algorithm>
#define maxn 40010
#define maxm 100010
#define N (maxn << 1)
#define bl(x) ((x) >> 9)
int n, m;
int in[maxn], out[maxn], date[N], idx;
struct Query {
int l, r, lca, id;
bool addlca;
inline bool operator < (const Query &rhs) const {
return (bl(l) == bl(rhs.l)) ? r < rhs.r : l < rhs.l;
}
} q[maxm];
namespace tree {
int head[maxn], cnt = 0;
struct Edge {
int to, nxt;
} e[maxn << 1];
inline void add(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
e[++cnt] = (Edge) {a, head[b]}; head[b] = cnt;
}
int fa[maxn]; void dfs(int u) {
date[in[u] = ++idx] = u;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa[u]) {
fa[v] = u;
dfs(v);
}
}
date[out[u] = ++idx] = u;
}
}
namespace tarjan {
int head[maxn], cnt = 0;
struct QUERY {
int v, nxt, id;
} Q[maxm << 1];
inline void add(int a, int b, int c) {
Q[++cnt] = (QUERY) {b, head[a], c}; head[a] = cnt;
Q[++cnt] = (QUERY) {a, head[b], c}; head[b] = cnt;
} int f[maxn];
inline void init(int n) {
for (int i = 1; i <= n; i++) f[i] = i;
}
int find(int x) {return (x == f[x] ? x : (f[x] = find(f[x])));} bool vis[maxn];
void dfs(int u) {
for (int i = tree::head[u]; i; i = tree::e[i].nxt) {
int v = tree::e[i].to;
if (v != tree::fa[u]) {
dfs(v);
f[v] = u;
}
}
for (int i = tarjan::head[u]; i; i = tarjan::Q[i].nxt) q[Q[i].id].lca = find(Q[i].v);
}
}
#define ONLINE_JUDGE
#include <cctype>
namespace R {
int x;
#ifdef ONLINE_JUDGE
char *ch, op[1 << 26];
inline void init() {
fread(ch = op, 1, 1 << 26, stdin);
}
inline int read() {
while (isspace(*ch)) ch++;
for (x = *ch & 15, ch++; isdigit(*ch); ch++) x = x * 10 + (*ch & 15);
return x;
}
#else
char ch;
inline int read() {
ch = getchar();
while (isspace(ch)) ch = getchar();
for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
return x;
}
#endif
} inline void swap(int &a, int &b) {a ^= b ^= a ^= b;}
int num[maxn], W[maxn], w[maxn], ans[maxm];
bool vis[maxn];
int main() {
#ifdef ONLINE_JUDGE
R::init();
#endif
tarjan::init(n = R::read()); m = R::read();
for (int i = 1; i <= n; i++) W[i] = w[i] = R::read();
int tot = (std::sort(W + 1, W + n + 1), std::unique(W + 1, W + n + 1) - W - 1);
for (int i = 1; i <= n; i++) w[i] = std::lower_bound(W + 1, W + tot + 1, w[i]) - W;
for (int i = 1; i < n; i++) tree::add(R::read(), R::read());
tree::dfs(1);
for (int i = 1; i <= m; i++) tarjan::add(q[i].l = R::read(), q[i].r = R::read(), q[i].id = i);
tarjan::dfs(1);
for (int i = 1; i <= m; i++) {
int &l = q[i].l, &r = q[i].r;
if (in[l] > in[r]) swap(l, r);
l = (q[i].addlca = (q[i].lca != l)) ? out[l] : in[l];
r = in[r];
}
std::sort(q + 1, q + m + 1);
int l, r, res; l = 1, r = 1, res = 1;
vis[date[1]] = 1; num[w[date[1]]]++;
for (int i = 1; i <= m; i++) {
while (l > q[i].l) (vis[date[--l]] ^= 1) ? (res += num[w[date[l]]]++ == 0) : (res -= --num[w[date[l]]] == 0);
while (r < q[i].r) (vis[date[++r]] ^= 1) ? (res += num[w[date[r]]]++ == 0) : (res -= --num[w[date[r]]] == 0);
while (l < q[i].l) (vis[date[l]] ^= 1) ? (res += num[w[date[l++]]]++ == 0) : (res -= --num[w[date[l++]]] == 0);
while (r > q[i].r) (vis[date[r]] ^= 1) ? (res += num[w[date[r--]]]++ == 0) : (res -= --num[w[date[r--]]] == 0);
ans[q[i].id] = res + (q[i].addlca && !num[w[q[i].lca]]);
}
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}
[SP10707]COT2 - Count on a tree II的更多相关文章
- SP10707 COT2 - Count on a tree II 莫队
链接 https://vjudge.net/problem/SPOJ-COT2 https://www.luogu.org/problemnew/show/SP10707 思路 dfs欧拉序转化为普通 ...
- SP10707 COT2 - Count on a tree II (树上莫队)
大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...
- SP10707 COT2 - Count on a tree II 莫队上树
题意:求一条链 \((u,v)\) 上不同的颜色数. 我们可以求出树的出栈入栈序(or 括号序?我也不确定). 图(from attack) 然后有一个很优美的性质: 设点 \(u\) 的入栈时间为 ...
- SP10707 COT2 - Count on a tree II [树上莫队学习笔记]
树上莫队就是把莫队搬到树上-利用欧拉序乱搞.. 子树自然是普通莫队轻松解决了 链上的话 只能用树上莫队了吧.. 考虑多种情况 [X=LCA(X,Y)] [Y=LCA(X,Y)] else void d ...
- 【SPOJ10707】 COT2 Count on a tree II
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...
- spoj COT2 - Count on a tree II
COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...
- SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)
COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from ...
- COT2 - Count on a tree II(树上莫队)
COT2 - Count on a tree II You are given a tree with N nodes. The tree nodes are numbered from 1 to N ...
- 【树上莫队】【SP10707】 COT2 - Count on a tree II
Description 给定一棵 \(n\) 个点的树,每个节点有一个权值,\(m\) 次询问,每次查询两点间路径上有多少不同的权值 Input 第一行是 \(n\) 和 \(m\) 第二行是 \(n ...
随机推荐
- JS动画与CSS3动画
Js动画 show / hide var div = $('#test-show-hide'); div.show('slow'); // 在0.6秒钟内逐渐显示 div.hide(3000); // ...
- LINUX安装好后无法访问网络
LINUX安装好后无法访问网络 在虚拟机安装好新的LINUX系统后,ping www.baidu.com ,发现无法ping通. 键入ifconfig查询配置: eno16777728: flags= ...
- 连接MYSQL 错误代码2003
问题是服务里面mysql没有启动或者mysql服务丢失 解决办法: 开始->运行->cmd,进到mysql安装的bin目录(以我的为例,我的安装在D盘)D:\MySQL\bin>my ...
- python--Wrapper
然后给大家介绍的是Wrapper(装饰器),使用广泛.python笔试,面试的话也会百分百问到的,基础和中级的知识储备还是必用的. 让我们开始. 先来一些基础相关知识 *args,**kwargs的区 ...
- python——input()函数
在使用input()内置函数输入数字时,要注意一点: input()的返回值始终是字符串,所以type(number)永远是<class 'str'>! 如: >>> t ...
- 笔记-scrapy-深入学习-sheduler
笔记-scrapy-深入学习-sheduler 1. scheduler.py source code:scrapy/core/scheduler.py: 1.1. 初始化的开始 在分 ...
- 3 网格 landing page
0.大框架 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- 局域网&广域网&Internet&计算机通信过程
1.局域网 覆盖范围小(100m以内),自己花钱购买,自己单位来维护,带宽固定的(10M,100M,1000M) 2.Internet ISP,有自己的机房,对网民提供访问Internet连接 3.广 ...
- 使用Eclipse把java文件打包成jar
本例仅限于要打包的文件没有使用第三方的jar包 在要打包的包或者文件上右键-Export 这里有一些选项: Export generated class files and resources 表示只 ...
- 【Binary Tree Right Side View 】cpp
题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the ...