hdu 5329 Question for the Leader
来源:互联网 发布:c语音编程招聘 编辑:程序博客网 时间:2024/06/06 20:28
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5329
题目大意:n个点n条边的联通图,无自环,无重边,把所有点拆分成n/k个k个点的集合,k整除n,使得集合内的点两两路径上的点都在集合内。问有多少个k满足这个条件。n<1e5
解题思路:对于一棵树来说,如果能够分成n/k个k个点的集合,那么任取一个点为根,所有子树的节点个数是k的倍数有n/k个,应该比较好理解,画一个图就知道了:前提是一棵树,并且能够分成n/k个k个点的集合,那么,你选的根一定只属于某一个集合,对其他的集合并没有破坏!所以有n/k个子树的节点的个数是k的倍数 是显然的了。
我们可以考虑将原来的环加外向树,给环上的点标号为1..m,拆掉1 和 m的边,使其变为一棵以1为根的树,然后变成了求这棵树有多少个子树的节点个数是k的倍数,对于不是圆环上的点,由于他们的结构不会改变,我们可以事先统计出来,对于圆环上的点,节点i的子树的节点个数,都是sigma(s i..m)si表示节点i的子树的节点个数 不包括圆环上的节点。记录一个s的前缀和,sigma(s i..m)=a[m]-a[i-1],对于a数组模k, 如果a[m]-a[i-1]是k的倍数 当且仅当 a[m]=a[i-1] 由于a[m]%k=0,所以需要统计 a[0...m-1]中0的个数,用cnt[i]b表示i出现的次数。顺次的枚举拆掉哪条边,更新cnt数组,求得最大的出现个数,加上预处理的是k的倍数的节点的个数,看是不是等于n/k,加到答案里面
#pragma comment(linker,"/STACK:102400000,102400000")#include<stdio.h>#include<iostream>#include<string.h>#include<math.h>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<string>#define ll long long#define db double#define PB push_back#define lson k<<1#define rson k<<1|1using namespace std;const int N = 100005;int head[N], nxt[N << 1], to[N << 1], nedge;bool on[N];int c[N], num[N], sz[N];void init(int n){ for(int i = 1; i <= n; i++) head[i] = -1, on[i] = false, sz[i] = 0; nedge = 0;}void addedge(int u, int v){ to[nedge] = v, nxt[nedge] = head[u], head[u] = nedge++;}bool oncir;int st, lc;void dfs(int k, int fa){ on[k] = true; for(int i = head[k]; i >= 0; i = nxt[i]) { if(to[i] != fa) { if(on[to[i]]) { c[++lc] = k; oncir = true; st = to[i]; return; } else { dfs(to[i], k); if(oncir) break; } } } if(oncir) c[++lc] = k; on[k] = oncir; if(k == st) oncir = false;}int cnt[N];int calc(int k){ for(int i = 0; i < k; i++) cnt[i] = 0; int s(0); for(int i = 0; i < lc; i++) { s += num[c[i]]; s %= k; cnt[s]++; } int p(0); int res(cnt[p]); for(int i = lc; i > 1; i--) { p = ((p - num[c[i]]) % k + k) % k; res = max(res, cnt[p]); } return res;}void getK(int k, int fa){ num[k] = 1; for(int i = head[k]; i >= 0; i = nxt[i]) { if(to[i] != fa && !on[to[i]]) { getK(to[i], k); num[k] += num[to[i]]; } } if(fa) sz[num[k]]++;}void sum(){ for(int i = 1; i <= lc; i++) { getK(c[i], 0); }}int gets(int k,int n){ int res(0); for(int i=k;i<=n;i+=k) res+=sz[i]; return res;}int main(){#ifdef PKWV freopen("in.in", "r", stdin);// freopen("1003.k.out","w",stdout);#endif // PKWV int n; while(scanf("%d", &n) + 1) { init(n); for(int i = 1; i <= n; i++) { int v; scanf("%d", &v); addedge(i, v), addedge(v, i); } oncir = false, lc = 0; dfs(1, 0); sum(); int ans(0); for(int i = 1; i * i <= n; i++) { if(n % i == 0) { if(gets(i,n) + calc(i) == n / i) { ans++; } if(i != n / i) { if(gets(n / i,n) + calc(n / i) == i) { ans++; } } } } printf("%d\n", ans); } return 0;}
0 0
- hdu 5329 Question for the Leader
- hdu 5329 Question for the Leader
- HDOJ 5329 Question for the Leader
- hdu 5329 Question for the Leader 枚举,划分图为k个联通的子图
- 2015多校第四场1003 hdu 5329 Question for the Leader
- hdu5329(2015多校4)--Question for the Leader
- The Question
- Thanks for the question regarding "About utl_smtp", version 8.1.7
- Exception: This server is not the leader for that topic-partition.
- nfs for Android question
- question for write
- The FOUR_QUEEN question solutions
- jd for webservice developer / leader
- A Boring Question HDU
- The answer to the question
- HDU 6002 Game Leader(贪心)
- wallacel -- Thanks for the question regarding "IN & EXISTS", version 8.1.6
- question:setValue:forUndefinedKey: this class is not key value coding-compliant for the key
- net.sf.json.JSONObject需要jar包
- Application的生命周期简介
- Java 提取文件编码
- Apache与Nginx的优缺点比较
- phonegap中 app 开发的那些坑~~!!
- hdu 5329 Question for the Leader
- [Github开源库PinterestLikeAdapterView]--Android瀑布流的实现
- 【翻译自mos文章】rman 标准版和企业版的兼容性
- 寻找素数
- 使用GDB调试程序
- 归并排序模板
- 初学JAVA简谈字符串String存储的以及字符串值的比较(==运算符以及equals())
- Web应用的组件化开发
- hdoj-5112-A Curious Matt