답안 #868665

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
868665 2023-11-01T09:50:16 Z prohacker Power Plant (JOI20_power) C++14
0 / 100
0 ms 348 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

const int N = 5010;
const int INF = INT_MAX;
const int mod = 1e9+7;

int n,ans,dp[N];
vector<int> adj[N];
string s;

void dfs(int u, int pre) {
    dp[u] = 0;
    int res = 0;
    for(int v:adj[u]) {
        if(v == pre) {
            continue;
        }
        dfs(v,u);
        res += dp[v];
    }
    dp[u] = max(res-(s[u] == '1'),(s[u] == '1' ? 1 : 0));
    if(pre == 0) {
        int cnt = 0;
        for(int v:adj[u]) {
            if(dp[v]) {
                cnt++;
            }
        }
        if(cnt < 2) {
            dp[u]++;
        }
        ans = max(ans,dp[u]);
    }
}

signed main()
{
    if (fopen("power.inp", "r")) {
        freopen("power.inp", "r", stdin);
        freopen("power.out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 1 ; i < n ; i++) {
        int u,v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    cin >> s;
    s = " " + s;
    for(int i = 1 ; i <= n ; i++) {
        if(s[i] == '1') {
            dfs(i,0);
        }
    }
    cout << ans;
    return 0;
}

Compilation message

power.cpp: In function 'int main()':
power.cpp:43:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |         freopen("power.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:44:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |         freopen("power.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -