Submission #588129

# Submission time Handle Problem Language Result Execution time Memory
588129 2022-07-02T17:53:35 Z MilosMilutinovic Logičari (COCI21_logicari) C++14
50 / 110
2 ms 596 KB
/**
 *    author:  wxhtzdy
 *    created: 02.07.2022 11:58:28
**/
#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}
string to_string(const char* s) {
  return to_string((string) s);
}
string to_string(bool b) {
  return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n; i++) {
    int u, v;
    cin >> u >> v;
    --u; --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vector<int> f(n);
  vector<int> par(n);
  vector<int> cyc;
  function<void(int, int)> Dfs = [&](int v, int pr) {
    if (!cyc.empty()) {
      return;
    }
    f[v] = 1;
    par[v] = pr;
    for (int u : g[v]) {
      if (u != pr && f[u] == 1) {
        int x = v;        
        while (x != u) {    
          cyc.push_back(x);
          x = par[x];
        }
        cyc.push_back(u);
        return;
      }     
      if (u != pr && f[u] == 0) {
        Dfs(u, v);
        if (!cyc.empty()) {
          return;
        }
      }
    }
    f[v] = 2;
  };
  Dfs(0, 0);
  reverse(cyc.begin(), cyc.end());
  vector<bool> on(n);
  for (int i = 0; i < (int) cyc.size(); i++) {
    on[cyc[i]] = true;
  }
  const int inf = 1e6;                                       
  vector<vector<vector<long long>>> dp(n, vector<vector<long long>>(2, vector<long long>(2, inf)));
  function<void(int, int)> Solve = [&](int v, int pr) {
    vector<int> ch;
    for (int u : g[v]) {
      if (u == pr || on[u]) {
        continue;
      }
      Solve(u, v);
      ch.push_back(u);
    }
    if (ch.empty()) {
      dp[v][0][0] = 0;
      dp[v][1][0] = 1;
      return;
    }
    {
      // color v
      long long ft = 0;
      for (int i : ch) {
        ft += dp[i][0][0];
      }
      dp[v][1][0] = ft + 1;
      for (int i : ch) {
        dp[v][1][1] = min(dp[v][1][1], ft - dp[i][0][0] + dp[i][1][0] + 1);  
      }
    }
    {
      // don't color v 
      long long ft = 0;
      for (int i : ch) { 
        ft += dp[i][0][1];
      }
      dp[v][0][0] = ft;
      for (int i : ch) {
        dp[v][0][1] = min(dp[v][0][1], ft - dp[i][0][1] + dp[i][1][1]);
      }       
    }
  };
  for (int i = 0; i < n; i++) {
    if (!on[i]) {
      continue;
    }
    Solve(i, i);
  }                                     
  int m = (int) cyc.size();               
  vector<vector<vector<long long>>> pref(m, vector<vector<long long>>(8, vector<long long>(8, inf)));
  vector<vector<vector<long long>>> suff(m, vector<vector<long long>>(8, vector<long long>(8, inf)));
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) {
        if (j == 1 && k == 1) {
          continue;
        }
        int state = i * 4 + j * 2 + k;
        pref[0][state][state] = dp[cyc[0]][i][j];   
        suff[m - 1][state][state] = dp[cyc[m - 1]][i][j];
      }    
    }
  }
  for (int i = 0; i < (int) cyc.size(); i++) {
    debug(cyc[i], dp[cyc[i]]);
  }   
  for (int i = 1; i < m; i++) {
    for (int x = 0; x < 8; x++) {
      for (int prv = 0; prv < 8; prv++) {
        for (int st = 0; st < 8; st++) {
          if ((st & 1) != (prv >> 2 & 1)) {
            continue;
          }
          if ((st & 1) && (st >> 1 & 1)) {
            continue;
          } 
          int cx = 0;
          if (st >> 2 & 1) {
            cx += 1;
          }
          if (prv & 1) {
            cx += 1;
          }
          if (prv >> 1 & 1) {
            cx += 1;
          }
          if (cx != 1) {
            continue;
          }
          pref[i][x][st] = min(pref[i][x][st], pref[i - 1][x][prv] + dp[cyc[i]][st >> 2 & 1][st >> 1 & 1]);
        }
      }
    }
  }
  debug(pref[1][5][1]);
  debug(suff[2][5][5]); 
  // todo: Dfs dp and merge of (xx, yy) 
  for (int i = m - 2; i >= 0; i--) {
    for (int x = 0; x < 8; x++) {
      for (int prv = 0; prv < 8; prv++) {
        for (int st = 0; st < 8; st++) {
          if ((st & 1) != (prv >> 2 & 1)) {
            continue;
          }
          if ((st & 1) && (st >> 1 & 1)) {
            continue;
          } 
          int cx = 0;
          if (st >> 2 & 1) {
            cx += 1;
          }
          if (prv & 1) {
            cx += 1;
          }
          if (prv >> 1 & 1) {
            cx += 1;
          }
          if (cx != 1) {
            continue;
          }
          suff[i][x][st] = min(suff[i][x][st], suff[i + 1][x][prv] + dp[cyc[i]][st >> 2 & 1][st >> 1 & 1]);
        }
      }
    }
  }
  long long ans = inf;
  for (int i = 0; i < 8; i++) {
    if (i < 4) {
      continue;
    }
    for (int j = 0; j < 8; j++) {
      int cx = 0;
      if (j & 1) {
        cx += 1;
      }
      if (j >> 1 & 1) {
        cx += 1;
      }
      if (i & 1) {
        cx += 1;
      }
      if (cx != 1) {
        continue;        
      }
      if (j < 4) {
        continue;
      }
      ans = min(ans, pref[m - 1][i][j]);
    }
  }
  for (int i = 0; i < m - 1; i++) {
    for (int x = 0; x < 8; x++) {
      for (int y = 0; y < 8; y++) {
        if ((x & 1) != (y >> 2 & 1)) {
          continue;
        }  
        if ((y & 1) != (x >> 2 & 1)) {
          continue;
        }    
        for (int xx = 0; xx < 8; xx++) {
          for (int yy = 0; yy < 8; yy++) {
            {
              int cx = (yy >= 4);
              if (xx & 1) {
                cx += 1;
              }
              if (xx >> 1 & 1) {
                cx += 1;
              }
              if (cx != 1) { 
                continue;
              }
            }
            {
              int cx = (xx >= 4);
              if (yy & 1) {
                cx += 1;
              }
              if (yy >> 1 & 1) {
                cx += 1;
              }
              if (cx != 1) { 
                continue;
              }
            }
            if (pref[i][x][xx] + suff[i + 1][y][yy] == 3) {
              debug(i, x, xx, y, yy);
            }
            ans = min(ans, pref[i][x][xx] + suff[i + 1][y][yy]);    
          }
        }
      }
    } 
  }
  cout << (ans == inf ? -1 : ans) << '\n';
  return 0;
}                                    

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:45:20: warning: statement has no effect [-Wunused-value]
   45 | #define debug(...) 42
      |                    ^~
Main.cpp:156:5: note: in expansion of macro 'debug'
  156 |     debug(cyc[i], dp[cyc[i]]);
      |     ^~~~~
Main.cpp:45:20: warning: statement has no effect [-Wunused-value]
   45 | #define debug(...) 42
      |                    ^~
Main.cpp:186:3: note: in expansion of macro 'debug'
  186 |   debug(pref[1][5][1]);
      |   ^~~~~
Main.cpp:45:20: warning: statement has no effect [-Wunused-value]
   45 | #define debug(...) 42
      |                    ^~
Main.cpp:187:3: note: in expansion of macro 'debug'
  187 |   debug(suff[2][5][5]);
      |   ^~~~~
Main.cpp:45:20: warning: statement has no effect [-Wunused-value]
   45 | #define debug(...) 42
      |                    ^~
Main.cpp:278:15: note: in expansion of macro 'debug'
  278 |               debug(i, x, xx, y, yy);
      |               ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -