#include<bits/stdc++.h>
using namespace std;
#define NAME "FLORENTINO"
#define nl '\n'
#define allofa(x,sz) x,x+sz+1
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,val) memset(x,val,sizeof(x))
#define couf(x) cout << fixed << setprecision(x)
template<class T> T Abs(T &x) {return (x>=0 ? x : -x);};
template<class X,class Y> bool minimize(X &a, Y b){if(a>b) {a=b;return true;}return false;};
template<class X,class Y> bool maximize(X &a, Y b){if(a<b) {a=b;return true;}return false;};
typedef long long ll;
const ll mod = (long long)1e9+7;
const ll LINF = (long long)1e15;
const int INF = (int)1e9;
const int MAXN = (int)2e5+5;
vector<int> graph[MAXN];
int dp[MAXN][2];
char s[MAXN];
int n;
void ccps() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen(NAME".inp","r")) {
freopen(NAME".inp","r",stdin);
freopen(NAME".out","w",stdout);
}
}
void dfs(int u, int pre) {
dp[u][1]=0;
for (int v : graph[u]) {
if(v^pre) {
dfs(v,u);
dp[u][1]+=dp[v][1];
}
}
dp[u][1] = max(dp[u][1]-(s[u]=='1' ? 1 : 0),(s[u]=='1' ? 1 : 0));
int opt1 = 0, opt2 = 0, opt3 = 0;
for (int v : graph[u]) {
if(v^pre) {
maximize(opt1,dp[v][1]);
maximize(opt3,dp[v][0]);
opt2+=dp[v][1];
}
}
dp[u][0]=max({(s[u]=='1' ? 1+opt1 : -INF),
opt2-(s[u]=='1' ? 1 : 0),
opt3,});
}
signed main() {
ccps();
cin >> n;
for (int i = 1; i<n; i++) {
int u,v; cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for (int i = 1; i<=n; i++) {
cin >> s[i];
}
dfs(1,0);
cout << dp[1][0];
}
컴파일 시 표준 에러 (stderr) 메시지
power.cpp: In function 'void ccps()':
power.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen(NAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
power.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen(NAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |