Puzzled Elena

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1247    Accepted Submission(s): 370

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 (1≤n≤105), after that has n−1 lines, each line has two numbers a and b (1≤a,b≤n), 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
 
题意:给定 n 个结点 n-1条边的一棵树 ,每个结点都有一个 value,问每个节点的子节点的value与其value互素的个数有多少?
题解:我们可以先预处理出 1 ~ 10^5 内所有整数的因子,然后进行 DFS,当进入结点 u 时,记录当前与 u 不互素的数的个数为 a ,出节点 u 时,记录这时与 u不互素的个数为b,那么 u 的子树中 与 value[u] 不互素的个数就为 b-a ,当前结点 u 的子节点结点个数为 s,那么与 其互素的数个数为 s - (b-a).这里用一个 cnt[i]数组记录当前含有因数 i 的结点的个数,每次退出结点 u 的时候要将含有其因子的数量累加.还有就是当 val[u] == 1时,他与本身互素,答案 +1 .
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = ;
vector <int> factor[N];
vector <int> edge[N];
void init(){
for(int i=;i<N;i++){
factor[i].clear();
int n = i;
for(int j=;j*j<=n;j++){
if(n%j==){
factor[i].push_back(j);
while(n%j==) n/=j;
}
}
if(n>) factor[i].push_back(n);
}
}
int cnt[N]; ///cnt[i]表示遍历到当前结点时候,含有因数i的结点个数。
int val[N];
int ans[N];
int solve(int n,int val){
int len = factor[n].size(),ans=;
for(int i=;i<(<<len);i++){
int odd = ;
int mul = ;
for(int j=;j<len;j++){
if((i>>j)&){
odd++;
mul*=factor[n][j];
}
}
if(odd&){
ans+=cnt[mul];
}
else ans-=cnt[mul];
cnt[mul]+=val; /// val = 1 代表退出当前结点时把因子加上
}
return ans;
}
int dfs(int u,int pre){
int L = solve(val[u],); ///第一次遍历到 u,拥有与 u 相同的因子个数
int s = ; ///s 代表当前结点下的子节点数目
for(int i=;i<edge[u].size();i++){
int v = edge[u][i];
if(v==pre) continue;
s+=dfs(v,u);
}
int R = solve(val[u],);
ans[u] = s - (R-L);
if(val[u]==) ans[u]++;
return s+;
}
int main()
{
init();
int n,t = ;
while(scanf("%d",&n)!=EOF){
memset(cnt,,sizeof(cnt));
for(int i=;i<=n;i++) edge[i].clear();
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
}
for(int i=;i<=n;i++){
scanf("%d",&val[i]);
}
dfs(,-);
bool flag = true;
printf("Case #%d: ",t++);
for(int i=;i<=n;i++){
if(!flag) printf(" ");
flag = false;
printf("%d",ans[i]);
}
printf("\n");
}
return ;
}

hdu 5468(dfs序+容斥原理)的更多相关文章

  1. HDU 3966 dfs序+LCA+树状数组

    题目意思很明白: 给你一棵有n个节点的树,对树有下列操作: I c1 c2 k 意思是把从c1节点到c2节点路径上的点权值加上k D c1 c2 k 意思是把从c1节点到c2节点路径上的点权值减去k ...

  2. HDU 5877 [dfs序][线段树][序]

    /* 题意: n个点的树,每个点给定一个权值,给定一个k,求任意一点的子树中,权值小于k/该点权值的点共有多少个. 思路: 1.很明显的子树的操作,应用dfs序. 2.比赛的时候傻逼了,一直在调划分树 ...

  3. hdu 5692(dfs序+线段树,好题)

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  4. HDU5468(dfs序+容斥原理)

    Puzzled Elena Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  5. HDU 5692 (DFS序+线段树)

    DFS获得从0到每一个顶点的距离,同时获得L和R数组.两数组为遍历时从i进入再从i出来的序列. #pragma comment(linker, "/STACK:1024000000,1024 ...

  6. Assign the task HDU - 3974 (dfs序 + 线段树)

    有一家公司有N个员工(从1到N),公司里每个员工都有一个直接的老板(除了整个公司的领导).如果你是某人的直接老板,那个人就是你的下属,他的所有下属也都是你的下属.如果你是没有人的老板,那么你就没有下属 ...

  7. dfs序题目练习

    参考博文:http://blog.csdn.net/qwe2434127/article/details/49819975 http://blog.csdn.net/qq_24489717/artic ...

  8. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  9. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

随机推荐

  1. [POI2005]Bank notes

    link 试题分析 我们发现此题是一个十分简单的多重背包.但是按照朴素写法会超时.所以要去考虑优化. 我们发现我们若$W=7$,可以拆成$1+2+4$,不用每次$1+1+1+1+1+1+1$,从$N$ ...

  2. Yura

    Portal --> broken qwq Description ​  给你一个长度为\(n\)的序列\(a\)和一个正整数\(k\),求满足如下条件的区间\([l,r]\)的数量:\((\s ...

  3. 【hash】【P5079】P5079 Tweetuzki 爱伊图

    Description Input 第一行两个正整数 \(r~,~c\),表示矩阵的行数和列数. 接下来 \(r\) 行,每行输入 \(c\) 个字符,用空格隔开,保证只含有 . 和 # 两种字符.输 ...

  4. Android缓存

    一个利用内存缓存和磁盘缓存图片的例子 public class BitmapCache { public static final String TAG = "debug"; pr ...

  5. Linux环境编译动态库和静态库总结

    对Linux环境动态库和静态库的一些基础知识做一些总结, 首先总结静态库的编译步骤. 1 先基于.cpp或者.c文件生成对应的.o文件 2将几个.o文件 使用ar -cr命令 生成libname.a文 ...

  6. 预处理 Gym - 101128H

    题目链接:http://codeforces.com/gym/101128 题目大意:给你一个区间[x,y],找出这个区间有多少个seldom的数字. seldom的数字定义如下:该数值的二进制数字符 ...

  7. Cloudera 安装

    参考网址: http://tcxiang.iteye.com/blog/2087597 http://archive.cloudera.com/cdh5/ http://archive.clouder ...

  8. rxjs自定义operator

    rxjs自定义operator

  9. Try finally的一个实验和为什么避免重载 finalize()方法--例子

    public class TryFinallTest { public TryFinallTest(){ } public void runSomething(String str){ System. ...

  10. js中字符串和json数组的相互转换

    //示例 var a={"name":"tom","sex":"男","age":"24& ...