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

解题关键:树dp,不必求因子。

#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 ;
}

[cerc2017J]Justified Jungle的更多相关文章

  1. GYM - 101620 J.Justified Jungle

    题意: 给出一棵树,删掉其中一些边,要求生成的每个子树节点数一样.输出所有可以删掉的边数. 题解: 以节点1为根,预处理每个子树的大小.对于每个n的因数x,还需满足子树为他倍数的点够n/x个,那么删的 ...

  2. 2017-2018 ACM-ICPC, Central Europe Regional Contest (CERC 17)

    A. Assignment Algorithm 按题意模拟即可. #include<stdio.h> #include<iostream> #include<string ...

  3. 2017 CERC

    2017 CERC Problem A:Assignment Algorithm 题目描述:按照规则安排在飞机上的座位. solution 模拟. 时间复杂度:\(O(nm)\) Problem B: ...

  4. CREC 2017

    A:Assignment Algorithm #include <bits/stdc++.h> using namespace std; ][]; ][],n,m,ans=,A,B,l=, ...

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

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

  6. Jungle Roads[HDU1301]

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

  7. Jungle Roads

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

  8. POJ 1251 Jungle Roads (prim)

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

  9. hdu 1301 Jungle Roads 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...

随机推荐

  1. 解决:return _compile(pattern, flags).search(string) TypeError: expected string or buffer

    今天写爬虫,爬取MM图片页面的标题时,遇到了一个问题,上图: 看看我的代码: import urllib import urllib2 import re class JPMSG: def __ini ...

  2. 《Advanced Bash-scripting Guide》学习(十四):HERE Document和cat <<EOF

    本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 #here document cat <<EOF \z EOF ca ...

  3. 分布式_事务_02_2PC框架raincat源码解析

    一.前言 上一节已经将raincat demo工程运行起来了,这一节来分析下raincat的源码 二.协调者启动过程 主要就是在启动类中通过如下代码来启动 netty nettyService.sta ...

  4. np.unique 的实现

    1. 简单实现 import numpy as np def unique(ar): perm = ar.argsort() aux = ar[perm] flag = np.concatenate( ...

  5. STL的erase函数和lower_bound

    前提摘要: [1]一般我们的区间是左闭右开,如下面例子2. [2]erase函数谨慎使用. [3]map也是有序保存的. [erase] 1,删除字符串的首字母: string s="ecu ...

  6. idea 创建maven工程(入门)

    转:http://blog.csdn.net/qq_32588349/article/details/51461182 1. 下载Maven 官方地址:http://maven.apache.org/ ...

  7. C#异步编程(四)混合模式线程同步

    之前讨论了基元用户模式和内核模式线程同步构造.其他所有线程同步构造都基于它们,而且一般都合并了用户模式和内核模式构造,我们称为混合线程同步构造.没有线程竞争时,混合构造提供了基元用户模式构造所具有的性 ...

  8. LeetCode Sum of Square Numbers

    原题链接在这里:https://leetcode.com/problems/sum-of-square-numbers/description/ 题目: Given a non-negative in ...

  9. linux下ioctl遇到的坑

    在驱动编程里面经常会用到ioctl的系统调用,发现cmd = 2的时候,用户ioctl直接返回-1. 原因在于在linux-x.xx/fs/ioctl.c定义的do_vfs_ioctl函数 int d ...

  10. BZOJ2648:SJY摆棋子

    浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...