#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int) x.size()
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define ROF(i, a, b) for(int i=(b-1); i>=(a); i--)
#define F0R(i, n) FOR(i, 0, n)
#define R0F(i, n) ROF(i, 0, n)
#define trav(x, y) for(auto&x:y)
using ll=long long;
using ld=long double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using pli=pair<ll, int>;
using vi=vector<int>;
using vl=vector<ll>;
using vpii=vector<pii>;
template<class T> inline bool ckmin(T&a, const T&b) {return b<a?a=b,1:0;}
template<class T> inline bool ckmax(T&a, const T&b) {return b>a?a=b,1:0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl='\n';
const int mxN=1e5+10;
const int MOD=1e9+7;
const ll infLL=1e18;
const ld eps=1e-6;
int n;
string s[mxN], t[mxN];
map<string, int> mp;
int a[mxN];
int deg[mxN];
int par[mxN], sz[mxN];
bool alive[mxN];
int used[mxN];
queue<int> q;
int ans=0;
int Find(int u){
return par[u]=(par[u]==u?par[u]:Find(par[u]));
}
bool comb(int e1, int e2){
e1=Find(e1); e2=Find(e2);
if(e1==e2) return 0;
if(sz[e1]<sz[e2]) swap(e1, e2);
sz[e1]+=sz[e2]; sz[e2]=0;
par[e2]=e1;
return 1;
}
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
if(n&1) {cout << "-1\n"; return 0;}
F0R(i, n) par[i]=i, sz[i]=1;
F0R(i, n)
cin >> s[i] >> t[i], mp[s[i]]=i;
F0R(i, n){
a[i]=mp[t[i]];
deg[a[i]]++;
alive[i]=1;
// cout << a[i] << nl;
}
F0R(i, n)
if(deg[i]==0){
q.push(i);
}
// cout << "-----\n";
while(!q.empty()){
auto T=q.front(); q.pop();
assert(deg[T]==0);
// cout << T << nl;
if(alive[T]==0) continue;
if(alive[a[T]]==0) continue;
alive[T]=0;
alive[a[T]]=0;
if(a[a[T]]!=T)
ans++;
if(alive[a[a[T]]]) {
deg[a[a[T]]]--;
if(deg[a[a[T]]]==0)
q.push(a[a[T]]);
}
}
F0R(i, n){
if(alive[i]){
if(alive[a[i]]) comb(a[i], i);
}
}
F0R(i, n){
if(alive[i]&&!used[Find(i)]){
used[Find(i)]=1;
if(sz[Find(i)]!=2){
ans+=(sz[Find(i)]+1)/2;
}
}
}
cout << ans << nl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6660 KB |
Output is correct |
2 |
Correct |
4 ms |
6532 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
4 ms |
6604 KB |
Output is correct |
5 |
Correct |
4 ms |
6476 KB |
Output is correct |
6 |
Correct |
4 ms |
6476 KB |
Output is correct |
7 |
Correct |
4 ms |
6604 KB |
Output is correct |
8 |
Incorrect |
4 ms |
6604 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
158 ms |
16064 KB |
Output is correct |
5 |
Correct |
152 ms |
16432 KB |
Output is correct |
6 |
Correct |
154 ms |
16024 KB |
Output is correct |
7 |
Correct |
4 ms |
6476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
16488 KB |
Output is correct |
2 |
Correct |
165 ms |
16468 KB |
Output is correct |
3 |
Correct |
166 ms |
16416 KB |
Output is correct |
4 |
Correct |
5 ms |
6476 KB |
Output is correct |
5 |
Correct |
148 ms |
16152 KB |
Output is correct |
6 |
Correct |
147 ms |
16688 KB |
Output is correct |
7 |
Correct |
167 ms |
16580 KB |
Output is correct |
8 |
Correct |
161 ms |
16492 KB |
Output is correct |
9 |
Correct |
140 ms |
16760 KB |
Output is correct |
10 |
Correct |
120 ms |
16516 KB |
Output is correct |
11 |
Correct |
4 ms |
6604 KB |
Output is correct |
12 |
Correct |
4 ms |
6604 KB |
Output is correct |
13 |
Correct |
4 ms |
6604 KB |
Output is correct |
14 |
Correct |
5 ms |
6604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6660 KB |
Output is correct |
2 |
Correct |
4 ms |
6532 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
4 ms |
6604 KB |
Output is correct |
5 |
Correct |
4 ms |
6476 KB |
Output is correct |
6 |
Correct |
4 ms |
6476 KB |
Output is correct |
7 |
Correct |
4 ms |
6604 KB |
Output is correct |
8 |
Incorrect |
4 ms |
6604 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |