#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#define mkp make_pair
const int INF = 2147483647;
const int LNF = INF*INF;
const int MOD = 1000000007;
const int mod = 998244353;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
const int MAX = 100010;
int n;
map<string, int> mp;
int edge[MAX];
int indeg[MAX];
bool exist[MAX];
vi qu;
int res;
int query(string str){
if(mp.find(str) == mp.end()){
mp[str] = szof(mp) + 1;
}
return mp[str];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
FOR(i, 1, n, 1){
string su, sv;
cin>>su>>sv;
int u = query(su);
int v = query(sv);
edge[u] = v;
indeg[v]++;
}
if(n%2 == 1){
cout<<-1<<'\n';
return 0;
}
FOR(i, 1, n, 1){
exist[i] = 1;
if(indeg[i] == 0){
qu.pb(i);
}
}
res = n;
while(!qu.empty()){
int u = qu.back();
qu.pop_back();
if(exist[u]==0) continue;
int v = edge[u];
if(exist[v]==0) continue;
int w = edge[v];
exist[u] = 0;
exist[v] = 0;
indeg[v]--;
indeg[w]--;
res--;
if(indeg[w] == 0 and exist[w] == 1) qu.pb(w);
}
FOR(i, 1, n, 1){
if(exist[i]==0) continue;
int ptr = i;
int cnt = 0;
while(exist[ptr] == 1){
exist[ptr] = 0;
ptr = edge[ptr];
cnt++;
}
if(cnt == 2 and ptr == i){
res -= 2;
}
else{
res -= cnt / 2;
}
}
cout<<res<<'\n';
/*
FOR(i, 1, n, 1){
cerr<<exist[i]<<" ";
}
cerr<<endl;
*/
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
229 ms |
9768 KB |
Output is correct |
5 |
Correct |
212 ms |
9792 KB |
Output is correct |
6 |
Correct |
220 ms |
9896 KB |
Output is correct |
7 |
Correct |
223 ms |
9668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
222 ms |
10056 KB |
Output is correct |
2 |
Correct |
222 ms |
9924 KB |
Output is correct |
3 |
Correct |
180 ms |
9872 KB |
Output is correct |
4 |
Correct |
212 ms |
9668 KB |
Output is correct |
5 |
Correct |
236 ms |
9664 KB |
Output is correct |
6 |
Correct |
224 ms |
10520 KB |
Output is correct |
7 |
Correct |
215 ms |
10448 KB |
Output is correct |
8 |
Correct |
201 ms |
10440 KB |
Output is correct |
9 |
Correct |
204 ms |
10912 KB |
Output is correct |
10 |
Correct |
163 ms |
10052 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |