-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path1278D.cpp
More file actions
executable file
·65 lines (54 loc) · 1.29 KB
/
1278D.cpp
File metadata and controls
executable file
·65 lines (54 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Problem Code: 1278D
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <set>
#include <algorithm>
using namespace std;
void DFS(int s, vector<bool>& visited, vector< vector<int> >& adj) {
visited[s] = true;
for (int v: adj[s])
if (!visited[v])
DFS(v, visited, adj);
}
string segment_tree(int n, vector< pair<int, int> >& seg) {
int r, edges = 0;
vector<bool> visited(n);
set< pair<int, int> > sweep;
vector< vector<int> > adj(n);
sort(seg.begin(), seg.end());
// Sweep line algorithm
for (int j, i = 0; i < n && edges < n; i++) {
for (auto it = sweep.begin(); it != sweep.end() && edges < n; ) {
r = (*it).first;
j = (*it).second;
if (r < seg[i].first) {
it = sweep.erase(it);
continue;
}
else if (r > seg[i].second)
break;
adj[i].push_back(j);
adj[j].push_back(i);
edges++;
it++;
}
sweep.insert({seg[i].second, i});
}
// Tree has exactly n - 1 edges
if (edges != n - 1)
return "NO";
// Check if tree is connected
DFS(0, visited, adj);
return (count(visited.begin(), visited.end(), true) == n) ? "YES" : "NO";
}
int main() {
int n;
cin >> n;
vector< pair<int, int> > seg(n);
for (int i = 0; i < n; i++)
cin >> seg[i].first >> seg[i].second;
cout << segment_tree(n, seg);
return 0;
}