Problem J: Justified Jungle

Time limit: 6 s Memory l

imit: 512 MiB

As you probably know, a tree is a graph consisting of n nodes and n−1 undirected edges in which any two nodes are connected by exactly one path. A forest is a graph consisting of one or more trees. In other words, a graph is a forest if every connected component is a tree. A forest is justified if all connected components have the same number of nodes. Given a tree G consisting of n nodes, find all positive integers k such that a justified forest can be obtained by erasing exactly k edges from G. Note that erasing an edge never erases any nodes. In particular when we erase all n−1 edges from G, we obtain a justified forest consisting of n one-node components.
Input The first line contains an integer n (2≤ n ≤1000000) — the number of nodes in G. The k-th of the following n−1 lines contains two different integers ak and bk (1≤ ak,bk ≤n) — the endpoints of the k-th edge.
Output
The first line should contain all wanted integers k, in increasing order.
Example
input
8 1 2 2 3 1 4 4 5 6 7 8 3 7 3
output
1 3 7
Figures depict justified forests obtained by erasing 1, 3 and 7 edges from the tree in the example input.

  1. // 题目大意:删去k条边,树变为相等个点的连通分量,求所有正整数k。
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. typedef long long ll;
  6. #define maxn 1000006
  7. int head[maxn],cnt,siz[maxn],v[maxn],n;
  8. struct edge{
  9. int to,nxt;
  10. }e[maxn<<];
  11. void add_edge(int u,int v){
  12. e[cnt].to=v;
  13. e[cnt].nxt=head[u];
  14. head[u]=cnt++;
  15. }
  16. void dfs(int u,int fa){
  17. siz[u]=;
  18. for(int i=head[u];i!=-;i=e[i].nxt){
  19. int v=e[i].to;
  20. if(v==fa) continue;
  21. dfs(v,u);
  22. siz[u]+=siz[v];
  23. }
  24. v[siz[u]]++;
  25. }
  26. bool check(int x){
  27. x++;
  28. if(n%x) return ;
  29. int w=n/x,sum=;
  30. for(int i=w;i<=n;i+=w) sum+=v[i];
  31. return sum==x;
  32. }
  33. int main(){
  34. memset(head,-,sizeof head);
  35. scanf("%d",&n);
  36. for(int i=;i<n-;i++){
  37. int a,b;
  38. scanf("%d%d",&a,&b);
  39. add_edge(a,b);
  40. add_edge(b,a);
  41. }
  42. dfs(,-);
  43. for(int i=;i<=n;i++){
  44. if(check(i)) printf("%d ",i);
  45. }
  46. return ;
  47. }

Justified Jungle的更多相关文章

  1. uva live 6190 Beautiful Spacing (二分法+dp试 基于优化的独特性质)

    I - Beautiful Spacing Time Limit:8000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

  2. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  3. Jungle Roads[HDU1301]

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  4. Jungle Roads

    Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...

  5. EDdb 是ED数据

    eddb  是ED数据统计汇总软件的简称,用于统计汇总企事业单位的各类信息数据. 采用Excel界面,操作简单. 对各类信息数据,均可以自定义数据格式,通过internet联网,收集各类信息数据,并通 ...

  6. POJ 1251 Jungle Roads (prim)

    D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Su ...

  7. ios CoreBluetooth 警告 is being dealloc'ed while pending connection

    ios CoreBluetooth 警告 is being dealloc'ed while pending connection CoreBluetooth[WARNING] <CBPerip ...

  8. ed编辑器使用

    evilxr@IdeaPad:/tmp$ ed aa.c 0 a enter another words hello nice www.evilxr.com . w aa.c 46 q a 表示添加内 ...

  9. Linux ed命令

    $ ed              <- 激活 ed 命令  a                 <- 告诉 ed 我要编辑新文件  My name is Titan. <- 输入第 ...

随机推荐

  1. 运维相关 docker

  2. \x 和 0x 的区别

    1.0x 表示整型数值 (十六进制) char c = 0x42; 表示的是一个数值(字母B对应的ASCII码——  66),可以认为等价于: int c = 0x42; 2.\x42用于字符表达,或 ...

  3. CString 转换为 wchar_t *

    1.将CString转换为const char* CString str = _T("231222"); std::string strDp = CStringA(str);  / ...

  4. redux源码解析(深度解析redux+异步demo)

    redux源码解析 1.首先让我们看看都有哪些内容 2.让我们看看redux的流程图 Store:一个库,保存数据的地方,整个项目只有一个 创建store Redux提供 creatStore 函数来 ...

  5. Weka里如何将arff文件或csv文件批量导入MySQL数据库(六)

    这里不多说,直接上干货! 前提博客是 Weka中数据挖掘与机器学习系列之数据格式ARFF和CSV文件格式之间的转换(四) 1.将arff文件批量导入MySQL数据库 我在这里,arff文件以Weka安 ...

  6. learning ddr mode register MR2

  7. IOS应用内支付IAP从零开始详解

    前言 什么是IAP,即in-app-purchase 这几天一直在搞ios的应用内购,查了很多博客,发现几乎没有一篇博客可以完整的概括出所有的点,为了防止大伙多次查阅资料,所以写了这一篇博客,希望大家 ...

  8. Linux下IP的存储位置

    在linux下,配置多个IP的话,通常是eth0... eth0. ..eth0.x等等, 那么如果要配置大量的IP呢,这么配置也是可以的,但是繁琐,虽说这种情况很少. 对于添加大量IP,有一定规定的 ...

  9. 洛谷 P4515 [COCI2009-2010#6] XOR

    题意 平面直角坐标系中有一些等腰直角三角形,且直角边平行于坐标轴,直角顶点在右下方,求奇数次被覆盖的面积.N<=10.输入为x,y,r,分别表示三角形顶点的坐标与三角形的边长. 如: 总面积为0 ...

  10. 解决了好几个软件的构建问题,在解决部署问题,bluemix部署

    https://www.puteulanus.com/archives/838#comment-961新版 Bluemix 一键搭建 SS 脚本 https://blog.feixueacg.com/ ...