답안 #1032290

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1032290 2024-07-23T15:03:46 Z Whisper Logičari (COCI21_logicari) C++17
110 / 110
96 ms 32928 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define MAX         100005
int numNode;

vector<int> G[MAX];

//dp(u, me, pa, root, special)

int vis[MAX];
int special = -1, root = -1;
int dp[MAX][2][2][2][2];
bool bad(int u, int self, int pa, int rt, int sc){
    if (u == root && self != rt) return true;
    if (u == special && self != sc) return true;
    if (u == special && (pa & rt) == 1) return true;
    return false;
}
struct Dsu{
    int n;
    vector<int> par, sz;
    Dsu(int _n = 0){
        this -> n = _n;
        par.resize(n + 5);
        sz.resize(n + 5, 1);
        iota(par.begin(), par.end(), 0ll);
    }
    int find(int u){
        return u == par[u] ? u : par[u] = find(par[u]);
    }
    bool unite(int u, int v){
        u = find(u), v = find(v);
        if (u == v) return 0;
        if (sz[u] < sz[v]) swap(u, v);
        sz[u] += sz[v];
        par[v] = u;
        return 1;
    }
};
int F(int u, int p, int self, int pa, int rt, int sc){
    int &ret = dp[u][self][pa][rt][sc];
    if(~ret) return ret;
    ret = INF;
    if (bad(u, self, pa, rt, sc)) {
        return ret;
    }
    bool must = 0;
    must |= pa;
    must |= (u == special && rt == 1);
    must |= (u == root && sc == 1);

    int sm = 0;
    if (self) ++sm;
    for (int&v : G[u]) if(v ^ p){
        sm += F(v, u, 0, self, rt, sc);
    }

    if (must){
        minimize(ret, sm);
    }

    if (!must){
        for (int &v : G[u]) if(v ^ p){
            int cur = sm - F(v, u, 0, self, rt, sc) + F(v, u, 1, self, rt, sc);
            minimize(ret, cur);
        }
    }
    if (ret > INF) ret = INF;
    return ret;
}
void process(void){
    cin >> numNode;
    Dsu dsu(numNode);
    for (int i = 1; i <= numNode; ++i){
        int u, v; cin >> u >> v;
        if(!dsu.unite(u, v)){
            special = u, root = v;
            continue;
        }
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }
    int ans = INF;
    REP(i, 2) REP(j, 2){
        memset(dp, -1, sizeof dp);
        minimize(ans, F(root, 0, i, 0, i, j));
    }
    if(ans == INF) ans = -1;
    cout << ans;
}
signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    process();
    return (0 ^ 0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15960 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Correct 3 ms 15964 KB Output is correct
4 Correct 3 ms 15964 KB Output is correct
5 Correct 82 ms 32924 KB Output is correct
6 Correct 78 ms 32852 KB Output is correct
7 Correct 96 ms 32928 KB Output is correct
8 Correct 81 ms 32920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Correct 3 ms 15960 KB Output is correct
4 Correct 3 ms 15960 KB Output is correct
5 Correct 4 ms 15964 KB Output is correct
6 Correct 3 ms 15964 KB Output is correct
7 Correct 3 ms 15964 KB Output is correct
8 Correct 3 ms 15960 KB Output is correct
9 Correct 3 ms 15960 KB Output is correct
10 Correct 3 ms 15964 KB Output is correct
11 Correct 3 ms 15964 KB Output is correct
12 Correct 3 ms 15964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Correct 3 ms 15960 KB Output is correct
4 Correct 3 ms 15960 KB Output is correct
5 Correct 4 ms 15964 KB Output is correct
6 Correct 3 ms 15964 KB Output is correct
7 Correct 3 ms 15964 KB Output is correct
8 Correct 3 ms 15960 KB Output is correct
9 Correct 3 ms 15960 KB Output is correct
10 Correct 3 ms 15964 KB Output is correct
11 Correct 3 ms 15964 KB Output is correct
12 Correct 3 ms 15964 KB Output is correct
13 Correct 4 ms 15964 KB Output is correct
14 Correct 4 ms 15964 KB Output is correct
15 Correct 3 ms 15964 KB Output is correct
16 Correct 4 ms 15924 KB Output is correct
17 Correct 3 ms 15960 KB Output is correct
18 Correct 4 ms 15960 KB Output is correct
19 Correct 3 ms 15964 KB Output is correct
20 Correct 4 ms 15964 KB Output is correct
21 Correct 4 ms 15960 KB Output is correct
22 Correct 4 ms 15964 KB Output is correct
23 Correct 4 ms 16220 KB Output is correct
24 Correct 3 ms 16220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15960 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Correct 3 ms 15964 KB Output is correct
4 Correct 3 ms 15964 KB Output is correct
5 Correct 82 ms 32924 KB Output is correct
6 Correct 78 ms 32852 KB Output is correct
7 Correct 96 ms 32928 KB Output is correct
8 Correct 81 ms 32920 KB Output is correct
9 Correct 3 ms 15964 KB Output is correct
10 Correct 3 ms 15964 KB Output is correct
11 Correct 3 ms 15960 KB Output is correct
12 Correct 3 ms 15960 KB Output is correct
13 Correct 4 ms 15964 KB Output is correct
14 Correct 3 ms 15964 KB Output is correct
15 Correct 3 ms 15964 KB Output is correct
16 Correct 3 ms 15960 KB Output is correct
17 Correct 3 ms 15960 KB Output is correct
18 Correct 3 ms 15964 KB Output is correct
19 Correct 3 ms 15964 KB Output is correct
20 Correct 3 ms 15964 KB Output is correct
21 Correct 4 ms 15964 KB Output is correct
22 Correct 4 ms 15964 KB Output is correct
23 Correct 3 ms 15964 KB Output is correct
24 Correct 4 ms 15924 KB Output is correct
25 Correct 3 ms 15960 KB Output is correct
26 Correct 4 ms 15960 KB Output is correct
27 Correct 3 ms 15964 KB Output is correct
28 Correct 4 ms 15964 KB Output is correct
29 Correct 4 ms 15960 KB Output is correct
30 Correct 4 ms 15964 KB Output is correct
31 Correct 4 ms 16220 KB Output is correct
32 Correct 3 ms 16220 KB Output is correct
33 Correct 60 ms 21596 KB Output is correct
34 Correct 56 ms 21584 KB Output is correct
35 Correct 72 ms 21588 KB Output is correct
36 Correct 58 ms 21780 KB Output is correct
37 Correct 16 ms 17496 KB Output is correct
38 Correct 59 ms 21884 KB Output is correct
39 Correct 9 ms 16476 KB Output is correct
40 Correct 61 ms 21596 KB Output is correct
41 Correct 32 ms 22480 KB Output is correct
42 Correct 33 ms 22604 KB Output is correct
43 Correct 87 ms 32804 KB Output is correct
44 Correct 48 ms 27712 KB Output is correct