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和线段树合并的题解,笔者第一次做也是用的线段树合并,但在原题赛的时候却怕线段树合并调不出来,于是就用了更好想更好调的莫队. 这里笔者就说说莫队怎么做吧. 我们可以通过 ...
随机推荐
- I Hate It(线段树)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- The "Run One Program Only" Phenomenon
As previously discussed,embedded devices normally embody the functionality they implement.In other w ...
- Android的MVC框架
http://www.cnblogs.com/wanghafan/archive/2012/07/20/2600786.html MVC是当前比较流行的框架,随便Google下,就可以发现几乎所有的应 ...
- iOS_词典阵列 按key分组和排序
// // main.m // SortGroup // // Created by beyond on 14-10-26. // Copyright (c) 2014年 beyond.com All ...
- 修改UISearchBar背景颜色
UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性.方法一:是直接将 UISearc ...
- BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )
离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...
- tengine install
./configure --prefix=/home/admin/local/tengine --with-http_stub_status_module --with-http_ssl_module ...
- Excel VLOOKUP等使用记录
1.LEFT 函数:LEFT(G2,ROW(INDIRECT("76:"&LEN(G2)))) 截取G2单元格从开头,SIZE=76的部分字符串 2.RIGHT 函数:RI ...
- win7系统下连接使用mac 蓝牙键盘(Apple Wireless Keyborad)
这几天买了一个apple wireless keyborad 玩玩,主要是给孩子买了一个ipad 搭配上wireless keyborad让她玩app足够了,就当一部电脑用吧. 看起来挺精致的,可以了 ...
- IIS上不能播放mp4
iis不支持mp4格式,需要手动添加. 进入iis服务管理器,打开你的网站,然后点击MIME类型---添加(扩展名:mp4 MIME类型:application/octet-stream) 如此即 ...