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不互素。
  1. #include <cstdio>
  2. #include <vector>
  3. #include <cstring>
  4. using namespace std;
  5. const int MAXN=;
  6. typedef long long LL;
  7. vector<LL> divisor[MAXN];
  8. void prep()
  9. {
  10. for(LL e=;e<MAXN;e++)
  11. {
  12. LL x=e;
  13. for(LL i=;i*i<=x;i++)
  14. {
  15. if(x%i==)
  16. {
  17. divisor[e].push_back(i);
  18. while(x%i==) x/=i;
  19. }
  20. }
  21. if(x>) divisor[e].push_back(x);
  22. }
  23. }
  24.  
  25. vector<int> arc[MAXN];
  26. int n,val[MAXN];
  27. int cnt[MAXN],res[MAXN];
  28. int cal(int n,int type)//求集合S中与n不互素的数的个数
  29. {
  30. int ans=;
  31. for(LL mark=;mark<(<<divisor[n].size());mark++)
  32. {
  33. LL odd=;
  34. LL mul=;
  35. for(LL i=;i<divisor[n].size();i++)
  36. {
  37. if(mark&(<<i))
  38. {
  39. odd++;
  40. mul*=divisor[n][i];
  41. }
  42. }
  43. if(odd&) ans+=cnt[mul];
  44. else ans-=cnt[mul];
  45. cnt[mul]+=type;
  46. }
  47. return ans;
  48. }
  49. int dfs(int u,int fa)
  50. {
  51. int pre=cal(val[u],);
  52. int s=;
  53. for(int i=;i<arc[u].size();i++)
  54. {
  55. int to=arc[u][i];
  56. if(to!=fa)
  57. {
  58. s+=dfs(to,u);
  59. }
  60. }
  61. int post=cal(val[u],);
  62. res[u]=s-(post-pre);//以u为根的子树结点数目-(遍历u之前与u不互素的结点数目-遍历u之后与u不互素的结点数目)
  63. if(val[u]==) res[u]++;//若u的值为1,那么u与自身互素
  64. return s+;
  65. }
  66. int main()
  67. {
  68. prep();
  69. int cas=;
  70. while(scanf("%d",&n)!=EOF)
  71. {
  72. memset(cnt,,sizeof(cnt));
  73. for(int i=;i<=n;i++) arc[i].clear();
  74. for(int i=;i<n-;i++)
  75. {
  76. int u,v;
  77. scanf("%d%d",&u,&v);
  78. arc[u].push_back(v);
  79. arc[v].push_back(u);
  80. }
  81. for(int i=;i<=n;i++)
  82. {
  83. scanf("%d",&val[i]);
  84. }
  85. dfs(,-);
  86. printf("Case #%d: ",++cas);
  87. for(int i=;i<n;i++)
  88. {
  89. printf("%d ",res[i]);
  90. }
  91. printf("%d\n",res[n-]);
  92. }
  93. return ;
  94. }
 

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. 20165101刘天野 2017-2018-2 《Java程序设计》第7周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十一章JDBC与MySQL数据库 JDBC简介 JDBC(Java Databas ...

  2. nodejs/REPL环境命令行操作命令

    1,输入node 进入node[REPL]环境 2,按两次[ctrl+c]退出node[REPL]环境 3,上箭头会查找上次输入的命令 4,cls清屏 5,tab键会自动补全路径 6,REPL环境

  3. OC_内存管理

      引言:      1.OC中的对象都是分配在堆中的                声明对象的格式:                     Person *person = [Person new ...

  4. C# 利用TTS实现文本转语音

    TSS(Text To Speech),语音朗读文本的技术,在Windows下台下,微软给我们提供了一套API接口(Speech API).金山词霸的朗读功能就是用的这个接口. WindowsXP自带 ...

  5. java.lang.NullPointerException报错的几种情况

    java.lang.NullPointerException报错的几种情况: 1.字符串变量未初始化: 2.接口类型的对象没有用具体的类初始化,比如: List stuList :这种情况就会报空指针 ...

  6. 【P1947】笨笨当粉刷匠(DP+前缀和)

    这个题乍一看觉得挺简单的,事实上却完全不是.首先,这个题看上去无脑直接刷就可以然而因为刷的次数远远大于木板的个数所以不行,然后开始考虑DP,自己一开始是这么想的,如果用f[t][i][j]表示刷t次时 ...

  7. 基于netty的异步http请求

    package com.pt.utils; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; im ...

  8. Apache Phoenix数据类型

    数据类型 Java Map 占用大小 (byte) 范围 INTEGER java.lang.Integer 4 -2147483648 to 2147483647 UNSIGNED_INT java ...

  9. Elasticsearch 存储模型

  10. Spring初学之使用外部配置文件dataSource

    一.在Spring的基础上还要另外导入c3p0包和mysql的驱动包. 二.配置文件, jdbc.propertices:这里只做了一些简单配置 user=root password=123 driv ...