AGC014E Blue and Red Tree
题意
There is a tree with \(N\) vertices numbered \(1\) through \(N\). The \(i\)-th of the \(N−1\) edges connects vertices \(a_i\) and \(b_i\).
Initially, each edge is painted blue. Takahashi will convert this blue tree into a red tree, by performing the following operation \(N−1\) times:
- Select a simple path that consists of only blue edges, and remove one of those edges.
- Then, span a new red edge between the two endpoints of the selected path.
His objective is to obtain a tree that has a red edge connecting vertices \(c_i\) and \(d_i\), for each \(i\).
Determine whether this is achievable.
分析
这题可以倒着想,在原图中最后删掉的边,在新图中也一定是以同样的方式出现。
那么我们可以看成一开始原图中有n个点连通块,每次找一条在原图和新图中都出现的边,把这条边的两个端点缩成一个点,然后继续进行以上操作。
若还未合并至一点且无法继续合并则不可行。
用并查集维护连通块,map维护边出现的次数,set维护点的邻接点,用queue维护当前可合并的点对。合并时需要找到第一个不在同一连通块中的可合并点对(因为以前的合并可能让它们属于同一连通块),find一下合并连通块的并查集的根,然后用启发式合并把x的邻接点赋给y,删掉x的一切记录(省空间),最后把出现次数改变成2的边放入queue中。
代码
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x){
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
typedef pair<int,int>pii;
const int MAXN=1e5+7;
int n;
map<ll,int>M;
queue<pii>Q;
set<int>l[MAXN];
int fa[MAXN];
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
}
ll getid(int x,int y)
{
if(x>y)
swap(x,y);
return (ll)x*n+y;
}
void link(int x,int y)
{
l[x].insert(y);
l[y].insert(x);
ll v=getid(x,y);
++M[v];
if(M[v]==2)
Q.push(pii(x,y));
}
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
read(n);
for(int i=1;i<=n;++i)
fa[i]=i;
for(int i=1;i<=2*n-2;++i)
{
int x,y;
read(x);read(y);
link(x,y);
}
for(int i=1;i<n;++i)
{
int x,y;
while(1)
{
if(Q.empty())
{
printf("NO\n");
return 0;
}
x=Q.front().first,y=Q.front().second;
Q.pop();
x=find(x),y=find(y);
if(x!=y)
break;
}
if(l[x].size()>l[y].size())
swap(x,y);
fa[x]=y;
M.erase(getid(x,y));
l[y].erase(x);
for(auto v:l[x])
{
v=find(v);
if(v==y)
continue;
M.erase(getid(x,v));
l[v].erase(x);
l[x].erase(v);
link(v,y);
}
}
printf("YES\n");
// fclose(stdin);
// fclose(stdout);
return 0;
}
AGC014E Blue and Red Tree的更多相关文章
- AtCoder AGC014E Blue and Red Tree (启发式合并)
题目链接 https://atcoder.jp/contests/agc014/tasks/agc014_e 题解 完了考场上树剖做法都没想到是不是可以退役了... 首先有一个巨难写的据说是\(O(n ...
- AT2377 Blue and Red Tree
AT2377 Blue and Red Tree 法一:正推 红色的边在蓝色的树上覆盖,一定每次选择的是覆盖次数为1的边的覆盖这条边的红色边连出来 覆盖次数可以树剖找到 这条红色边,可以开始的时候每个 ...
- 【AGC014E】Blue and Red Tree 并查集 启发式合并
题目描述 有一棵\(n\)个点的树,最开始所有边都是蓝边.每次你可以选择一条全是蓝边的路径,删掉其中一条,再把这两个端点之间连一条红边.再给你一棵树,这棵树的所有边都是红边,问你最终能不能把原来的树变 ...
- 【AGC014E】Blue and Red Tree
Description 给定一棵\(n\)个节点的蓝边树,再给定一棵\(n\)个节点的红边树.请通过若干次操作将蓝树变成红树.操作要求和过程如下: 1.选定一条边全为蓝色的路径: 2.将路径上的一条蓝 ...
- AGC 014 E Blue and Red Tree [树链剖分]
传送门 思路 官方题解是倒推,这里提供一种正推的做法. 不知道你们是怎么想到倒推的--感觉正推更好想啊QwQ就是不好码 把每一条红边,将其转化为蓝树上的一条路径.为了连这条红边,需要保证这条路径仍然完 ...
- AGC 014E.Blue and Red Tree(思路 启发式合并)
题目链接 \(Description\) 给定两棵\(n\)个点的树,分别是由\(n-1\)条蓝边和\(n-1\)条红边组成的树.求\(n-1\)次操作后,能否把蓝树变成红树. 每次操作是,选择当前树 ...
- [AT2377] [agc014_e] Blue and Red Tree
题目链接 AtCoder:https://agc014.contest.atcoder.jp/tasks/agc014_e 洛谷:https://www.luogu.org/problemnew/sh ...
- AtCoder Grand Contest 014 E:Blue and Red Tree
题目传送门:https://agc014.contest.atcoder.jp/tasks/agc014_e 题目翻译 有一棵有\(N\)个点的树,初始时每条边都是蓝色的,每次你可以选择一条由蓝色边构 ...
- [atAGC014E]Blue and Red Tree
不断删除重边,然后将两个点的边集启发式合并(要考虑到两棵树),合并时发现重边就加入队列,最后判断是否全部删完即可 1 #include<bits/stdc++.h> 2 using nam ...
随机推荐
- android--------AndroidStudio 关闭 Install Run
前面讲热修复的时候说到了一个 AndroidStudio关闭Instant Run的问题 ,今天来简单的写一下. Android Studio 工具中是有很多好东西的,要全部的知道的话,还是要下点功夫 ...
- Docker 构建 redis 集群
安装docker 1.yum install docker 方法一: 1. docker pull redis 2.docker run -d --name redis-1 -p 7001:6379 ...
- thinkphp3.2 输入框默认值
1,我们在做项目的时候输入框有的时候要提示用户输入什么,当用户输入值的时候也不用客户删除,用户只管输入.这里我们就用一个很简单的就OK了. <input type="text" ...
- Oracle11g温习-第五章:数据字典
1.数据字典(Data dictionary)的功能 1) central of oracle database 每个oracle数据库的核心 2) descr ...
- $LANG、$NLS_LANG 记录一下
环境:linux $LANG 为linux termal终端环境下的 语言环境 $NLS_LANG 为oracle数据库中 会话中的语言环境. 个人理解,望大家补充
- Oracle性能诊断艺术-读书笔记(范围分区)
1. PARTITION RANGE SINGLE 注意:操作2 中的 TABLE ACCESS FULL 并不是全表扫描,只是对分区1 做 全分区扫描 case2 2. 分区范围迭代(PARTITI ...
- 在linux下出现cannot restore segment prot after reloc: Permission denied
应用程序连接oracle的库时会出现如下错误:XXXXX:: error while loading shared libraries: /usr/local/oracle/product/10.2. ...
- sql 数据库显示 正在恢复
问题原因:Sql Server 一直显示正在恢复.有事务未恢复或者还原数据库造成 处理办法: 步骤一:数据库上右键->任务->分离 步骤二:数据库上右键->任务->脱机 数据库 ...
- PHP生成GIF动态图片验证码
<?php /** * 调用示例 * */ session_start(); $randCode = ''; //验证码随机 $str="abcdefghjkmnpqrstuvwsyz ...
- Linux免密码登陆
A电脑实现不用密码登陆到B电脑! vim /etc/ssh/sshd_config 编辑文件 01.在A电脑上生成ssh密钥 ssh-keygen -t rsa02.把A电脑生成的id_rsa.p ...