Submission #531892

# Submission time Handle Problem Language Result Execution time Memory
531892 2022-03-01T20:22:49 Z aryan12 Power Plant (JOI20_power) C++17
0 / 100
3 ms 4940 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

string power_plants;
const int N = 2e5 + 5;
vector<int> g[N];
int dp[N][2];
int ans = 0;

void dfs(int node, int par) {
	int sum = 0, maxi = 0;
	vector<pair<int, int> > children;
	for(int i = 0; i < g[node].size(); i++) {
		if(g[node][i] == par) continue;
		dfs(g[node][i], node);
		sum += max(dp[g[node][i]][0], dp[g[node][i]][1] - 2);
		maxi = max({maxi, dp[g[node][i]][0], dp[g[node][i]][1] - 2});
	}
	if(power_plants[node - 1] == '0') {
		dp[node][0] = max(0LL, sum);
		dp[node][1] = max(sum, 0LL);
	}
	else {
		dp[node][1] = max(1LL, maxi + 1);
		dp[node][0] = max(1LL, sum - 1);
	}
	//cout << "node = " << node << ", dp[node][0] = " << dp[node][0] << ", dp[node][1] = " << dp[node][1] << "\n";
}

void Solve() {
	int n;
	cin >> n;
	for(int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	cin >> power_plants;
	dfs(1, -1);
	cout << max({ans, dp[1][0], dp[1][1]}) << "\n";
}

int32_t main() {
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) {
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}

Compilation message

power.cpp: In function 'void dfs(long long int, long long int)':
power.cpp:16:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Incorrect 3 ms 4940 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Incorrect 3 ms 4940 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Incorrect 3 ms 4940 KB Output isn't correct
8 Halted 0 ms 0 KB -