#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myrand(ll B){
return (ull)rng() % B;
}
inline double time() {
return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n; cin >> n;
map<string,int> mp;
auto id = [&](string s)->int{
if(mp.find(s) != mp.end()){
return mp[s];
}
int sz = mp.size();
mp[s] = sz; return sz;
};
vector<int> g(n,-1);
vector<int> deg(n);
vector<int> res(n,-1);
for (int i = 0; i < n; ++i) {
string s,t; cin >> s >> t;
int x = id(s), y = id(t);
g[x] = y;
deg[y]++;
}
if(n%2){
cout << -1 << endl;
return 0;
}
vector<vector<int>> v(n);
vector<pair<int,int>> pr;
queue<int> q;
for (int i = 0; i < n; ++i) {
if(deg[i] == 0){
q.push(i);
}
}
while (q.size()){
int s = q.front(); q.pop();
int t = g[s];
pr.push_back({s,t});
deg[t]--;
if(deg[t] == 0) q.push(t);
}
for(auto p:pr){
auto [x,y] = p;
if(deg[y] != 0){
if(res[x] == -1) v[y].push_back(x);
}
else{
if(res[x] == -1){
if(res[y] == -1){
res[x] = 0;
res[y] = 1;
}
else{
res[x] = 1;
}
}
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if(res[i] != -1 or deg[i] == 0) continue;
vector<int> cyc;
int cur = i;
do{
cyc.push_back(cur);
res[cur] = 0;
for(int j:v[cur]){
res[j] = 0;
}
cur = g[cur];
} while (cur != i);
int opt = 1e9;
int m = cyc.size();
if(m == 2){
opt = v[cyc[0]].size() + v[cyc[1]].size();
ans += opt;
continue;
}
{
vector<int> dp(m+1,1e9);
dp[0] = 0;
for (int j = 0; j < m; ++j) {
if(j+1 < m){
dp[j+2] = min(dp[j+2], dp[j]+1+(int)v[cyc[j]].size()+(int)v[cyc[j+1]].size());
}
dp[j+1] = min(dp[j+1], dp[j]+1+(int)v[cyc[j]].size());
if(v[cyc[j]].size()){
dp[j+1] = min(dp[j+1], dp[j]+1+(int)v[cyc[j]].size()-1);
}
}
opt = min(opt, dp[m]);
}
reverse(cyc.begin(), cyc.end());
int c = cyc.back(); cyc.pop_back();
reverse(cyc.begin(), cyc.end()); cyc.push_back(c);
{
vector<int> dp(m+1,1e9);
dp[0] = 0;
for (int j = 0; j < m; ++j) {
if(j+1 < m){
dp[j+2] = min(dp[j+2], dp[j]+1+(int)v[cyc[j]].size()+(int)v[cyc[j+1]].size());
}
dp[j+1] = min(dp[j+1], dp[j]+1+(int)v[cyc[j]].size());
if(v[cyc[j]].size()){
dp[j+1] = min(dp[j+1], dp[j]+1+(int)v[cyc[j]].size()-1);
}
}
opt = min(opt, dp[m]);
}
ans += opt;
}
for (int i = 0; i < n; ++i) {
ans += res[i];
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
159 ms |
14628 KB |
Output is correct |
5 |
Correct |
160 ms |
13776 KB |
Output is correct |
6 |
Correct |
170 ms |
14668 KB |
Output is correct |
7 |
Correct |
160 ms |
11616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
15052 KB |
Output is correct |
2 |
Correct |
152 ms |
15044 KB |
Output is correct |
3 |
Correct |
139 ms |
13756 KB |
Output is correct |
4 |
Correct |
143 ms |
11464 KB |
Output is correct |
5 |
Correct |
156 ms |
14964 KB |
Output is correct |
6 |
Correct |
151 ms |
15172 KB |
Output is correct |
7 |
Correct |
148 ms |
15360 KB |
Output is correct |
8 |
Correct |
159 ms |
14760 KB |
Output is correct |
9 |
Correct |
134 ms |
15248 KB |
Output is correct |
10 |
Correct |
110 ms |
15648 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
159 ms |
14628 KB |
Output is correct |
20 |
Correct |
160 ms |
13776 KB |
Output is correct |
21 |
Correct |
170 ms |
14668 KB |
Output is correct |
22 |
Correct |
160 ms |
11616 KB |
Output is correct |
23 |
Correct |
162 ms |
15052 KB |
Output is correct |
24 |
Correct |
152 ms |
15044 KB |
Output is correct |
25 |
Correct |
139 ms |
13756 KB |
Output is correct |
26 |
Correct |
143 ms |
11464 KB |
Output is correct |
27 |
Correct |
156 ms |
14964 KB |
Output is correct |
28 |
Correct |
151 ms |
15172 KB |
Output is correct |
29 |
Correct |
148 ms |
15360 KB |
Output is correct |
30 |
Correct |
159 ms |
14760 KB |
Output is correct |
31 |
Correct |
134 ms |
15248 KB |
Output is correct |
32 |
Correct |
110 ms |
15648 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
324 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
187 ms |
15008 KB |
Output is correct |
38 |
Correct |
184 ms |
15136 KB |
Output is correct |
39 |
Correct |
156 ms |
15140 KB |
Output is correct |
40 |
Correct |
150 ms |
15132 KB |
Output is correct |
41 |
Correct |
149 ms |
15168 KB |
Output is correct |
42 |
Correct |
171 ms |
15104 KB |
Output is correct |
43 |
Correct |
152 ms |
15044 KB |
Output is correct |
44 |
Correct |
156 ms |
15128 KB |
Output is correct |
45 |
Correct |
156 ms |
15044 KB |
Output is correct |
46 |
Correct |
174 ms |
15092 KB |
Output is correct |
47 |
Correct |
165 ms |
15184 KB |
Output is correct |
48 |
Correct |
148 ms |
15068 KB |
Output is correct |
49 |
Correct |
150 ms |
15004 KB |
Output is correct |
50 |
Correct |
152 ms |
13820 KB |
Output is correct |
51 |
Correct |
166 ms |
11424 KB |
Output is correct |
52 |
Correct |
156 ms |
14924 KB |
Output is correct |
53 |
Correct |
150 ms |
15100 KB |
Output is correct |
54 |
Correct |
166 ms |
15132 KB |
Output is correct |
55 |
Correct |
159 ms |
14768 KB |
Output is correct |
56 |
Correct |
138 ms |
15236 KB |
Output is correct |
57 |
Correct |
108 ms |
15604 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
328 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
1 ms |
324 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
324 KB |
Output is correct |
65 |
Correct |
149 ms |
14532 KB |
Output is correct |
66 |
Correct |
150 ms |
13716 KB |
Output is correct |
67 |
Correct |
158 ms |
14876 KB |
Output is correct |
68 |
Correct |
188 ms |
11428 KB |
Output is correct |
69 |
Correct |
1 ms |
212 KB |
Output is correct |
70 |
Correct |
1 ms |
324 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
0 ms |
320 KB |
Output is correct |
78 |
Correct |
0 ms |
320 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
1 ms |
212 KB |
Output is correct |
81 |
Correct |
1 ms |
212 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
0 ms |
212 KB |
Output is correct |