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.

 // 题目大意:删去k条边,树变为相等个点的连通分量,求所有正整数k。

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 1000006
int head[maxn],cnt,siz[maxn],v[maxn],n;
struct edge{
int to,nxt;
}e[maxn<<];
void add_edge(int u,int v){
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
void dfs(int u,int fa){
siz[u]=;
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
if(v==fa) continue;
dfs(v,u);
siz[u]+=siz[v];
}
v[siz[u]]++;
}
bool check(int x){
x++;
if(n%x) return ;
int w=n/x,sum=;
for(int i=w;i<=n;i+=w) sum+=v[i];
return sum==x;
}
int main(){
memset(head,-,sizeof head);
scanf("%d",&n);
for(int i=;i<n-;i++){
int a,b;
scanf("%d%d",&a,&b);
add_edge(a,b);
add_edge(b,a);
}
dfs(,-);
for(int i=;i<=n;i++){
if(check(i)) printf("%d ",i);
}
return ;
}

最新文章

  1. querystring 解析url 查询字符串
  2. KVM 介绍(4):I/O 设备直接分配和 SR-IOV [KVM PCI/PCIe Pass-Through SR-IOV]
  3. NLP的两种工具的java版使用:复旦FudanNLP,中科院计算所ICTCLAS2013
  4. python 应用xml.dom.minidom读xml
  5. Jqeury获取table当前行与指定列
  6. 前后端分离--构建前端Mock Server--windows部署rap
  7. HTML TAG FROM MDN
  8. log4j 详解
  9. Charles安装破解及使用
  10. MacRuby 0.3发布,支持Interface Builder,和创建GUI用的HotCocoa
  11. [BZOJ1925][SDOI2010]地精部落(DP)
  12. spark使用udf给dataFrame新增列
  13. 解读经典《C#高级编程》第七版 Page38-45.核心C#.Chapter2
  14. K8S入门学习
  15. python - 代码缩进
  16. faster-rcnn自己的理解总结(包括它的前世今身R-CNN和fast R-CNN)
  17. 【redis专题(5)】命令语法介绍之sets
  18. [No0000B8]WPF或Winform调用系统Console控制台显示信息
  19. P3924 康娜的线段树(期望)
  20. WinterCamp2017吃饭睡觉记

热门文章

  1. oracle中查询当前系统时间用到的dual是什么?
  2. shell shell基本概述
  3. Uboot中汇编指令
  4. datatime 模块
  5. java高级⑴
  6. 《Python》常用模块之collections模块
  7. 从头入手jenkins
  8. python验证代理IP
  9. 7 Serial Configuration 理解(二)
  10. day 67 django 之ORM 增删改查基础