Puzzled Elena

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

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
 
思路:该题涉及到一个典型问题.问x与S中有多少个数不互素。解决办法是将S中所有元素依次进行两个步骤:①将元素进行质因数分解。②将质因数可能产生的乘积的出现次数加1。最后将x进行质因数分解,利用容斥原理求解。具体方案见代码。
容斥原理在OJ中常解决两个典型问题:①求S中有多少个数与x不互素。②求1~m中有多少个数与n不互素。
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN=;
typedef long long LL;
vector<LL> divisor[MAXN];
void prep()
{
for(LL e=;e<MAXN;e++)
{
LL x=e;
for(LL i=;i*i<=x;i++)
{
if(x%i==)
{
divisor[e].push_back(i);
while(x%i==) x/=i;
}
}
if(x>) divisor[e].push_back(x);
}
} vector<int> arc[MAXN];
int n,val[MAXN];
int cnt[MAXN],res[MAXN];
int cal(int n,int type)//求集合S中与n不互素的数的个数
{
int ans=;
for(LL mark=;mark<(<<divisor[n].size());mark++)
{
LL odd=;
LL mul=;
for(LL i=;i<divisor[n].size();i++)
{
if(mark&(<<i))
{
odd++;
mul*=divisor[n][i];
}
}
if(odd&) ans+=cnt[mul];
else ans-=cnt[mul];
cnt[mul]+=type;
}
return ans;
}
int dfs(int u,int fa)
{
int pre=cal(val[u],);
int s=;
for(int i=;i<arc[u].size();i++)
{
int to=arc[u][i];
if(to!=fa)
{
s+=dfs(to,u);
}
}
int post=cal(val[u],);
res[u]=s-(post-pre);//以u为根的子树结点数目-(遍历u之前与u不互素的结点数目-遍历u之后与u不互素的结点数目)
if(val[u]==) res[u]++;//若u的值为1,那么u与自身互素
return s+;
}
int main()
{
prep();
int cas=;
while(scanf("%d",&n)!=EOF)
{
memset(cnt,,sizeof(cnt));
for(int i=;i<=n;i++) arc[i].clear();
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
arc[u].push_back(v);
arc[v].push_back(u);
}
for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
}
dfs(,-);
printf("Case #%d: ",++cas);
for(int i=;i<n;i++)
{
printf("%d ",res[i]);
}
printf("%d\n",res[n-]);
}
return ;
}
 

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

  1. hdu 5468(dfs序+容斥原理)

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

  2. dfs序题目练习

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

  3. Codeforces 916E(思维+dfs序+线段树+LCA)

    题面 传送门 题目大意:给定初始根节点为1的树,有3种操作 1.把根节点更换为r 2.将包含u,v的节点的最小子树(即lca(u,v)的子树)所有节点的值+x 3.查询v及其子树的值之和 分析 看到批 ...

  4. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  5. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  6. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  7. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

  8. 【BZOJ-1146】网络管理Network DFS序 + 带修主席树

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3495  Solved: 1032[Submi ...

  9. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

随机推荐

  1. 谈Swift中的访问控制

    访问控制(Access Control) 访问控制可以限定其他源文件或模块中的代码对你的代码的访问级别.这个特性可以让我们隐藏代码的一些实现细节,并且可以指定一些代码和访问和使用的优先接口. 你可以明 ...

  2. Android系统--Binder系统具体框架分析(一)

    Binder系统具体框架分析(一) 一.Binder系统核心框架 1. IPC:Inter-Process Communication, 进程间通信 A进程将数据原原本本发送B进程,主要负责进程间数据 ...

  3. SSM mapper.xml

    MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大约 ...

  4. 初识Spring security-无Security的SpringMVC

    百度百科定义: Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了 ...

  5. springboot---aop切片编程

    1.介绍 面向切面编程,关注点代码与业务代码分离,就是给指定方法执行前执行后..插入重复代码 关注点:重复代码 切面:被切面的类 切入点:执行目标对象方法,动态植入切片代码 2.部署步骤 2.1:添加 ...

  6. java深入探究11-基础加强

    1. ? extends String:String 子类;? super String:String 父类 2.反射->参数化类型表示 ParameteredType:参数化类型表示,就是获得 ...

  7. 如何使用REST请求风格

    REST:即 Representational State Transfer.(资源)表现层状态转化.是目前最流行的一种互联网软件架构. 它结构清晰.符合标准.易于理解.扩展方便, 所以正得到越来越多 ...

  8. ROS配置C++14环境

    ROS配置C++11环境 打开Terminal, sudo vim /opt/ros/<yourversion>/share/catkin/cmake/toplevel.cmake 比如: ...

  9. 浏览器指纹--纯js拿到浏览器指纹

    序言: 前两天有接收到一下问题,如何拿到浏览器指纹中的位置信息和CPU,在这之前完全没有接触过浏览器指纹,抱着学习和好奇的心态,就去网上查了大量的资料.下面我将学习过程和成果贴出来给大家. 步骤 1. ...

  10. java:jsp: ResourceBundle国际化多语言

    java提供了一个资源类java.util.ResourceBundle来试下多国语言版本.其实ResourceBundle只是一个抽象的类,她有两个子类:ListResourceBundle,和,P ...