题意

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的更多相关文章

  1. AtCoder AGC014E Blue and Red Tree (启发式合并)

    题目链接 https://atcoder.jp/contests/agc014/tasks/agc014_e 题解 完了考场上树剖做法都没想到是不是可以退役了... 首先有一个巨难写的据说是\(O(n ...

  2. AT2377 Blue and Red Tree

    AT2377 Blue and Red Tree 法一:正推 红色的边在蓝色的树上覆盖,一定每次选择的是覆盖次数为1的边的覆盖这条边的红色边连出来 覆盖次数可以树剖找到 这条红色边,可以开始的时候每个 ...

  3. 【AGC014E】Blue and Red Tree 并查集 启发式合并

    题目描述 有一棵\(n\)个点的树,最开始所有边都是蓝边.每次你可以选择一条全是蓝边的路径,删掉其中一条,再把这两个端点之间连一条红边.再给你一棵树,这棵树的所有边都是红边,问你最终能不能把原来的树变 ...

  4. 【AGC014E】Blue and Red Tree

    Description 给定一棵\(n\)个节点的蓝边树,再给定一棵\(n\)个节点的红边树.请通过若干次操作将蓝树变成红树.操作要求和过程如下: 1.选定一条边全为蓝色的路径: 2.将路径上的一条蓝 ...

  5. AGC 014 E Blue and Red Tree [树链剖分]

    传送门 思路 官方题解是倒推,这里提供一种正推的做法. 不知道你们是怎么想到倒推的--感觉正推更好想啊QwQ就是不好码 把每一条红边,将其转化为蓝树上的一条路径.为了连这条红边,需要保证这条路径仍然完 ...

  6. AGC 014E.Blue and Red Tree(思路 启发式合并)

    题目链接 \(Description\) 给定两棵\(n\)个点的树,分别是由\(n-1\)条蓝边和\(n-1\)条红边组成的树.求\(n-1\)次操作后,能否把蓝树变成红树. 每次操作是,选择当前树 ...

  7. [AT2377] [agc014_e] Blue and Red Tree

    题目链接 AtCoder:https://agc014.contest.atcoder.jp/tasks/agc014_e 洛谷:https://www.luogu.org/problemnew/sh ...

  8. AtCoder Grand Contest 014 E:Blue and Red Tree

    题目传送门:https://agc014.contest.atcoder.jp/tasks/agc014_e 题目翻译 有一棵有\(N\)个点的树,初始时每条边都是蓝色的,每次你可以选择一条由蓝色边构 ...

  9. [atAGC014E]Blue and Red Tree

    不断删除重边,然后将两个点的边集启发式合并(要考虑到两棵树),合并时发现重边就加入队列,最后判断是否全部删完即可 1 #include<bits/stdc++.h> 2 using nam ...

随机推荐

  1. 理解Spring4.0新特性@RestController注解

    参考原文 @RestController注解是它继承自@Controller注解.4.0之前的版本,spring MVC的组件都使用@Controller来标识当前类是一个控制器servlet. 使用 ...

  2. 对微服务API服务网关的理解

    目录微服务专栏地址目录1. 简介2. 什么是API网关3. 为什么需要API网关4. API网关在微服务架构体系中处于什么位置4.1 调用者眼中的API网关4.2 所处的位置5. 网关技术实现有哪些6 ...

  3. 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(一)

    源自:http://blog.163.com/zwx_gis/blog/static/32434435201122193611576/ (主页:http://blog.163.com/zwx_gis/ ...

  4. English trip -- VC(情景课)2 D Reading

    Xu言: 业精于勤,荒于嬉:行成于思,毁于随 Before you read 阅读准备 Talk about the picture, what do you see?看图说话,你看到了什么? Lis ...

  5. LeetCode--119--杨辉三角II

    问题描述: 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: 你可以优化你 ...

  6. 用Javascript 实现倒计时

    用Javascript 实现倒计时<!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  7. hulu

    一. 一开始因为没收到含有共享文档链接的邮件,所以简单自我介绍,聊了几句项目.问了: 1. 玩嗨如果数据库结构变化要怎么办 2. 哈佛项目是否为官方渠道 一直没收到邮件,面试官读网址给我,进到共享文档 ...

  8. java并发编程:线程安全管理类--原子操作类--AtomicReferenceArray<E>

    1.类 AtomicReferenceArray<E> public class AtomicReferenceArray<E>extends Objectimplements ...

  9. js组件的写法

    工作之中的不足,报了js培训班,因为工作加班原因缺了几天课(js组件开发),现在拾起来补补 <!doctype html> <html> <head> <me ...

  10. html和css命名标准以及常用的框架,我使用的是网易nec

    前端的工作很细,涉及的东西也很多,静态页面和js开发,调接口之类,有时还要自己设计.现在css管理使用less和sass,新东西起码要支持下,具体用与不用看公司的业务需求.前端人员之间的配合也很重要要 ...