Submission #960089

# Submission time Handle Problem Language Result Execution time Memory
960089 2024-04-09T16:00:52 Z Pety Logičari (COCI21_logicari) C++14
110 / 110
97 ms 23108 KB
#include <bits/stdc++.h>
//#pragma GCC target ("avx2")

using namespace std;

int viz[100002], dp[100002][2][2], n, nod1, nod2;
vector<int>G[100002];
int c1, c2;

void dfs (int x, int par) {
  viz[x] = 1;
  for (auto it : G[x]) {
    if (it != par) {
      if (viz[it]) {
        nod1 = x, nod2 = it;
      }
      else
        dfs(it, x);
    }
  }
}


void dfs (int x) {
  viz[x] = 1;
  vector<int>sons;
  for (auto it : G[x])
    if (!viz[it]){
      dfs(it);
      sons.push_back(it);
    }
  for (int par = 0; par < 2; par++)
    for (int c= 0; c < 2; c++) {
      if (x == nod1 && (c != c1 || par != 0))
        dp[x][c][par]= -1;
      else if (x == nod2 && (c != c2 || par + c1 == 2))
        dp[x][c][par] = -1;
      else {
        int sum = 0, prost = 0;
        for (auto it : sons) {
          if (dp[it][0][c] == -1) prost++;
          sum += dp[it][0][c];
        }
        if (par || (x == nod1 && c2) || (x == nod2 && c1)) 
          dp[x][c][par] = (!prost ? sum+c : -1);
        else {
          int mn  = 1e9;
          if (prost > 1) {
            dp[x][c][par] = -1;
            continue;
          }
          for (auto it : sons) {
            if (dp[it][0][c] == -1) {
              if (dp[it][1][c] != -1)
                mn = dp[it][1][c] - dp[it][0][c];
              else
                 mn = 1e9;
              break;
            }
            if (dp[it][1][c] != -1)
            mn = min(dp[it][1][c] - dp[it][0][c], mn);
          }
          if (mn != 1e9)
            dp[x][c][par] = mn+sum+c;
          else
            dp[x][c][par] = -1;
        }
      }
    }
    
}


int main () 
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(1, 0);
//  cout << nod1 << " " << nod2 << "\n";

  int ans = 1e9;
  for (c1 = 0; c1 < 2; c1++)
    for (c2 = 0; c2 < 2; c2++) {
      memset(dp, 0, sizeof(dp));
      memset(viz, 0, sizeof(viz));
      dfs(nod1);
      if (dp[nod1][0][0] != -1) ans = min(ans, dp[nod1][0][0]);
      if (dp[nod1][1][0] != -1) ans = min(ans, dp[nod1][1][0]);
    }
  cout << ((ans == 1e9 || ans < 0) ? -1 : ans);
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 92 ms 22724 KB Output is correct
6 Correct 94 ms 22716 KB Output is correct
7 Correct 90 ms 22608 KB Output is correct
8 Correct 86 ms 22712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4704 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4696 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4704 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4696 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 2 ms 4700 KB Output is correct
14 Correct 2 ms 4700 KB Output is correct
15 Correct 2 ms 4696 KB Output is correct
16 Correct 2 ms 4700 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 2 ms 4700 KB Output is correct
19 Correct 2 ms 4696 KB Output is correct
20 Correct 2 ms 4700 KB Output is correct
21 Correct 1 ms 4748 KB Output is correct
22 Correct 1 ms 4696 KB Output is correct
23 Correct 2 ms 4700 KB Output is correct
24 Correct 2 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 92 ms 22724 KB Output is correct
6 Correct 94 ms 22716 KB Output is correct
7 Correct 90 ms 22608 KB Output is correct
8 Correct 86 ms 22712 KB Output is correct
9 Correct 1 ms 4696 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4704 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4696 KB Output is correct
19 Correct 1 ms 4700 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 2 ms 4700 KB Output is correct
22 Correct 2 ms 4700 KB Output is correct
23 Correct 2 ms 4696 KB Output is correct
24 Correct 2 ms 4700 KB Output is correct
25 Correct 1 ms 4700 KB Output is correct
26 Correct 2 ms 4700 KB Output is correct
27 Correct 2 ms 4696 KB Output is correct
28 Correct 2 ms 4700 KB Output is correct
29 Correct 1 ms 4748 KB Output is correct
30 Correct 1 ms 4696 KB Output is correct
31 Correct 2 ms 4700 KB Output is correct
32 Correct 2 ms 4700 KB Output is correct
33 Correct 53 ms 8588 KB Output is correct
34 Correct 62 ms 8800 KB Output is correct
35 Correct 58 ms 8608 KB Output is correct
36 Correct 60 ms 8540 KB Output is correct
37 Correct 13 ms 5528 KB Output is correct
38 Correct 52 ms 8540 KB Output is correct
39 Correct 5 ms 4956 KB Output is correct
40 Correct 60 ms 8432 KB Output is correct
41 Correct 29 ms 10180 KB Output is correct
42 Correct 32 ms 9888 KB Output is correct
43 Correct 97 ms 23108 KB Output is correct
44 Correct 57 ms 16652 KB Output is correct