链接:

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 【cf比赛记录】Educational Codeforces Round 78 (Rated for Div. 2)

    比赛传送门 A. Shuffle Hashing 题意:加密字符串.可以把字符串的字母打乱后再从前面以及后面接上字符串.问加密后的字符串是否符合加密规则. 题解:字符串的长度很短,直接暴力搜索所有情况 ...

  5. 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- ...

  6. Educational Codeforces Round 78 (Rated for Div. 2)

    A题 给出n对串,求s1,是否为s2一段连续子串的重排,串长度只有100,从第一个字符开始枚举,sort之后比较一遍就可以了: char s1[200],s2[200],s3[200]; int ma ...

  7. Educational Codeforces Round 78 (Rated for Div. 2) --补题

    链接 直接用数组记录每个字母的个数即可 #include<bits/stdc++.h> using namespace std; int a[26] = {0}; int b[26] = ...

  8. 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 ...

  9. Educational Codeforces Round 78 (Rated for Div. 2) C - Berry Jam(前缀和)

随机推荐

  1. Comet OJ - Contest #13-C2

    Comet OJ - Contest #13-C2 C2-佛御石之钵 -不碎的意志-」(困难版) 又是一道并查集.最近做过的并查集的题貌似蛮多的. 思路 首先考虑,每次处理矩形只考虑从0变成1的点.这 ...

  2. prometheus consul docker redis_exporter 自动注册配置

    0.启动redis_exporter redis_exporter: version: '2'services: redis_exporter: image: oliver006/redis_expo ...

  3. 为什么我的resharper控件安装之后没有显示

    Resharper和Resharper C++有时候会出现,安装之后不显示,VisualStudio菜单栏内找不到的情况,大多数是因为启动VisualStudio的时候没有激活Resharper. 安 ...

  4. 【前端知识体系-HTML相关】HTML基础知识强化总结

    1.如何理解HTML? HTML类似于一份word"文档" 描述文档的"结构" 有区块和大纲 2.对WEB标准的理解? Web标准是由一系列标准组合而成.一个网 ...

  5. CentOS7. 6 上部署MongoDB

    *安装步骤** 配置yum源 vim /etc/yum.repos.d/mongodb-org-4.0.repo #添加以下内容: [mongodb-org-4.0] name=MongoDB Rep ...

  6. Java & Android未捕获异常处理机制

    一.背景 无论是Java还是Android项目,往往都会用到多线程.不管是主线程还是子线程,在运行过程中,都有可能出现未捕获异常.未捕获异常中含有详细的异常信息堆栈,可以很方便的去帮助我们排查问题. ...

  7. Java se课程设计详解——数据库接口类(1)

    开始做课程设计的时候根本无从下手,后来查阅资料后发现是先从数据库开始的.整个课程设计需要用到的如下图,今天总结一下数据库接口! 数据库接口需要用到两个类,一个是DAO.java,另一个是propert ...

  8. 关于插件Markdown Preview Enhanced的使用技巧

    目录 1.关于TOC 2.关于转义符 3.绘图 3.0 绘图配色主题 3.1 Flowchart(流程图) 3.2 Sequence diagram(顺序图) 3.4 保存为HTML shanzm 1 ...

  9. SQL 增、删、改、查语句

    1.SQL SELECT 语句 SELECT语句用于从表中选取数据. 结果被存储在一个结果表中(称为结果集). SQL SELECT语法 SELECT 列名称 FROM 表名称 以及 SELECT * ...

  10. MySQL中的存储过程、游标和存储函数

    MySQL中的存储过程首先来看两个问题: 1.什么是存储过程? 存储过程(Stored Procedure)是在数据库系统中,一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存 ...