답안 #773264

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
773264 2023-07-04T18:03:28 Z CyberCow Power Plant (JOI20_power) C++17
0 / 100
1 ms 340 KB
//#include <bits/stdc++.h>
#include <random>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <chrono>
#define fr first
#define sc second
#define ad push_back
using namespace std;
using ll = long long;
mt19937 rnd(348502);
const int N = 2005;
int n;
vector<int> v[N];
int dp[N], a[N];

void Dfs(int g, int p)
{
    for (auto to : v[g])
    {
        if (to != p)
        {
            Dfs(to, g);
        }
    }
    if (p == -1)
    {
        int ma = 0, gum = 0;
        for(auto to: v[g])
        {
            ma = max(ma, dp[to]);
            gum += dp[to];
        }
        dp[g] = max(ma + 1, gum - 1);
        return;
    }
    int gum = 0;
    for (auto to : v[g])
    {
        if (to != p)
            gum += dp[to];
    }
    if (a[g] == 1)
        dp[g] = max(gum - 1, 1);
    else
        dp[g] = gum;
}

int ans = 0;

void Dfs1(int g, int p, int arj)
{
    int gum = arj, ma = arj, ma1 = 0;
    for (auto to : v[g])
    {
        if (to != p)
        {
            gum += dp[to];
            if (ma <= dp[to])
            {
                ma1 = max(ma, ma1);
                ma = max(ma, dp[to]);
            }
            else
            {
                ma1 = max(ma1, dp[to]);
            }
            ma = max(ma, dp[to]);
        }
    }
    if (a[g] == 1)
    {
        ans = max(max(ans, ma + 1), max(1, gum - 1));
    }
    for (auto to : v[g])
    {
        if (to != p)
        {
            if (a[to] == 1)
            {
            if (dp[to] == ma)
                Dfs1(to, g, max(gum - dp[to] - 1, ma1));
            else
                Dfs1(to, g, max(gum - dp[to] - 1, ma));
            }
            else
            {
                if (dp[to] == ma)
                    Dfs1(to, g, max(gum - dp[to], ma1));
                else
                    Dfs1(to, g, max(gum - dp[to], ma));
            }
        }
    }
}

void solve()
{
    int i, j, x, y;
    cin >> n;
    for (i = 0; i < n - 1; i++)
    {
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    char c;
    for (i = 1; i <= n; i++)
    {
        cin >> c;
        a[i] = c - '0';
    }
    for ( i = 1; i <= n; i++)   
    {
        if (a[i] == 1)
        {
            Dfs(i, -1);
            Dfs1(i, -1, 0);
            cout << ans;
            break;
        }
    }
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int tt = 1;
    //cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}

Compilation message

power.cpp: In function 'void solve()':
power.cpp:114:12: warning: unused variable 'j' [-Wunused-variable]
  114 |     int i, j, x, y;
      |            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Incorrect 1 ms 340 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Incorrect 1 ms 340 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Incorrect 1 ms 340 KB Output isn't correct
13 Halted 0 ms 0 KB -