HDU5468(dfs序+容斥原理)
Puzzled Elena
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1162 Accepted Submission(s): 339
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.
- #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序+容斥原理)的更多相关文章
- hdu 5468(dfs序+容斥原理)
Puzzled Elena Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- dfs序题目练习
参考博文:http://blog.csdn.net/qwe2434127/article/details/49819975 http://blog.csdn.net/qq_24489717/artic ...
- Codeforces 916E(思维+dfs序+线段树+LCA)
题面 传送门 题目大意:给定初始根节点为1的树,有3种操作 1.把根节点更换为r 2.将包含u,v的节点的最小子树(即lca(u,v)的子树)所有节点的值+x 3.查询v及其子树的值之和 分析 看到批 ...
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- 【BZOJ-1146】网络管理Network DFS序 + 带修主席树
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3495 Solved: 1032[Submi ...
- 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
随机推荐
- 20165101刘天野 2017-2018-2 《Java程序设计》第7周学习总结
#20165101刘天野 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十一章JDBC与MySQL数据库 JDBC简介 JDBC(Java Databas ...
- nodejs/REPL环境命令行操作命令
1,输入node 进入node[REPL]环境 2,按两次[ctrl+c]退出node[REPL]环境 3,上箭头会查找上次输入的命令 4,cls清屏 5,tab键会自动补全路径 6,REPL环境
- OC_内存管理
引言: 1.OC中的对象都是分配在堆中的 声明对象的格式: Person *person = [Person new ...
- C# 利用TTS实现文本转语音
TSS(Text To Speech),语音朗读文本的技术,在Windows下台下,微软给我们提供了一套API接口(Speech API).金山词霸的朗读功能就是用的这个接口. WindowsXP自带 ...
- java.lang.NullPointerException报错的几种情况
java.lang.NullPointerException报错的几种情况: 1.字符串变量未初始化: 2.接口类型的对象没有用具体的类初始化,比如: List stuList :这种情况就会报空指针 ...
- 【P1947】笨笨当粉刷匠(DP+前缀和)
这个题乍一看觉得挺简单的,事实上却完全不是.首先,这个题看上去无脑直接刷就可以然而因为刷的次数远远大于木板的个数所以不行,然后开始考虑DP,自己一开始是这么想的,如果用f[t][i][j]表示刷t次时 ...
- 基于netty的异步http请求
package com.pt.utils; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; im ...
- Apache Phoenix数据类型
数据类型 Java Map 占用大小 (byte) 范围 INTEGER java.lang.Integer 4 -2147483648 to 2147483647 UNSIGNED_INT java ...
- Elasticsearch 存储模型
- Spring初学之使用外部配置文件dataSource
一.在Spring的基础上还要另外导入c3p0包和mysql的驱动包. 二.配置文件, jdbc.propertices:这里只做了一些简单配置 user=root password=123 driv ...