HGC 1356 Prime Node【树形DP+线性素数打表】

Description

Suppose there is a tree named A. All nodes of A have a weight v(0<v<4000000).Now, we will give the definition of "Prime Node".A node is a Prime Node if the following conditions are satisfied.The subtree of A whose root node is b will be marked as B. If all nodes in B have prime weights and b's weight is greater than other nodes', then b is a Prime Node.Now you are to calculate how many Prime Nodes are in A.

Input

For each test case:The fist line will contains an integer n(1<=n<=10000) indicating the number of nodes in A,the root of A will always be node 1.The second line has n integers giving the weights of each node numbered from 1 to n.The following n-1 lines, each contains a pair of integers x and y indicating there is an edge between x and y, give the n-1 edges of A.

Process to the end of file.

Output

For each test case, first print a line saying "Scenario #k", where k is the number of the test case.Then, print the number of Prime Nodes on a single line.Print a blank line after each test case, even after the last one.

Sample Input

5
12 11 5 7 3
1 2
2 3
2 5
3 4

Sample Output

Scenario #1
3

code :

View Code
#include<stdio.h>
#include<string.h>
int su,tot;
bool pp[4000001];
bool v[10002];
int prime[400000];
int head[10002];
void print()
{
    memset(pp,true,sizeof(pp));
    int i,j,k;
    k=0;
    prime[k++]=2;
    for(i=3;i<=4000000;i++)
    {
        if(pp[i])
                prime[k++]=i;
        for(j=0;j<k&&prime[j]*i<=4000000;j++)
        {
                pp[prime[j]*i]=false;
                if(i%prime[j]==0)
                        break;
        }
    }
}
struct node
{
    int ls,rs,wi,max,p;
}tree[10001];
struct node1
{
    int next,to;
}q[20000];
void add(int s,int u)
{
    q[tot].to=u;
    q[tot].next=head[s];
    head[s]=tot++;
}
void dfs(int r)
{
    int son;
        tree[r].max=tree[r].wi;
    if(!tree[r].ls)
    {
        if(pp[tree[r].wi])
        {
            tree[r].p=1;
            su++;
        }
        else tree[r].p=0;
        return;
    }
    son=tree[r].ls;
    int flag=1;
    if(pp[tree[r].wi])
        tree[r].p=1;
    else {
        tree[r].p=0;
        flag=0;
    }
    while(son)
    {
        dfs(son);
        if(tree[r].p)
        {
            if(!tree[son].p)
            {
                tree[r].p=0;
                flag=0;
            }
            if(tree[son].max>=tree[r].max)
            {
                flag=0;
                tree[r].max=tree[son].max;
            }
        }
        son=tree[son].rs;
    }
    if(flag)su++;
}
int que[10002];
int main()
{
    print();
    pp[1]=pp[4]=0;
    int i,n,k,l,ca=1;
    while(scanf("%d",&n)!=EOF)
    {
        tot=1;  su=0;
        memset(head,0,sizeof(head));
        memset(tree,0,sizeof(tree));
        for(i=1;i<=n;i++)
            scanf("%d",&tree[i].wi);
        for(i=1;i<n;i++)
        {
            scanf("%d%d",&k,&l);
            add(k,l);
            add(l,k);
        }
        int front=0,rear=0;
        memset(v,false,sizeof(v));
        que[rear++]=1;
        v[1]=1;
        while(front<rear)
        {
            k=que[front++];
            for(i=head[k];i;i=q[i].next)
            {
                l=q[i].to;
                if(v[l])continue;
                v[l]=true;
                tree[l].rs=tree[k].ls;
                tree[k].ls=l;
                que[rear++]=l;
            }
    
        }
        dfs(1);
        printf("Scenario #%d\n",ca++);
        printf("%d\n\n",su);
    }
    return 0;
}

 

posted @ 2012-04-13 00:06  'wind  阅读(335)  评论(0编辑  收藏  举报