Submission #674706

# Submission time Handle Problem Language Result Execution time Memory
674706 2022-12-26T00:34:05 Z rafatoa Logičari (COCI21_logicari) C++17
110 / 110
238 ms 48436 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
 
#define F first
#define S second
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define vpi vector<pi>
#define vb vector<bool>
#define vvb vector<vb>
#define pb push_back
#define ppb pop_back
#define read(a) for(auto &x:a) cin >> x;
#define print(a) for(auto x:a) cout << x << " "; cout << "\n";
#define vc vector<char>
#define vvc vector<vc>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
 
#define int long long
#define ld long double
const int INF = 1e18;
const int inf = 1e9;
 
struct DSU{
    vi link, sz;
    DSU(int n){
        link = sz = vi(n+1);
        for(int i=1; i<=n; i++)
            link[i] = i, sz[i] = 1;
    }
    int find(int x){
        if(x == link[x]) return x;
        return link[x] = find(link[x]);
    }
    bool same(int a, int b){
        return find(a) == find(b);
    }
    void unite(int a, int b){
        a = find(a), b = find(b);
        if(a == b) return;
        if(sz[a] < sz[b]) swap(a, b);
        link[b] = a;
        sz[a] += sz[b];
    }
};
 
void solve(){
    int n; cin >> n;
    vvi adj(n+1);
    DSU dsu(n);
 
    int root, special;
    for(int i=0; i<n; i++){
        int u, v; cin >> u >> v;
        if(dsu.same(u, v)){
            root = u;
            special = v;
        } else {
            dsu.unite(u, v);
            adj[u].pb(v); adj[v].pb(u);
        }
    }
 
    vi par(n+1, -1);
    function<void(int)> dfs = [&](int s){
        for(auto u:adj[s]){
            if(u == par[s]) continue;
            par[u] = s;
            dfs(u);
        }
    };
    dfs(root);
 
    int dp[n+1][2][2][2][2];
    memset(dp, -1, sizeof dp);
    function<int(int, int, int, int, int)> solve = [&](int x, int me, int up, int rt, int sp){
        if(dp[x][me][up][rt][sp] != -1) return dp[x][me][up][rt][sp];
 
        bool ok = (!(x == root && rt != me) && !(x == special && sp != me) && !(x == special && up && rt));
        if(!ok) return dp[x][me][up][rt][sp] = inf;
 
        bool cov = (up || (x == special && rt) || (x == root && sp));
        
        int sum = me;
        for(auto u:adj[x])
            if(u != par[x]) sum += solve(u, 0, me, rt, sp);
        
        if(cov) return dp[x][me][up][rt][sp] = sum;
        else {
            int ans = inf;
            for(auto u:adj[x])
                if(u != par[x]) ans = min(ans,
                    sum-solve(u, 0, me, rt, sp)+solve(u, 1, me, rt, sp));
            return dp[x][me][up][rt][sp] = ans;
        }
    };
 
    int ans = inf;
    for(int rt=0; rt<=1; rt++)
    for(int sp=0; sp<=1; sp++)
        ans = min(ans, solve(root, rt, 0, rt, sp));
    cout << (ans == inf ? -1 : ans) << "\n";
}
 
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    // #ifndef ONLINE_JUDGE
    //     freopen("in.txt", "r", stdin);
    //     freopen("out.txt", "w", stdout);
    // #endif
 
    int tt = 1;
    // cin >> tt;
    while(tt--)
        solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 238 ms 47288 KB Output is correct
6 Correct 235 ms 47212 KB Output is correct
7 Correct 231 ms 47168 KB Output is correct
8 Correct 227 ms 47288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 452 KB Output is correct
15 Correct 1 ms 452 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 212 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 724 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 238 ms 47288 KB Output is correct
6 Correct 235 ms 47212 KB Output is correct
7 Correct 231 ms 47168 KB Output is correct
8 Correct 227 ms 47288 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 324 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 452 KB Output is correct
23 Correct 1 ms 452 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 724 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 160 ms 20100 KB Output is correct
34 Correct 147 ms 20100 KB Output is correct
35 Correct 146 ms 20104 KB Output is correct
36 Correct 137 ms 20072 KB Output is correct
37 Correct 25 ms 5204 KB Output is correct
38 Correct 148 ms 20080 KB Output is correct
39 Correct 8 ms 1992 KB Output is correct
40 Correct 138 ms 19748 KB Output is correct
41 Correct 84 ms 22592 KB Output is correct
42 Correct 100 ms 22460 KB Output is correct
43 Correct 177 ms 48436 KB Output is correct
44 Correct 155 ms 35484 KB Output is correct