zoukankan      html  css  js  c++  java
  • CoderForces-375D

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will assume that the tree vertices are numbered by integers from 1 to n. Then we represent the color of vertex v as cv. The tree root is a vertex with number 1.

    In this problem you need to answer to m queries. Each query is described by two integers vj, kj. The answer to query vj, kj is the number of such colors of verticesx, that the subtree of vertex vj contains at least kj vertices of color x.

    You can find the definition of a rooted tree by the following link:http://en.wikipedia.org/wiki/Tree_(graph_theory).

    Input

    The first line contains two integers n and m (2 ≤ n ≤ 105; 1 ≤ m ≤ 105). The next line contains a sequence of integers c1, c2, ..., cn (1 ≤ ci ≤ 105). The next n - 1 lines contain the edges of the tree. The i-th line contains the numbers ai, bi (1 ≤ ai, bi ≤ nai ≠ bi) — the vertices connected by an edge of the tree.

    Next m lines contain the queries. The j-th line contains two integers vj, kj (1 ≤ vj ≤ n; 1 ≤ kj ≤ 105).

    Output

    Print m integers — the answers to the queries in the order the queries appear in the input.

    Example

    Input
    8 5
    1 2 2 3 3 2 3 3
    1 2
    1 5
    2 3
    2 4
    5 6
    5 7
    5 8
    1 2
    1 3
    1 4
    2 3
    5 3
    
    Output
    2
    2
    1
    0
    1
    
    Input
    4 1
    1 2 3 4
    1 2
    2 3
    3 4
    1 1
    
    Output
    4
    

    Note

    A subtree of vertex v in a rooted tree with root r is a set of vertices{u : dist(r, v) + dist(v, u) = dist(r, u)}. Where dist(x, y) is the length (in edges) of the shortest path between vertices x and y.

    题解:首先,通过dfs,可以把查询一棵树的子树转化为查询一段区间[l,r]。接下来,把整个区间分成n/sqrt(n)+1份,将查询按照l所在的区间排序,在同一区间内,将查询按r排序(由大到小)。对于查询,用一个数组表示某颜色数为x的颜色的个数,查询这个数组用分块法查询。

    AC代码:

    #include <iostream>  
    #include<cstdio>  
    #include<cstring>  
    #include<string>  
    #include<algorithm>   
    #include<queue>  
    #include<cmath>  
    #include<vector>   
    using namespace std;
    
    
    typedef long long ll;
    const int maxn = 100000 + 3000;
    const int SIZE = 300;
    int bnt[maxn], block[maxn / SIZE + 10], cnt[maxn];
    int l[maxn], r[maxn], dfs_clock;
    int c[maxn], cval[maxn], ans[maxn], n, m;
    vector<int> g[maxn];
    
    
    struct Query
    {
    int l, r, k, id;
    Query() {}
    Query(int lx, int rx, int kk, int i) 
    {
    l = lx;
    r = rx;
    k = kk;
    id = i;
    }
    bool operator <(const Query &q) const
    {
    if (l / SIZE != q.l / SIZE) 
    return l<q.l;
    return r>q.r;
    }
    }querys[maxn];
    
    
    void dfs(int u, int fa)
    {
    l[u] = ++dfs_clock;
    cval[l[u]] = c[u];
    int sz = g[u].size();
    for (int i = 0; i<sz; ++i)
    {
    int v = g[u][i];
    if (v == fa) 
    continue;
    dfs(v, u);
    }
    r[u] = dfs_clock;
    }
    
    
    void Insert(int k, int x)
    {
    bnt[k] += x;
    block[k / SIZE] += x;
    }
    void Add(int v)
    {
    int p = ++cnt[cval[v]];
    Insert(p, 1);
    Insert(p - 1, -1);
    }
    
    
    void Dec(int v)
    {
    int p = --cnt[cval[v]];
    Insert(p, 1);
    Insert(p + 1, -1);
    }
    
    
    int cal(int k)
    {
    int sum = 0, p = k / SIZE;
    for (int i = k; i<(p + 1)*SIZE; ++i)
    sum += bnt[i];
    for (int i = p + 1; i*SIZE<maxn; ++i)
    sum += block[i];
    return sum;
    }
    
    
    void solve()
    {
    memset(cnt, 0, sizeof(cnt));
    memset(bnt, 0, sizeof(bnt));
    memset(block, 0, sizeof(block));
    Insert(0, n);
    Add(1);
    int l = 1, r = 1;
    for (int i = 0; i<m; ++i)
    {
    while (querys[i].l<l) Add(--l);
    while (querys[i].r>r) Add(++r);
    while (querys[i].l>l) Dec(l++);
    while (querys[i].r<r) Dec(r--);
    ans[querys[i].id] = cal(querys[i].k);
    }
    }
    
    
    int main()
    {
    cin >> n >> m;
    for (int i = 0; i <= n; ++i) 
    g[i].clear();
    dfs_clock = 0;
    for (int i = 1; i <= n; ++i)
    cin >> c[i];
    int u, v, k;
    for (int i = 0; i<n - 1; ++i)
    {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
    }
    
    
    dfs(1, -1);
    
    
    for (int i = 0; i<m; ++i)
    {
    cin >> v >> k;
    querys[i] = Query(l[v], r[v], k, i);
    }
    sort(querys, querys + m);
    solve();
    for (int i = 0; i < m; ++i)
    cout << ans[i] << endl;
    return 0;
    }
    View Code
  • 相关阅读:
    服务器扫描工具
    Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:443
    [warn] _default_ VirtualHost overlap on port 443, the first has precedence
    Apache环境下配置多个站点的SSL证书
    Linux漏洞扫描工具
    详细解读html中的Map,area标签
    Linux操作系统加固
    MySQL服务安全加固
    redis cluster管理工具redis-trib.rb详解
    Java中使用Jedis操作Redis
  • 原文地址:https://www.cnblogs.com/csushl/p/9386602.html
Copyright © 2011-2022 走看看