Advertisement
PikMike

Untitled

Mar 18th, 2016
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define pb push_back
  4. #define pf push_front
  5. #define mp make_pair
  6. #define sz size
  7. #define ll long long
  8. #define ld long double
  9. #define fs first
  10. #define sc second
  11. #define forn(i, f, t) for(int i = f; i < t; i++)
  12. #define all(x) (x).begin(), (x).end()
  13. #define ins insert
  14.  
  15. const int INF = 2147483647;
  16. const int MOD = 1000000007;
  17. const ll INF64 = 9223372036854775807;
  18. const ld EPS = 1e-7;
  19.  
  20. using namespace std;
  21.  
  22. pair<int, int> q[100001];
  23.  
  24.  
  25. vector<char> used;
  26. set<int> d[100001];
  27. vector<int> res;
  28.  
  29.  
  30. void dfs(int v){
  31.     used[v] = 1;
  32.     for (auto it = d[v].begin(); it != d[v].end(); it++) if (!used[*it]) dfs(*it);
  33.     res.pb(v);
  34. }
  35.  
  36.  
  37. bool check(int n, int m){
  38.     // cout << n << " " << m << "\n";
  39.     forn(i, 0, n) d[i].clear();
  40.     used.clear();
  41.     res.clear();
  42.     used.assign(n, 0);
  43.     forn(i, 0, m) d[q[i].fs].ins(q[i].sc);
  44.     forn(i, 0, n) if (!used[i]) dfs(i);
  45.     reverse(all(res));
  46.     // forn(i, 0, res.sz()) cout << res[i] << " "; cout << "\n";
  47.     forn(i, 1, res.sz()) if (d[res[i - 1]].find(res[i]) == d[res[i - 1]].end()) return 0;
  48.     if (res.sz() < n) return 0;
  49.     return 1;
  50. }
  51.  
  52.  
  53. int main(){
  54.     int n, m, f, t;
  55.     scanf("%d%d", &n, &m);
  56.     forn(i, 0, n){
  57.         scanf("%d%d", &f, &t);
  58.         q[i] = mp(f - 1, t - 1);
  59.     }
  60.     int l = 1, r = m;
  61.     while (l < r - 1){
  62.         int m = (l + r) / 2;
  63.         if (check(n, m)) r = m;
  64.         else l = m;
  65.     }
  66.     printf("%d\n", check(n, l) ? l : (check(n, r) ? r : -1));
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement