题意

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. 12月12日 has_many through:的interference, option

    has_many :products, through: :cart_items, source: :product build定义:collection.build(attributes = {}, ...

  2. 4-6 select_tag和select的区别和理解。javascript_tag

    via: :all是什么意思?主要用于约束http动作. <%= select_tag "set_locale", options_for_select(LANGUAGES, ...

  3. robot framework学习笔记1之_环境安装(win7)

    一.简介 Robotframework是基于Python的自动化测试框架.使用关键字驱动的测试方法,自带丰富的库函数可直接引用,可使用Java/Python进行功能库扩展,测试用例使用TSV/HTML ...

  4. uva-10561-nim

    题意: 给出一个连续的棋盘,有的位置为'.',有的位置为'X',二者轮流下子,当有一方获得连续三个子的时候取胜. 对于胜态,一种情况是当前局面出现"XX"/"X.X&qu ...

  5. OAF SubTabLayoutBean隐藏子控件

    SubLayout隐藏子控件有两种方法 OASubTabLayoutBean layBean = (OASubTabLayoutBean) webBean.findIndexedChildRecurs ...

  6. Authentication failed (rejected by the remote node), please check the Erlang

    官网提供的解决方案 Synchronise Erlang Cookies (when running a manually installedWindows Service) Erlang Secur ...

  7. vue上传文件

    <div> <input type="file" class="file" name="file" @change=&qu ...

  8. jsonp 跨域2

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  9. OO第二次单元总结

    OO第二次单元总结 前言 第二单元的三次作业:系列电梯与多线程. 第五次作业 (1)设计策略 电梯的第一次作业是单部傻瓜电梯,采用FAFS调度策略,电梯按队列顺序依次处理请求,单次只处理一个请求.本次 ...

  10. zabbix项目实践

    一,Zabbix生产环境监测案例概述 1.1 项目规划 [x] :主机分组 交换机 Nginx Tomcat MySQL Apache PHP-fpm redis(也有状态页, 自己研究) memca ...