답안 #880189

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
880189 2023-11-29T02:30:55 Z vjudge1 Logičari (COCI21_logicari) C++17
110 / 110
140 ms 34448 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10, MOD = 998244353;
#define int long long
int n;
vector<int> g[N];
vector<pair<int, int>> e;
int used[N], rt, sp, dp[N][2][2][2][2];
void dfs(int v, int pr = -1)
{
    used[v] = 1;
    for (int to : g[v])
    {
        if (to == pr)
            continue;
        if (!used[to])
        {
            dfs(to, v);
        }
        else
        {
            sp = v;
            rt = to;
        }
    }
}
void fill(int v, int val)
{
    for (int me = 0; me <= 1; me++)
    {
        for (int up = 0; up <= 1; up++)
        {
            for (int r = 0; r <= 1; r++)
            {
                for (int s = 0; s <= 1; s++)
                {
                    dp[v][me][up][r][s] = val;
                }
            }
        }
    }
}
int go(int v, int me, int up, int r, int s, int pr = -1)
{
    if (dp[v][me][up][r][s] != -1)
    {
        return dp[v][me][up][r][s];
    }
    int res = 1e9;
    dp[v][me][up][r][s] = 1e9;
    
    if (v == sp && me != s)
        return dp[v][me][up][r][s];
    if (v == rt && r != me)
        return dp[v][me][up][r][s];
    if (v == sp && up && r)
        return dp[v][me][up][r][s];
    bool is_black = 0;
    if (up)
        is_black = 1;
    if (v == sp && r)
        is_black = 1;
    if (v == rt && s)
        is_black = 1;
 
    int sum = me;
    for (int to : g[v])
    {
        if (to == pr)
            continue;
        sum += go(to, 0, me, r, s, v);
    }
 
    if (is_black)
    {
        res = min(res,sum);
    }
    else
    {
        for (int to : g[v])
        {
            if (to == pr){
                continue;
            }
            res = min(res, sum - go(to, 0, me, r, s, v) + go(to, 1, me, r, s, v));
        }
    }
    
    return dp[v][me][up][r][s] = res;
}
void test()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
        e.push_back({x, y});
    }
    dfs(1);
    for (int i = 1; i <= n; i++)
    {
        g[i].clear();
    }
    for (auto [x, y] : e)
    {
        if ((x == rt && y == sp) || (x == sp && y == rt))
            continue;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    int res = 1e9;
    for (int i = 1; i <= n; i++)
    {
        fill(i, -1);
    }
    // cout << rt << ' ' << sp << '\n';
    for (int me = 0; me <= 1; me++)
    {
 
        for (int s = 0; s <= 1; s++)
        {
            res = min(res, go(rt, me, 0, me, s));
        }
    }
    if (res == 1e9)
        res = -1;
    cout << res;
}
main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++)
    {
        test();
    }
}

Compilation message

Main.cpp:133:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  133 | main()
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 133 ms 34308 KB Output is correct
6 Correct 138 ms 34240 KB Output is correct
7 Correct 130 ms 34348 KB Output is correct
8 Correct 140 ms 34448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3416 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
7 Correct 1 ms 3420 KB Output is correct
8 Correct 1 ms 3420 KB Output is correct
9 Correct 1 ms 3416 KB Output is correct
10 Correct 1 ms 3416 KB Output is correct
11 Correct 1 ms 3672 KB Output is correct
12 Correct 1 ms 3416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3416 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
7 Correct 1 ms 3420 KB Output is correct
8 Correct 1 ms 3420 KB Output is correct
9 Correct 1 ms 3416 KB Output is correct
10 Correct 1 ms 3416 KB Output is correct
11 Correct 1 ms 3672 KB Output is correct
12 Correct 1 ms 3416 KB Output is correct
13 Correct 2 ms 3676 KB Output is correct
14 Correct 2 ms 3676 KB Output is correct
15 Correct 2 ms 3676 KB Output is correct
16 Correct 2 ms 3676 KB Output is correct
17 Correct 1 ms 3420 KB Output is correct
18 Correct 2 ms 3676 KB Output is correct
19 Correct 1 ms 3420 KB Output is correct
20 Correct 2 ms 3676 KB Output is correct
21 Correct 2 ms 3676 KB Output is correct
22 Correct 1 ms 3676 KB Output is correct
23 Correct 1 ms 3932 KB Output is correct
24 Correct 2 ms 3884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 133 ms 34308 KB Output is correct
6 Correct 138 ms 34240 KB Output is correct
7 Correct 130 ms 34348 KB Output is correct
8 Correct 140 ms 34448 KB Output is correct
9 Correct 1 ms 3420 KB Output is correct
10 Correct 1 ms 3420 KB Output is correct
11 Correct 1 ms 3416 KB Output is correct
12 Correct 1 ms 3420 KB Output is correct
13 Correct 1 ms 3420 KB Output is correct
14 Correct 1 ms 3420 KB Output is correct
15 Correct 1 ms 3420 KB Output is correct
16 Correct 1 ms 3420 KB Output is correct
17 Correct 1 ms 3416 KB Output is correct
18 Correct 1 ms 3416 KB Output is correct
19 Correct 1 ms 3672 KB Output is correct
20 Correct 1 ms 3416 KB Output is correct
21 Correct 2 ms 3676 KB Output is correct
22 Correct 2 ms 3676 KB Output is correct
23 Correct 2 ms 3676 KB Output is correct
24 Correct 2 ms 3676 KB Output is correct
25 Correct 1 ms 3420 KB Output is correct
26 Correct 2 ms 3676 KB Output is correct
27 Correct 1 ms 3420 KB Output is correct
28 Correct 2 ms 3676 KB Output is correct
29 Correct 2 ms 3676 KB Output is correct
30 Correct 1 ms 3676 KB Output is correct
31 Correct 1 ms 3932 KB Output is correct
32 Correct 2 ms 3884 KB Output is correct
33 Correct 76 ms 21696 KB Output is correct
34 Correct 72 ms 21696 KB Output is correct
35 Correct 74 ms 21708 KB Output is correct
36 Correct 75 ms 21708 KB Output is correct
37 Correct 21 ms 9432 KB Output is correct
38 Correct 78 ms 21696 KB Output is correct
39 Correct 6 ms 6236 KB Output is correct
40 Correct 78 ms 21684 KB Output is correct
41 Correct 53 ms 22468 KB Output is correct
42 Correct 58 ms 22464 KB Output is correct
43 Correct 117 ms 34316 KB Output is correct
44 Correct 82 ms 28356 KB Output is correct