Submission #520002

# Submission time Handle Problem Language Result Execution time Memory
520002 2022-01-28T05:20:11 Z goodluck2020 Logičari (COCI21_logicari) C++14
110 / 110
180 ms 26492 KB
#include <bits/stdc++.h>
#define task "main"

using namespace std;
const long long inf = 1e9;
const int N = 1e5 + 5;
int n, RootTree, Spec, Par[N];
vector < int > V[N];
struct DisjointSet
{
    int par[N];
    void init()
    {
        for(int i = 1; i <= 1e5; i++) par[i] = -1;
    }
    int root(int u)
    {
        return (par[u] < 0) ? u : (par[u] = root(par[u]));
    }
    void Merge(int u, int v)
    {
        if((u = root(u)) == (v = root(v))) return;
        if(par[u] > par[v]) swap(u, v);
        par[u] += par[v];
        par[v] = u;
    }
} DSU;
void DFS(int u)
{
    for(int v : V[u])
    if(v != Par[u])
    {
        Par[v] = u;
        DFS(v);
    }
}
int dp[N][2][2][2][2];
int Sol(int x, int me, int up, int rt, int sp)
{
    if(x == RootTree && me != rt) return inf;
    if(x == Spec && me != sp) return inf;
    if(x == Spec && up && rt) return inf;
    if(dp[x][me][up][rt][sp] != -1) return dp[x][me][up][rt][sp];
    bool covered = 0;
    if(up) covered = 1;
    if(x == RootTree && sp) covered = 1;
    if(x == Spec && rt) covered = 1;
    long long sum = me, Min = 1e9;
    for(int v : V[x])
    if(v != Par[x])
    {
        sum += 1LL * Sol(v, 0, me, rt, sp);
        Min = min(Min, 1LL * (Sol(v, 1, me, rt, sp) - Sol(v, 0, me, rt, sp)));
    }

    if(covered) return dp[x][me][up][rt][sp] = min(sum, inf);
    return dp[x][me][up][rt][sp] = min(sum + Min, inf);
}
int main()
{
    if(fopen(task ".inp","r"))
    {
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    DSU.init();
    for(int i = 1; i <= n; i++)
    {
        int u, v;
        cin >> u >> v;
        if(DSU.root(u) == DSU.root(v))
        {
            RootTree = u;
            Spec = v;
        }
        else
        {
            DSU.Merge(u, v);
            V[u].push_back(v);
            V[v].push_back(u);
        }
    }
    DFS(RootTree);
    for(int i = 0; i <= n; i++)
        for(int me = 0; me <= 1; me++)
            for(int up = 0; up <= 1; up++)
                for(int rt = 0; rt <= 1; rt++)
                    for(int sp = 0; sp <= 1; sp++) dp[i][me][up][rt][sp] = -1;
    int ans = inf;
    for(int rt = 0; rt <= 1; rt++)
        for(int sp = 0; sp <= 1; sp++)
        ans = min(ans, Sol(RootTree, rt, 0, rt, sp));
    if(ans == inf) cout << -1;
    else cout << ans;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |         freopen(task ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         freopen(task ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3020 KB Output is correct
2 Correct 1 ms 3020 KB Output is correct
3 Correct 2 ms 3020 KB Output is correct
4 Correct 2 ms 3020 KB Output is correct
5 Correct 168 ms 25252 KB Output is correct
6 Correct 171 ms 25288 KB Output is correct
7 Correct 164 ms 25324 KB Output is correct
8 Correct 173 ms 25244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3020 KB Output is correct
2 Correct 2 ms 3060 KB Output is correct
3 Correct 2 ms 3020 KB Output is correct
4 Correct 2 ms 2988 KB Output is correct
5 Correct 2 ms 3020 KB Output is correct
6 Correct 2 ms 3020 KB Output is correct
7 Correct 2 ms 3020 KB Output is correct
8 Correct 2 ms 3020 KB Output is correct
9 Correct 2 ms 3052 KB Output is correct
10 Correct 2 ms 3020 KB Output is correct
11 Correct 2 ms 3020 KB Output is correct
12 Correct 2 ms 3068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3020 KB Output is correct
2 Correct 2 ms 3060 KB Output is correct
3 Correct 2 ms 3020 KB Output is correct
4 Correct 2 ms 2988 KB Output is correct
5 Correct 2 ms 3020 KB Output is correct
6 Correct 2 ms 3020 KB Output is correct
7 Correct 2 ms 3020 KB Output is correct
8 Correct 2 ms 3020 KB Output is correct
9 Correct 2 ms 3052 KB Output is correct
10 Correct 2 ms 3020 KB Output is correct
11 Correct 2 ms 3020 KB Output is correct
12 Correct 2 ms 3068 KB Output is correct
13 Correct 2 ms 3060 KB Output is correct
14 Correct 3 ms 3064 KB Output is correct
15 Correct 2 ms 3148 KB Output is correct
16 Correct 3 ms 3064 KB Output is correct
17 Correct 2 ms 3056 KB Output is correct
18 Correct 2 ms 3068 KB Output is correct
19 Correct 2 ms 3020 KB Output is correct
20 Correct 2 ms 3148 KB Output is correct
21 Correct 2 ms 3068 KB Output is correct
22 Correct 2 ms 3148 KB Output is correct
23 Correct 2 ms 3276 KB Output is correct
24 Correct 2 ms 3188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3020 KB Output is correct
2 Correct 1 ms 3020 KB Output is correct
3 Correct 2 ms 3020 KB Output is correct
4 Correct 2 ms 3020 KB Output is correct
5 Correct 168 ms 25252 KB Output is correct
6 Correct 171 ms 25288 KB Output is correct
7 Correct 164 ms 25324 KB Output is correct
8 Correct 173 ms 25244 KB Output is correct
9 Correct 2 ms 3020 KB Output is correct
10 Correct 2 ms 3060 KB Output is correct
11 Correct 2 ms 3020 KB Output is correct
12 Correct 2 ms 2988 KB Output is correct
13 Correct 2 ms 3020 KB Output is correct
14 Correct 2 ms 3020 KB Output is correct
15 Correct 2 ms 3020 KB Output is correct
16 Correct 2 ms 3020 KB Output is correct
17 Correct 2 ms 3052 KB Output is correct
18 Correct 2 ms 3020 KB Output is correct
19 Correct 2 ms 3020 KB Output is correct
20 Correct 2 ms 3068 KB Output is correct
21 Correct 2 ms 3060 KB Output is correct
22 Correct 3 ms 3064 KB Output is correct
23 Correct 2 ms 3148 KB Output is correct
24 Correct 3 ms 3064 KB Output is correct
25 Correct 2 ms 3056 KB Output is correct
26 Correct 2 ms 3068 KB Output is correct
27 Correct 2 ms 3020 KB Output is correct
28 Correct 2 ms 3148 KB Output is correct
29 Correct 2 ms 3068 KB Output is correct
30 Correct 2 ms 3148 KB Output is correct
31 Correct 2 ms 3276 KB Output is correct
32 Correct 2 ms 3188 KB Output is correct
33 Correct 105 ms 12896 KB Output is correct
34 Correct 119 ms 12960 KB Output is correct
35 Correct 107 ms 12824 KB Output is correct
36 Correct 103 ms 12896 KB Output is correct
37 Correct 21 ms 5452 KB Output is correct
38 Correct 106 ms 12868 KB Output is correct
39 Correct 10 ms 3844 KB Output is correct
40 Correct 100 ms 12580 KB Output is correct
41 Correct 72 ms 14232 KB Output is correct
42 Correct 91 ms 14328 KB Output is correct
43 Correct 180 ms 26492 KB Output is correct
44 Correct 141 ms 20476 KB Output is correct