HDU 5468 Puzzled Elena (DFS序+莫比乌丝反演 好题)


Puzzled Elena

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 470    Accepted Submission(s): 103

Problem Description

Since both Stefan and Damon fell in love with Elena, and it was really difficult for her to choose. Bonnie, her best friend, suggested her to throw a question to them, and she would choose the one who can solve it.

Suppose there is a tree with n vertices and n - 1 edges, and there is a value at each vertex. The root is vertex 1. Then for each vertex, could you tell me how many vertices of its subtree can be said to be co-prime with itself?
NOTES: Two vertices are said to be co-prime if their values' GCD (greatest common divisor) equals 1.
 
Input
There are multiply tests (no more than 8).
For each test, the first line has a number n (1n105) , after that has n1 lines, each line has two numbers a and b (1a,bn) , representing that vertex a is connect with vertex b. Then the next line has n numbers, the ith number indicates the value of the ith vertex. Values of vertices are not less than 1 and not more than 105 .
 
Output
For each test, at first, please output "Case #k: ", k is the number of test. Then, please output one line with n numbers (separated by spaces), representing the answer of each vertex.
 
Sample Input
   
   
   
   
5 1 2 1 3 2 4 2 5 6 2 3 4 5
 
Sample Output
   
   
   
   
Case #1: 1 1 0 0 0
 
Source
2015 ACM/ICPC Asia Regional Shanghai Online

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5468


题目大意:给一棵树,每个结点有一个值,现在求以每个结点为根的子树中与其互质的结点的个数

题目分析:如果能把数字都放在一个集合中,求集合中与一数字互质的数的个数就是裸的莫比乌斯反演了,但是这题是在树上,因此考虑DFS序,一个点从往下深搜遍历到回溯到自身的过程正好遍历了以当前点为根的整个子树,那么答案ans[x] = Σ(d|val[x]) mob[d] * num[d],mob[d]是d的莫比乌斯函数,num[d]是当前子树中d的个数(注d|val[x]),因此一开始我们还需要通过nlogn的筛子预处理一棵带有倍数关系的树,且只需以无平方因子数做叶结点,这棵树大概有70多万条边,注意对于结点x的DFS序[st,ed],可以利用前缀和性质等价成[1,ed] - [1,st],显然[1,st]是往下深搜的过程,而[1,ed]是回溯的过程,因此往下过程减,回溯时加,最后还有一个wa点,1的时候,注意1和本身也是互质的

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 1e5 + 5;
int const MAXM = 8e5;
int mob[MAX], p[MAX], head[MAXM], HEAD[MAX], val[MAX];
int seq[MAX << 1], ans[MAX], num[MAX];
bool noprime[MAX];
int n, cnt, dfn, CNT;

struct EDGE
{
    int to, next; 
}e[MAXM], E[MAX << 1];

void ADD(int u, int v)
{
    E[CNT].to = v;
    E[CNT].next = HEAD[u];
    HEAD[u] = CNT++;
}

void Add(int u, int v)
{
    e[cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt++;
}

void Mobius()
{
    int pnum = 0;
    mob[1] = 1;
    for(int i = 2; i < MAX; i++)
    {
        if(!noprime[i])
        {
            p[pnum ++] = i;
            mob[i] = -1;
        }
        for(int j = 0; j < pnum && i * p[j] < MAX; j++)
        {
            noprime[i * p[j]] = true;
            if(i % p[j] == 0)
            {
                mob[i * p[j]] = 0;
                break;
            }
            mob[i * p[j]] = -mob[i];
        }
    }
}

void DFS(int u, int fa)
{
    dfn ++;
    seq[dfn] = u;
    for(int i = HEAD[u]; i != -1; i = E[i].next)
    {
        int v = E[i].to;
        if(v != fa)
            DFS(v, u);
    }
    dfn ++;
    seq[dfn] = -u;
}

int main()
{
    Mobius();
    int nnn = 0;
    memset(head, -1, sizeof(head));
    cnt = 0;
    for(int i = 1; i <= 100000; i++)
        if(mob[i])
            for(int j = i; j <= 100000; j += i)
                Add(j, i);
    int ca = 1;
    while(scanf("%d", &n) != EOF)
    {
        memset(HEAD, -1, sizeof(HEAD));
        memset(num, 0, sizeof(num));
        memset(ans, 0, sizeof(ans));
        CNT = 0;
        dfn = 0;
        int u, v;
        for(int i = 1; i < n; i++)
        {
            scanf("%d %d", &u, &v);
            ADD(u, v);
            ADD(v, u);
        }
        for(int i = 1; i <= n; i++)
            scanf("%d", &val[i]);
        DFS(1, 0);
        for(int i = 1; i <= n; i++)
            ans[i] = (val[i] == 1);
        for(int i = 1; i <= dfn; i++)
        {
            if(seq[i] > 0)
            {
                for(int j = head[val[seq[i]]]; j != -1; j = e[j].next)
                {
                    int v = e[j].to;
                    num[v] ++;
                    ans[seq[i]] -= mob[v] * num[v];
                }
            }
            else
            {
                for(int j = head[val[-seq[i]]]; j != -1; j = e[j].next)
                {
                    int v = e[j].to;
                    ans[-seq[i]] += mob[v] * num[v];
                }
            }
        }
        printf("Case #%d:", ca ++);
        for(int i = 1; i <= n; i++)
            printf(" %d", ans[i]);
        printf("\n");
    }
}


你可能感兴趣的:(HDU,dfs序,莫比乌丝反演)