#include <bits/stdc++.h>
using namespace std;
#define int int64_t
signed main() {
int n;cin>>n;
if (n % 2 != 0) {
cout<<-1<<"\n";
return 0;
}
map<string,int> mp;
vector<vector<int>> in(n);
vector<int> out(n);
int cnt=0;
for (int i=0;i<n;i++) {
string s1, s2;cin>>s1>>s2;
if (mp.find(s1) == mp.end()) {
mp[s1] = cnt++;
}
if (mp.find(s2) == mp.end()) {
mp[s2] = cnt++;
}
if (mp[s1] != mp[s2])
in[mp[s2]].push_back(mp[s1]);
out[mp[s1]] = mp[s2];
}
// remove all relationships (cycles of length 2)
int swaps = n;
vector<bool> vis1(n, false);
for (int i=0;i<n;i++) {
if (vis1[i])continue;
if (out[out[i]] != i || out[i] == i) continue;
vis1[i] = true;
vis1[out[i]] = true;
for (auto &x:in[i]) {
out[x] = x;
}
for (auto &x:in[out[i]]) {
out[x] = x;
}
swaps -= 2;
}
// get rid of all cycles of length 1 (people that love themselves), and the respective lines connected to them
vector<int> dpWith(n, 0), dpWithout(n, 0);
function<void(int)> dfs1=[&](int node) {
vis1[node] = true;
int sm = 0;
for (auto &x:in[node]) {
dfs1(x);
dpWithout[node] += max(dpWith[x], dpWithout[x]);
sm += max(dpWith[x], dpWithout[x]);
}
for (auto &x:in[node]) {
dpWith[node] = max(dpWith[node], sm - max(dpWith[x], dpWithout[x]) + dpWithout[x] + 1);
}
};
for (int i=0;i<n;i++) {
if (out[i] == i && !vis1[i]) {
dfs1(i);
swaps -= max(dpWith[i], dpWithout[i]);
}
}
// find all nodes that are part of a cycle
vector<bool> incycle(n, false);
vector<int> stat(n, 0);
vector<bool> vis = vis1;
function<int(int)> dfs2=[&](int node) -> int {
vis[node] = true;
// still being processed
stat[node] = 1;
if (stat[out[node]] == 0) {
int d = dfs2(out[node]);
if (d != -1) {
incycle[node] = true;
}
if (d == node) {
d = -1;
}
stat[node] = 2;
return d;
}
else if (stat[out[node]] == 1) {
stat[node] = 2;
incycle[node] = true;
return out[node];
}
// finished processing
stat[node] = 2;
return -1;
};
for (int i=0;i<n;i++) {
if (!vis[i]) dfs2(i);
}
// calculate dp values for all nodes in a cycle
function<void(int)> dfs3=[&](int node) {
int sm = 0;
for (auto &x:in[node]) {
if (incycle[x])continue;
dfs3(x);
dpWithout[node] += max(dpWith[x], dpWithout[x]);
sm += max(dpWith[x], dpWithout[x]);
}
for (auto &x:in[node]) {
if (incycle[x])continue;
dpWith[node] = max(dpWith[node], sm - max(dpWith[x], dpWithout[x]) + dpWithout[x] + 1);
}
};
for (int i=0;i<n;i++) {
if (incycle[i]) {
dfs3(i);
swaps -= dpWith[i];
}
}
vis.clear();
vis.assign(n, false);
for (int i=0;i<n;i++) {
if (incycle[i] && !vis[i]) {
int pt = i;
vector<bool> pos;
do {
pos.push_back(dpWith[pt] == dpWithout[pt]);
vis[pt] = true;
pt = out[pt];
} while (pt != i);
vector<bool> tmp;
while (pos.size() && pos.back() == true) {
tmp.push_back(true);
pos.pop_back();
}
for (bool x:pos) {
tmp.push_back(x);
}
tmp.push_back(false);
stack<bool> st;
for (bool x:tmp) {
if (x) {
st.push(x);
}
else {
swaps -= ((int)st.size() / 2);
while (st.size()) st.pop();
}
}
}
}
cout<<swaps<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
1 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
257 ms |
24396 KB |
Output is correct |
5 |
Correct |
260 ms |
19000 KB |
Output is correct |
6 |
Correct |
289 ms |
25324 KB |
Output is correct |
7 |
Correct |
0 ms |
444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
18688 KB |
Output is correct |
2 |
Correct |
260 ms |
20564 KB |
Output is correct |
3 |
Correct |
190 ms |
15716 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
267 ms |
27732 KB |
Output is correct |
6 |
Correct |
249 ms |
17672 KB |
Output is correct |
7 |
Correct |
253 ms |
17672 KB |
Output is correct |
8 |
Correct |
228 ms |
17148 KB |
Output is correct |
9 |
Correct |
234 ms |
17292 KB |
Output is correct |
10 |
Correct |
188 ms |
16896 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
1 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
257 ms |
24396 KB |
Output is correct |
20 |
Correct |
260 ms |
19000 KB |
Output is correct |
21 |
Correct |
289 ms |
25324 KB |
Output is correct |
22 |
Correct |
0 ms |
444 KB |
Output is correct |
23 |
Correct |
276 ms |
18688 KB |
Output is correct |
24 |
Correct |
260 ms |
20564 KB |
Output is correct |
25 |
Correct |
190 ms |
15716 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
267 ms |
27732 KB |
Output is correct |
28 |
Correct |
249 ms |
17672 KB |
Output is correct |
29 |
Correct |
253 ms |
17672 KB |
Output is correct |
30 |
Correct |
228 ms |
17148 KB |
Output is correct |
31 |
Correct |
234 ms |
17292 KB |
Output is correct |
32 |
Correct |
188 ms |
16896 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
266 ms |
18772 KB |
Output is correct |
38 |
Correct |
254 ms |
18768 KB |
Output is correct |
39 |
Correct |
268 ms |
17656 KB |
Output is correct |
40 |
Correct |
263 ms |
17856 KB |
Output is correct |
41 |
Correct |
252 ms |
17748 KB |
Output is correct |
42 |
Correct |
256 ms |
18000 KB |
Output is correct |
43 |
Correct |
256 ms |
17924 KB |
Output is correct |
44 |
Correct |
257 ms |
17996 KB |
Output is correct |
45 |
Correct |
270 ms |
18076 KB |
Output is correct |
46 |
Correct |
256 ms |
18060 KB |
Output is correct |
47 |
Correct |
239 ms |
17492 KB |
Output is correct |
48 |
Correct |
258 ms |
18488 KB |
Output is correct |
49 |
Correct |
271 ms |
20564 KB |
Output is correct |
50 |
Correct |
190 ms |
15784 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
274 ms |
27524 KB |
Output is correct |
53 |
Correct |
247 ms |
17560 KB |
Output is correct |
54 |
Correct |
267 ms |
17796 KB |
Output is correct |
55 |
Correct |
223 ms |
16888 KB |
Output is correct |
56 |
Correct |
236 ms |
17236 KB |
Output is correct |
57 |
Correct |
192 ms |
16672 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
257 ms |
24456 KB |
Output is correct |
66 |
Correct |
275 ms |
18844 KB |
Output is correct |
67 |
Correct |
256 ms |
25424 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
344 KB |
Output is correct |
74 |
Correct |
0 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 |
344 KB |
Output is correct |
79 |
Correct |
0 ms |
348 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 |