Submission #674709

# Submission time Handle Problem Language Result Execution time Memory
674709 2022-12-26T00:34:44 Z vjudge1 Logičari (COCI21_logicari) C++17
110 / 110
231 ms 47284 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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 228 ms 47284 KB Output is correct
6 Correct 230 ms 47180 KB Output is correct
7 Correct 224 ms 47280 KB Output is correct
8 Correct 229 ms 47180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 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 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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 228 ms 47284 KB Output is correct
6 Correct 230 ms 47180 KB Output is correct
7 Correct 224 ms 47280 KB Output is correct
8 Correct 229 ms 47180 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 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 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 340 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 151 ms 19064 KB Output is correct
34 Correct 156 ms 19056 KB Output is correct
35 Correct 157 ms 19056 KB Output is correct
36 Correct 170 ms 19136 KB Output is correct
37 Correct 28 ms 5004 KB Output is correct
38 Correct 173 ms 19020 KB Output is correct
39 Correct 11 ms 1924 KB Output is correct
40 Correct 171 ms 18540 KB Output is correct
41 Correct 104 ms 21440 KB Output is correct
42 Correct 128 ms 21436 KB Output is correct
43 Correct 231 ms 47280 KB Output is correct
44 Correct 153 ms 34240 KB Output is correct