链接:

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;
}

最新文章

  1. 页面加载完成后,触发事件——trigger()
  2. Android开发常用属性
  3. Tesseract-OCR text2image.exe [ x86 支持 XP ]
  4. OC 实例变量(instance var)与属性(@property)的关系 isa指针
  5. Swift利用闭包(closure)来实现传值--&amp;gt;前后两个控制器的反向传值
  6. mh
  7. 折扣&amp;折让-看清实质的思考
  8. linux下安装openmpi
  9. Linux学习总结(十三)—— CentOS用户组管理:创建用户组、修改用户组、删除用户组
  10. 32位汇编第二讲,编写窗口程序,加载资源,响应消息,以及调用C库函数
  11. @property括号内属性讲解
  12. 《网络攻防》实验五:MSF基础应用
  13. Sequelize-nodejs-4-Model usage
  14. SQLServer cast()函数
  15. 【BZOJ 4449】[Neerc2015]Distance on Triangulation 多边形分治结构
  16. vue使用插件 使用库
  17. Dev DateEdit控件格式设置
  18. 《Thinking in Java》 10~
  19. MyTest——边界检测
  20. 洛谷 P4538 收集邮票

热门文章

  1. Oracle常用函数集锦
  2. gulp的初阶使用方法(转)
  3. Isilon Gen6的换盘步骤
  4. 【C++】Debug模式和Release模式的区别
  5. vue router 常用操作
  6. 获取Url地址中参数的3种方法【华为云技术分享】
  7. 插件油泼猴+脚本 for chrome 安装 - https://greasyfork.org/zh-CN
  8. Zookeeper到底是什么
  9. Golang 模块(Module)官方手册
  10. 费劲周折的Haskell开发环境搭建过程