Codeforces 1278D

violence

The sorting section and then the left end point set to maintain the right side is added to the n plus violence without a determination can then again dfs

 

When the thought of the first half of the race but did not think added to the n-hop has always wanted to merge the point you want to connect an equal share is relatively small and then exploded

The problem is thinking about where rigid thinking judge whether the tree

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int n;
int mir[maxn * 2], vis[maxn];
vector<pair<int, int> > v;
set<int> s;
vector<int> G[maxn];
void dfs(int u) {
    if(vis[u]) {
        return;
    }
    vis[u] = 1;
    for(auto v : G[u]) {
        dfs(v);
    }
}
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        int l, r; scanf("%d%d", &l, &r);
        v.push_back(make_pair(l, r));
        mir[r] = i;
    }
    int cnt = 0;
    sort(v.begin(), v.end());
    for(int i = 0; i < v.size(); ++i) {
        if(cnt == n) {
            break;
        }
        pair<int, int> o = v[i];
        int l = o.first, r = o.second;
        for(auto it = s.lower_bound(l); it != s.end(); ++it) {
            if(*it > r) {
                break;
            }
            G[mir[r]].push_back(mir[*it]);
            G[mir[*it]].push_back(mir[r]);
            ++cnt;
            if(cnt == n) {
                break;
            }
        }
        s.insert(r);
    }
    if(cnt != n - 1) {
        puts("NO");
        return 0;
    }
    dfs(1);
    for(int i = 1; i <= n; ++i) {
        if(!vis[i]) {
            puts("NO");
            return 0;
        }
    }
    puts("YES");
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/19992147orz/p/12071112.html