This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n+1)
#define pii pair<int, int>
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define f first
#define s second
#define pb push_back
#define endl '\n'
const ll maxn = 1e5+5;
const ll inf = (1ll<<60);
vector<int> graph(maxn);
vector<bool> occ(maxn);
int dfs(int x){
if (occ[graph[x]]==0) {
occ[graph[x]]=1;
return dfs(graph[x])+1;
}
else return 1;
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n; cin>>n;
vector<string> pos(n);
vector<pair<string, string> > Graph(n);
REP(i, n){
cin>>Graph[i].f>>Graph[i].s;
pos[i]=Graph[i].f;
}
sort(ALL(pos));
REP(i, n){
int a=upper_bound(ALL(pos), Graph[i].f)-pos.begin();
int b=upper_bound(ALL(pos), Graph[i].s)-pos.begin();
graph[a]=b;
}
int ans=0;
REP1(i, n){
if (occ[i]) continue;
occ[i]=1;
int ret=dfs(i);
if (ret!=2) ans+=ret/2+ret%2;
}
if (n%2==1) ans=-1;
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |