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 ...
随机推荐
- GetImageURL
Sub GetImageUrl(ByVal URL As String) Dim strText As String Dim i As Long Dim OneImg With CreateObjec ...
- PHP函数笔记
一.函数(Function) 1.什么是函数:封装的,可以重复使用的 完成特定功能的代码段. 2.分类 (1)系统函数 (2)自定义函数 3.自定义函数 ...
- python 多线程稀疏矩阵乘法
import threading, time import numpy as np res = [] class MyThread(threading.Thread): def __init__(se ...
- HDU1789时间贪心
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 使用kubeadm安装Kubernetes v1.10
关于K8S: Kubernetes是Google开源的容器集群管理系统.它构建于docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩 容缩容等整一套功能,本质上可看作是基于容器技术 ...
- 白化whitening
原文地址:http://blog.csdn.net/hjimce/article/details/50864602 作者:hjimce 一.相关理论 白化这个词,可能在深度学习领域比较常遇到, ...
- Idea热部署jrebel失败
Idea热部署jrebel
- 华为root手机
- 如何解决请求URL长度超过配置的maxurlLength值问题
当我们批量请求的数据太多时,会出现请求的url长度超过配置maxurllength值的问题(比如一次性操作1000条数据) 1.问题描述: 我在进行批量选择单据进行发送时,出现这个问题(批量500条) ...
- C#winform窗体如何通过windowApi的FindWindow函数获取窗体句柄
在同一个程序里,传统方式是通过this来设置当前窗体的最大化.最小化等操作, 那么怎样通过窗体句柄来设置窗体的最大化.最小化呢? 1.界面布局 通过this设置窗体最大化: name:btnWindo ...