forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshibuyacrossing.cpp
85 lines (71 loc) · 1.73 KB
/
shibuyacrossing.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
// Returns an array with the indexes of the LIS
template <class T>
vector<int> LIS(vector<T>& v) {
vector<int> p(v.size(), -1);
vector<int> t(v.size(), 0);
int lis = 1;
for(int i = 1; i < v.size(); i++) {
if(v[i] <= v[t[0]]) {
t[0] = i;
}
else if(v[i] > v[t[lis - 1]]) {
p[i] = t[lis - 1];
t[lis++] = i;
}
else {
int l = -1;
int r = lis - 1;
while(r - l > 1) {
int m = l + (r - l) / 2;
if(v[t[m]] >= v[i]) r = m;
else l = m;
}
p[i] = t[r - 1];
t[r] = i;
}
}
vector<int> ans;
for(int i = t[lis - 1]; i >= 0; i = p[i]) {
ans.push_back(i);
}
reverse(ans.begin(), ans.end());
return ans;
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
vector<int> deg(n,0);
for(int i = 0; i < m; i++) {
int n1, n2;
cin >> n1 >> n2;
n1--; n2--;
adj[n2].push_back(n1);
deg[n1]++;
}
set<int> zeroin;
for(int i = 0; i < n; i++) {
if(deg[i] == 0) {
zeroin.insert(i);
}
}
// Use toposort to build the permutation graph
vector<int> permutation;
while(!zeroin.empty()) {
int curr = *zeroin.begin();
zeroin.erase(zeroin.begin());
permutation.push_back(curr);
for(auto next : adj[curr]) {
deg[next]--;
if(deg[next] == 0) {
zeroin.insert(next);
}
}
}
for(auto& i : permutation) {
i *= -1;
}
cout << LIS(permutation).size() << endl;
}