hdu 5468(dfs序+容斥原理)
Puzzled Elena
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1247 Accepted Submission(s): 370
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.
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.
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.
1 2
1 3
2 4
2 5
6 2 3 4 5
#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序+容斥原理)的更多相关文章
- HDU 3966 dfs序+LCA+树状数组
题目意思很明白: 给你一棵有n个节点的树,对树有下列操作: I c1 c2 k 意思是把从c1节点到c2节点路径上的点权值加上k D c1 c2 k 意思是把从c1节点到c2节点路径上的点权值减去k ...
- HDU 5877 [dfs序][线段树][序]
/* 题意: n个点的树,每个点给定一个权值,给定一个k,求任意一点的子树中,权值小于k/该点权值的点共有多少个. 思路: 1.很明显的子树的操作,应用dfs序. 2.比赛的时候傻逼了,一直在调划分树 ...
- hdu 5692(dfs序+线段树,好题)
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU5468(dfs序+容斥原理)
Puzzled Elena Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- HDU 5692 (DFS序+线段树)
DFS获得从0到每一个顶点的距离,同时获得L和R数组.两数组为遍历时从i进入再从i出来的序列. #pragma comment(linker, "/STACK:1024000000,1024 ...
- Assign the task HDU - 3974 (dfs序 + 线段树)
有一家公司有N个员工(从1到N),公司里每个员工都有一个直接的老板(除了整个公司的领导).如果你是某人的直接老板,那个人就是你的下属,他的所有下属也都是你的下属.如果你是没有人的老板,那么你就没有下属 ...
- dfs序题目练习
参考博文:http://blog.csdn.net/qwe2434127/article/details/49819975 http://blog.csdn.net/qq_24489717/artic ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
随机推荐
- linux内核分析第3章&第18章读书笔记
linux内核分析第3章&第18章读书笔记 第三章 进程管理 进程:处于执行期的程序(目标码存放在某种存储介质上) 包含资源:可执行程序代码,打开的文件,挂起的信号,内核内部数据,处理器状态, ...
- golang字符串常用函数
package utils import "fmt" import "strconv" import "strings" var str s ...
- 线性判别分析(Linear Discriminant Analysis)
1. 问题 之前我们讨论的PCA.ICA也好,对样本数据来言,可以是没有类别标签y的.回想我们做回归时,如果特征太多,那么会产生不相关特征引入.过度拟合等问题.我们可以使用PCA来降维,但PCA没有将 ...
- mysql group_concat方法用法
目前有三张表: 课程表: course,里面有student_id 学生表: student,里面有 name 字段 中间表(课程和学生一对多): course_student, 里面有 co ...
- HDU 6194 后缀数组
string string string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 预处理 Gym - 101128H
题目链接:http://codeforces.com/gym/101128 题目大意:给你一个区间[x,y],找出这个区间有多少个seldom的数字. seldom的数字定义如下:该数值的二进制数字符 ...
- CF540 C BFS 水
'.'->'X' 前者走后变成后者,后者除了是终点不能再走.初始位置是X很傻的以为这样从初始点走出去后初始位置就变成不能走了,实际上是还能走一次的. 其他就是BFS,路上记得把路变成X就好了 太 ...
- CSS属性的私有前缀
在CSS属性能中,我们常常能看到-webkit-,-moz-之类的前缀,这种就叫做浏览器私有前缀,是浏览器对于新CSS属性的一个提前支持.-webkit-是webkit内核的,-moz-是Firefo ...
- Lua的工具资源3
[LuaSrcDiet] (5.0.2) - 通过删除不必要的空白和注释缩减Lua文件的大小. [LuaProfiler] (5.0) - 一个用来查找Lua应用瓶颈的工具time profiler ...
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...