Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// author: Mr.Hakimov
- #include <bits/stdc++.h>
- #include <chrono>
- #define fi first
- #define se second
- #define pb push_back
- #define pf push_front
- #define Pb pop_back
- #define Pf pop_front
- #define all(x) (x).begin(), (x).end()
- #define fin(s) freopen(s, "r", stdin);
- #define fout(s) freopen(s, "w", stdout);
- /* Just some advices:
- 1. If I use set/multiset, I will check it - is it correct to use set/multiset in a problem?
- 2. If I can't solve problem, I must write a program, which could help me to understand it much more better)
- ...
- */
- using namespace std;
- #pragma GCC optimize("Ofast")
- #pragma GCC optimize ("unroll-loops")
- #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- typedef long long LL;
- typedef long double LD;
- int TN = 1;
- vector<vector<int>> g(1001, vector<int> (1001));
- vector<vector<int>> revG(1001, vector<int> (1001));
- bool usd[1001];
- int n;
- void dfs(int u, int m, vector<vector<int>> & g) {
- usd[u] = true;
- for (int i = 1; i <= n; ++i) {
- if (g[u][i] <= m && !usd[i]) {
- dfs(i, m, g);
- }
- }
- }
- bool can(int m) {
- for (int i = 1; i <= n; ++i) {
- usd[i] = false;
- }
- dfs(1, m, g);
- for (int i = 1; i <= n; ++i) {
- if (!usd[i]) {
- return false;
- }
- }
- for (int i = 1; i <= n; ++i) {
- usd[i] = false;
- }
- dfs(1, m, revG);
- for (int i = 1; i <= n; ++i) {
- if (!usd[i]) {
- return false;
- }
- }
- return true;
- }
- void solve() {
- cin >> n;
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= n; ++j) {
- cin >> g[i][j];
- revG[j][i] = g[i][j];
- }
- }
- int l = -1;
- int r = 1e9;
- while (r - l > 1) {
- int m = (l + r) / 2;
- //cout << m << " " << can(m) << endl;
- if (can(m)) {
- r = m;
- } else {
- l = m;
- }
- }
- if (can(r)) {
- cout << r;
- } else {
- cout << -1;
- }
- }
- int main() {
- auto start = chrono::steady_clock::now();
- ios_base::sync_with_stdio(0);
- cin.tie(nullptr); cout.tie(nullptr);
- /// =========================================
- /// fin("input.txt"); fout("output.txt");
- fin("avia.in"); fout("avia.out");
- /// cin >> TN;
- /// =========================================
- while (TN--) solve();
- auto finish = chrono::steady_clock::now();
- auto elapsed_ms = chrono::duration_cast<chrono::milliseconds>(finish - start);
- cerr << endl << "Time: " << elapsed_ms.count() << " ms\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement