NEFU 641 素数树

112 篇文章 0 订阅
89 篇文章 0 订阅

素数树

Time Limit 1000ms

Memory Limit 65536K

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,  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

3


我觉得我的码可读性更强orz

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;


const int OO=1e9;
const int INF=1e9;
const int maxn=51111;
const int max_prime=4000009;

struct NODE{
    int num;
    int max_num;
    bool visit;
    bool prime;
}tree[maxn];

struct EDGE{
    int to;
    int next;
}edges[2*maxn];

int head[maxn];
int n,edge;
int ans;

void init()
{
    memset(head,-1,sizeof(head));
    memset(edges,0,sizeof(edges));
    memset(tree,0,sizeof(tree));
    edge=0;
    ans=0;
}

void addedge(int u,int v)
{
    edges[edge].to=v;edges[edge].next=head[u];head[u]=edge++;
    edges[edge].to=u;edges[edge].next=head[v];head[v]=edge++;
}

bool not_prime[max_prime];
void find_prime()
{
    memset(not_prime,0,sizeof(not_prime));
    not_prime[0]=true;
    not_prime[1]=true;
    for (int i=2;i*i<max_prime;i++)
    {
        if (!not_prime[i])
        {
            for (int j=i+i;j<max_prime;j+=i)
            {
                not_prime[j]=true;
            }
        }
    }
}

void dfs(int u)
{
    int v;
    bool ok=tree[u].prime;
    tree[u].visit=true;
    for (int i=head[u];i!=-1;i=edges[i].next)
    {
        v=edges[i].to;
        if (!tree[v].visit)
        {
            dfs(v);
            if (!tree[v].prime)
            {
                tree[u].prime=false;
                ok=false;
            }
            if (tree[v].max_num>=tree[u].num)
            {
                ok=false;
            }
            if (tree[v].max_num>=tree[u].max_num)
            {
                tree[u].max_num=tree[v].max_num;
            }
        }
    }
    if (ok) ans++;
}

int main()
{
    find_prime();
    while (scanf("%d",&n)!=EOF)
    {
        init();
        for (int i=1;i<=n;i++)
        {
            scanf("%d",&tree[i].num);
            if ( !not_prime[tree[i].num] )
            {
                tree[i].prime=true;
            }
            else
            {
                tree[i].prime=false;
            }
            tree[i].visit=false;
            tree[i].max_num=tree[i].num;
        }
        for (int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        dfs(1);
        printf("%d\n\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值