Submission #872640

# Submission time Handle Problem Language Result Execution time Memory
872640 2023-11-13T13:25:48 Z Foolestboy Logičari (COCI21_logicari) C++14
110 / 110
157 ms 26224 KB
#include <bits/stdc++.h>

#define SQR(x)    (1LL * ((x) * (x)))
#define MASK(i)   (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi        first
#define se        second
#define pb        push_back
#define all(x)    x.begin(), x.end()
#define rall(x)   x.rbegin(), x.rend()
#define sz(s)     (int)s.size()
#define prev      __prev
#define next      __next
#define left      __left
#define right     __right

#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int ui;

using namespace std;

const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);

const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};

template<class BUI, class TRONG>
    bool minimize(BUI &x, const TRONG y){
        if(x > y){
            x = y;
            return true;
        } else return false;
    }
template<class BUI, class TRONG>
    bool maximize(BUI &x, const TRONG y){
        if(x < y){
            x = y;
            return true;
        } else return false;
    }

/* Author : Bui Nguyen Duc Trong, Luong Van Chanh High School for the gifted*/
/* Template is copied by Trong */

                           /** Losing in Provincial Contests **/
                                    /** TRYING HARDER**/
                                   /**      ORZ     **/

/* -----------------[ MAIN CODE GOES HERE ]----------------- */

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1e5 + 10;

int n;
vector<int> adj[N];
int c[N];
int specNode, root;
int f[N][2][2][2][2];

void preDfs(int u, int p){
    c[u] = 1;
    for(int v : adj[u]){
        if(v == p) continue;
        if(c[v] == 0) preDfs(v, u);
        else if(c[v] == 1){
            specNode = v;
            root = u;
            return;
        }
    }
    c[u] = 2;
}

int dfs(int u, int p, int me, int up, int rt, int sp){
    if(f[u][me][up][rt][sp] != -1) return f[u][me][up][rt][sp];
    if(u == root && rt != me) return f[u][me][up][rt][sp] = INF;
    if(u == specNode && sp != me) return f[u][me][up][rt][sp] = INF;
    if(u == specNode && rt && up) return f[u][me][up][rt][sp] = INF;
    f[u][me][up][rt][sp] = INF;
    ll tot = 0;
    for(int v : adj[u]){
        if(v == p || (u == specNode && v == root) || (u == root && v == specNode)) continue;
        tot += dfs(v, u, 0, me, rt, sp);
    }
    if(up || (u == specNode && rt)){
        if(tot < INF) f[u][me][up][rt][sp] = tot + me;
    } else{
        for(int v : adj[u]){
            if(v == p || (u == specNode && v == root) || (u == root && v == specNode)) continue;
            ll z = tot - dfs(v, u, 0, me, rt, sp) + dfs(v, u, 1, me, rt, sp);
            if(z < INF) minimize(f[u][me][up][rt][sp], z + me);
        }
    }
    return f[u][me][up][rt][sp];
}

