Submission #1013286

#TimeUsernameProblemLanguageResultExecution timeMemory
1013286atomLogičari (COCI21_logicari)C++17
110 / 110
107 ms35160 KiB
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;

#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif

#define int long long

struct DisJointSet {
    int n;
    vector <int> pa, sz;
    DisJointSet(int _n) {
        n = _n;
        pa.resize(n + 5);
        sz.resize(n + 5);
        for (int i = 1; i <= n; ++i) {
           pa[i] = i;
           sz[i] = 1;
        }
    }
    int get(int u) { return (u == pa[u]? u : pa[u] = get(pa[u])); }
    bool unite(int u, int v) {
        u = get(u);
        v = get(v);
        if (u == v) return 0;
        if (sz[u] < sz[v]) swap(u, v);
        sz[u] += sz[v];
        pa[v] = u;
        return 1;
    }
};

const int N = 1e5 + 5;
const int INF = 1e9;

vector <int> adj[N];
int st, en;
int ans;

int dp[N][2][2][2][2];
// dp(x, self, pa, root, end) : 
// number of 1 nodes in x's subtree with color self, color of parent self, color of root, color of other root: end 

int dfs(int u, int p, bool self, bool pa, bool root, bool end) {
    if (dp[u][self][pa][root][end] != -1) 
        return dp[u][self][pa][root][end];

    // invalid case
    bool invalid = false;
    if (u == st && root != self) invalid = true;
    if (u == en && end != self) invalid = true;
    if (u == en && pa == 1 && root == 1) invalid = true;

    if (invalid) 
        return dp[u][self][pa][root][end] = INF;

    int res = INF;

    bool must = false; // handle case when current node having adjacent one 1-node
    if (pa) must = true;
    if (u == st && end == 1) must = true;
    if (u == en && root == 1) must = true;

    int sum = 0;
    if (self) ++sum;
    // must = 1 -> u only takes result from 0-node child
    for (int v : adj[u]) {
        if (v == p) continue;
        sum += dfs(v, u, 0, self, root, end);
    }
    if (must)
        res = min(res, sum);
    // must = 0 -> fix one child by 1, others by 0
    if (!must) {
        for (int v : adj[u]) {
            if (v == p) continue;
            int cur = sum - dfs(v, u, 0, self, root, end) + dfs(v, u, 1, self, root, end);
            res = min(res, cur);
        }
    }

    if (res > INF) res = INF;
    return dp[u][self][pa][root][end] = res;
}

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

    #ifdef JASPER
       freopen("in1", "r", stdin);
    #endif

    int n; cin >> n;
    DisJointSet dsu(n);

    
    for (int i = 1; i <= n; ++i) {
        int u, v; 
        cin >> u >> v;

        if (!dsu.unite(u, v)) {
            st = u;
            en = v;
        }
        else {
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
    }

    if (st > en) swap(st, en);

    debug(st, en);

    ans = INF;
    for (int i = 0; i <= 1; ++i) {
        for (int j = 0; j <= 1; ++j) {
            memset(dp, -1, sizeof dp);  
            ans = min(ans, dfs(st, 0, i, 0, i, j));
        }
    }

    if (ans == INF) ans = -1;
    cout << ans << "\n";


    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:9:20: warning: statement has no effect [-Wunused-value]
    9 | #define debug(...) 166
      |                    ^~~
Main.cpp:118:5: note: in expansion of macro 'debug'
  118 |     debug(st, en);
      |     ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...