Educational Codeforces Round 78 (Rated for Div. 2) D. Segment Tree
链接:
https://codeforces.com/contest/1278/problem/D
题意:
As the name of the task implies, you are asked to do some work with segments and trees.
Recall that a tree is a connected undirected graph such that there is exactly one simple path between every pair of its vertices.
You are given n segments [l1,r1],[l2,r2],…,[ln,rn], li<ri for every i. It is guaranteed that all segments' endpoints are integers, and all endpoints are unique — there is no pair of segments such that they start in the same point, end in the same point or one starts in the same point the other one ends.
Let's generate a graph with n vertices from these segments. Vertices v and u are connected by an edge if and only if segments [lv,rv] and [lu,ru] intersect and neither of it lies fully inside the other one.
For example, pairs ([1,3],[2,4]) and ([5,10],[3,7]) will induce the edges but pairs ([1,2],[3,4]) and ([5,7],[3,10]) will not.
Determine if the resulting graph is a tree or not.
思路:
并差集维护关系,set查找所有能加入的点,因为最多就n个,所以不满足的条件中途就退出了,不会导致超时。
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6+10;
map<int, int> Mp;
int F[MAXN];
pair<int, int> Pa[MAXN];
int n;
int GetF(int x)
{
return (x == F[x]) ? x : F[x] = GetF(F[x]);
}
int main()
{
scanf("%d", &n);
for (int i = 1;i <= n;i++)
F[i] = i;
for (int i = 1;i <= n;i++)
cin >> Pa[i].first >> Pa[i].second;
sort(Pa+1, Pa+1+n);
int cnt = 0;
for (int i = 1;i <= n;i++)
{
auto it = Mp.lower_bound(Pa[i].first);
while(it != Mp.end() && it->first < Pa[i].second)
{
int tl = GetF(i);
int tr = GetF(it->second);
if (tl == tr || cnt >= n)
{
cout << "NO\n";
return 0;
}
else
{
cnt++;
F[tl] = tr;
}
++it;
}
Mp[Pa[i].second] = i;
}
if (cnt != n-1)
cout << "NO\n";
else
cout << "YES\n";
return 0;
}
Educational Codeforces Round 78 (Rated for Div. 2) D. Segment Tree的更多相关文章
- Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam
链接: https://codeforces.com/contest/1278/problem/C 题意: Karlsson has recently discovered a huge stock ...
- Educational Codeforces Round 78 (Rated for Div. 2) B. A and B
链接: https://codeforces.com/contest/1278/problem/B 题意: You are given two integers a and b. You can pe ...
- Educational Codeforces Round 78 (Rated for Div. 2) A. Shuffle Hashing
链接: https://codeforces.com/contest/1278/problem/A 题意: Polycarp has built his own web service. Being ...
- 【cf比赛记录】Educational Codeforces Round 78 (Rated for Div. 2)
比赛传送门 A. Shuffle Hashing 题意:加密字符串.可以把字符串的字母打乱后再从前面以及后面接上字符串.问加密后的字符串是否符合加密规则. 题解:字符串的长度很短,直接暴力搜索所有情况 ...
- Educational Codeforces Round 78 (Rated for Div. 2)B. A and B(1~n的分配)
题:https://codeforces.com/contest/1278/problem/B 思路:还是把1~n分配给俩个数,让他们最终相等 假设刚开始两个数字相等,然后一个数字向前走了abs(b- ...
- Educational Codeforces Round 78 (Rated for Div. 2)
A题 给出n对串,求s1,是否为s2一段连续子串的重排,串长度只有100,从第一个字符开始枚举,sort之后比较一遍就可以了: char s1[200],s2[200],s3[200]; int ma ...
- Educational Codeforces Round 78 (Rated for Div. 2) --补题
链接 直接用数组记录每个字母的个数即可 #include<bits/stdc++.h> using namespace std; int a[26] = {0}; int b[26] = ...
- Educational Codeforces Round 78 (Rated for Div. 2) 题解
Shuffle Hashing A and B Berry Jam Segment Tree Tests for problem D Cards Shuffle Hashing \[ Time Lim ...
- Educational Codeforces Round 78 (Rated for Div. 2) C - Berry Jam(前缀和)
随机推荐
- 推荐一款万年历App 诸葛万年历
推荐一款万年历App 诸葛万年历 1 介绍 应用简介: 提供标准和专业的时间信息查询,记录和承载生活中的美好记忆,帮助用户高效快捷的管理个人时间.精美的日期展示和完善的重要事件提醒功能,可以方便安排日 ...
- Js apply方法与call方法详解 附ES6新写法
我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里我做如下笔记,希望和大家 ...
- vins_fusion学习笔记
Vins-Fusion源码:https://github.com/HKUST-Aerial-Robotics/VINS-Fusion 摘要 应项目需要,侧重学习stereo+gps融合 转载几篇写的比 ...
- $scope里的$watch方法
一.$watch的作用 监听model,如果model发生变化,则触发某些事情. 二.$watch的格式 $scope. $watch(watchFn,watchAction,deepWatch); ...
- 固定定位导致$(window).scrollTop();获取滚动后到顶部距离总是为0
如下移动端索引列表页面(点击某元素后弹出的页面) 我想用 $(window).scrollTop(); 获取页面滚动后距离顶部的距离,但获取到的值总是0 期间查了很久,但都无疾而终,后来看到一篇 ...
- 使用python把gdb格式的文本文件转为utf-8的格式
# coding=utf-8 from os import listdir if __name__ =="__main__": d=u"D:\\files\\" ...
- 一、hexo+github搭建个人博客的过程记录
前提: 1.新建一个github仓库 2.安装配置Node.js 3.安装配置Git 前提 步骤1.新建一个github仓库 打开github网站,(注册)登录账号,新建一个仓库; 注:==仓库名称要 ...
- 使用RunTime.getRunTime().addShutdownHook优雅关闭线程池
有时候我们用到的程序不一定总是在JVM里面驻守,可能调用完就不用了,释放资源. RunTime.getRunTime().addShutdownHook的作用就是在JVM销毁前执行的一个线程.当然这个 ...
- moodle3.7中文语言包
Moodle官方有中文语言包,但是还有没有翻译的,为了提高用户体验,可以将部分未翻译的应用在Moodle网站管理中自己修改. 具体步骤: 先确定需要修改的关键字,也就是网站中没有翻译成中文的文字 在c ...
- Logstash——multiline 插件,匹配多行日志
本文内容 测试数据 字段属性 按多行解析运行时日志 把多行日志解析到字段 参考资料 在处理日志时,除了访问日志外,还要处理运行时日志,该日志大都用程序写的,比如 log4j.运行时日志跟访问日志最大的 ...