void solve(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for(int i = 1; i <= n; i++) c[i] = 0;
    preDfs(1, 0);
    int ans = INF;
    memset(f, -1, sizeof f);
    for(int x = 0; x < 2; x++){
        for(int y = 0; y < 2; y++){
            minimize(ans, dfs(root, 0, x, y, x, y));
        }
    }
    cout << (ans < INF ? ans : -1) << '\n';
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    const bool multitest = 0;
    int tt = 1; if(multitest) cin >> tt;

    while( tt-- ){

        solve();

    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9308 KB Output is correct
2 Correct 2 ms 9284 KB Output is correct
3 Correct 2 ms 9304 KB Output is correct
4 Correct 2 ms 9308 KB Output is correct
5 Correct 150 ms 26028 KB Output is correct
6 Correct 121 ms 26192 KB Output is correct
7 Correct 119 ms 26224 KB Output is correct
8 Correct 157 ms 26192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9308 KB Output is correct
2 Correct 2 ms 9308 KB Output is correct
3 Correct 2 ms 9308 KB Output is correct
4 Correct 3 ms 9308 KB Output is correct
5 Correct 2 ms 9308 KB Output is correct
6 Correct 3 ms 9308 KB Output is correct
7 Correct 3 ms 9308 KB Output is correct
8 Correct 2 ms 9308 KB Output is correct
9 Correct 2 ms 9308 KB Output is correct
10 Correct 2 ms 9308 KB Output is correct
11 Correct 2 ms 9308 KB Output is correct
12 Correct 2 ms 9308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9308 KB Output is correct
2 Correct 2 ms 9308 KB Output is correct
3 Correct 2 ms 9308 KB Output is correct
4 Correct 3 ms 9308 KB Output is correct
5 Correct 2 ms 9308 KB Output is correct
6 Correct 3 ms 9308 KB Output is correct
7 Correct 3 ms 9308 KB Output is correct
8 Correct 2 ms 9308 KB Output is correct
9 Correct 2 ms 9308 KB Output is correct
10 Correct 2 ms 9308 KB Output is correct
11 Correct 2 ms 9308 KB Output is correct
12 Correct 2 ms 9308 KB Output is correct
13 Correct 2 ms 9308 KB Output is correct
14 Correct 2 ms 9308 KB Output is correct
15 Correct 2 ms 9308 KB Output is correct
16 Correct 3 ms 9304 KB Output is correct
17 Correct 2 ms 9308 KB Output is correct
18 Correct 2 ms 9436 KB Output is correct
19 Correct 2 ms 9280 KB Output is correct
20 Correct 3 ms 9308 KB Output is correct
21 Correct 3 ms 9312 KB Output is correct
22 Correct 2 ms 9312 KB Output is correct
23 Correct 2 ms 9568 KB Output is correct
24 Correct 2 ms 9564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9308 KB Output is correct
2 Correct 2 ms 9284 KB Output is correct
3 Correct 2 ms 9304 KB Output is correct
4 Correct 2 ms 9308 KB Output is correct
5 Correct 150 ms 26028 KB Output is correct
6 Correct 121 ms 26192 KB Output is correct
7 Correct 119 ms 26224 KB Output is correct
8 Correct 157 ms 26192 KB Output is correct
9 Correct 2 ms 9308 KB Output is correct
10 Correct 2 ms 9308 KB Output is correct
11 Correct 2 ms 9308 KB Output is correct
12 Correct 3 ms 9308 KB Output is correct
13 Correct 2 ms 9308 KB Output is correct
14 Correct 3 ms 9308 KB Output is correct
15 Correct 3 ms 9308 KB Output is correct
16 Correct 2 ms 9308 KB Output is correct
17 Correct 2 ms 9308 KB Output is correct
18 Correct 2 ms 9308 KB Output is correct
19 Correct 2 ms 9308 KB Output is correct
20 Correct 2 ms 9308 KB Output is correct
21 Correct 2 ms 9308 KB Output is correct
22 Correct 2 ms 9308 KB Output is correct
23 Correct 2 ms 9308 KB Output is correct
24 Correct 3 ms 9304 KB Output is correct
25 Correct 2 ms 9308 KB Output is correct
26 Correct 2 ms 9436 KB Output is correct
27 Correct 2 ms 9280 KB Output is correct
28 Correct 3 ms 9308 KB Output is correct
29 Correct 3 ms 9312 KB Output is correct
30 Correct 2 ms 9312 KB Output is correct
31 Correct 2 ms 9568 KB Output is correct
32 Correct 2 ms 9564 KB Output is correct
33 Correct 72 ms 13144 KB Output is correct
34 Correct 108 ms 13296 KB Output is correct
35 Correct 88 ms 13296 KB Output is correct
36 Correct 82 ms 13304 KB Output is correct
37 Correct 18 ms 10328 KB Output is correct
38 Correct 78 ms 13144 KB Output is correct
39 Correct 7 ms 9564 KB Output is correct
40 Correct 65 ms 13084 KB Output is correct
41 Correct 27 ms 14060 KB Output is correct
42 Correct 36 ms 13908 KB Output is correct
43 Correct 114 ms 26192 KB Output is correct
44 Correct 74 ms 19952 KB Output is correct