Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pb push_back
- #define pf push_front
- #define mp make_pair
- #define sz size
- #define ll long long
- #define ld long double
- #define fs first
- #define sc second
- #define forn(i, f, t) for(int i = f; i < t; i++)
- #define all(x) (x).begin(), (x).end()
- #define ins insert
- const int INF = 2147483647;
- const int MOD = 1000000007;
- const ll INF64 = 9223372036854775807;
- const ld EPS = 1e-7;
- using namespace std;
- pair<int, int> q[100001];
- vector<char> used;
- set<int> d[100001];
- vector<int> res;
- void dfs(int v){
- used[v] = 1;
- for (auto it = d[v].begin(); it != d[v].end(); it++) if (!used[*it]) dfs(*it);
- res.pb(v);
- }
- bool check(int n, int m){
- // cout << n << " " << m << "\n";
- forn(i, 0, n) d[i].clear();
- used.clear();
- res.clear();
- used.assign(n, 0);
- forn(i, 0, m) d[q[i].fs].ins(q[i].sc);
- forn(i, 0, n) if (!used[i]) dfs(i);
- reverse(all(res));
- // forn(i, 0, res.sz()) cout << res[i] << " "; cout << "\n";
- forn(i, 1, res.sz()) if (d[res[i - 1]].find(res[i]) == d[res[i - 1]].end()) return 0;
- if (res.sz() < n) return 0;
- return 1;
- }
- int main(){
- int n, m, f, t;
- scanf("%d%d", &n, &m);
- forn(i, 0, n){
- scanf("%d%d", &f, &t);
- q[i] = mp(f - 1, t - 1);
- }
- int l = 1, r = m;
- while (l < r - 1){
- int m = (l + r) / 2;
- if (check(n, m)) r = m;
- else l = m;
- }
- printf("%d\n", check(n, l) ? l : (check(n, r) ? r : -1));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement