题目链接

一个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序+莫队的更多相关文章

  1. CF 375D. Tree and Queries加强版!!!【dfs序分块 大小分类讨论】

    传送门 题意: 一棵树,询问一个子树内出现次数$\ge k$的颜色有几种,Candy?这个沙茶自带强制在线 吐槽: 本来一道可以离散的莫队我非要强制在线用分块做:上午就开始写了然后发现思路错了...: ...

  2. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

  3. CodeForces 375D Tree and Queries 莫队||DFS序

    Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...

  4. 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 ...

  5. Codeforces 375D - Tree and Queries(dfs序+莫队)

    题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...

  6. CodeForces 375D Tree and Queries

    传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...

  7. hdu 4358 Boring counting dfs序+莫队+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  8. hdu 4358 Boring counting 离散化+dfs序+莫队算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...

  9. CF600E Lomsat gelral (dfs序+莫队)

    题面 题解 看到网上写了很多DSU和线段树合并的题解,笔者第一次做也是用的线段树合并,但在原题赛的时候却怕线段树合并调不出来,于是就用了更好想更好调的莫队. 这里笔者就说说莫队怎么做吧. 我们可以通过 ...

随机推荐

  1. 剑指offer26 复杂链表的复制

    /* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : ...

  2. CSS3 旋转3D立方体

    <meta charset="utf-8"> <style> *{ margin:0px; padding:0px; } @-webkit-keyframe ...

  3. English - in the light of(按照,根据)与according to的区别是什么

    according to 表示as stated by 像陈述的那样 如According to Sarah they're not getting on very well at the momen ...

  4. matlab GUI之常用对话框(三)-- dialog \ errordlg \ warndlg \ helpdlg \ msgbox \questdlg

    常用的对话框(三) 1.普通对话框  dialog 调用格式: h=dialog('PropertyName','PropertyValue'......) %普通对话框 h=dialog( ]); ...

  5. 关于多线程中GCD的使用

    GCD 分为异步和同步 异步: ```objc  dispatch_async (  参数1  , {      } 同步: dispatch_sync( 参数1   , {   } ``` ###参 ...

  6. c++ 文件读写(转)

    C/C++ code //创建一个文本文件并写入信息 //同向屏幕上输出信息一样将信息输出至文件 #include<iomanip.h> #include<fstream.h> ...

  7. SSH框架的简化

    ---恢复内容开始--- 一.简易化的SSH框架,如图: SSH框架的搭建,我就不多说了. 二.简易的ssh框架的步骤: 1.重新编写applicationContext.xmlwen文件 <一 ...

  8. STL模板_multimap_智能指针作为键值

    map的键值的类型 -可以是自定的类型(对象.函数指针.智能指针....) -但是有副作用-当自己定义的类型键值无法用系统自己提供的 < 或者 > 进行排序的时候,会出现各种问题 -所以需 ...

  9. C - 下沙小面的(2)

    C – 下沙小面的(2)Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uSubmit StatusDes ...

  10. BZOJ 1021: [SHOI2008]Debt 循环的债务( dp )

    dp(i, j, k)表示考虑了前i种钱币(从小到大), Alice的钱数为j, Bob的钱数为k, 最小次数. 脑补一下可以发现, 只有A->B.C, B->A.C, C->A.B ...