답안 #391456

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
391456 2021-04-18T19:15:15 Z MrRobot_28 Svjetlo (COCI20_svjetlo) C++17
110 / 110
726 ms 117728 KB
#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define sz(a) (int)a.size()
#define ll long long
#define int long long
#define ld long double
const int N = 5e5 + 100;
int dp[N][2][2];
int n;
vector <int> g[N];
string s;
void dfs(int v, int p = -1)
{
    int sum = 0;
    bool fl = (s[v] == '0' ? 1 : 0);
    for(auto to : g[v])
    {
        if(to == p)
        {
            continue;
        }
        dfs(to, v);
        if(dp[to][0][0] != 1e9)
        {
            sum += dp[to][0][0] + (dp[to][0][0] == 0 ? 0 : 3);
        }
        else
        {
            sum += dp[to][0][1] + 1;
            fl = !fl;
        }
    }
    if(sum || s[v] == '0')
    {
        sum++;
    }
    dp[v][0][fl] = sum;
}
int dfs1(int v, int p = -1, int g1 = 0, int g2 = 0)
{
    int ret = 1e9;
    int sum = 0;
    bool fl = 0;
    if(dp[v][0][0] != 1e9) fl = 0;
    else fl = 1;
    sum = dp[v][0][fl];
    for(auto to : g[v])
    {
        if(to == p)
        {
            continue;
        }
        int new_sum = sum;
        bool new_fl = fl;
        if(dp[to][0][0] != 1e9)
        {
            new_sum -= dp[to][0][0] + (dp[to][0][0] == 0 ? 0 : 3);
        }
        else
        {
            new_sum -= dp[to][0][1] + 1;
            new_fl = !new_fl;
        }
        if(new_sum == 1 && !new_fl) new_sum = 0;
        int new_sum1 = new_sum;
        if(g1 && !new_sum1) new_sum1++;
        new_sum1 += g1;
        if(g2) new_sum1++;
        else if(g1) new_sum1 += 3;
        ret = min(ret, dfs1(to, v, new_sum1, g2 ^ new_fl));
        if(new_fl == 0)
        {
            if(new_sum == 0)
            {
                new_sum = 1;
            }
            dp[v][1][0] = min(dp[v][1][0], new_sum + dp[to][1][1]);
            dp[v][1][1] = min(dp[v][1][1], new_sum + dp[to][1][0] + 2);
         }
        else
        {
            dp[v][1][0] = min(dp[v][1][0], new_sum + dp[to][1][0] + 2);
            dp[v][1][1] = min(dp[v][1][1], new_sum + dp[to][1][1]);
        }
    }
    dp[v][1][fl] = min(dp[v][1][fl], max(1LL, dp[v][0][fl]));
   // cout << dp[v][1][1] << " " << dp[v][1][0] << " ";
    if(g1 == 0)
    {
        ret = min(ret, dp[v][1][1]);
    }
    else if(g2)
    {
        ret = min(ret, dp[v][1][0] + g1 + 1);
    }
    else
    {
        ret = min(ret, dp[v][1][1] + g1 + 3);
    }
    int fl1 = 0;
    if(dp[v][0][1] != 1e9)
    {
        fl1 = 1;
    }
    int sum1 = dp[v][0][fl];
    if(!sum1)
    {
        sum1 = 1;
    }
    sum1 += g1;
    if(g2)
    {
        sum1++;
        fl1 ^= 1;
    }
    else if(g1)
    {
        sum1 += 3;
    }
    int mini[2];
    mini[0] = mini[1] = 1e9;
    for(auto to : g[v])
    {
        if(to == p)
        {
            continue;
        }
        int fl2 = 0;
        if(dp[to][0][1] != 1e9)
        {
            fl2 = 1;
        }
        int s1 = dp[to][0][fl2];
        if(fl2)
        {
            s1++;
        }
        else if(s1)
        {
            s1 += 3;
        }
        int new_sum = sum1 - s1;
        int new_fl = fl2 ^ fl1;
        ret = min(ret, dp[to][1][0] + 2 + new_sum + mini[new_fl]);
        ret = min(ret, dp[to][1][1] + new_sum + mini[!new_fl]);
        if(!fl2)
        {
            mini[0] = min(mini[0], dp[to][1][1] - s1);
            mini[1] = min(mini[1], dp[to][1][0] + 2 - s1);
        }
        else
        {
            mini[0] = min(mini[0], dp[to][1][0] + 2 - s1);
            mini[1] = min(mini[1], dp[to][1][1] - s1);
        }
    }
 //   cout << ret << "\n";
    return ret;
}
signed main()
{
//    ios_base::sync_with_stdio(0);
  //  cin.tie(0);
    //cout.tie(0);
    cin >> n;
    cin >> s;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < 2; j++)
        {
            for(int t = 0; t < 2; t++)
            {
                dp[i][j][t] = 1e9;
            }
        }
    }
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(0);
    cout << dfs1(0);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11976 KB Output is correct
2 Correct 8 ms 12056 KB Output is correct
3 Correct 8 ms 12004 KB Output is correct
4 Correct 8 ms 11980 KB Output is correct
5 Correct 8 ms 11980 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 8 ms 11980 KB Output is correct
8 Correct 8 ms 11980 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 465 ms 70448 KB Output is correct
2 Correct 678 ms 105456 KB Output is correct
3 Correct 663 ms 117728 KB Output is correct
4 Correct 644 ms 81688 KB Output is correct
5 Correct 684 ms 94676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 569 ms 43404 KB Output is correct
2 Correct 726 ms 51904 KB Output is correct
3 Correct 659 ms 51352 KB Output is correct
4 Correct 519 ms 46684 KB Output is correct
5 Correct 544 ms 51772 KB Output is correct
6 Correct 478 ms 46944 KB Output is correct
7 Correct 574 ms 52004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11976 KB Output is correct
2 Correct 8 ms 12056 KB Output is correct
3 Correct 8 ms 12004 KB Output is correct
4 Correct 8 ms 11980 KB Output is correct
5 Correct 8 ms 11980 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 8 ms 11980 KB Output is correct
8 Correct 8 ms 11980 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 465 ms 70448 KB Output is correct
11 Correct 678 ms 105456 KB Output is correct
12 Correct 663 ms 117728 KB Output is correct
13 Correct 644 ms 81688 KB Output is correct
14 Correct 684 ms 94676 KB Output is correct
15 Correct 569 ms 43404 KB Output is correct
16 Correct 726 ms 51904 KB Output is correct
17 Correct 659 ms 51352 KB Output is correct
18 Correct 519 ms 46684 KB Output is correct
19 Correct 544 ms 51772 KB Output is correct
20 Correct 478 ms 46944 KB Output is correct
21 Correct 574 ms 52004 KB Output is correct
22 Correct 618 ms 47344 KB Output is correct
23 Correct 673 ms 49404 KB Output is correct
24 Correct 619 ms 47708 KB Output is correct
25 Correct 561 ms 45048 KB Output is correct
26 Correct 578 ms 54500 KB Output is correct
27 Correct 588 ms 54256 KB Output is correct
28 Correct 507 ms 48552 KB Output is correct
29 Correct 545 ms 51460 KB Output is correct
30 Correct 520 ms 48716 KB Output is correct