#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
lli n,a,b,cont,res;
lli dp[2][MAX+2];
string A,B;
map<string, lli> mapa;
vector<lli> raices,hijos[MAX+2];
void dfs(lli pos) {
if (hijos[pos].empty() || hijos[pos][0] == pos) {
dp[0][pos] = 0;
dp[1][pos] = 1;
return;
}
lli MIN = INT_MAX;
lli sum = 0;
lli x;
for(auto h : hijos[pos]) {
dfs(h);
sum += dp[1][h];
x = dp[0][h] - dp[1][h];
if (x < MIN) MIN = x;
}
dp[0][pos] = sum;
sum += MIN;
dp[1][pos] = sum + 1;
}
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];
if (a == b) raices.push_back(a);
else hijos[b].push_back(a);
}
if (n&1) {
cout << "-1";
return 0;
}
res = 0;
for(auto r : raices) {dfs(r); res += dp[1][r];}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2664 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
234 ms |
16524 KB |
Output is correct |
2 |
Correct |
230 ms |
18500 KB |
Output is correct |
3 |
Correct |
178 ms |
14920 KB |
Output is correct |
4 |
Correct |
227 ms |
14404 KB |
Output is correct |
5 |
Correct |
231 ms |
24488 KB |
Output is correct |
6 |
Correct |
230 ms |
15960 KB |
Output is correct |
7 |
Correct |
233 ms |
15936 KB |
Output is correct |
8 |
Correct |
201 ms |
15688 KB |
Output is correct |
9 |
Correct |
206 ms |
15600 KB |
Output is correct |
10 |
Correct |
154 ms |
14956 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2592 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |