#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define printv(a, b) { \
for(auto pv : a) b << pv << " "; \
b << "\n"; \
}
using namespace std;
using pii = pair<int, int>;
map<string, int> id;
int num = 1;
int getid(string& s){
if(id.find(s) == id.end()) id[s] = num++;
return id[s];
}
int n;
vector<vector<int>> g;
vector<int> dp0, dp1;
void init(){
g.resize(n + 1);
dp0.resize(n + 1);
dp1.resize(n + 1);
}
void dfs(int now){
int sum = 0, mn = n;
int cnt = 0;
for(int i : g[now]){
if(i == now) continue;
dfs(i);
cnt++;
sum += dp1[i];
mn = min(mn, dp0[i] - dp1[i]);
}
if(cnt == 0){
dp0[now] = 0; dp1[now] = 1;
}
else{
dp0[now] = sum;
dp1[now] = sum + mn + 1;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
init();
vector<int> f(n + 1);
for(int i = 1; i <= n; i++){
string s, t;
cin >> s >> t;
int u = getid(s), v = getid(t);
f[u] = v;
}
for(int i = 1; i <= n; i++){
if(f[f[i]] == i && f[i] != i) continue;
g[f[i]].eb(i);
}
//for(auto& i : id) cerr << i.F << " " << i.S << "\n";
if(n % 2){
cout << "-1\n";
return 0;
}
int ans = 0;
for(int i = 1; i <= n; i++){
if(f[i] != i) continue;
dfs(i);
ans += dp1[i];
}
//printv(dp0, cerr);
//printv(dp1, cerr);
cout << ans << "\n";
return 0;
}
# |
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 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
13916 KB |
Output is correct |
2 |
Correct |
186 ms |
18148 KB |
Output is correct |
3 |
Correct |
147 ms |
16860 KB |
Output is correct |
4 |
Correct |
170 ms |
15408 KB |
Output is correct |
5 |
Correct |
178 ms |
24148 KB |
Output is correct |
6 |
Correct |
196 ms |
15416 KB |
Output is correct |
7 |
Correct |
194 ms |
15444 KB |
Output is correct |
8 |
Correct |
161 ms |
15728 KB |
Output is correct |
9 |
Correct |
162 ms |
14852 KB |
Output is correct |
10 |
Correct |
128 ms |
14436 KB |
Output is correct |
11 |
Correct |
0 ms |
312 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 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 |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |