Submission #780164

# Submission time Handle Problem Language Result Execution time Memory
780164 2023-07-12T07:05:01 Z cig32 Logičari (COCI21_logicari) C++17
110 / 110
142 ms 72916 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 1e6 + 10;
const int MOD = 998244353;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
int inv(int b) { 
  return bm(b, MOD-2);
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
int n;
vector<int> adj[MAXN];
vector<int> cycle;
vector<int> dhs[MAXN];
bool vis[MAXN];
int par[MAXN];
int dep[MAXN];
void dfs0(int node, int prv) {
  vis[node] = 1;
  dep[node] = (prv == -1 ? 0 : dep[prv] + 1);
  par[node] = prv;
  for(int x: dhs[node]) {
    if(!vis[x]) {
      dfs0(x, node);
    }
    else if(x != prv && prv != -1) {
      cycle.clear();
      int u = node, v = x;
      if(dep[u] > dep[v]) swap(u, v);
      while(u != v) {
        cycle.push_back(v);
        v = par[v];
      }
      cycle.push_back(u);
    }
  }
}
bool constraint[MAXN];//must be white
int dp[MAXN][2][2];
void dfs(int node, int prv) {
  vector<int>child;
  for(int x: adj[node]) {
    if(x != prv) {
      dfs(x, node);
      child.push_back(x);
    }
  }
  // dp[node][0][0]
  dp[node][0][0]=0;
  for(int x:child)dp[node][0][0]+=dp[x][0][1];
  dp[node][0][0]=min(dp[node][0][0],(int)(1e9));
  // dp[node][0][1]
  int sm=0;
  for(int x:child)sm+=dp[x][0][1];
  dp[node][0][1]=1e9;
  for(int x:child){
    int nw=sm-dp[x][0][1]+dp[x][1][1];
    dp[node][0][1]=min(dp[node][0][1],nw);
  }
  // dp[node][1][0]
  dp[node][1][0]=1;
  for(int x:child)dp[node][1][0]+=dp[x][0][0];
  dp[node][1][0]=min(dp[node][1][0],(int)(1e9));
  // dp[node][1][1]
  sm=0;
  for(int x:child)sm+=dp[x][0][0];
  dp[node][1][1]=1e9;
  for(int x:child){
    int nw=sm-dp[x][0][0]+dp[x][1][0];
    dp[node][1][1]=min(dp[node][1][1],nw+1);
  }
  if(constraint[node]==1) dp[node][1][0]=dp[node][1][1]=1e9;
}
void solve(int tc){
  cin >> n;
  vector<pair<int,int>>edges;
  for(int i=1; i<=n; i++) {
    int u, v;
    cin >> u >> v;
    edges.push_back({u,v});
    dhs[u].push_back(v);
    dhs[v].push_back(u);
  }
  dfs0(1, -1);
  vector<pair<int,int>>banned;
  int len=cycle.size();
  for(int i=0;i<min(len,4ll);i++) banned.push_back({cycle[i],cycle[(i+1)%len]});
  int ans = 1e9;
  for(pair<int,int>x:banned){
    for(int i=1; i<=n; i++) {
      constraint[i] = 0;
      adj[i].clear();
      for(int j=0; j<2; j++) {
        for(int k=0; k<2; k++) {
          dp[i][j][k] = 1e9;
        }
      }
    }
    constraint[x.first] = constraint[x.second] = 1;
    pair<int,int>alt=x;
    swap(alt.first,alt.second);
    for(pair<int,int>y:edges){
      if(x==y || alt==y) continue;
      adj[y.first].push_back(y.second);
      adj[y.second].push_back(y.first);
    }
    dfs(1, -1);
    ans = min(ans, dp[1][0][1]);
    ans = min(ans, dp[1][1][1]);
  }
  cout << (ans == 1e9 ? -1 : ans) << '\n';
  
}
int32_t main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47316 KB Output is correct
2 Correct 23 ms 47296 KB Output is correct
3 Correct 22 ms 47312 KB Output is correct
4 Correct 22 ms 47264 KB Output is correct
5 Correct 125 ms 72916 KB Output is correct
6 Correct 129 ms 72848 KB Output is correct
7 Correct 136 ms 72860 KB Output is correct
8 Correct 142 ms 72852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 47328 KB Output is correct
2 Correct 20 ms 47312 KB Output is correct
3 Correct 23 ms 47224 KB Output is correct
4 Correct 22 ms 47316 KB Output is correct
5 Correct 25 ms 47316 KB Output is correct
6 Correct 19 ms 47316 KB Output is correct
7 Correct 25 ms 47300 KB Output is correct
8 Correct 19 ms 47244 KB Output is correct
9 Correct 24 ms 47288 KB Output is correct
10 Correct 25 ms 47316 KB Output is correct
11 Correct 27 ms 47280 KB Output is correct
12 Correct 24 ms 47308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 47328 KB Output is correct
2 Correct 20 ms 47312 KB Output is correct
3 Correct 23 ms 47224 KB Output is correct
4 Correct 22 ms 47316 KB Output is correct
5 Correct 25 ms 47316 KB Output is correct
6 Correct 19 ms 47316 KB Output is correct
7 Correct 25 ms 47300 KB Output is correct
8 Correct 19 ms 47244 KB Output is correct
9 Correct 24 ms 47288 KB Output is correct
10 Correct 25 ms 47316 KB Output is correct
11 Correct 27 ms 47280 KB Output is correct
12 Correct 24 ms 47308 KB Output is correct
13 Correct 23 ms 47404 KB Output is correct
14 Correct 22 ms 47408 KB Output is correct
15 Correct 25 ms 47424 KB Output is correct
16 Correct 24 ms 47444 KB Output is correct
17 Correct 21 ms 47304 KB Output is correct
18 Correct 21 ms 47336 KB Output is correct
19 Correct 21 ms 47212 KB Output is correct
20 Correct 21 ms 47456 KB Output is correct
21 Correct 24 ms 47444 KB Output is correct
22 Correct 24 ms 47468 KB Output is correct
23 Correct 24 ms 47420 KB Output is correct
24 Correct 24 ms 47524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47316 KB Output is correct
2 Correct 23 ms 47296 KB Output is correct
3 Correct 22 ms 47312 KB Output is correct
4 Correct 22 ms 47264 KB Output is correct
5 Correct 125 ms 72916 KB Output is correct
6 Correct 129 ms 72848 KB Output is correct
7 Correct 136 ms 72860 KB Output is correct
8 Correct 142 ms 72852 KB Output is correct
9 Correct 21 ms 47328 KB Output is correct
10 Correct 20 ms 47312 KB Output is correct
11 Correct 23 ms 47224 KB Output is correct
12 Correct 22 ms 47316 KB Output is correct
13 Correct 25 ms 47316 KB Output is correct
14 Correct 19 ms 47316 KB Output is correct
15 Correct 25 ms 47300 KB Output is correct
16 Correct 19 ms 47244 KB Output is correct
17 Correct 24 ms 47288 KB Output is correct
18 Correct 25 ms 47316 KB Output is correct
19 Correct 27 ms 47280 KB Output is correct
20 Correct 24 ms 47308 KB Output is correct
21 Correct 23 ms 47404 KB Output is correct
22 Correct 22 ms 47408 KB Output is correct
23 Correct 25 ms 47424 KB Output is correct
24 Correct 24 ms 47444 KB Output is correct
25 Correct 21 ms 47304 KB Output is correct
26 Correct 21 ms 47336 KB Output is correct
27 Correct 21 ms 47212 KB Output is correct
28 Correct 21 ms 47456 KB Output is correct
29 Correct 24 ms 47444 KB Output is correct
30 Correct 24 ms 47468 KB Output is correct
31 Correct 24 ms 47420 KB Output is correct
32 Correct 24 ms 47524 KB Output is correct
33 Correct 88 ms 60568 KB Output is correct
34 Correct 107 ms 60600 KB Output is correct
35 Correct 101 ms 60508 KB Output is correct
36 Correct 109 ms 60560 KB Output is correct
37 Correct 47 ms 50568 KB Output is correct
38 Correct 96 ms 60764 KB Output is correct
39 Correct 28 ms 48428 KB Output is correct
40 Correct 90 ms 60456 KB Output is correct
41 Correct 76 ms 64476 KB Output is correct
42 Correct 75 ms 63724 KB Output is correct
43 Correct 100 ms 68420 KB Output is correct
44 Correct 94 ms 68256 KB Output is correct