답안 #674703

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
674703 2022-12-26T00:16:40 Z rafatoa Logičari (COCI21_logicari) C++17
10 / 110
258 ms 45808 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 = 4e18;

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];

    int res = INF;

    bool ok = 1;
    if (x == root && me != rt) ok = 0;
    if (x == special && me != sp) ok = 0;
    if (x == special && up && rt) ok = 0;
    if (!ok) return dp[x][me][up][rt][sp] = INF;

    bool covered = 0;
    if (up) covered = 1;
    if (x == root && sp) covered = 1;
    if (x == special && rt) covered = 1;

    int sum = me;
    for (auto sus : adj[x]) {
        if (sus == par[x]) continue;
        sum += solve(sus, 0, me, rt, sp);
    }

    if (covered) {
        res = min(res, sum);
    } else {
        for (auto sus : adj[x]) {
            if (sus == par[x]) continue;
            int val = sum - solve(sus, 0, me, rt, sp) + solve(sus, 1, me, rt, sp);
            res = min(res, val);
        }
    }

    return dp[x][me][up][rt][sp] = res;
    };

    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;
}
# 결과 실행 시간 메모리 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 258 ms 45712 KB Output is correct
6 Correct 217 ms 45808 KB Output is correct
7 Correct 216 ms 45712 KB Output is correct
8 Correct 225 ms 45772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 258 ms 45712 KB Output is correct
6 Correct 217 ms 45808 KB Output is correct
7 Correct 216 ms 45712 KB Output is correct
8 Correct 225 ms 45772 KB Output is correct
9 Incorrect 1 ms 212 KB Output isn't correct
10 Halted 0 ms 0 KB -