[UOJ 579] tree color

[Title Description:

N points have given a rooted tree, nodes numbered l, 2,3, ......, N, the root node number 1, number of node i be colored Ci. Now there are M inquiry, inquiry request each node u to determine the number of colors as the number of the sub-tree root node coated with such color number is not less than k.

[Input Description:

The first line contains two positive integers N and M.

The second line contains N positive integers, C1, C2, ..., CN.

The next N-1 lines each have two positive integers x and y, x and y represent the node has connected to side.

The next M rows each row having two positive integers u and k, represents a challenge.

[Output] Description:

M output lines, each line of a non-negative integer, each answer corresponding to the interrogation.

Sample input [1]:

4 1
1 2 3 4
1 2
2 3
3 4
1 1

[1] sample output:

4

Sample input [2]:

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

[2] sample output:

2
2
1
0
1

[Time limit, and the range of data Description:

Time: 1s space: 256M

For 10% of the data, N≤100, M≤100, Ci≤100;

For 30% of the data, N≤500, M≤100;

For 60% of the data, N≤2000, M≤100000;

To 100% of the data, N≤100000, M≤100000, Ci≤100000.

Solution: Why I can not get points violence ...... puzzling, if there is dalao spot the mistake, it will leave a message, thank you

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int N=100002;
int n,ques,x,y;
struct node{
    int u,v,next;
}a[N];
int h[N],cnt,ans;
int c[N];
int z,fj[N],biu[N];

void add(int u,int v){
    a[++cnt].u=u; a[cnt].v=v;
    a[cnt].next=h[u]; h[u]=cnt;
}

void init(){
    scanf("%d %d",&n,&ques);
    for(int i=1;i<=n;i++)
        scanf("%d",&c[i]);
    for(int i=1;i<n;i++){
        scanf("%d %d",&x,&y); 
        add(x,y);
    }
}

void dfs(int x){
    for(int i=h[x];i;i=a[i].next) 
        { int v=a[i].v; fj[c[v]]++; dfs(v); }
}

void work(){
    while(ques--){
        scanf("%d %d",&x,&y);
        int ans=0;
        memset(fj,0,sizeof(fj));
        memset(biu,0,sizeof(biu));
        fj[c[x]]++; dfs(x);
        for(int i=1;i<=n;i++){
            if(fj[c[i]]>=y && biu[c[i]]==0) 
               { ans++; biu[c[i]]=1; }
        }
        printf("%d\n",ans);
    }
    
}
int main(){
    freopen("579.in","r",stdin);
    freopen("579.out","w",stdout);
    init(); work();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11518014.html