This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
#include <utility>
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
#define temp(T) template<class T>
temp(T) using V = vector<T>;
using vi = V<int>;
using vl = V<ll>;
using vvi = V<vi>;
using vpi = V<pi>;
using vb = V<bool>;
using vvb = V<vb>;
vi dx = {0,1,-1,0};
vi dy = {1,0,0,-1};
#define sz(x) x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
using str = string;
const int MOD = 1e9 + 7;
const ll BIG = 1e18;
const int INF = 1e9 + 2;
temp(T) bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; } // set a = min(a,b)
temp(T) bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0; } // set a = max(a,b)
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s = "") {
unsyncIO();
if (sz(s)) setIn(s+".in"), setOut(s+".out"); // for USACO
}
#define read(x) int x; cin >> x
temp(T) void pr(T x) {
cout << to_string(x);
}
temp(T) void prln(T x) {
cout << to_string(x) << "\n";
}
vi readV(int n) {
vi input(n);
F0R(i,n)
cin >> input[i];
return input;
}
vi dp2;
vi up;
vvi g;
string active;
int get(int k, int y) {
if (y != - 1 && active[y] == '1') { k--; if (k < 1) k = 1;}
return k;
}
void dfs(int x, int p) {
each(y, g[x]) {
if (y == p) continue;
dfs(y, x);
int cs = dp2[y];
dp2[x] += get(cs, y);
}
}
void dfs2(int x, int p, int pp) {
if (p != -1) {
up[x] = get(dp2[p] - get(dp2[x], x) + get(up[p], pp), p);
}
each(y, g[x]) {
if (y == p) continue;
dfs2(y, x, p);
}
}
int test(int x, int p) {
int ans = 0;
each(y, g[x]) {
if (y == p) continue;
int cs = test(y, x);
}
return ans;
}
int main() {
setIO();
read(n);
g.rsz(n + 1);
F0R(i, n -1) {
int a, b; cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
cin >> active;
active = " " + active;
dp2.rsz(n + 1);
up.rsz(n + 1);
dfs(1, -1);
dfs2(1, -1, -1);
int ans =0;
FOR(i, 1, n + 1) {
int sum = dp2[i] + up[i];
if (active[i] == '1' and sum > 2) sum--;
if (active[i] == '1' and sum == 0) sum = 1;
ckmax(ans, sum);
}
cout << ans;
return 0;
}
Compilation message (stderr)
power.cpp: In function 'int test(int, int)':
power.cpp:117:13: warning: unused variable 'cs' [-Wunused-variable]
117 | int cs = test(y, x);
| ^~
power.cpp: In function 'void setIn(str)':
power.cpp:60:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
power.cpp: In function 'void setOut(str)':
power.cpp:61:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | void setOut(str s) { freopen(s.c_str(),"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... |