GYM - 101620 J.Justified Jungle
题意:
给出一棵树,删掉其中一些边,要求生成的每个子树节点数一样。输出所有可以删掉的边数。
题解:
以节点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的更多相关文章
- 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 ...
- 【codeforces.com/gym/100240 J】
http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces Gym 100500 J. Bye Bye Russia
Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- Gym - 100187J J - Deck Shuffling —— dfs
题目链接:http://codeforces.com/gym/100187/problem/J 题目链接:问通过洗牌器,能否将编号为x的牌子转移到第一个位置? 根据 洗牌器,我们可以知道原本在第i位置 ...
- Gym 100801 J. Journey to the “The World’s Start” DP+单调队列优化+二分
http://codeforces.com/gym/100801 题目大意:有从左到右有n个车站,有n-1种车票,第i种车票一次最多可以坐 i 站(1<=i<=n) 每种票有固定的价钱 ...
随机推荐
- lintcode 110最小路径和
最小路径和 描述 笔记 数据 评测 给定一个只含非负整数的m*n网格,找到一条从左上角到右下角的可以使数字和最小的路径. 注意事项 你在同一时间只能向下或者向右移动一步 您在真实的面试中是否遇到过 ...
- 【c学习-5】
int main(){ //二维数组的应用 int i,j; int a[2][3]; for(i=0;i void myFunction(){ int a[3]; int i; int max; f ...
- GIL 线程池 进程池 同步 异步
1.GIL(理论 重点)2.线程池 进程池3.同步 异步 GIL 是一个全局解释器锁,是一个互斥锁 为了防止竞争解释器资源而产生的 为何需要gil:因为一个python.exe进程中只有一份解释器,如 ...
- tomcat 无论如何都无法创建新的Servers
1.退出eclipse 2.到[工程目录下 workspace ]/.metadata/.plugins/org.eclipse.core.runtime 3.把org.eclipse.wst.ser ...
- 接口&抽象类&继承&父类和子类
1.接口是什么? 接口可以理解为一个特殊的抽象类,是由全局常量和公共抽象方法组成的. 所以,如果,一个抽象类的方法都是抽象的,那么就可以通过接口的形式来表示. class 用于定义类 interfac ...
- R语言中的机器学习包
R语言中的机器学习包 Machine Learning & Statistical Learning (机器学习 & 统计学习) 网址:http://cran.r-project ...
- web自动化测试,定位不到元素的原因及解决方案(持续更新中2018年9月29日)
主要讲自己在实战中遇到的坑: 1.动态id定位不到元素 分析原因:每次打开页面,ID都会变化.用ID去找元素,每次刷新页面ID都会发生变化. 解决方案:推荐使用xpath的相对路径方法或者cssSel ...
- 孤荷凌寒自学python第四十三天python 的线程同步之Queue对象
孤荷凌寒自学python第四十三天python的线程同步之Queue对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Queue对象是直接操作队列池的对象,队列中可以存放多种对象,当然也 ...
- IntelliJ IDEA 注释模版 输入/**后 不显示配置好的模板
简单一句话就是 当你在live templetes里配置好以后,记住abbreviation:输入框里的字母 比如我的是cc ,我要想写注释怎么办? 在方法上输入 /*cc 然后按tab键就出来了
- Qt Creator : Read an image from resources
最近两周碰到的一个问题是: opencv无法读取qt中的资源文件. 参考网址:https://stackoverflow.com/questions/45233559/qt-creator-read- ...