CF1278D.Segment Tree

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn segments [l1,r1],[l2,r2],…,[ln,rn][l_1, r_1], [l_2, r_2], \dots, [l_n, r_n], li<ril_i \lt r_i for every ii. 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 nn vertices from these segments. Vertices vv and uu are connected by an edge if and only if segments [lv,rv][l_v, r_v] and [lu,ru][l_u, r_u] intersect and neither of it lies fully inside the other one.

For example, pairs ([1,3],[2,4])([1, 3], [2, 4]) and ([5,10],[3,7])([5, 10], [3, 7]) will induce the edges but pairs ([1,2],[3,4])([1, 2], [3, 4]) and ([5,7],[3,10])([5, 7], [3, 10]) will not.

Determine if the resulting graph is a tree or not.

正如题目名称所暗示的,你需要对线段和树进行一些操作。

回忆一下,树是一种连通的无向图,且其任意两个顶点之间恰好存在一条简单路径。

你将得到 nn 条线段 [l1,r1],[l2,r2],…,[ln,rn][l_1, r_1], [l_2, r_2], \dots, [l_n, r_n],其中对每个 ii 都有 li<ril_i \lt r_i。保证所有线段的端点均为整数,且所有端点互不相同——即不存在任意两条线段,使得它们起点相同、终点相同,或其中一条的起点与另一条的终点相同。

我们从这些线段出发构造一个含 nn 个顶点的图:顶点 vv 与 uu 之间有一条边,当且仅当线段 [lv,rv][l_v, r_v] 与 [lu,ru][l_u, r_u] 相交,且二者中没有任何一条完全包含于另一条之内。

例如,线段对 ([1,3],[2,4])([1, 3], [2, 4]) 和 ([5,10],[3,7])([5, 10], [3, 7]) 会产生边;而线段对 ([1,2],[3,4])([1, 2], [3, 4]) 和 ([5,7],[3,10])([5, 7], [3, 10]) 则不会产生边。

请判断所得到的图是否为一棵树。

输入格式

The first line contains a single integer nn (1≤n≤5⋅1051 \le n \le 5 \cdot 10^5) — the number of segments.

The ii-th of the next nn lines contain the description of the ii-th segment — two integers lil_i and rir_i (1≤li<ri≤2n1 \le l_i \lt r_i \le 2n).

It is guaranteed that all segments borders are pairwise distinct.

第一行包含一个整数 nn(1≤n≤5⋅1051 \le n \le 5 \cdot 10^5)—— 表示线段的数量。

接下来的 nn 行中,第 ii 行描述第 ii 条线段——包含两个整数 lil_i 和 rir_i(1≤li<ri≤2n1 \le l_i \lt r_i \le 2n)。

保证所有线段的端点两两互不相同。

输出格式

Print "YES" if the resulting graph is a tree and "NO" otherwise.

如果所得图是一棵树,则输出“YES”,否则输出“NO”。

输入输出样例

  • 输入#1

    6
    9 12
    2 11
    1 3
    6 10
    5 7
    4 8

    输出#1

    YES
  • 输入#2

    5
    1 3
    2 4
    5 9
    6 8
    7 10

    输出#2

    NO
  • 输入#3

    5
    5 8
    3 6
    2 9
    7 10
    1 4

    输出#3

    NO

说明/提示

The graph corresponding to the first example:

The graph corresponding to the second example:

The graph corresponding to the third example:

第一个示例对应的图:

第二个示例对应的图:

第三个示例对应的图:

输入解题思路,AI测评打分。不知道怎么写?

首页