답안 #868759

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
868759 2023-11-02T00:46:14 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 = 2010;
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(v == pre) {
                continue;
            }
            if(dp[v] > 0) {
                cnt++;
            }
        }
        ans = max(ans,dp[u]+(cnt == 1));
    }
}

signed main()
{
    if (fopen("solve.inp", "r")) {
        freopen("solve.inp", "r", stdin);
        freopen("solve.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("solve.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("solve.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -