Submission #7277565


Source Code Expand

Copy
#include <bits/stdc++.h>
 
#define int long long
 
#define pii pair<int, int>
 
#define x1 x1228
#define y1 y1228
 
#define left left228
#define right right228
 
#define pb push_back
#define eb emplace_back
 
#define mp make_pair                                                                
                                                                                                                                        
#define ff first                                                                  
#define ss second   
 
#define matr vector<vector<int> > 
 
#define all(x) x.begin(), x.end()
 
 
using namespace std;
typedef long long ll; 
typedef long double ld; 
                                                                                                   
const int maxn = 1000 + 7, mod = 1e9 + 9, inf = 1e9, MAXN = 1e6 + 7;
const double eps = 1e-9;                                                        
mt19937 rnd(time(0));
int n; 
int used[maxn][maxn]; 
vector<int> ord[maxn]; 

void solve() { 
    cin >> n; 
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n - 1; ++j) {
            int a; cin >> a, --a; 
            ord[i].pb(a); 
        }
        reverse(all(ord[i])); 
    }
    vector<pii> change;  
    for (int i = 0; i < n; ++i) {
        change.eb(i, ord[i].back()); 
    }
    int ans = 0; 
    while (change.size()) {
        vector<pii> change1; 
        for (auto v : change) {
            used[v.ff][v.ss] = 1;
            if (used[v.ss][v.ff]) {
                ord[v.ff].pop_back();
                ord[v.ss].pop_back();
                if (ord[v.ff].size())                               
                    change1.eb(v.ff, ord[v.ff].back()); 
                if (ord[v.ss].size())  
                    change1.eb(v.ss, ord[v.ss].back()); 
            }
        }
        swap(change, change1); 
        ++ans; 
    }
    for (int i = 0; i < n; ++i) {
        if (ord[i].size()) {
            cout << -1; 
            return; 
        }
    }
    cout << ans; 
}                                
   
signed main() {
#ifdef LOCAL
    freopen("TASK.in", "r", stdin);
    freopen("TASK.out", "w", stdout);
#else 
    
#endif // LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(20); 
    cout << fixed; 
    int t = 1; 
    for (int i = 0; i < t; ++i) {              
        solve();
    }
    return 0;
}  

Submission Info

Submission Time
Task E - League
User JuliaDream
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2490 Byte
Status AC
Exec Time 129 ms
Memory 16256 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 23
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23
Case Name Status Exec Time Memory
a01 AC 1 ms 256 KB
a02 AC 1 ms 256 KB
a03 AC 1 ms 256 KB
b04 AC 1 ms 256 KB
b05 AC 1 ms 256 KB
b06 AC 1 ms 256 KB
b07 AC 1 ms 256 KB
b08 AC 129 ms 16256 KB
b09 AC 72 ms 14336 KB
b10 AC 107 ms 16256 KB
b11 AC 98 ms 16256 KB
b12 AC 31 ms 10752 KB
b13 AC 24 ms 6272 KB
b14 AC 13 ms 5504 KB
b15 AC 11 ms 5120 KB
b16 AC 6 ms 2048 KB
b17 AC 5 ms 1536 KB
b18 AC 21 ms 6016 KB
b19 AC 18 ms 5760 KB
b20 AC 6 ms 2176 KB
b21 AC 5 ms 1792 KB
b22 AC 3 ms 1152 KB
b23 AC 2 ms 640 KB