#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define MAX 100000
pair<lli,lli> ult;
lli n,a,b,cont,res,cant;
lli dp[2][MAX+2],dir[MAX+2],visitados[MAX+2];
string A,B;
map<string, lli> mapa;
vector<lli> hijos[MAX+2];
void dfs(lli pos, lli marca) {
cant++;
visitados[pos] = marca;
dp[0][pos] = 0;
dp[1][pos] = 0;
lli MIN = INT_MAX;
lli sum = 0;
lli x;
bool tengoH = false;
for(auto h : hijos[pos]) {
if (visitados[h] == marca) continue;
tengoH = true;
dfs(h,marca);
sum += dp[1][h];
x = dp[0][h] - dp[1][h];
if (x < MIN) MIN = x;
}
if (!tengoH) {
dp[0][pos] = 0;
dp[1][pos] = 1;
return;
}
dp[0][pos] = sum;
sum += MIN;
dp[1][pos] = sum + 1;
}
pair<lli,lli> busca(lli pos) {
while (visitados[pos] == 0) {
visitados[pos] = 1;
if (visitados[dir[pos]] == 1) return {pos,dir[pos]};
pos = dir[pos];
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
cont = 1;
rep(i,1,n) {
cin >> A >> B;
if (mapa.find(A) == mapa.end()) mapa[A] = cont++;
if (mapa.find(B) == mapa.end()) mapa[B] = cont++;
a = mapa[A];
b = mapa[B];
dir[a] = b;
hijos[b].push_back(a);
hijos[a].push_back(b);
}
if (n&1) {
cout << "-1";
return 0;
}
res = 0;
rep(i,1,n) if (visitados[i] == 0){
ult = busca(i);
cant = 0;
dfs(ult.first, 2);
a = dp[1][ult.first];
if (cant == 2) continue;
if (ult.second != ult.first) {
dfs(ult.second, 3);
a = min(a, dp[1][ult.second]);
}
res += a;
}
cout << res;
}
Compilation message
polygon.cpp: In function 'std::pair<long long int, long long int> busca(long long int)':
polygon.cpp:61:1: warning: control reaches end of non-void function [-Wreturn-type]
61 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2672 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Incorrect |
2 ms |
2676 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
292 ms |
27260 KB |
Output is correct |
5 |
Correct |
260 ms |
18780 KB |
Output is correct |
6 |
Correct |
292 ms |
28168 KB |
Output is correct |
7 |
Correct |
287 ms |
16456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
275 ms |
19284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2672 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Incorrect |
2 ms |
2676 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |