Submission #2623193
Source Code Expand
#include <bits/stdc++.h> #include <unistd.h> #define ll long long #define inf 1000000007 #define inf16 0x3f3f3f3f #define INF 1000000000000000007LL #define VI vector<int> #define VPII vector<pair<int, int> > #define VLL vector<ll> #define PII pair<int, int> #define st first #define nd second #define pb push_back #define mp make_pair #define eb emplace_back #define endl '\n' #define ALL(c) (c).begin(), (c).end() using namespace std; const int N = 1e5+7; int n, ans; int A[N]; int B[N]; PII mB[N]; //maksymalne B dla danego A PII mA[N]; //maksymalne A dla danego B map<PII, int> takie; VI G[N]; queue<int> Q; int dist[3][N]; bool check() { for(int i = 1; i <= 2; ++i) { for(int v = 1; v <= n; ++v) { dist[i][v] = inf; } dist[i][i] = 0; Q.push(i); while(!Q.empty()) { int v = Q.front(); Q.pop(); for(auto it:G[v]) { if(dist[i][it]>dist[i][v]+1) { dist[i][it] = dist[i][v]+1; Q.push(it); } } } } for(int i = 1; i <= n; ++i) { if(A[i]!=dist[1][i]) return 0; if(B[i]!=dist[2][i]) return 0; } return 1; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n; for(int i = 1; i <= n; ++i) { cin >> A[i] >> B[i]; takie[{A[i], B[i]}] = i; mB[A[i]] = max(mB[A[i]], mp(B[i], i)); mA[B[i]] = max(mA[B[i]], mp(A[i], i)); } if(A[2]!=B[1] || A[1] || B[2]) { cout << -1 << endl; return 0; } if(A[2]==1) { G[1].pb(2); G[2].pb(1); ++ans; } for(int i = 3; i <= n; ++i) { if(takie.find({A[i]-1, B[i]-1})!=takie.end()) { G[i].pb(takie[{A[i]-1, B[i]-1}]); G[takie[{A[i]-1, B[i]-1}]].pb(i); ++ans; } else { if(mB[A[i]-1].st>B[i]-1 && mA[B[i]-1].st>A[i]-1) { G[i].pb(mB[A[i]-1].nd); G[i].pb(mA[B[i]-1].nd); G[mB[A[i]-1].nd].pb(i); G[mA[B[i]-1].nd].pb(i); ans += 2; } else { cout << -1 << endl; return 0; } } } if(!check()) { cout << -1 << endl; return 0; } cout << ans << endl; }
Submission Info
Submission Time | |
---|---|
Task | A - Distance Pairs |
User | shadowatyy |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 2117 Byte |
Status | WA |
Exec Time | 137 ms |
Memory | 15104 KB |
Judge Result
Set Name | sample | All | ||||||
---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 1500 | ||||||
Status |
|
|
Set Name | Test Cases |
---|---|
sample | sample-01.txt, sample-02.txt |
All | sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, sample-01.txt, sample-02.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01-01.txt | AC | 58 ms | 11264 KB |
01-02.txt | AC | 18 ms | 3968 KB |
01-03.txt | AC | 2 ms | 3200 KB |
01-04.txt | AC | 2 ms | 3200 KB |
01-05.txt | WA | 3 ms | 3328 KB |
01-06.txt | WA | 18 ms | 6272 KB |
01-07.txt | WA | 62 ms | 8704 KB |
01-08.txt | WA | 64 ms | 8704 KB |
01-09.txt | WA | 46 ms | 8576 KB |
01-10.txt | WA | 31 ms | 8576 KB |
01-11.txt | WA | 55 ms | 8576 KB |
01-12.txt | WA | 52 ms | 8576 KB |
01-13.txt | WA | 137 ms | 14592 KB |
01-14.txt | WA | 128 ms | 15104 KB |
01-15.txt | AC | 136 ms | 15104 KB |
01-16.txt | AC | 24 ms | 8696 KB |
01-17.txt | AC | 27 ms | 9072 KB |
01-18.txt | AC | 28 ms | 9072 KB |
01-19.txt | WA | 132 ms | 14464 KB |
01-20.txt | WA | 128 ms | 13568 KB |
01-21.txt | WA | 131 ms | 13696 KB |
01-22.txt | WA | 108 ms | 11392 KB |
01-23.txt | AC | 65 ms | 8704 KB |
01-24.txt | AC | 55 ms | 7552 KB |
01-25.txt | AC | 24 ms | 4224 KB |
01-26.txt | AC | 24 ms | 4224 KB |
01-27.txt | AC | 44 ms | 6528 KB |
01-28.txt | AC | 41 ms | 6016 KB |
01-29.txt | AC | 2 ms | 3200 KB |
01-30.txt | AC | 2 ms | 3200 KB |
01-31.txt | AC | 2 ms | 3200 KB |
sample-01.txt | AC | 2 ms | 3200 KB |
sample-02.txt | AC | 2 ms | 3200 KB |