This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |