题意

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. 雷林鹏分享:Ruby 判断

    Ruby 判断 Ruby 提供了其他现代语言中很常见的条件结构.在这里,我们将解释所有的条件语句和 Ruby 中可用的修饰符. Ruby if...else 语句 语法 if conditional ...

  2. English trip -- VC(情景课)9 D Reading 阅读练习

    Read 阅读 Dear Susie(苏西) It's after dinner, My family is working in the kitchen. My daughter Li is  wa ...

  3. English trip -- VC(情景课)9 B Outside chores 室外家务

    Vocabulary focus 核心词汇 cutting the grass 修剪草坪 getting the mail  收到邮件 taking out the trash  把垃圾带出去 wal ...

  4. hdu6405Make ZYB Happy 广义sam

    题意:给出n(n<=10000)个字符串S[1~n],每个S[i]有权值val[i],随机等概率造一个由小写字母构成的字符串T,Sum = 所有含有子串T的S[i]的val[i]之积,求Sum的 ...

  5. HDU-4856 Tunnels (BFS+状压DP)

    Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In hi ...

  6. Java GUI编程中AWT/swing/SWT的优缺点

    http://www.cnblogs.com/dugang/archive/2010/10/22/1858478.html AWT AWT是Abstract Window Toolkit(抽象窗口工具 ...

  7. Tomcat类加载器破坏双亲委派

    转载:https://blog.csdn.net/qq_38182963/article/details/78660779 http://www.cnblogs.com/aspirant/p/8991 ...

  8. UVALive 6320

    .............先让我哭一会....... 因为样例三.... 没敢敲...然后确实是样例错了...然后...上次比赛刚做过的一道类似的题...URAL 1941...大水题啊......悔 ...

  9. jquery条形码生成器

    <!DOCTYPE html><html> <head>        <meta charset="UTF-8">         ...

  10. spoj8406

    题解: 二分+树状数组 记录以下i在当前拍第几 代码: #include<bits/stdc++.h> using namespace std; ; int a[N],f1[N],f2[N ...