Submission #987781

# Submission time Handle Problem Language Result Execution time Memory
987781 2024-05-23T15:04:30 Z MilosMilutinovic Love Polygon (BOI18_polygon) C++14
29 / 100
233 ms 31136 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> to(n);
  map<string, int> mp;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    string t;
    cin >> t;
    if (!mp.count(s)) {
      mp[s] = (int) mp.size();
    }
    if (!mp.count(t)) {
      mp[t] = (int) mp.size();
    }
    to[mp[s]] = mp[t];
  }
  if (n % 2 == 1) {
    cout << -1 << '\n';
    return 0;
  }
  vector<vector<int>> ch(n);
  for (int i = 0; i < n; i++) {
    if (to[i] == i) {
      continue;
    }
    ch[to[i]].push_back(i);
  }
  vector<int> deg(n);
  for (int i = 0; i < n; i++) {
    deg[to[i]] += 1;
  }
  vector<int> que;
  for (int i = 0; i < n; i++) {
    if (deg[i] == 0) {
      que.push_back(i);
    }
  }
  for (int b = 0; b < (int) que.size(); b++) {
    int i = que[b];
    deg[to[i]] -= 1;
    if (deg[to[i]] == 0) {
      que.push_back(to[i]);
    }
  }
  vector<vector<int>> dp(n, vector<int>(2));
  function<void(int)> Solve = [&](int v) {
    int mx = 0;
    for (int u : ch[v]) {
      Solve(u);
      dp[v][0] += max(dp[u][0], dp[u][1]);
      dp[v][1] += max(dp[u][0], dp[u][1]);
      mx = max(mx, -max(dp[u][0], dp[u][1]) + dp[u][0] + 1);
    }
    dp[v][1] += mx;
  };
  int res = 0;
  for (int i = 0; i < n; i++) {
    if (to[i] != i) {
      continue;
    }
    Solve(i);
    res += max(dp[i][1], dp[i][0]); 
  }
  cout << (n - 2 * res) + res << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 168 ms 19492 KB Output is correct
2 Correct 181 ms 24008 KB Output is correct
3 Correct 128 ms 19000 KB Output is correct
4 Correct 139 ms 10636 KB Output is correct
5 Correct 233 ms 31136 KB Output is correct
6 Correct 158 ms 20932 KB Output is correct
7 Correct 181 ms 21024 KB Output is correct
8 Correct 163 ms 20508 KB Output is correct
9 Correct 158 ms 20420 KB Output is correct
10 Correct 131 ms 19628 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -