Submission #899130

# Submission time Handle Problem Language Result Execution time Memory
899130 2024-01-05T13:53:18 Z duckindog Logičari (COCI21_logicari) C++14
110 / 110
137 ms 35152 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) {
    par |= cst;
    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:74:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |     freopen("duck.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:75:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     freopen("duck.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15192 KB Output is correct
2 Correct 2 ms 15196 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 2 ms 15196 KB Output is correct
5 Correct 137 ms 34188 KB Output is correct
6 Correct 123 ms 34028 KB Output is correct
7 Correct 137 ms 34192 KB Output is correct
8 Correct 126 ms 34128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15196 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 15228 KB Output is correct
5 Correct 3 ms 15232 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 2 ms 15196 KB Output is correct
8 Correct 2 ms 15196 KB Output is correct
9 Correct 3 ms 15196 KB Output is correct
10 Correct 2 ms 15196 KB Output is correct
11 Correct 2 ms 15196 KB Output is correct
12 Correct 3 ms 15196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15196 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 15228 KB Output is correct
5 Correct 3 ms 15232 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 2 ms 15196 KB Output is correct
8 Correct 2 ms 15196 KB Output is correct
9 Correct 3 ms 15196 KB Output is correct
10 Correct 2 ms 15196 KB Output is correct
11 Correct 2 ms 15196 KB Output is correct
12 Correct 3 ms 15196 KB Output is correct
13 Correct 4 ms 15460 KB Output is correct
14 Correct 3 ms 15452 KB Output is correct
15 Correct 4 ms 15448 KB Output is correct
16 Correct 3 ms 15452 KB Output is correct
17 Correct 3 ms 15196 KB Output is correct
18 Correct 3 ms 15452 KB Output is correct
19 Correct 3 ms 15208 KB Output is correct
20 Correct 3 ms 15448 KB Output is correct
21 Correct 3 ms 15452 KB Output is correct
22 Correct 3 ms 15452 KB Output is correct
23 Correct 3 ms 15448 KB Output is correct
24 Correct 3 ms 15452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15192 KB Output is correct
2 Correct 2 ms 15196 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 2 ms 15196 KB Output is correct
5 Correct 137 ms 34188 KB Output is correct
6 Correct 123 ms 34028 KB Output is correct
7 Correct 137 ms 34192 KB Output is correct
8 Correct 126 ms 34128 KB Output is correct
9 Correct 3 ms 15196 KB Output is correct
10 Correct 3 ms 15196 KB Output is correct
11 Correct 3 ms 15196 KB Output is correct
12 Correct 3 ms 15228 KB Output is correct
13 Correct 3 ms 15232 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 2 ms 15196 KB Output is correct
16 Correct 2 ms 15196 KB Output is correct
17 Correct 3 ms 15196 KB Output is correct
18 Correct 2 ms 15196 KB Output is correct
19 Correct 2 ms 15196 KB Output is correct
20 Correct 3 ms 15196 KB Output is correct
21 Correct 4 ms 15460 KB Output is correct
22 Correct 3 ms 15452 KB Output is correct
23 Correct 4 ms 15448 KB Output is correct
24 Correct 3 ms 15452 KB Output is correct
25 Correct 3 ms 15196 KB Output is correct
26 Correct 3 ms 15452 KB Output is correct
27 Correct 3 ms 15208 KB Output is correct
28 Correct 3 ms 15448 KB Output is correct
29 Correct 3 ms 15452 KB Output is correct
30 Correct 3 ms 15452 KB Output is correct
31 Correct 3 ms 15448 KB Output is correct
32 Correct 3 ms 15452 KB Output is correct
33 Correct 80 ms 19680 KB Output is correct
34 Correct 86 ms 19548 KB Output is correct
35 Correct 77 ms 19684 KB Output is correct
36 Correct 87 ms 19680 KB Output is correct
37 Correct 19 ms 16472 KB Output is correct
38 Correct 82 ms 19792 KB Output is correct
39 Correct 9 ms 15708 KB Output is correct
40 Correct 82 ms 19536 KB Output is correct
41 Correct 46 ms 20176 KB Output is correct
42 Correct 54 ms 20296 KB Output is correct
43 Correct 123 ms 35152 KB Output is correct
44 Correct 88 ms 27748 KB Output is correct