Submission #899117

# Submission time Handle Problem Language Result Execution time Memory
899117 2024-01-05T13:43:28 Z duckindog Logičari (COCI21_logicari) C++14
10 / 110
167 ms 33396 KB
// from duckindog wth depression
#include<bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e5 + 10;
int n;
vector<int> ad[N];
bool dd[N];
int st, ed;
void locate(int u, int pre = 0) {
  dd[u] = 1;
  for (int v : ad[u]) {
    if (v == pre) continue;
    if (dd[v]) {
      st = u, ed = v;
      continue;
    }
    locate(v, u);
  }
}
int f[N][2][2][2][2];
int dfs(int u, int pre, int sub, int par, int cst, int ced) {
  if (u == ed && (par && cst || sub != ced)) return 1e6;

  auto &ret = f[u][sub][par][cst][ced];
  if (ret > -1) return ret; ret = sub;
  bool seen = par;
  if (u == ed) seen |= cst;

  int diff = 1e6, nv = 0;

  for (int v : ad[u]) {
    if (v == pre) continue;

    if (par) {
      ret += dfs(v, u, 0, sub, cst, ced);
    }

    if (!par) {
      int upper = dfs(v, u, 1, sub, cst, ced);
      int lower = dfs(v, u, 0, sub, cst, ced);
      int differ = upper - lower;
      if (differ < diff) diff = differ, nv = v;
    }

  }

  if (nv) {
    for (auto v : ad[u]) {
      if (v == pre) continue;

      if (!par) {
        seen = 1;
        if (v == nv) ret += dfs(v, u, 1, sub, cst, ced);
        else ret += dfs(v, u, 0, sub, cst, ced);
      }
    }
  }

  if (!seen) ret = 1e6;

  return ret;
}

int32_t main() {
  cin.tie(0)->sync_with_stdio(0);

  if (fopen("duck.inp", "r")) {
    freopen("duck.inp", "r", stdin);
    freopen("duck.out", "w", stdout);
  }
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int u, v; cin >> u >> v;
    ad[u].push_back(v);
    ad[v].push_back(u);
  } locate(1);
  ad[st].erase(remove(ad[st].begin(), ad[st].end(), ed), ad[st].end());
  ad[ed].erase(remove(ad[ed].begin(), ad[ed].end(), st), ad[ed].end());


  memset(f, -1, sizeof f);
  int answer = 1e6;


//  for (int i = 0; i <= 1; ++i)
//    for (int j = 0; j <= 1; ++j) cerr << dfs(st, ed, i, j, i, j) << '\n';

  for (int i = 0; i <= 1; ++i)
    for (int j = 0; j <= 1; ++j) answer = min(answer, dfs(st, ed, i, j, i, j));

  cout << (answer >= n ? -1 : answer) << '\n';


}

Compilation message

Main.cpp: In function 'long long int dfs(long long int, long long int, long long int, long long int, long long int, long long int)':
Main.cpp:26:23: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   26 |   if (u == ed && (par && cst || sub != ced)) return 1e6;
      |                   ~~~~^~~~~~
Main.cpp:29:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   29 |   if (ret > -1) return ret; ret = sub;
      |   ^~
Main.cpp:29:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   29 |   if (ret > -1) return ret; ret = sub;
      |                             ^~~
Main.cpp: In function 'int32_t main()':
Main.cpp:72:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |     freopen("duck.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     freopen("duck.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15192 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15448 KB Output is correct
4 Correct 3 ms 15192 KB Output is correct
5 Correct 148 ms 33392 KB Output is correct
6 Correct 161 ms 33392 KB Output is correct
7 Correct 124 ms 33188 KB Output is correct
8 Correct 167 ms 33396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15192 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Incorrect 3 ms 15196 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15192 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Incorrect 3 ms 15196 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15192 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15448 KB Output is correct
4 Correct 3 ms 15192 KB Output is correct
5 Correct 148 ms 33392 KB Output is correct
6 Correct 161 ms 33392 KB Output is correct
7 Correct 124 ms 33188 KB Output is correct
8 Correct 167 ms 33396 KB Output is correct
9 Correct 3 ms 15192 KB Output is correct
10 Correct 3 ms 15196 KB Output is correct
11 Incorrect 3 ms 15196 KB Output isn't correct
12 Halted 0 ms 0 KB -