Submission #487129

# Submission time Handle Problem Language Result Execution time Memory
487129 2021-11-14T13:53:23 Z JovanB Logičari (COCI21_logicari) C++17
110 / 110
71 ms 21440 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

const int N = 100000;
const ll INF = 1000000;

vector <int> graf[N+5];
vector <int> cyc;
bool in_cycle[N+5];
ll dp[N+5][2][2];
ll pd[2][2][2][2];
ll ppd[2][2][2][2];
bool visited[N+5];

int pcc;

bool dfs_cycle(int v, int p){
    visited[v] = 1;
    for(auto c : graf[v]){
        if(c == p) continue;
        if(visited[c]){
            pcc = c;
            cyc.push_back(v);
            return 1;
        }
        if(dfs_cycle(c, v)){
            if(pcc) cyc.push_back(v);
            if(v == pcc) pcc = 0;
            return 1;
        }
    }
    return 0;
}

void dfs(int v, int p){
    dp[v][0][0] = dp[v][0][1] = dp[v][1][0] = dp[v][1][1] = INF;
    vector <int> vec;
    for(auto c : graf[v]){
        if(in_cycle[c] || c == p) continue;
        dfs(c, v);
        vec.push_back(c);
    }
    /// ukljucen, sat
    if(!vec.empty()){
        dp[v][1][1] = 1;
        ll mn = dp[vec[0]][1][0] - dp[vec[0]][0][0];
        for(auto c : vec){
            dp[v][1][1] += dp[c][0][0];
            mn = min(mn, dp[c][1][0] - dp[c][0][0]);
        }
        dp[v][1][1] += mn;
    }
    /// ukljucen, nesat
    dp[v][1][0] = 1;
    for(auto c : vec) dp[v][1][0] += dp[c][0][0];
    /// neukljucen, sat
    if(!vec.empty()){
        dp[v][0][1] = 0;
        ll mn = dp[vec[0]][1][1] - dp[vec[0]][0][1];
        for(auto c : vec){
            dp[v][0][1] += dp[c][0][1];
            mn = min(mn, dp[c][1][1] - dp[c][0][1]);
        }
        dp[v][0][1] += mn;
    }
    /// neukljucen, nesat
    dp[v][0][0] = 0;
    for(auto c : vec) dp[v][0][0] += dp[c][0][1];
}

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int n;
    cin >> n;
    for(int i=1; i<=n; i++){
        int a, b;
        cin >> a >> b;
        graf[a].push_back(b);
        graf[b].push_back(a);
    }
    if(!dfs_cycle(1, 0)){
        return 0;
        dfs(1, 0);
        ll res = INF;
        for(int j=0; j<=1; j++) for(int k=0; k<=1; k++) res = min(res, dp[1][j][k]);
        cout << (res >= INF ? -1 : res) << "\n";
        return 0;
    }
    for(auto v : cyc) in_cycle[v] = 1;
    for(auto v : cyc) dfs(v, 0);
    int x = cyc.back();
    cyc.pop_back();
    int y = cyc.back();
    cyc.pop_back();
    for(int a=0; a<=1; a++) for(int b=0; b<=1; b++) for(int c=0; c<=1; c++) for(int d=0; d<=1; d++) pd[a][b][c][d] = INF;
    for(int a=0; a<=1; a++) for(int b=0; b<=1; b++) for(int c=0; c<=1; c++) for(int d=0; d<=1; d++){
        if(a && d){
            continue;
        }
        if(c && b){
            continue;
        }
        pd[a][b | c][c][d | a] = min(pd[a][b |c][c][d | a], dp[x][a][b] + dp[y][c][d]);
    }
    reverse(cyc.begin(), cyc.end());
    for(auto v : cyc){
        for(int a=0; a<=1; a++) for(int b=0; b<=1; b++) for(int c=0; c<=1; c++) for(int d=0; d<=1; d++) ppd[a][b][c][d] = pd[a][b][c][d], pd[a][b][c][d] = INF;
        for(int a=0; a<=1; a++) for(int b=0; b<=1; b++) for(int c=0; c<=1; c++) for(int d=0; d<=1; d++){
            for(int e=0; e<=1; e++){
                for(int f=0; f<=1; f++){
                    if(a && d) continue;
                    if(b && c) continue;
                    if(!b && !c) continue;
                    pd[e][f][c][d | a] = min(pd[e][f][c][d | a], ppd[e][f][a][b] + dp[v][c][d]);
                }
            }
        }
    }
    ll res = INF;
    for(int a=0; a<=1; a++) for(int b=0; b<=1; b++) for(int c=0; c<=1; c++) for(int d=0; d<=1; d++){
        if(a && d) continue;
        if(b && c) continue;
        if(!a && !d) continue;
        if(!b && !c) continue;
        res = min(res, pd[a][b][c][d]);
    }
    cout << (res >= INF ? -1 : res) << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 55 ms 15868 KB Output is correct
6 Correct 53 ms 15812 KB Output is correct
7 Correct 53 ms 15876 KB Output is correct
8 Correct 55 ms 15808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 1 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 1 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
13 Correct 1 ms 2636 KB Output is correct
14 Correct 1 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 1 ms 2636 KB Output is correct
18 Correct 1 ms 2636 KB Output is correct
19 Correct 1 ms 2636 KB Output is correct
20 Correct 1 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2764 KB Output is correct
24 Correct 1 ms 2764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 55 ms 15868 KB Output is correct
6 Correct 53 ms 15812 KB Output is correct
7 Correct 53 ms 15876 KB Output is correct
8 Correct 55 ms 15808 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 1 ms 2636 KB Output is correct
11 Correct 1 ms 2636 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 1 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 1 ms 2636 KB Output is correct
21 Correct 1 ms 2636 KB Output is correct
22 Correct 1 ms 2636 KB Output is correct
23 Correct 1 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 1 ms 2636 KB Output is correct
26 Correct 1 ms 2636 KB Output is correct
27 Correct 1 ms 2636 KB Output is correct
28 Correct 1 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2764 KB Output is correct
32 Correct 1 ms 2764 KB Output is correct
33 Correct 63 ms 8472 KB Output is correct
34 Correct 45 ms 8384 KB Output is correct
35 Correct 37 ms 8428 KB Output is correct
36 Correct 37 ms 8360 KB Output is correct
37 Correct 9 ms 4044 KB Output is correct
38 Correct 41 ms 8404 KB Output is correct
39 Correct 4 ms 3148 KB Output is correct
40 Correct 38 ms 8356 KB Output is correct
41 Correct 39 ms 10128 KB Output is correct
42 Correct 40 ms 9916 KB Output is correct
43 Correct 71 ms 21440 KB Output is correct
44 Correct 50 ms 15812 KB Output is correct