#include <bits/stdc++.h>
using namespace std;
#define vb vector<bool>
#define vi vector<int>
#define vvi vector<vi>
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
// ungerade fertig
if (n % 2 == 1) { cout << -1; return 0; }
map<string, int> namen;
vi beziehungen(n);
vi degree(n); // Grad des Knoten δ+ und δ-
string sa, sb;
int a, b;
int idx = 0;
for (int j = 0; j < n; j++){
cin >> sa >> sb;
if (namen.count(sa) == 0) { namen[sa] = idx; idx++; }
if (namen.count(sb) == 0) { namen[sb] = idx; idx++; }
a = namen[sa]; b = namen[sb];
beziehungen[a] = b;
degree[b]++;
}
// for (map<string, int>::iterator it = namen.begin(); it != namen.end(); ++it) { cout << it->first << " = " << it->second << endl; }
// for (int i = 0; i < n; i++){ cout << i << " --> " << beziehungen[i] << " (" << degree[i] << endl; }
// Aussortieren, wer in einer Beziehung ist
// Knoten mit geringsten Grad finden
// -> Grad = 1: Erstell Beziehung mit Nachbarn
int new_edges = 0;
queue<int> q;
vb inRelation(n, false);
for (int i = 0; i < n; ++i) {
if (beziehungen[i] != i && i == beziehungen[beziehungen[i]]) inRelation[i] = true;
if (degree[i] == 0) q.push(i);
}
while (!q.empty()) {
int s = q.front(); q.pop();
if (inRelation[s]) continue;
int t = beziehungen[s];
if (inRelation[t]) {
--degree[t];
++degree[s];
inRelation[s] = true;
++new_edges;
} else {
int u = beziehungen[t];
--degree[u];
++degree[s];
++new_edges;
inRelation[s] = inRelation[t] = true;
if (degree[u] == 0) q.push(u);
}
}
// cout << endl << "Herumgespielt" << endl; for (int i = 0; i < n; i++){ cout << i << " --> " << beziehungen[i] << " (" << degree[i] << endl; } cout << endl;
// -> Grad = 2: Eulerkreise, größer als 2: verbleibende Knoten auf Beziehung überprüfen, dann 0 Kanten, sonst 1 pro Person
// -> grad >= 3: unmöglich, wegen Schubfach
int clen, running_neighbor;
for (int i = 0; i < n; i++){
// falls in Beziehung: keine Umkopplung
if (inRelation[i]) continue;
// andernfalls - find cycle length
clen = 0;
running_neighbor = i;
do {
inRelation[running_neighbor] = true;
running_neighbor = beziehungen[running_neighbor];
++clen;
} while(running_neighbor != i);
new_edges += (clen + 1) / 2;
}
cout << new_edges << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
131 ms |
11032 KB |
Output is correct |
5 |
Correct |
138 ms |
11140 KB |
Output is correct |
6 |
Correct |
151 ms |
11072 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
11108 KB |
Output is correct |
2 |
Correct |
129 ms |
11156 KB |
Output is correct |
3 |
Correct |
131 ms |
11036 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
130 ms |
10972 KB |
Output is correct |
6 |
Correct |
130 ms |
11176 KB |
Output is correct |
7 |
Correct |
127 ms |
11208 KB |
Output is correct |
8 |
Correct |
139 ms |
11504 KB |
Output is correct |
9 |
Correct |
121 ms |
11360 KB |
Output is correct |
10 |
Correct |
100 ms |
11484 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
131 ms |
11032 KB |
Output is correct |
20 |
Correct |
138 ms |
11140 KB |
Output is correct |
21 |
Correct |
151 ms |
11072 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
133 ms |
11108 KB |
Output is correct |
24 |
Correct |
129 ms |
11156 KB |
Output is correct |
25 |
Correct |
131 ms |
11036 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
130 ms |
10972 KB |
Output is correct |
28 |
Correct |
130 ms |
11176 KB |
Output is correct |
29 |
Correct |
127 ms |
11208 KB |
Output is correct |
30 |
Correct |
139 ms |
11504 KB |
Output is correct |
31 |
Correct |
121 ms |
11360 KB |
Output is correct |
32 |
Correct |
100 ms |
11484 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
132 ms |
11052 KB |
Output is correct |
38 |
Correct |
128 ms |
11272 KB |
Output is correct |
39 |
Correct |
155 ms |
11256 KB |
Output is correct |
40 |
Correct |
130 ms |
11184 KB |
Output is correct |
41 |
Correct |
129 ms |
11200 KB |
Output is correct |
42 |
Correct |
136 ms |
11208 KB |
Output is correct |
43 |
Correct |
131 ms |
11220 KB |
Output is correct |
44 |
Correct |
138 ms |
11212 KB |
Output is correct |
45 |
Correct |
136 ms |
11188 KB |
Output is correct |
46 |
Correct |
138 ms |
11188 KB |
Output is correct |
47 |
Correct |
123 ms |
11332 KB |
Output is correct |
48 |
Correct |
132 ms |
11096 KB |
Output is correct |
49 |
Correct |
143 ms |
11100 KB |
Output is correct |
50 |
Correct |
139 ms |
11020 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Correct |
139 ms |
11000 KB |
Output is correct |
53 |
Correct |
126 ms |
11212 KB |
Output is correct |
54 |
Correct |
145 ms |
11284 KB |
Output is correct |
55 |
Correct |
123 ms |
11212 KB |
Output is correct |
56 |
Correct |
122 ms |
11340 KB |
Output is correct |
57 |
Correct |
105 ms |
11444 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
0 ms |
320 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
141 ms |
10996 KB |
Output is correct |
66 |
Correct |
142 ms |
11084 KB |
Output is correct |
67 |
Correct |
135 ms |
11028 KB |
Output is correct |
68 |
Correct |
0 ms |
340 KB |
Output is correct |
69 |
Correct |
0 ms |
212 KB |
Output is correct |
70 |
Correct |
0 ms |
324 KB |
Output is correct |
71 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
320 KB |
Output is correct |
78 |
Correct |
0 ms |
316 KB |
Output is correct |
79 |
Correct |
1 ms |
332 KB |
Output is correct |
80 |
Correct |
0 ms |
212 KB |
Output is correct |
81 |
Correct |
1 ms |
212 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
316 KB |
Output is correct |