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 ...
随机推荐
- Azure EA (3) 使用Postman访问海外Azure Billing API
<Windows Azure Platform 系列文章目录> 本文介绍的是海外版的Azure Global服务,因为跨境内境外网络,访问速度会比较慢 在开始使用Azure Billing ...
- 1+x 证书 Web 前端开发 MySQL 知识点梳理
官方QQ群 1+x 证书 Web 前端开发 MySQL 知识点梳理 http://blog.zh66.club/index.php/archives/199/
- 《一起学mysql》1
自从达内毕业后就没用过mysql,一直用的hive,hdfs 存储数据,最近突然又接触到了关系型数据库.本想随便从网上 找个教程看看,但是都不是很满意,pdf看着又难受,还是自己个儿写个笔记吧. ...
- linux内核参数sysctl.conf,TCP握手ack,洪水攻击syn,超时关闭wait
题记:优化Linux内核sysctl.conf参数来提高服务器并发处理能力 PS:在服务器硬件资源额定有限的情况下,最大的压榨服务器的性能,提高服务器的并发处理能力,是很多运维技术人员思考的问题.要提 ...
- css彩虹文字
用CSS3实现彩虹文字的效果,只在Webkit内核的浏览器(谷歌浏览器或移动端)上有效果. background-image: -webkit-gradient(linear, left top, r ...
- Kubernetes Pod 调度约束
Kubernetes Pod 调度约束 可以将pod调度到指定的节点Node内 默认:根据节点资源利用率等分配Node节点. nodeName用于将Pod调度到指定的Node名称上 nodeSelec ...
- 如何取消 SqlDataAdapter.Fill() 的执行(转载)
问 Scenario: We have a DataGridView which is attached to DataAdapter (datatable), we load the data in ...
- C#,二分法,BinarySearch()
static int BinarySearch(int[] arr,int key,int low,int high) { low = 0;high = arr.Length - 1; while(l ...
- Delphi - 16进制取反 Not
//Not直接实现十六进制取反var I, J : word; begin I := $96E5; J := Not I; ShowMessage(Format('%x',[J])); end; 作者 ...
- select和checkbox回绑
$("#STATUS option[value=" + STATUS + "]").attr("selected", true);[sele ...