[AT2377] [agc014_e] Blue and Red Tree
题目链接
AtCoder:https://agc014.contest.atcoder.jp/tasks/agc014_e
洛谷:https://www.luogu.org/problemnew/show/AT2377
Solution
秒了\(O(n^2)\)不会优化是什么鬼...最后膜了大佬的题解才会写...
注意到最后一条边一定在蓝图上存在,在红图上也存在,那么我们可以找到任意一条这样的边,把两端的点合并起来,蓝图和红图都合并,剩下的是一个子问题,做\(n-1\)遍就好了,复杂度\(O(n^2)\)。
那么合并节点可以用并查集,然后每次暴力遍历点度小的那个点启发式合并就好了。
全程\(STL\)代码很恶心...注意并查集到处都要\(find\)一下,我就是挂的这里然后调了好久...
复杂度\(O(n\log ^2 n)\)。
#include<bits/stdc++.h>
using namespace std;
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double
#define ll long long
#define pb push_back
#define pii pair<int,int >
#define mp make_pair
#define fr first
#define sc second
#define iter_map map<int,int > :: iterator
#define iter_vec vector<int > :: iterator
#define iter_set set<int > :: iterator
const int maxn = 5e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
struct DSU {
int fa[maxn];
void init(int n) {for(int i=1;i<=n;i++) fa[i]=i;}
int find(int x) {return fa[x]==x?x:fa[x]=find(fa[x]);}
}dsu;
queue<pii > q;
set<int > e[maxn];
map<pii,int > s;
int n,d[maxn];
pii get(int x,int y) {return mp(min(x,y),max(x,y));}
void ins(int x,int y) {
e[x].insert(y),e[y].insert(x);
pii now=get(x,y);s[now]++;
if(s[now]==2) q.push(now);
}
int main() {
read(n);dsu.init(n);
for(int i=1,x,y;i<n*2-1;i++) read(x),read(y),ins(x,y);
for(int i=1,x,y;i<n;i++) {
while(1) {
if(q.empty()) {puts("NO");exit(0);}
x=dsu.find(q.front().fr),y=dsu.find(q.front().sc);q.pop(); //记得find...
if(x!=y) break;
}
if(e[x].size()>e[y].size()) swap(x,y);
dsu.fa[x]=y,s.erase(get(x,y)),e[y].erase(x);
for(iter_set it=e[x].begin();it!=e[x].end();it++) {
int t=dsu.find(*it);
if(t==y) continue;
s.erase(get(x,t));ins(t,y);
e[t].erase(x),e[x].erase(t);
}
}puts("YES");
return 0;
}
[AT2377] [agc014_e] Blue and Red Tree的更多相关文章
- AT2377 Blue and Red Tree
AT2377 Blue and Red Tree 法一:正推 红色的边在蓝色的树上覆盖,一定每次选择的是覆盖次数为1的边的覆盖这条边的红色边连出来 覆盖次数可以树剖找到 这条红色边,可以开始的时候每个 ...
- AGC014E Blue and Red Tree
题意 There is a tree with \(N\) vertices numbered \(1\) through \(N\). The \(i\)-th of the \(N−1\) edg ...
- AtCoder Grand Contest 014 E:Blue and Red Tree
题目传送门:https://agc014.contest.atcoder.jp/tasks/agc014_e 题目翻译 有一棵有\(N\)个点的树,初始时每条边都是蓝色的,每次你可以选择一条由蓝色边构 ...
- AtCoder AGC014E Blue and Red Tree (启发式合并)
题目链接 https://atcoder.jp/contests/agc014/tasks/agc014_e 题解 完了考场上树剖做法都没想到是不是可以退役了... 首先有一个巨难写的据说是\(O(n ...
- 【AGC014E】Blue and Red Tree 并查集 启发式合并
题目描述 有一棵\(n\)个点的树,最开始所有边都是蓝边.每次你可以选择一条全是蓝边的路径,删掉其中一条,再把这两个端点之间连一条红边.再给你一棵树,这棵树的所有边都是红边,问你最终能不能把原来的树变 ...
- AGC 014 E Blue and Red Tree [树链剖分]
传送门 思路 官方题解是倒推,这里提供一种正推的做法. 不知道你们是怎么想到倒推的--感觉正推更好想啊QwQ就是不好码 把每一条红边,将其转化为蓝树上的一条路径.为了连这条红边,需要保证这条路径仍然完 ...
- AGC 014E.Blue and Red Tree(思路 启发式合并)
题目链接 \(Description\) 给定两棵\(n\)个点的树,分别是由\(n-1\)条蓝边和\(n-1\)条红边组成的树.求\(n-1\)次操作后,能否把蓝树变成红树. 每次操作是,选择当前树 ...
- 【AGC014E】Blue and Red Tree
Description 给定一棵\(n\)个节点的蓝边树,再给定一棵\(n\)个节点的红边树.请通过若干次操作将蓝树变成红树.操作要求和过程如下: 1.选定一条边全为蓝色的路径: 2.将路径上的一条蓝 ...
- [atAGC014E]Blue and Red Tree
不断删除重边,然后将两个点的边集启发式合并(要考虑到两棵树),合并时发现重边就加入队列,最后判断是否全部删完即可 1 #include<bits/stdc++.h> 2 using nam ...
随机推荐
- OpenStack入门篇(四)之KVM虚拟机介绍和管理
1.查看虚拟机,启动虚拟机 [root@linux-node1 ~]# virsh list --all Id Name State --------------------------------- ...
- 带偏移量的AES加密工具
自定义的一个对称加密工具类AESUtil.java public static final String ENCRYPTION_ALGORITHM = "AES"; public ...
- java阻塞队列之ArrayBlockingQueue
在Java的java.util.concurrent包中定义了和多线程并发相关的操作,有许多好用的工具类,今天就来看下阻塞队列.阻塞队列很好的解决了多线程中数据的安全传输问题,其中最典型的例子就是客园 ...
- unity游戏在ios11上不显示泰语解决办法
最近在开发中遇到unity游戏在ios11上不显示泰语的问题,全部显示为方框内一个问号. 通过搜索发现这是Unity的一个bug,在2017.3中修复了 但升级unity风险很大,所以我采用了该文中提 ...
- hdu2061 Treasure the new start, freshmen!(暴力简单题)
Treasure the new start, freshmen! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...
- 令自己的本地ip可以被外网访问
https://www.ngrok.cc/_book/general/open.html
- 用Micro:bit做床头灯
这是一个非常简单的项目,给孩子们介绍感应和控制,使用光敏电阻LDR作为光线传感器和床头灯的LED. 这也介绍了模拟输入的概念.数字输入为ON或OFF.只有0和1两种可能的条件.仿真输入是一系列可能值中 ...
- Java Basic&Security Tools
JDK Tools and Utilities Basic Tools These tools are the foundation of the JDK. They are the tools yo ...
- python基础知识-03-字符串
python其他知识目录 1.for循环遍历字符串中单个字符 s_str="mcw" for i in s_str: print(i) -----------结果: m c w 2 ...
- spring mvc 详细配置
转自: http://www.cnblogs.com/superjt/p/3309255.html 现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是 ...