#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5;
int n;
bool vis[MAX_N + 1], isOnCycle[MAX_N + 1];
int repr[MAX_N + 1], parent[MAX_N + 1], sizee[MAX_N + 1], minChanges[MAX_N + 1][2], dp1[MAX_N + 1], dp2[MAX_N + 1];
vector <int> adj[MAX_N + 1];
unordered_map <string, int> nameToId;
void findCycles( int u, int r ) {
repr[u] = r;
for ( int v: adj[u] ) {
if ( repr[v] == r ) {
isOnCycle[u] = true;
return;
}
if ( repr[v] == 0 )
findCycles( v, r );
if ( isOnCycle[v] ) {
isOnCycle[u] = true;
return;
}
}
}
void detectCycles() {
for ( int v = 1; v <= n; v++ )
vis[v] = false;
for ( int v = 1; v <= n; v++ ) {
if ( vis[v] )
continue;
findCycles( v, v );
}
}
void calc( int u ) {
sizee[u] = 1;
for ( int v: adj[u] ) {
if ( isOnCycle[v] )
continue;
calc( v );
minChanges[u][false] += minChanges[v][true];
sizee[u] += sizee[v];
}
minChanges[u][true] = minChanges[u][false] + 1;
for ( int v: adj[u] ) {
if ( isOnCycle[v] )
continue;
minChanges[u][true] = min( minChanges[u][true], minChanges[u][false] - minChanges[v][true] + minChanges[v][false] + 1 );
}
}
int main() {
cin >> n;
int id = 0;
for ( int i = 0; i < n; i++ ) {
string s, t;
cin >> s >> t;
nameToId[s] = (nameToId[s] == 0 ? ++id : nameToId[s]);
nameToId[t] = (nameToId[t] == 0 ? ++id : nameToId[t]);
parent[nameToId[s]] = nameToId[t];
adj[nameToId[t]].push_back( nameToId[s] );
//cout << nameToId[s] << " " << nameToId[t] << "\n";
}
if ( n % 2 == 1 ) {
cout << -1 << "\n";
return 0;
}
detectCycles();
for ( int u = 1; u <= n; u++ ) {
if ( !isOnCycle[u] )
continue;
calc( u );
}
for ( int v = 1; v <= n; v++ )
vis[v] = false;
int ans = 0;
for ( int v = 1; v <= n; v++ ) {
if ( !isOnCycle[v] || vis[v] )
continue;
vector <int> cycle;
cycle.push_back( v );
int u = v;
do {
vis[u] = true;
u = parent[u];
cycle.push_back( u );
} while ( u != v );
if ( cycle.size() == 2 ) {
ans += minChanges[cycle[0]][true];
continue;
}
if ( cycle.size() == 3 ) {
ans += minChanges[cycle[0]][false] + minChanges[cycle[1]][false];
continue;
}
dp1[1] = 0;
dp2[1] = minChanges[cycle[1]][true];
dp1[0] = n;
//printf( "%d %d %d\n", cycle[1], dp1[1], dp2[1] );
for ( int i = 2; i < cycle.size(); i++ ) {
dp1[i] = min( dp1[i - 1] + minChanges[cycle[i]][true], dp1[i - 2] + minChanges[cycle[i - 1]][false] + minChanges[cycle[i]][false] + 1 );
dp2[i] = min( dp2[i - 1] + minChanges[cycle[i]][true], dp2[i - 2] + minChanges[cycle[i - 1]][false] + minChanges[cycle[i]][false] + 1 );
//printf( "%d %d %d\n", cycle[i], dp1[i], dp2[i] );
}
ans += min( dp2[cycle.size() - 1], dp1[cycle.size() - 2] + minChanges[cycle.back()][false] + minChanges[cycle[1]][false] + 1 );
//printf( "baa %d %d\n", dp2[cycle.size() - 1], dp1[cycle.size() - 2] + minChanges[cycle.back()][false] + minChanges[cycle[1]][false] + 1 );
}
cout << ans << "\n";
return 0;
}
Compilation message
polygon.cpp: In function 'int main()':
polygon.cpp:117:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for ( int i = 2; i < cycle.size(); i++ ) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4720 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4936 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
115 ms |
22636 KB |
Output is correct |
5 |
Correct |
103 ms |
16416 KB |
Output is correct |
6 |
Correct |
126 ms |
23488 KB |
Output is correct |
7 |
Correct |
113 ms |
15772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
15556 KB |
Output is correct |
2 |
Correct |
128 ms |
17384 KB |
Output is correct |
3 |
Correct |
87 ms |
16252 KB |
Output is correct |
4 |
Correct |
105 ms |
14312 KB |
Output is correct |
5 |
Correct |
164 ms |
23552 KB |
Output is correct |
6 |
Correct |
101 ms |
14788 KB |
Output is correct |
7 |
Correct |
101 ms |
14788 KB |
Output is correct |
8 |
Correct |
114 ms |
15304 KB |
Output is correct |
9 |
Correct |
95 ms |
14276 KB |
Output is correct |
10 |
Correct |
82 ms |
13500 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4952 KB |
Output is correct |
14 |
Correct |
1 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4720 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4724 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4936 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
115 ms |
22636 KB |
Output is correct |
20 |
Correct |
103 ms |
16416 KB |
Output is correct |
21 |
Correct |
126 ms |
23488 KB |
Output is correct |
22 |
Correct |
113 ms |
15772 KB |
Output is correct |
23 |
Correct |
162 ms |
15556 KB |
Output is correct |
24 |
Correct |
128 ms |
17384 KB |
Output is correct |
25 |
Correct |
87 ms |
16252 KB |
Output is correct |
26 |
Correct |
105 ms |
14312 KB |
Output is correct |
27 |
Correct |
164 ms |
23552 KB |
Output is correct |
28 |
Correct |
101 ms |
14788 KB |
Output is correct |
29 |
Correct |
101 ms |
14788 KB |
Output is correct |
30 |
Correct |
114 ms |
15304 KB |
Output is correct |
31 |
Correct |
95 ms |
14276 KB |
Output is correct |
32 |
Correct |
82 ms |
13500 KB |
Output is correct |
33 |
Correct |
1 ms |
4700 KB |
Output is correct |
34 |
Correct |
1 ms |
4700 KB |
Output is correct |
35 |
Correct |
1 ms |
4952 KB |
Output is correct |
36 |
Correct |
1 ms |
4696 KB |
Output is correct |
37 |
Correct |
106 ms |
18148 KB |
Output is correct |
38 |
Correct |
107 ms |
18416 KB |
Output is correct |
39 |
Correct |
104 ms |
17132 KB |
Output is correct |
40 |
Correct |
100 ms |
16840 KB |
Output is correct |
41 |
Correct |
133 ms |
17164 KB |
Output is correct |
42 |
Correct |
120 ms |
17352 KB |
Output is correct |
43 |
Correct |
102 ms |
17488 KB |
Output is correct |
44 |
Correct |
104 ms |
17352 KB |
Output is correct |
45 |
Correct |
116 ms |
17344 KB |
Output is correct |
46 |
Correct |
154 ms |
17344 KB |
Output is correct |
47 |
Correct |
92 ms |
16580 KB |
Output is correct |
48 |
Correct |
100 ms |
17752 KB |
Output is correct |
49 |
Correct |
144 ms |
19740 KB |
Output is correct |
50 |
Correct |
86 ms |
18372 KB |
Output is correct |
51 |
Correct |
138 ms |
16344 KB |
Output is correct |
52 |
Correct |
118 ms |
25620 KB |
Output is correct |
53 |
Correct |
98 ms |
16924 KB |
Output is correct |
54 |
Correct |
99 ms |
17092 KB |
Output is correct |
55 |
Correct |
120 ms |
17340 KB |
Output is correct |
56 |
Correct |
116 ms |
16376 KB |
Output is correct |
57 |
Correct |
88 ms |
15904 KB |
Output is correct |
58 |
Correct |
1 ms |
4696 KB |
Output is correct |
59 |
Correct |
1 ms |
4700 KB |
Output is correct |
60 |
Correct |
1 ms |
4700 KB |
Output is correct |
61 |
Correct |
1 ms |
4700 KB |
Output is correct |
62 |
Correct |
1 ms |
4700 KB |
Output is correct |
63 |
Correct |
1 ms |
4700 KB |
Output is correct |
64 |
Correct |
1 ms |
4700 KB |
Output is correct |
65 |
Correct |
104 ms |
24732 KB |
Output is correct |
66 |
Correct |
127 ms |
18620 KB |
Output is correct |
67 |
Correct |
118 ms |
25540 KB |
Output is correct |
68 |
Correct |
92 ms |
17892 KB |
Output is correct |
69 |
Correct |
1 ms |
4700 KB |
Output is correct |
70 |
Correct |
1 ms |
4700 KB |
Output is correct |
71 |
Correct |
1 ms |
4696 KB |
Output is correct |
72 |
Correct |
1 ms |
4700 KB |
Output is correct |
73 |
Correct |
1 ms |
4700 KB |
Output is correct |
74 |
Correct |
1 ms |
4700 KB |
Output is correct |
75 |
Correct |
1 ms |
4700 KB |
Output is correct |
76 |
Correct |
1 ms |
4700 KB |
Output is correct |
77 |
Correct |
1 ms |
4700 KB |
Output is correct |
78 |
Correct |
1 ms |
4700 KB |
Output is correct |
79 |
Correct |
1 ms |
4700 KB |
Output is correct |
80 |
Correct |
1 ms |
4700 KB |
Output is correct |
81 |
Correct |
1 ms |
4700 KB |
Output is correct |
82 |
Correct |
2 ms |
4724 KB |
Output is correct |
83 |
Correct |
2 ms |
4700 KB |
Output is correct |