Regular Forestation CodeForces - 1252F(树同构)
Regular Forestation
\]
题意
给出一个节点为 \(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(树同构)的更多相关文章
- 【CF1252F】Regular Forestation(重心,树同构)
题意:给定一棵n个点的树,问删去某个点之后所有的树同构,这样分割出来的树最多能有几棵 n<=4000 思路:分割成至少两个size相等的联通块之后size必定小于n/2,与树的重心的定义相同 预 ...
- uva12489 Combating cancer(树同构)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud https://uva.onlinejudge.org/index.php?opt ...
- BZOJ4337: BJOI2015 树的同构(hash 树同构)
题意 题目链接 Sol 树的同构问题,直接拿hash判一下,具体流程大概是这样的: 首先转化为有根树,预处理出第\(i\)棵树以\(j\)为根时的hash值. 那么两个树同构当且仅当把两棵树的hash ...
- Luogu 5043 【模板】树同构([BJOI2015]树的同构)
BZOJ 4337 简单记录一种树哈希的方法:以$x$为根的子树的哈希值为$\sum_{y \in son(x)}f_y*base_i$,$f_y$表示以$y$为根的树的哈希值,其中$i$表示$f_y ...
- 『Andrew and Chemistry 树同构』
Andrew and Chemistry Description During the chemistry lesson Andrew learned that the saturated hydro ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- luogu P5043 【模板】树同构 hash 最小表示法
LINK:模板 树同构 题目说的很迷 给了一棵有根树 但是重新标号 言外之意还是一棵无根树 然后要求判断是否重构. 由于时无根的 所以一个比较显然的想法暴力枚举根. 然后做树hash或者树的最小表示法 ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- 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 ...
随机推荐
- 当职责链遇到DI
在GitHub上有个项目,本来是作为自己研究学习.net core的Demo,没想到很多同学在看,还给了很多星,所以觉得应该升成3.0,整理一下,写成博分享给学习.net core的同学们. 项目名称 ...
- Harbor + Https 部署
关闭防火墙和selinux systemctl stop firewalld sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selin ...
- HttpUtility.HtmlDecode ,HttpUtility.HtmlEncode 与 Server.HtmlDecode ,Server.HtmlEncode 与 HttpServerUtility.HtmlDecode , HttpServerUtility.HtmlEncode
HtmlEncode: 将 Html 源文件中不允许出现的字符进行编码,通常是编码以下字符"<".">"."&" 等. ...
- 明解C语言 入门篇 第十二章答案
练习12-1 /* 用表示学生的结构体来显示高尾的信息 */ #include <stdio.h> #define NAME_LEN 64 /* 姓名的字符数 */ /*=== 表示学生的 ...
- 记录一个 C# 导出 Excel 的坑
Ø 简介 其实该文章并没有什么技术含量,只是记录下个人心得.在以后有朋友遇到类似问题,可以借鉴下,或者遇到相同问题时的提供个参考方向. 也算我的一个经历吧,这个问题我花了足足一天多的时间才找到问题, ...
- vsdbg 下载方法 使用下载工具下载后手动安装
vsdbg国内下载太慢了,这里提供一个使用下载工具下载后,手动安装的处理方法 查看vs build控制台输出: 1>C:\WINDOWS\System32\WindowsPowerShell\v ...
- VSCode搭建django项目
之前我们使用VSCode搭建C#项目,今天写一篇关于django项目的搭建,其实以其说是搭建django框架,不如说是如何通过vscode开发django项目:django官网:https://www ...
- STP生成树理解
1.STP的功能 a. 防止二层环路 b .实现网络冗余备份 2.STP的选择机制 目的: 确定阻塞的端口 STP 交换机的角色: 根交换机,非根交换机 STP的选票: BPDU Ro ...
- php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos
php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...
- python 检查站点是否可以访问
最近碰到系统有时候会访问不了,想写一个程序来检测站点是不是可以访问的功能,正好在学python,于是写了一个方法来练练手,直接上代码. import urllib.request import smt ...