Justified Jungle
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 ;
}
Justified Jungle的更多相关文章
- uva live 6190 Beautiful Spacing (二分法+dp试 基于优化的独特性质)
I - Beautiful Spacing Time Limit:8000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Jungle Roads
Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...
- EDdb 是ED数据
eddb 是ED数据统计汇总软件的简称,用于统计汇总企事业单位的各类信息数据. 采用Excel界面,操作简单. 对各类信息数据,均可以自定义数据格式,通过internet联网,收集各类信息数据,并通 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- ios CoreBluetooth 警告 is being dealloc'ed while pending connection
ios CoreBluetooth 警告 is being dealloc'ed while pending connection CoreBluetooth[WARNING] <CBPerip ...
- ed编辑器使用
evilxr@IdeaPad:/tmp$ ed aa.c 0 a enter another words hello nice www.evilxr.com . w aa.c 46 q a 表示添加内 ...
- Linux ed命令
$ ed <- 激活 ed 命令 a <- 告诉 ed 我要编辑新文件 My name is Titan. <- 输入第 ...
随机推荐
- python requests 的cookie 操作
结论: 1.requests模块的请求和响应分别有cookie对象. 可以通过此对象设置和获取cookie. 2.通过在requests.get,requests.post等方法请求中传入cookie ...
- Uva 11520 - Fill the Square 贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 使用laravel搭建CURD后台页面
配置即一切 一切皆于需求,后台从0开始搭建,但是写了一两个页面后发现太多的是对单表的增删改查操作,于是就想到了,能不能做一个快速搭建的后台.想到一句话,配置即一切.如果一个CURD后台能只进行配置就自 ...
- python中字典常用的方法
#定义一个空字典: a={ } 定义一个字典: d={'age':18} #增加一个元素: d['age']=20 d[k]=v d.setdefault('age',18) d.setde ...
- 每天CSS学习之top/left/right/bottom
top:值域是数值或百分比,正负都可以.该值表示 距离顶部有多少像素.例如top:10px:即距离顶部10个像素. left/right/bottom与top如出一辙,只是方向不一样而已. 这些属性一 ...
- Edit Distance II
Given two strings S and T, determine if they are both one edit distance apart. Example Given s = &qu ...
- Cracking The Coding Interview 3.2
//How would you design a stack which, in addition to push and pop, also has a function min which ret ...
- 2.6 C++通过引用来传递和返回类对象
参考:http://www.weixueyuan.net/view/6338.html 总结: C++语言中,由类声明的对象,和其它类型声明的变量一样,同样可以通过传值.引用和指针的方式作为函数的参数 ...
- 1080 MOOC期终成绩
对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分,然后总评获得不少于60分( ...
- 玩转X-CTR100 l STM32F4 l FPU单精度浮点性能测试
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍X-CTR100控制器如何开启ST ...