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. 公式推导【IoUNet//ECCV2018】

    Jiang B, Luo R, Mao J, Xiao T, Jiang Y. Acquisition of localization confidence for accurate object d ...

  2. NoNodeAvailableException[None of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]

    我在springboot 集成 elasticsearch,启动springboot测试创建索引,建立索引的时候报 : NoNodeAvailableException[None of the con ...

  3. Pandas 学习 第9篇:DataFrame - 数据的输入输出

    常用的数据存储介质是数据库和csv文件,pandas模块包含了相应的API对数据进行输入和输出: 对于格式化的平面文件:read_table() 对于csv文件:read_csv().to_csv() ...

  4. F#周报2019年第23期

    新闻 支持社区的WF与WCF开源项目 视频及幻灯片 F# MonoGame平台游戏系列:摄像头 Xamarin.Forms的F#与Fabulous ML.NET端到端之二:构建Web API 使用F# ...

  5. kali渗透综合靶机(八)--Billu_b0x靶机

    kali渗透综合靶机(八)--Billu_b0x靶机 靶机下载地址:https://download.vulnhub.com/billu/Billu_b0x.zip 一.主机发现 1.netdisco ...

  6. MySQL5.6升级到5.7详细教程

    前言:最近看了下系统的数据库是5.6的,想着升级到5.7,特此记录 一.官网下载MySQL5.7rpm包(4个) 进入MySQL community download页面,默认是MySQL最新版8.0 ...

  7. c# "As" 与 "Is"效率 (原发布csdn 2017-10-07 11:49:18)

    十一长假就要过去了,今年假期没有回家,一个人闲着无聊就在看C#语言规范5.0中文版.昨天看了 is运算符和 as运算符,平时项目中也有用到这两种符号,对于其效率也没有进行比较过,趁着假期有空,先看下效 ...

  8. DataTable 转List

    忘了出处   ,这个是转别人的 public class DataToList<T> where T : new() { /// <summary> /// 利用反射和泛型 / ...

  9. Unable to parse composition

    Unable to parse composition When i tried to load gif to my project suddenly return me error: Java.La ...

  10. webpack4 打包静态资源

    demo 代码点此,开始之前,先做点准备工作. 准备工作 准备一个空文件夹,然后执行下列命令: npm init -y npm i -D webpack webpack-cli 然后创建一个 dist ...