codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队
一个n个节点的树, 每一个节点有一个颜色, 1是根节点。 m个询问, 每个询问给出u, k。 输出u的子树中出现次数大于等于k的颜色的数量。
启发式合并, 先将输入读进来, 然后dfs完一个节点就处理跟它有关的询问。
感觉不是很难, 然而.....WA了n次最后还是看的别人的代码
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e5+;
int head[maxn*], num, id[maxn], val[maxn], ans[maxn];
struct node
{
int to, nextt, w;
}e[maxn*];
void add(int u, int v) {
e[num].to = v, e[num].nextt = head[u], head[u] = num++;
}
void init() {
num = ;
mem1(head);
}
struct Node
{
map <int, int> mp;
vector <int> ve;
int sz = ;
void add(int x) {
mp[x]++;
while(mp[x]>=ve.size()) {
ve.push_back();
}
ve[mp[x]]++;
sz++;
}
}st[maxn];
vector <pll> v[maxn];
void combine(int& x, int& y) {
if(st[x].sz<st[y].sz)
swap(x, y);
for(auto it = st[y].mp.begin(); it!=st[y].mp.end(); it++) {
for(int i = ; i<it->second; i++) {
st[x].add(it->first);
}
}
}
void dfs(int u, int fa) {
st[u].add(val[u]);
for(int i = head[u]; ~i; i = e[i].nextt) {
int vx = e[i].to;
if(vx == fa)
continue;
dfs(vx, u);
combine(id[u], id[vx]);
}
for(int i = ; i<v[u].size(); i++) {
int x = v[u][i].fi, y = v[u][i].se;
if(x>=st[id[u]].ve.size())
continue;
ans[y] = st[id[u]].ve[x];
}
}
int main()
{
int n, m, x, y;
cin>>n>>m;
init();
for(int i = ; i<=n; i++) {
scanf("%d", &val[i]);
id[i] = i;
}
for(int i = ; i<n-; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
for(int i = ; i<m; ++i) {
scanf("%d%d", &x, &y);
v[x].pb(mk(y, i));
}
dfs(, );
for(int i = ; i<m; i++)
cout<<ans[i]<<endl;
return ;
}
codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队的更多相关文章
- CF 375D. Tree and Queries加强版!!!【dfs序分块 大小分类讨论】
传送门 题意: 一棵树,询问一个子树内出现次数$\ge k$的颜色有几种,Candy?这个沙茶自带强制在线 吐槽: 本来一道可以离散的莫队我非要强制在线用分块做:上午就开始写了然后发现思路错了...: ...
- Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)
题目链接 Tree and Queries 题目大意 给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...
- CodeForces 375D Tree and Queries 莫队||DFS序
Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...
- CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)
You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...
- Codeforces 375D - Tree and Queries(dfs序+莫队)
题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...
- CodeForces 375D Tree and Queries
传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...
- hdu 4358 Boring counting dfs序+莫队+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- hdu 4358 Boring counting 离散化+dfs序+莫队算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...
- CF600E Lomsat gelral (dfs序+莫队)
题面 题解 看到网上写了很多DSU和线段树合并的题解,笔者第一次做也是用的线段树合并,但在原题赛的时候却怕线段树合并调不出来,于是就用了更好想更好调的莫队. 这里笔者就说说莫队怎么做吧. 我们可以通过 ...
随机推荐
- nodejs项目中的路由写法
//两种路由写法,一种封装成函数,返回结果,此种方法可以传递参数, "use strict"; var _ = require("lodash"); var e ...
- NYOJ-252 01串
01串 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 ACM的zyc在研究01串,他知道某一01串的长度,但他想知道不含有"11"子串的这样的长 ...
- C++四种强制类型转换详解
什么是类型转换? 类型转换的含义是通过改变一个变量的类型为别的类型从而改变该变量的表示方式.为了类型转换一个简单对象为另一个对象你会使用传统的类型转换操作符. C与C++的类型转换 //C中: //复 ...
- HTML系列(六):划分文档结构
常见的网页结构布局是酱紫的,真是美美哒^O^: 一.添加基本标题h1~h6(没什么好说的): 二.标题分组hgroup <hgroup>用来将标题和子标题进行分组.如果一篇文章articl ...
- Validform表单验证的完美解决方案,推荐给大家
http://validform.rjboy.cn/ 功能简介: 可以在input上直接绑定正则,可以自定义datatype,自定义datatype可以是正则,也可以是函数,datatype可以累加或 ...
- SVN权限配置
初始化SVN仓库后,里面有以下文件. 其中conf是对授权.认证进行管理的,conf目录里的内容有: passwd设立账户密码: authz权限管理: 假设pwd里有user1,user2两个账户 @ ...
- SSE && WebSockets
SSE && WebSockets 参考 http://www.bitscn.com/school/HTMLCSS/201402/194940.html WebSockets 定义了一 ...
- safari的调试工具
safari的调试工具默认是没有打开的设置——>偏好设置——>高级———>在菜单栏中显示开发菜单
- RedisService
package com.sprucetec.bone.common.redis;import com.alibaba.fastjson.JSON;import org.springframework. ...
- nginx 编译参数
jrhnpt01:/root# nginx -V nginx version: nginx/1.7.7 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (G ...