zoukankan      html  css  js  c++  java
  • 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
    ";
                    return 0;
                }
                else
                {
                    cnt++;
                    F[tl] = tr;
                }
                ++it;
            }
            Mp[Pa[i].second] = i;
        }
        if (cnt != n-1)
            cout << "NO
    ";
        else
            cout << "YES
    ";
    
        return 0;
    }
    
  • 相关阅读:
    市场定位和硬件设计的错误浅谈GM8126的封装
    在Protel的机械层中如何加“机械孔”的问题
    Protel中导入导出GERBER的相关问题
    程序猿与鸡
    AltiumDesinger中Comment属性与BOM表的联系
    用CSS实现动态效果的画廊
    Two scripts work with git pull/push
    emacs中remember.el 日期乱码问题
    使用Python解压,对比文件
    Save a tree as XML using XmlSerializer
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12076359.html
Copyright © 2011-2022 走看看