Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. /// author: Mr.Hakimov
  2.  
  3. #include <bits/stdc++.h>
  4. #include <chrono>
  5.  
  6. #define fi first
  7. #define se second
  8. #define pb push_back
  9. #define pf push_front
  10. #define Pb pop_back
  11. #define Pf pop_front
  12. #define all(x) (x).begin(), (x).end()
  13. #define fin(s) freopen(s, "r", stdin);
  14. #define fout(s) freopen(s, "w", stdout);
  15.  
  16. /* Just some advices:
  17. 1. If I use set/multiset, I will check it - is it correct to use set/multiset in a problem?
  18. 2. If I can't solve problem, I must write a program, which could help me to understand it much more better)
  19. ...
  20. */
  21.  
  22. using namespace std;
  23.  
  24. #pragma GCC optimize("Ofast")
  25. #pragma GCC optimize ("unroll-loops")
  26. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  27.  
  28. typedef long long LL;
  29. typedef long double LD;
  30.  
  31. int TN = 1;
  32. vector<vector<int>> g(1001, vector<int> (1001));
  33. vector<vector<int>> revG(1001, vector<int> (1001));
  34. bool usd[1001];
  35. int n;
  36.  
  37. void dfs(int u, int m, vector<vector<int>> & g) {
  38.     usd[u] = true;
  39.     for (int i = 1; i <= n; ++i) {
  40.         if (g[u][i] <= m && !usd[i]) {
  41.             dfs(i, m, g);
  42.         }
  43.     }
  44. }
  45.  
  46. bool can(int m) {
  47.     for (int i = 1; i <= n; ++i) {
  48.         usd[i] = false;
  49.     }
  50.    
  51.     dfs(1, m, g);
  52.  
  53.     for (int i = 1; i <= n; ++i) {
  54.         if (!usd[i]) {
  55.             return false;
  56.         }
  57.     }
  58.    
  59.     for (int i = 1; i <= n; ++i) {
  60.         usd[i] = false;
  61.     }
  62.    
  63.     dfs(1, m, revG);
  64.    
  65.     for (int i = 1; i <= n; ++i) {
  66.         if (!usd[i]) {
  67.             return false;
  68.         }
  69.     }
  70.  
  71.     return true;
  72. }
  73.  
  74. void solve() {
  75.     cin >> n;
  76.  
  77.     for (int i = 1; i <= n; ++i) {
  78.         for (int j = 1; j <= n; ++j) {
  79.             cin >> g[i][j];
  80.             revG[j][i] = g[i][j];
  81.         }
  82.     }
  83.  
  84.     int l = -1;
  85.     int r = 1e9;
  86.  
  87.     while (r - l > 1) {
  88.         int m = (l + r) / 2;
  89.        
  90.         //cout << m << " " << can(m) << endl;
  91.  
  92.         if (can(m)) {
  93.             r = m;
  94.         } else {
  95.             l = m;
  96.         }
  97.     }
  98.  
  99.     if (can(r)) {
  100.         cout << r;
  101.     } else {
  102.         cout << -1;
  103.     }
  104. }
  105.  
  106. int main() {
  107.     auto start = chrono::steady_clock::now();
  108.     ios_base::sync_with_stdio(0);
  109.     cin.tie(nullptr); cout.tie(nullptr);
  110.     /// =========================================
  111.     /// fin("input.txt"); fout("output.txt");
  112.     fin("avia.in"); fout("avia.out");
  113.     /// cin >> TN;
  114.     /// =========================================
  115.     while (TN--) solve();
  116.     auto finish = chrono::steady_clock::now();
  117.     auto elapsed_ms = chrono::duration_cast<chrono::milliseconds>(finish - start);
  118.     cerr << endl << "Time: " << elapsed_ms.count() << " ms\n";
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement