#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 && ult.first != ult.second) 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 |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Incorrect |
2 ms |
2636 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 |
2636 KB |
Output is correct |
4 |
Correct |
306 ms |
24824 KB |
Output is correct |
5 |
Correct |
309 ms |
16664 KB |
Output is correct |
6 |
Correct |
300 ms |
26124 KB |
Output is correct |
7 |
Correct |
252 ms |
14276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
282 ms |
17044 KB |
Output is correct |
2 |
Correct |
293 ms |
21188 KB |
Output is correct |
3 |
Correct |
191 ms |
18868 KB |
Output is correct |
4 |
Correct |
265 ms |
17220 KB |
Output is correct |
5 |
Correct |
300 ms |
27448 KB |
Output is correct |
6 |
Correct |
284 ms |
19404 KB |
Output is correct |
7 |
Correct |
286 ms |
19492 KB |
Output is correct |
8 |
Correct |
235 ms |
19316 KB |
Output is correct |
9 |
Correct |
245 ms |
19608 KB |
Output is correct |
10 |
Correct |
170 ms |
19640 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
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 |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |