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 <bits/stdc++.h>
using namespace std;
#define lli long long int
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define test(args...) abc("[" + string(#args) + "]", args)
void abc() {cerr << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cerr << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
while (l != r) cerr << *l << " \n"[++l == r];
}
const int mod = 1e9 + 7, N = 200000;
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
int n, _id = 0;
cin >> n;
if (n & 1) return cout << -1 << endl, 0;
map <string, int> m1;
auto get = [&](string s) {
if (m1.count(s)) return m1[s];
return m1[s] = _id++;
};
vector <vector <int>> adj(n);
vector <int> root;
string s, t;
for (int i = 0; i < n; ++i) {
cin >> s >> t;
int u = get(s), v = get(t);
if (u != v) adj[v].pb(u);
else root.pb(u);
}
vector <vector <int>> dp(n, vector <int>(2, 0));
// 0 not choose, 1 choose
function<void(int)> dfs = [&](int v) {
for (int u : adj[v]) {
dfs(u);
dp[v][0] += max(dp[u][0], dp[u][1]);
}
for (int u : adj[v]) {
dp[v][1] = max(dp[v][1], dp[v][0] - max(dp[u][0], dp[u][1]) + dp[u][0] + 1);
}
};
int ans = n;
vector <bool> vis(n, false);
for (int i : root) {
dfs(i);
ans -= max(dp[i][0], dp[i][1]);
}
cout << ans << endl;
}
# | 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... |