#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
vector<set <ll>> idg(100005);
vector<set <ll>> odg(100005);
map <string, ll> mapa;
map <string, bool> ima;
queue <ll> samci;
vector <ll> outcast;
ll neparni = 0;
int main()
{
ll n;
cin >> n;
vector <string> prvi;
vector <string> drugi;
ll nodes = 0;
for (int i = 0; i < n; i++){
string a, b;
cin >> a >> b;
prvi.push_back(a), drugi.push_back(b);
if (!ima[a])
ima[a] = true, mapa[a] = nodes, nodes++;
if (!ima[b])
ima[b] = true, mapa[b] = nodes, nodes++;
}
if (n%2 == 1){
cout << "-1" << endl;
return 0;
}
for (int i = 0; i < n; i++)
odg[mapa[prvi[i]]].insert(mapa[drugi[i]]), idg[mapa[drugi[i]]].insert(mapa[prvi[i]]);
for (int i = 0; i < n; i++)
if (idg[i].size() == 0)
samci.push(i);
ll brop = 0;
//cout << "ok" << endl;
vector <bool> vis(100000, false);
for (int i = 0; i < n; i++){
ll j = *odg[i].begin();
if (i == j)
continue;
if (i == *odg[j].begin())
vis[i] = true, vis[j] = true;
}
while (samci.size()){
ll tren = samci.front();
vis[tren] = true;
samci.pop();
ll ljubav = *odg[tren].begin();
if (vis[ljubav]){
idg[ljubav].erase(tren);
outcast.push_back(tren);
continue;
}
ll njljubav = *odg[ljubav].begin();
vis[ljubav] = true;
odg[ljubav].erase(njljubav);
idg[njljubav].erase(ljubav);
odg[ljubav].insert(tren);
brop++;
if (idg[njljubav].size() == 0 && !vis[njljubav])
samci.push(njljubav);
idg[tren].insert(ljubav);
}
//cout << brop << endl;
//cout << "ok" << endl;
for (int i = 0; i < n; i++){
if (!vis[i]){
ll pocetak = i;
ll len = 1;
ll tren = *odg[i].begin();
vis[i] = true;
while (tren != pocetak)
vis[tren] = true, len++, tren = *odg[tren].begin();
if (len == 2)
continue;
if (len%2 == 0)
brop += len/2;
else
brop += len/2, neparni++;
}
}
ll rjes = brop + neparni + outcast.size();
cout << rjes << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9692 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9692 KB |
Output is correct |
6 |
Correct |
6 ms |
9692 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9700 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
7 ms |
9672 KB |
Output is correct |
13 |
Correct |
7 ms |
9692 KB |
Output is correct |
14 |
Correct |
7 ms |
9660 KB |
Output is correct |
15 |
Correct |
7 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9688 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
656 ms |
43160 KB |
Output is correct |
5 |
Correct |
577 ms |
43112 KB |
Output is correct |
6 |
Correct |
606 ms |
43176 KB |
Output is correct |
7 |
Correct |
366 ms |
33660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
625 ms |
43540 KB |
Output is correct |
2 |
Correct |
664 ms |
43280 KB |
Output is correct |
3 |
Correct |
450 ms |
43068 KB |
Output is correct |
4 |
Correct |
350 ms |
33808 KB |
Output is correct |
5 |
Correct |
610 ms |
43132 KB |
Output is correct |
6 |
Correct |
613 ms |
43924 KB |
Output is correct |
7 |
Correct |
614 ms |
43928 KB |
Output is correct |
8 |
Correct |
547 ms |
43480 KB |
Output is correct |
9 |
Correct |
597 ms |
44616 KB |
Output is correct |
10 |
Correct |
457 ms |
45744 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9612 KB |
Output is correct |
14 |
Correct |
6 ms |
9692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9692 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9692 KB |
Output is correct |
6 |
Correct |
6 ms |
9692 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9700 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
7 ms |
9672 KB |
Output is correct |
13 |
Correct |
7 ms |
9692 KB |
Output is correct |
14 |
Correct |
7 ms |
9660 KB |
Output is correct |
15 |
Correct |
7 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9688 KB |
Output is correct |
18 |
Correct |
7 ms |
9676 KB |
Output is correct |
19 |
Correct |
656 ms |
43160 KB |
Output is correct |
20 |
Correct |
577 ms |
43112 KB |
Output is correct |
21 |
Correct |
606 ms |
43176 KB |
Output is correct |
22 |
Correct |
366 ms |
33660 KB |
Output is correct |
23 |
Correct |
625 ms |
43540 KB |
Output is correct |
24 |
Correct |
664 ms |
43280 KB |
Output is correct |
25 |
Correct |
450 ms |
43068 KB |
Output is correct |
26 |
Correct |
350 ms |
33808 KB |
Output is correct |
27 |
Correct |
610 ms |
43132 KB |
Output is correct |
28 |
Correct |
613 ms |
43924 KB |
Output is correct |
29 |
Correct |
614 ms |
43928 KB |
Output is correct |
30 |
Correct |
547 ms |
43480 KB |
Output is correct |
31 |
Correct |
597 ms |
44616 KB |
Output is correct |
32 |
Correct |
457 ms |
45744 KB |
Output is correct |
33 |
Correct |
6 ms |
9676 KB |
Output is correct |
34 |
Correct |
6 ms |
9676 KB |
Output is correct |
35 |
Correct |
6 ms |
9612 KB |
Output is correct |
36 |
Correct |
6 ms |
9692 KB |
Output is correct |
37 |
Correct |
690 ms |
43320 KB |
Output is correct |
38 |
Correct |
603 ms |
43924 KB |
Output is correct |
39 |
Correct |
650 ms |
43920 KB |
Output is correct |
40 |
Correct |
613 ms |
44084 KB |
Output is correct |
41 |
Correct |
610 ms |
44056 KB |
Output is correct |
42 |
Correct |
624 ms |
43712 KB |
Output is correct |
43 |
Correct |
674 ms |
43796 KB |
Output is correct |
44 |
Correct |
647 ms |
43768 KB |
Output is correct |
45 |
Correct |
644 ms |
43684 KB |
Output is correct |
46 |
Correct |
632 ms |
43652 KB |
Output is correct |
47 |
Correct |
624 ms |
44712 KB |
Output is correct |
48 |
Correct |
676 ms |
43588 KB |
Output is correct |
49 |
Correct |
627 ms |
43300 KB |
Output is correct |
50 |
Correct |
471 ms |
43136 KB |
Output is correct |
51 |
Correct |
376 ms |
33868 KB |
Output is correct |
52 |
Correct |
673 ms |
43176 KB |
Output is correct |
53 |
Correct |
654 ms |
43908 KB |
Output is correct |
54 |
Correct |
605 ms |
44052 KB |
Output is correct |
55 |
Correct |
543 ms |
43548 KB |
Output is correct |
56 |
Correct |
610 ms |
44660 KB |
Output is correct |
57 |
Correct |
448 ms |
45756 KB |
Output is correct |
58 |
Correct |
6 ms |
9676 KB |
Output is correct |
59 |
Correct |
6 ms |
9628 KB |
Output is correct |
60 |
Correct |
6 ms |
9676 KB |
Output is correct |
61 |
Correct |
7 ms |
9716 KB |
Output is correct |
62 |
Correct |
7 ms |
9676 KB |
Output is correct |
63 |
Correct |
6 ms |
9676 KB |
Output is correct |
64 |
Correct |
7 ms |
9696 KB |
Output is correct |
65 |
Correct |
648 ms |
43196 KB |
Output is correct |
66 |
Correct |
685 ms |
43184 KB |
Output is correct |
67 |
Correct |
674 ms |
43196 KB |
Output is correct |
68 |
Correct |
352 ms |
33736 KB |
Output is correct |
69 |
Correct |
7 ms |
9700 KB |
Output is correct |
70 |
Correct |
7 ms |
9700 KB |
Output is correct |
71 |
Correct |
7 ms |
9700 KB |
Output is correct |
72 |
Correct |
7 ms |
9692 KB |
Output is correct |
73 |
Correct |
6 ms |
9700 KB |
Output is correct |
74 |
Correct |
7 ms |
9684 KB |
Output is correct |
75 |
Correct |
7 ms |
9676 KB |
Output is correct |
76 |
Correct |
7 ms |
9684 KB |
Output is correct |
77 |
Correct |
7 ms |
9676 KB |
Output is correct |
78 |
Correct |
6 ms |
9676 KB |
Output is correct |
79 |
Correct |
7 ms |
9700 KB |
Output is correct |
80 |
Correct |
7 ms |
9676 KB |
Output is correct |
81 |
Correct |
7 ms |
9676 KB |
Output is correct |
82 |
Correct |
7 ms |
9696 KB |
Output is correct |
83 |
Correct |
7 ms |
9700 KB |
Output is correct |