#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
312 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
273 ms |
18596 KB |
Output is correct |
2 |
Correct |
270 ms |
22432 KB |
Output is correct |
3 |
Correct |
174 ms |
18444 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
363 ms |
28508 KB |
Output is correct |
6 |
Correct |
231 ms |
19908 KB |
Output is correct |
7 |
Correct |
265 ms |
19724 KB |
Output is correct |
8 |
Correct |
234 ms |
19460 KB |
Output is correct |
9 |
Correct |
242 ms |
19348 KB |
Output is correct |
10 |
Correct |
208 ms |
18492 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
3 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |