Regular Forestation

\[Time Limit: 1000 ms\quad Memory Limit: 262144 kB
\]

题意

给出一个节点为 \(n\) 的树,问删掉树上的一个点和这个点相连的边以后,剩下的子树是不是都是同构的。

思路

首先删掉的这个点一定是这棵树的重心,而且一棵树的重点至多只会有两个。

那么就暴力枚举判断删掉这棵树的重心,然后对于剩下的子树去判断是否是同构的。

判断两棵树是否是同构的,也是先找出重心,然后从重心开始,用进某个节点为 \(0\) 表示,出某个节点为 \(1\) 表示,然后用最小的字典序来表示出这个树。最后枚举两棵树的重心,判断是否有一对表示出来的 \(string\) 是相等的,如果有就是同构的。

/***************************************************************
> File Name : F.cpp
> Author : Jiaaaaaaaqi
> Created Time : 2019年11月06日 星期三 18时45分28秒
***************************************************************/ #include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int> typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std; int n, m;
int cas, tol, T; vector<int> vv[maxn];
int sz[maxn], gsz[maxn]; void dfs(int u, int fa) {
sz[u] = 1;
for(auto v : vv[u]) if(v != fa) {
dfs(v, u);
sz[u] += sz[v];
}
} void getroot(int u, int fa, int N, pair<int, int> &rt) {
gsz[u] = 0;
for(auto v : vv[u]) if(v != fa) {
gsz[u] = max(gsz[u], sz[v]);
getroot(v, u, N, rt);
}
gsz[u] = max(gsz[u], N-sz[u]);
if(rt.fi == -1 || gsz[rt.fi] > gsz[u]) {
rt.fi = rt.se = u;
} else if(gsz[rt.fi] == gsz[u]){
rt.se = u;
}
} string get(int u, int fa, int st) {
vector<string> vec;
vec.clear();
for(auto v : vv[u]) if(v!=fa && v!=st)
vec.push_back(get(v, u, st));
sort(vec.begin(), vec.end());
string ans = "0";
for(auto s : vec) ans = ans+s;
ans = ans+"1";
return ans;
} int calc(int u) {
if((int)vv[u].size() <= 1) return -1;
string A, B, C;
A = B = C = "";
bool ok = 1;
for(int i=0; i<vv[u].size(); i++) {
int v = vv[u][i];
if(v == u) continue;
pair<int, int> rt;
rt.fi = rt.se = -1;
dfs(v, u);
getroot(v, u, sz[v], rt);
if(i == 0) {
A = get(rt.fi, 0, u);
B = get(rt.se, 0, u);
} else {
C = get(rt.fi, 0, u);
if(C==A || C==B) continue;
C = get(rt.se, 0, u);
if(C==A || C==B) continue;
ok = 0;
break;
}
}
if(ok) return vv[u].size();
else return -1;
} int main() {
// freopen("in", "r", stdin);
scanf("%d", &n);
for(int i=2,u,v; i<=n; i++) {
scanf("%d%d", &u, &v);
vv[u].pb(v);
vv[v].pb(u);
}
pair<int, int> rt;
rt.fi = rt.se = -1;
dfs(1, 0);
getroot(1, 0, n, rt);
printf("%d\n", max(calc(rt.fi), calc(rt.se)));
return 0;
}

Regular Forestation CodeForces - 1252F(树同构)的更多相关文章

  1. 【CF1252F】Regular Forestation(重心,树同构)

    题意:给定一棵n个点的树,问删去某个点之后所有的树同构,这样分割出来的树最多能有几棵 n<=4000 思路:分割成至少两个size相等的联通块之后size必定小于n/2,与树的重心的定义相同 预 ...

  2. uva12489 Combating cancer(树同构)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud https://uva.onlinejudge.org/index.php?opt ...

  3. BZOJ4337: BJOI2015 树的同构(hash 树同构)

    题意 题目链接 Sol 树的同构问题,直接拿hash判一下,具体流程大概是这样的: 首先转化为有根树,预处理出第\(i\)棵树以\(j\)为根时的hash值. 那么两个树同构当且仅当把两棵树的hash ...

  4. Luogu 5043 【模板】树同构([BJOI2015]树的同构)

    BZOJ 4337 简单记录一种树哈希的方法:以$x$为根的子树的哈希值为$\sum_{y \in son(x)}f_y*base_i$,$f_y$表示以$y$为根的树的哈希值,其中$i$表示$f_y ...

  5. 『Andrew and Chemistry 树同构』

    Andrew and Chemistry Description During the chemistry lesson Andrew learned that the saturated hydro ...

  6. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  7. luogu P5043 【模板】树同构 hash 最小表示法

    LINK:模板 树同构 题目说的很迷 给了一棵有根树 但是重新标号 言外之意还是一棵无根树 然后要求判断是否重构. 由于时无根的 所以一个比较显然的想法暴力枚举根. 然后做树hash或者树的最小表示法 ...

  8. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  9. Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

随机推荐

  1. 不支持中国移动的N79频段,红米K30是假5G手机么?影响有多大?

    原文:https://mparticle.uc.cn/article.html?uc_param_str=frdnsnpfvecpntnwprdssskt&btifl=100&app= ...

  2. jpa复杂查询groupby失败的原因以及替代方法-20190824

    问题 1  jpa specification 复杂查询,拼接group by 时,分页会触发select  count (*),导致指定select * from table group by 字段 ...

  3. LeetCode 394:字符串解码 Decode String

    题目: 给定一个经过编码的字符串,返回它解码后的字符串. Given an encoded string, return its decoded string. 编码规则为: k[encoded_st ...

  4. LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum

    公众号: 爱写bug(ID:icodebugs) 作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子 ...

  5. 三、Spring注解之@Import

    spring注解之@Import [1]@Import ​ 参数value接收一个Class数组,将你传入的类以全类名作为id加入IOC容器中 ​ 比较简单,此处不做详细解释 [2]ImportSel ...

  6. vue 移动端上传图片结合localResizeIMG插件进行图片压缩

    localResizeIMG插件的功能是将图片进行压缩,然后转换成base64传给后台. 首先, npm i lrz -save 然后,再main.js里面引入lrz import lrz from ...

  7. 「ASCII 流程图」工具——Graph Easy

    https://juejin.im/post/5a09c43451882535c56c6bbf 「ASCII 流程图」工具——Graph Easy // 1. brew install graphvi ...

  8. sql server中的临时表、表变量和公用表表达式

    在编写T-SQL语句的时候,SQL Server提供了三种方法临时存储某些结果集,分别是临时表.表变量和公用表表达式. 临时表 临时表需要在临时数据库TempDB中通过I/O操作来创建表结构,一旦用户 ...

  9. HTML教程详解

    HTML学习笔记 目录 一.html简介 1.html是什么? 2.html能做什么(html的作用)? 3.html书写规范 二.html基本标签 1.标签的语法 2.标签的分类 3.常用标签: 1 ...

  10. Asp.Net MVC 的19个管道事件

    httpApplication调用ProcessRequest方法,内部执行19个管道事件,如下 BeginRequest  开始处理请求 AuthenticateRequest 授权验证请求开始,获 ...