Submission #974059

# Submission time Handle Problem Language Result Execution time Memory
974059 2024-05-02T16:50:15 Z VinhLuu Logičari (COCI21_logicari) C++17
110 / 110
122 ms 54356 KB
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()

using namespace std;

typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 2e5 + 5;
const int oo = 1e9 + 1;
const int mod = 1e9 + 7;
//const ll oo = 5e18;

int n, d[N], X[N], Y[N], fl[N], f[N][5][5], ans, A, B, sA, sB;
vector<int> p[N], g[N];

void dfs(int u,int v){
    fl[u] = 1;
    for(auto j : p[u]){
        if(j == v) continue;
        if(fl[j]){
            A = u;
            B = j;
            continue;
        }
        g[u].pb(j);
        g[j].pb(u);
        dfs(j, u);
    }
}

void solve(int u,int v){
    f[u][0][0] = f[u][1][1] = f[u][1][0] = f[u][0][1] = oo;
    int sum = 0, w = 0;
    f[u][0][0] = f[u][1][0] = 0;
    for(auto j : g[u]){
        if(j == v) continue;
        solve(j, u);
        f[u][0][0] += f[j][0][1]; // 0
        sum += f[j][0][1]; // 1
        f[u][1][0] += f[j][0][0];
        w += f[j][0][0];
    }
    f[u][1][0]++;
    f[u][1][0] = min(f[u][1][0], oo);
    for(auto j : g[u]){
        if(j == v) continue;
//        cerr << j << " " << sum << "  "<< f[j][0][1] << " " << f[j][1][1] << " " << f[u][0][1] << " y\n";
        f[u][0][1] = min(f[u][0][1], sum - f[j][0][1] + f[j][1][1]);
        f[u][1][1] = min(f[u][1][1], 1 + w - f[j][0][0] + f[j][1][0]);
    }
//    cerr << u << " " << sum << " " << w << " " << f[u][0][0] << " " << f[u][0][1] << " " << f[u][1][0] << " " << f[u][1][1] << " bf\n";
    f[u][1][1] = min(f[u][1][1], oo);
    if((int)g[u].size() == 1 && u != A){
        f[u][0][0] = 0;
        f[u][0][1] = oo;
        f[u][1][0] = 1;
        f[u][1][1] = oo;
    }
    if(u == A){
        if(sA == 1 && sB == 1) f[u][1][1] = f[u][1][0];
        else if(sA == 0 && sB == 1) f[u][0][1] = f[u][0][0];
    }else if(u == B){
        if(sA == 1 && sB == 1){
            f[u][1][1] = f[u][1][0];
            f[u][0][1] = f[u][0][0] = f[u][1][0] = oo;
        }else if(sA == 1 && sB == 0){
            f[u][0][1] = f[u][0][0];
            f[u][0][0] = f[u][1][1] = f[u][1][0] = oo;
        }else if(sA == 0 && sB == 1){
            f[u][0][0] = f[u][0][1] = oo;
        }else if(sA == 0 && sB == 0){
            f[u][1][0] = f[u][1][1] = oo;
        }
    }

//    cerr << u << " " << sum << " " << w << " " << f[u][0][0] << " " << f[u][0][1] << " " << f[u][1][0] << " " << f[u][1][1] << " j\n";
}

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

    #define task "v"
    if(fopen(task ".inp","r")){
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }

    cin >> n;
    vector<int> vr;
    for(int i = 1; i <= n; i ++){
        cin >> X[i] >> Y[i];
        p[Y[i]].pb(X[i]);
        p[X[i]].pb(Y[i]);
    }
    dfs(1, 0);
