题意:

  给出一棵树,删掉其中一些边,要求生成的每个子树节点数一样。输出所有可以删掉的边数。

题解:

  以节点1为根,预处理每个子树的大小。对于每个n的因数x,还需满足子树为他倍数的点够n/x个,那么删的边数就为n/x-1。

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+;
int n;
int u, v;
int sz[N], vis[N];
int tot;
int head[N], point[N<<], nxt[N<<];
vector<int> ans;
void add_edge(int u, int v) {
point[++tot] = v;
nxt[tot] = head[u];
head[u] = tot;
}
void dfs(int u, int pre) {
sz[u] = ;
for(int i = head[u]; i; i = nxt[i]) {
if(point[i]==pre) continue;
dfs(point[i], u);
sz[u] += sz[point[i]];
}
vis[sz[u]]++;
}
bool check(int x) {
int cnt = ;
for(int i = x; i <= n; i+=x) cnt += vis[i];
return cnt==n/x;
}
int main() {
scanf("%d", &n);
ans.push_back(n-);
for(int i = ; i < n; i++) {
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
dfs(, );
int up = sqrt(n);
for(int i = ; i <= up; i++) {
if(!(n % i)) {
if(check(i)) ans.push_back(n/i-);
if(i*i!=n && check(n/i)) ans.push_back(i-);
}
}
sort(ans.begin(), ans.end());
int len = ans.size();
for(int i = ; i < len; i++) {
printf("%d", ans[i]);
if(i!=len-) printf(" ");
}
}

GYM - 101620 J.Justified Jungle的更多相关文章

  1. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  2. 【codeforces.com/gym/100240 J】

    http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...

  3. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  4. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  5. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  6. codeforces Gym 100500 J. Bye Bye Russia

    Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...

  7. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  8. Gym - 100187J J - Deck Shuffling —— dfs

    题目链接:http://codeforces.com/gym/100187/problem/J 题目链接:问通过洗牌器,能否将编号为x的牌子转移到第一个位置? 根据 洗牌器,我们可以知道原本在第i位置 ...

  9. Gym 100801 J. Journey to the “The World’s Start” DP+单调队列优化+二分

    http://codeforces.com/gym/100801 题目大意:有从左到右有n个车站,有n-1种车票,第i种车票一次最多可以坐 i 站(1<=i<=n)   每种票有固定的价钱 ...

随机推荐

  1. windows tensorflow 简单安装

    需要64位系统windows, 下载64位的python3.5 官网下载地址:https://www.python.org/downloads/release/python-352/ 百度云下载地址: ...

  2. python中上双互斥锁的线程执行流程

    import threading def sing(): print('进入sing -----------------') for i in range(3): print('进入sing循环 -- ...

  3. oracle之bitmap索引

    oracle常见的索引是BTree索引和Bitmap索引. BTree索引特点: 默认索引 适合大量增删改查 不能用or操作符 适合高基数的列(即唯一值多) 创建sql:create index li ...

  4. 线程、进程、协程和GIL(三)

    上一篇文章介绍了:创建线程的两种方式.Event对象判断线程是否启动.利用信号量控制线程并发. 博客链接:线程.进程.协程和GIL(二) 这一篇来说说线程间通信的那些事儿: 一个线程向另一个线程发送数 ...

  5. Atlantis HDU - 1542

    Problem Description There are several ancient Greek texts that contain descriptions of the fabled is ...

  6. Hive 文件格式 & Hive操作(外部表、内部表、区、桶、视图、索引、join用法、内置操作符与函数、复合类型、用户自定义函数UDF、查询优化和权限控制)

    本博文的主要内容如下: Hive文件存储格式 Hive 操作之表操作:创建外.内部表 Hive操作之表操作:表查询 Hive操作之表操作:数据加载 Hive操作之表操作:插入单表.插入多表 Hive语 ...

  7. MyBatis---简单增删改查的带事物的例子

    本例子包含了对数据库表简单的增删改查的操作,并且包含事物.该例子只适用于MySQL数据库.该例子需要手动创建数据库以及数据库表 例子中所需要的jar包,详查MyBatis---简介 一个entity类 ...

  8. 1079: [SCOI2008]着色方案

    链接 思路 首先是dp,如果直接用每个种颜色的剩余个数做状态的话,复杂度为5^15. 由于c<=5,所以用剩余数量的颜色的种类数做状态:f[a][b][c][d][e][last]表示剩余数量为 ...

  9. MySQL之查询性能优化(三)

    MySQL查询优化器的局限性 MySQL的万能“嵌套循环”并不是对每种查询都是最优的.不过还好,MySQL查询优化只对少部分查询不适用,而且我们往往可以通过改写查询让MySQL高效地完成工作. 关联子 ...

  10. Spark 的情感分析

    Spark 的情感分析 本文描述了基于 Spark 如何构建一个文本情感分析系统.文章首先介绍文本情感分析基本概念和应用场景,其次描述采用 Spark 作为分析的基础技术平台的原因和本文使用到技术组件 ...