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

原文地址:https://www.cnblogs.com/YDDDD/p/12076359.html

时间: 2024-08-30 13:22:37

Educational Codeforces Round 78 (Rated for Div. 2) D. Segment Tree的相关文章

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 perform a sequence of operations: during the first operation you choose one of these numbers and increase it by 1; during the second operation you choos

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 a modern web service it includes login feature. And that always implies password security problems. Polycarp decided to store the hash of the password

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

比赛传送门 A. Shuffle Hashing 题意:加密字符串.可以把字符串的字母打乱后再从前面以及后面接上字符串.问加密后的字符串是否符合加密规则. 题解:字符串的长度很短,直接暴力搜索所有情况 // https://codeforces.com/contest/1278/problem/A #include<iostream> #include<cstdio> #include<cstring> using namespace std; int T; char

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

链接 直接用数组记录每个字母的个数即可 #include<bits/stdc++.h> using namespace std; int a[26] = {0}; int b[26] = {0}; int judge() { //cout<<"111"<<endl; for (int i = 0; i < 26; ++i) { if (a[i]!=b[i]) { return 0; } } return 1; } int main() { //

Educational Codeforces Round 78 (Rated for Div. 2)

C - Berry Jam 题意:给2n个罐子,分别是[1,2n],站在[n,n+1]的中间,吃掉包含这个点的左边连续一段和右边连续一段,使得剩下的罐子红色的和蓝色的数量相等.吃最少的罐子. 题解:把红色蓝色变成+1和-1,那么就把左边的前缀和插进map里面,然后右边的后缀和在map里面查询相反数,查到就更新.注意因为是吃得越少越好所以同一个值,map里存的应该是最靠右的前缀. 注意!要加上为0的前缀和和后缀和! 不过现场可以看出来并造样例验证,也是能力提高了一点. 4 1 1 1 1 1 2

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 Limit: 2 s\quad Memory Limit: 256 MB \] 处理出 \(s_1\) 中各个字符出现的次数,然后双指针维护 \(s_2\) 中每一段长度为 \(len(s_1)\) 的串中字符出现的次数,如果存在某一段和 \(s_1\) 的字符次数相同,则是答案. view #inclu

Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam

Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were 2n2n jars of strawberry and blueberry jam. All the 2n2n jars are arranged in a row. The stairs to the basement are exactly in

Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum

https://codeforces.com/contest/1073/problem/E 题意 求出l到r之间的符合要求的数之和,结果取模998244353 要求:组成数的数位所用的数字种类不超过k种 思路 这题一看就是个数位dp的模板题,但是由于以前没有完全理解数位dp加上xjb套模版,导致样例都没算出来 一开始这样定义状态 dp[i][j] 代表第cnt-i(高位到低位)位,cnt~i状态为j的总和 一直记录当前数字的数值,到边界的时候返回加到答案中,但是由于这样定义状态会导致一个状态对应

Educational Codeforces Round 48 (Rated for Div. 2) B Segment Occurrences

翻译 给你一个字符串\(s\)和另一个字符串\(t\),然后给你\(q\)个区间,问\(s\)在这些区间里的子串有多少个与\(t\)相同. 思路 一道要细心的模拟题,使用\(STL string\),暴力,前缀和,\(Hash\),\(Kmp\)都能做出来,然后我来介绍一下用 \(vector\)的做法. 首先预处理\(s\),从头到位找到每一个长度是字符串t的长度\(m\)的字符串,如果其与\(t\)相等,那么就往vector中压入\(1\),否则压入\(0\),这样,我们就找到每个编号的字符