[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 ...
随机推荐
- 在spring boot上基于maven使用redis——批量匹配并删除 (二)
一.背景 在搭建了项目之后,由于需要通过触发动作,并删除redis中多个key. 二.思路 在查询了jedis并没有类似的删除方法之后,事情就变得清晰起来.完成上述任务,分为两个步骤,第一,找到要删除 ...
- 解决E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable) E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
是不是在使用ubuntu的时候特别是安装或更新的时候会出现下面的情况: E: Could not get lock /var/lib/dpkg/lock - open (11: Resource t ...
- Git操作指令
1.创建版本库 git init 2.把工作区修改过的文件添加到版本库暂存区,点号表示当前目录下所有文件; git add .#查看仓库状态git status 3.把版本库暂存区的文件提交到当前分支 ...
- Python环境搭建和pycharm安装
Python环境搭建和pycharm安装 本人安装环境为Windows10系统,下载的Python版本为3.4社区版本,可参考 1.下载Python3.4版本 官网:https://www.pytho ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- CsvHelper文档-5配置
CsvHelper文档-5配置 CsvHelper库被设计成快速且简单易用,但是有时候默认的是设置不符合要求,需要你自己改变一些东西.所以csvHelper内置了很多自定义设置选项来改变读写行为.特别 ...
- nginx 日志模块
ngx_http_log_module.c 数据结构 typedef struct { void **main_conf; void **srv_conf; void **loc_conf;} ngx ...
- [leetcode-908-Smallest Range I]
Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...
- DPDK报文分类与访问控制
原创翻译,转载请注明出处. dpdk提供了一个访问控制库,提供了基于一系列分类规则对接收到的报文进行分类的能力.ACL库用来在一系列规则上执行N元组查找,可以实现多个分类和对每个分类查找最佳匹配(最高 ...
- C++ Primer Plus学习:第七章
C++入门第七章:函数-C++的编程模块 函数的基本知识 要使用C++函数,必须完成如下工作: 提供函数定义 提供函数原型 调用函数 库函数是已经定义和编译好的函数,可使用标准库头文件提供原型. 定义 ...