Submission #1162093

#TimeUsernameProblemLanguageResultExecution timeMemory
1162093nguynPower Plant (JOI20_power)C++20
100 / 100
152 ms31044 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long 
#define F first
#define S second
#define pb push_back 
#define pii pair<int,int>

const int N = 2e5 + 5;

int n;
vector<int> g[N]; 
int f[N][3];
int s[N];

void dfs(int u, int p) {
	int sum1 = 0;  
	for (int v : g[u]) {
		if (v == p) continue;
		dfs(v, u); 
		f[u][2] = max(f[u][2], f[v][1] + s[u]);
        f[u][2] = max(f[u][2], f[v][2]);
        f[u][1] = max(f[u][1], f[v][1]); 
		sum1 += f[v][1];
	}
	f[u][1] = max(s[u], sum1 - s[u]);
}

signed main(){
    ios_base::sync_with_stdio(false) ; 
    cin.tie(0) ; cout.tie(0) ; 
    if (fopen("INP.INP" ,"r")) {
        freopen("INP.INP" ,"r" , stdin) ;
        freopen("OUT.OUT" , "w" , stdout) ;
    }
    cin >> n;
    for (int i = 1; i < n; i++) {
    	int u, v; 
    	cin >> u >> v;
    	g[u].pb(v);
    	g[v].pb(u);
    }
    for (int i = 1; i <= n; i++) {
    	char c; 
    	cin >> c;
    	s[i] = c - '0'; 
    }
    dfs(1, 0);
    // for (int i = 1; i <= n; i++) {
    // 	cout << i << ' ' << f[i][1] << ' ' << f[i][2] << '\n';
    // }
    cout << max(f[1][1], f[1][2]) << '\n'; 
}

Compilation message (stderr)

power.cpp: In function 'int main()':
power.cpp:34:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |         freopen("INP.INP" ,"r" , stdin) ;
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         freopen("OUT.OUT" , "w" , stdout) ;
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...