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(前缀和)
随机推荐
- java web开发入门八(ssm整合)基于intellig idea
ssm整合 一.导入相关包 二.开发流程 1.写entity package com.eggtwo.euq.entity; import java.io.Serializable; import ja ...
- Elasticsearch由浅入深(十一)内核原理
倒排索引组成结构以及索引不可变原因 对于倒排索引是非常适合用来进行搜索的它的结构:(1)包含这个关键词的document list(2)包含这个关键词的所有document的数量:IDF(invers ...
- 妖娆的HTML
初涉前端之HTML 1.首先是Web服务的本质,是怎么工作的?(基于python的网络实现) import socket server = socket.socket() server.bind((' ...
- 基于Laravel框架下使用守护进程supervisor实现定时任务(毫秒)
本篇文章给大家带来的内容是关于基于Laravel框架下使用守护进程supervisor实现定时任务(毫秒),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 公司需要实现X分钟内每隔Y秒 ...
- JS修改URL参数,并修改前页面的地址
function changeURLArg(url,arg,arg_val){ var pattern=arg+'=([^&]*)'; var replaceText=arg+'='+arg_ ...
- 第十九节:Asp.Net Core WebApi基础总结和请求方式
一. 基础总结 1.Restful服务改造 Core下的WebApi默认也是Restful格式服务,即通过请求方式(Get,post,put,delete)来区分请求哪个方法,请求的URL中不需要写方 ...
- 一个小巧,也很nice的“小日历”--一个Android App
一个小巧也很Nice的“小日历” 背景 因为,常用日历记一些事情,Android自带的日历,如果有事情,会显示一个小点,然后点击进去后才能看到事情的具体内容,不是很方便. 所以,写了一个“小日历” 特 ...
- 【linux】CentOS 查看系统时间,修改时区
===============CentOS 7.6================ 1.查看系统时间 date 查看当前系统时间以及时区结果是: Mon Jul 8 09:23:31 UTC 2019 ...
- [转] js网络请求跨域问题汇总(携带cookie)
前端程序使用extjs写,在本地测试,发送请求到服务器时,发现存在跨域的问题,cookie也没有set成功,于是乎在这里整理一下解决过程 由于篇幅较长,不想看解决过程的可以翻到最后看总结1.跨域允许2 ...
- 关于宝塔面板windows版6.2的一些使用心得
关于宝塔面板windows版6.2的一些使用心得 第一次使用windows版本的 给客户搭建 asp+mssql的需求 心得1 安装 server2012 基于python开发的,所以安装的 ...