//    cerr << A << " " << B << "\n";
    ans = oo;
    for(int i = 0; i <= 1; i ++){
        for(int j = 0; j <= 1; j ++){
            sA = i, sB = j;
            solve(A, 0);
//            cerr << " " << sA << " " << sB << " " << f[A][sA][1] << " t\n";
            ans = min(ans, f[A][sA][1]);
        }
    }
    if(ans <= n) cout << ans;
    else cout << -1;

}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         freopen(task ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         freopen(task ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 3 ms 16988 KB Output is correct
3 Correct 5 ms 16988 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 107 ms 54188 KB Output is correct
6 Correct 101 ms 54352 KB Output is correct
7 Correct 122 ms 54352 KB Output is correct
8 Correct 95 ms 54356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 3 ms 16988 KB Output is correct
3 Correct 3 ms 16988 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 3 ms 16988 KB Output is correct
8 Correct 3 ms 16988 KB Output is correct
9 Correct 3 ms 16988 KB Output is correct
10 Correct 3 ms 16988 KB Output is correct
11 Correct 3 ms 16984 KB Output is correct
12 Correct 3 ms 16984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 3 ms 16988 KB Output is correct
3 Correct 3 ms 16988 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 3 ms 16988 KB Output is correct
8 Correct 3 ms 16988 KB Output is correct
9 Correct 3 ms 16988 KB Output is correct
10 Correct 3 ms 16988 KB Output is correct
11 Correct 3 ms 16984 KB Output is correct
12 Correct 3 ms 16984 KB Output is correct
13 Correct 4 ms 16988 KB Output is correct
14 Correct 3 ms 16988 KB Output is correct
15 Correct 4 ms 16988 KB Output is correct
16 Correct 4 ms 16988 KB Output is correct
17 Correct 3 ms 16988 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 3 ms 17240 KB Output is correct
20 Correct 4 ms 16988 KB Output is correct
21 Correct 3 ms 16988 KB Output is correct
22 Correct 3 ms 16988 KB Output is correct
23 Correct 3 ms 16988 KB Output is correct
24 Correct 3 ms 16988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 3 ms 16988 KB Output is correct
3 Correct 5 ms 16988 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 107 ms 54188 KB Output is correct
6 Correct 101 ms 54352 KB Output is correct
7 Correct 122 ms 54352 KB Output is correct
8 Correct 95 ms 54356 KB Output is correct
9 Correct 3 ms 16988 KB Output is correct
10 Correct 3 ms 16988 KB Output is correct
11 Correct 3 ms 16988 KB Output is correct
12 Correct 3 ms 16988 KB Output is correct
13 Correct 3 ms 16988 KB Output is correct
14 Correct 4 ms 16988 KB Output is correct
15 Correct 3 ms 16988 KB Output is correct
16 Correct 3 ms 16988 KB Output is correct
17 Correct 3 ms 16988 KB Output is correct
18 Correct 3 ms 16988 KB Output is correct
19 Correct 3 ms 16984 KB Output is correct
20 Correct 3 ms 16984 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 3 ms 16988 KB Output is correct
23 Correct 4 ms 16988 KB Output is correct
24 Correct 4 ms 16988 KB Output is correct
25 Correct 3 ms 16988 KB Output is correct
26 Correct 4 ms 16988 KB Output is correct
27 Correct 3 ms 17240 KB Output is correct
28 Correct 4 ms 16988 KB Output is correct
29 Correct 3 ms 16988 KB Output is correct
30 Correct 3 ms 16988 KB Output is correct
31 Correct 3 ms 16988 KB Output is correct
32 Correct 3 ms 16988 KB Output is correct
33 Correct 71 ms 42988 KB Output is correct
34 Correct 84 ms 42992 KB Output is correct
35 Correct 67 ms 42836 KB Output is correct
36 Correct 64 ms 42844 KB Output is correct
37 Correct 14 ms 22872 KB Output is correct
38 Correct 72 ms 43220 KB Output is correct
39 Correct 7 ms 19548 KB Output is correct
40 Correct 88 ms 40792 KB Output is correct
41 Correct 72 ms 46472 KB Output is correct
42 Correct 70 ms 46412 KB Output is correct
43 Correct 102 ms 54352 KB Output is correct
44 Correct 86 ms 50376 KB Output is correct