# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
518678 |
2022-01-24T12:07:19 Z |
cig32 |
Power Plant (JOI20_power) |
C++17 |
|
6 ms |
7376 KB |
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
#define int long long
vector<int> adj[MAXN];
char c[MAXN];
int dpf(int node, int prev) {
int sum = 0;
for(int x: adj[node]) {
if(x != prev) sum += dpf(x, node);
}
if(c[node] == '0') {
return sum;
}
else {
return max(1ll, sum - 1);
}
}
int sz[MAXN];
bool cis[MAXN];
void dfs(int node) {
cis[node] = 1;
for(int x: adj[node]) {
if(!cis[x] && c[x] == '0') {
sz[x]--;
if(sz[x] == 1) {
dfs(x);
}
}
}
}
void solve(int tc) {
int N;
cin >> N;
for(int i=1; i<N; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
if(N == 1) {
cout << "1\n"; return;
}
for(int i=1; i<=N; i++) cin >> c[i];
for(int i=1; i<=N; i++) sz[i] = adj[i].size();
for(int i=1; i<=N; i++) {
if(c[i] == '0' && sz[i] == 1 && !cis[i]) {
dfs(i);
}
}
int cnt = 0;
for(int i=1; i<=N; i++) cnt += (c[i] == '0' && !cis[i]);
if(cnt == 0) {
int ans = 0;
for(int i=1; i<=N; i++) ans += (c[i] == '1');
ans = min(ans, 2ll);
for(int i=1; i<=N; i++) ans = max(ans, (int)adj[i].size() - 1);
cout << ans << "\n"; return;
}
for(int i=1; i<=N; i++) {
if(c[i] == '0' && !cis[i]) {
cout << dpf(i, -1) << "\n";
return;
}
}
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Incorrect |
6 ms |
7376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Incorrect |
6 ms |
7376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Incorrect |
6 ms |
7376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |