#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const int MAXN = 1e5 + 7;
int n;
int g[MAXN];
int f[MAXN];
int deg[MAXN];
map<string, int> used;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
int k = 1;
for(int i = 1; i <= n; i++){
string s, t;
cin >> s >> t;
int aktS = used[s];
if(aktS == 0){
used[s] = k++;
aktS = used[s];
}
int aktT = used[t];
if(aktT == 0){
used[t] = k++;
aktT = used[t];
}
g[aktS] = aktT;
deg[aktT]++;
}
if(n % 2 == 1){
cout << -1 << '\n';
return 0;
}
set<pii> s;
int ans = n;
for(int i = 1; i <= n; i++){
if(g[i] != i && g[g[i]] == i){
ans--;
f[i] = 1;
continue;
}
s.insert({deg[i], i});
}
while(sz(s)){
pii v = *s.begin();
s.erase(s.begin());
if(deg[v.se] != 0){
break;
}
int i = v.se;
if(g[i] != i && !f[g[i]]){
f[i] = 1;
f[g[i]] = 1;
ans--;
s.erase({deg[g[i]], g[i]});
if(!f[g[g[i]]]){
s.erase({deg[g[g[i]]], g[g[i]]});
deg[g[g[i]]]--;
s.insert({deg[g[g[i]]], g[g[i]]});
}
}
}
vi vec;
for(int i = 1; i <= n; i++){
if(!f[i]){
vec.pb(i);
}
}
for(auto ele : vec){
if(f[ele]){
continue;
}
int x = 0;
int ind = ele;
while(!f[ind]){
x++;
f[ind] = 1;
ind = g[ind];
}
ans -= (x / 2);
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
131 ms |
16588 KB |
Output is correct |
5 |
Correct |
127 ms |
16600 KB |
Output is correct |
6 |
Correct |
128 ms |
16632 KB |
Output is correct |
7 |
Correct |
111 ms |
11088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
16212 KB |
Output is correct |
2 |
Correct |
145 ms |
16212 KB |
Output is correct |
3 |
Correct |
109 ms |
16588 KB |
Output is correct |
4 |
Correct |
106 ms |
11144 KB |
Output is correct |
5 |
Correct |
148 ms |
16176 KB |
Output is correct |
6 |
Correct |
144 ms |
16208 KB |
Output is correct |
7 |
Correct |
147 ms |
16208 KB |
Output is correct |
8 |
Correct |
135 ms |
16592 KB |
Output is correct |
9 |
Correct |
125 ms |
16236 KB |
Output is correct |
10 |
Correct |
100 ms |
15696 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
131 ms |
16588 KB |
Output is correct |
20 |
Correct |
127 ms |
16600 KB |
Output is correct |
21 |
Correct |
128 ms |
16632 KB |
Output is correct |
22 |
Correct |
111 ms |
11088 KB |
Output is correct |
23 |
Correct |
145 ms |
16212 KB |
Output is correct |
24 |
Correct |
145 ms |
16212 KB |
Output is correct |
25 |
Correct |
109 ms |
16588 KB |
Output is correct |
26 |
Correct |
106 ms |
11144 KB |
Output is correct |
27 |
Correct |
148 ms |
16176 KB |
Output is correct |
28 |
Correct |
144 ms |
16208 KB |
Output is correct |
29 |
Correct |
147 ms |
16208 KB |
Output is correct |
30 |
Correct |
135 ms |
16592 KB |
Output is correct |
31 |
Correct |
125 ms |
16236 KB |
Output is correct |
32 |
Correct |
100 ms |
15696 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
152 ms |
16184 KB |
Output is correct |
38 |
Correct |
144 ms |
16208 KB |
Output is correct |
39 |
Correct |
140 ms |
16196 KB |
Output is correct |
40 |
Correct |
140 ms |
16128 KB |
Output is correct |
41 |
Correct |
155 ms |
16268 KB |
Output is correct |
42 |
Correct |
160 ms |
16212 KB |
Output is correct |
43 |
Correct |
151 ms |
16232 KB |
Output is correct |
44 |
Correct |
152 ms |
16208 KB |
Output is correct |
45 |
Correct |
147 ms |
16208 KB |
Output is correct |
46 |
Correct |
149 ms |
16208 KB |
Output is correct |
47 |
Correct |
133 ms |
16212 KB |
Output is correct |
48 |
Correct |
144 ms |
16212 KB |
Output is correct |
49 |
Correct |
164 ms |
16256 KB |
Output is correct |
50 |
Correct |
110 ms |
16588 KB |
Output is correct |
51 |
Correct |
107 ms |
11092 KB |
Output is correct |
52 |
Correct |
145 ms |
16064 KB |
Output is correct |
53 |
Correct |
139 ms |
16280 KB |
Output is correct |
54 |
Correct |
147 ms |
16208 KB |
Output is correct |
55 |
Correct |
126 ms |
16620 KB |
Output is correct |
56 |
Correct |
120 ms |
16292 KB |
Output is correct |
57 |
Correct |
101 ms |
15696 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
476 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
344 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
126 ms |
16692 KB |
Output is correct |
66 |
Correct |
123 ms |
16724 KB |
Output is correct |
67 |
Correct |
129 ms |
16588 KB |
Output is correct |
68 |
Correct |
108 ms |
11088 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
1 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
604 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |