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>
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "Power Plant"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rng);
}
int n;
int cl[maxn];
vector<int> edges[maxn];
namespace Sub1
{
int a[maxn];
int calc(int u, int par) {
int ans = 0;
for(int v : edges[u]) if(v != par) ans += calc(v, u);
if(a[u]) ans = 1;
return ans;
}
int solve()
{
int ans = 0;
fi(x, 1, (1 << n) - 1)
{
fi(i, 1, n) {
a[i] = 0;
if(getbit(x, i - 1)) a[i] = cl[i];
}
int sum = 0;
fi(i, 1, n) if(cl[i])
{
int w = calc(i, 0);
if(w > 1) sum --;
else sum += (w == 1) && a[i];
}
ans = max(ans, sum);
}
return ans;
}
}
namespace Sub2
{
int res = 1;
int dp[maxn];
void dfs(int u, int par)
{
vector<int> P;
for(int v : edges[u]) if(v != par) {
dfs(v, u);
P.push_back(dp[v]);
}
sort(all(P), greater<int>());
int ans = cl[u];
int sum = 0;
if(P.size() >= 1)
{
res = max(res, P[0] + cl[u]), ans = max(ans, P[0] + cl[u]);
fi(i, 0, P.size() - 1) {
sum += P[i];
if(i > 1) res = max(res, sum - cl[u]), ans = max(ans, sum - cl[u]);
}
dp[u] = sum;
}
if(cl[u]) dp[u] = max(dp[u] - 1, 1);
}
int solve()
{
dfs(1, 0);
return res;
}
}
void Input()
{
cin >> n;
fi(i, 1, n - 1) {
int x, y;
cin >> x >> y;
edges[x].push_back(y), edges[y].push_back(x);
}
fi(i, 1, n) {
char x; cin >> x;
cl[i] = x - '0';
}
}
pair<int, int> E[maxn];
void gen()
{
n = GetRandom(2, 15);
fi(i, 1, n - 1) {
int x = i + 1, y = GetRandom(1, i);
edges[x].push_back(y), edges[y].push_back(x);
E[i] = {x, y};
}
fi(i, 1, n) cl[i] = GetRandom(0, 1);
}
void Print()
{
cout << n << '\n';
fi(i, 1, n - 1) {
cout << E[i].first << " " << E[i].second << '\n';
}
fi(i, 1, n) cout << cl[i];
cout << '\n';
cout << "-----------------------\n";
}
void solve()
{
Input();
// gen();
cout << Sub1::solve();
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message (stderr)
power.cpp: In function 'int Sub1::solve()':
power.cpp:50:32: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
50 | if(getbit(x, i - 1)) a[i] = cl[i];
| ~~^~~
power.cpp:8:28: note: in definition of macro 'getbit'
8 | #define getbit(x,i) ((x >> i)&1)
| ^
power.cpp: In function 'void Sub2::dfs(int, int)':
power.cpp:12:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | #define fi(i, a, b) for(int i = a; i <= b; i++)
......
89 | fi(i, 0, P.size() - 1) {
| ~~~~~~~~~~~~~~~~~~
power.cpp:89:13: note: in expansion of macro 'fi'
89 | fi(i, 0, P.size() - 1) {
| ^~
power.cpp: In function 'int main()':
power.cpp:169:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
169 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:170:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
170 | freopen(Neco".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... |