#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) > 0;
if(a[u]) ans = max(ans, 1);
return ans;
}
int solve()
{
int ans = 1;
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 > 0) 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()
{
res = 1;
fi(i, 1, n) dp[i] = 0;
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, 16);
fi(i, 1, n) edges[i].clear();
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();
cout << Sub2::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
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:172:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
172 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:173:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
173 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8536 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8636 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8536 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8636 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
2 ms |
8540 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
3 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
8640 KB |
Output is correct |
28 |
Correct |
2 ms |
8536 KB |
Output is correct |
29 |
Correct |
2 ms |
8796 KB |
Output is correct |
30 |
Correct |
2 ms |
8796 KB |
Output is correct |
31 |
Correct |
2 ms |
8536 KB |
Output is correct |
32 |
Correct |
2 ms |
8540 KB |
Output is correct |
33 |
Correct |
3 ms |
8628 KB |
Output is correct |
34 |
Correct |
2 ms |
8692 KB |
Output is correct |
35 |
Correct |
2 ms |
8540 KB |
Output is correct |
36 |
Correct |
2 ms |
8540 KB |
Output is correct |
37 |
Correct |
2 ms |
8540 KB |
Output is correct |
38 |
Correct |
2 ms |
8540 KB |
Output is correct |
39 |
Correct |
2 ms |
8796 KB |
Output is correct |
40 |
Correct |
2 ms |
8540 KB |
Output is correct |
41 |
Correct |
2 ms |
8796 KB |
Output is correct |
42 |
Correct |
2 ms |
8540 KB |
Output is correct |
43 |
Correct |
2 ms |
8792 KB |
Output is correct |
44 |
Correct |
2 ms |
8540 KB |
Output is correct |
45 |
Correct |
2 ms |
8540 KB |
Output is correct |
46 |
Correct |
3 ms |
8540 KB |
Output is correct |
47 |
Correct |
2 ms |
8540 KB |
Output is correct |
48 |
Correct |
2 ms |
8796 KB |
Output is correct |
49 |
Correct |
2 ms |
8796 KB |
Output is correct |
50 |
Correct |
2 ms |
8748 KB |
Output is correct |
51 |
Correct |
2 ms |
8540 KB |
Output is correct |
52 |
Correct |
2 ms |
8540 KB |
Output is correct |
53 |
Correct |
2 ms |
8540 KB |
Output is correct |
54 |
Correct |
2 ms |
8536 KB |
Output is correct |
55 |
Correct |
2 ms |
8540 KB |
Output is correct |
56 |
Correct |
2 ms |
8540 KB |
Output is correct |
57 |
Correct |
2 ms |
8796 KB |
Output is correct |
58 |
Correct |
2 ms |
8540 KB |
Output is correct |
59 |
Correct |
2 ms |
8540 KB |
Output is correct |
60 |
Correct |
3 ms |
9044 KB |
Output is correct |
61 |
Correct |
2 ms |
8540 KB |
Output is correct |
62 |
Correct |
2 ms |
8796 KB |
Output is correct |
63 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8536 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8636 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
2 ms |
8540 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
3 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
8640 KB |
Output is correct |
28 |
Correct |
2 ms |
8536 KB |
Output is correct |
29 |
Correct |
2 ms |
8796 KB |
Output is correct |
30 |
Correct |
2 ms |
8796 KB |
Output is correct |
31 |
Correct |
2 ms |
8536 KB |
Output is correct |
32 |
Correct |
2 ms |
8540 KB |
Output is correct |
33 |
Correct |
3 ms |
8628 KB |
Output is correct |
34 |
Correct |
2 ms |
8692 KB |
Output is correct |
35 |
Correct |
2 ms |
8540 KB |
Output is correct |
36 |
Correct |
2 ms |
8540 KB |
Output is correct |
37 |
Correct |
2 ms |
8540 KB |
Output is correct |
38 |
Correct |
2 ms |
8540 KB |
Output is correct |
39 |
Correct |
2 ms |
8796 KB |
Output is correct |
40 |
Correct |
2 ms |
8540 KB |
Output is correct |
41 |
Correct |
2 ms |
8796 KB |
Output is correct |
42 |
Correct |
2 ms |
8540 KB |
Output is correct |
43 |
Correct |
2 ms |
8792 KB |
Output is correct |
44 |
Correct |
2 ms |
8540 KB |
Output is correct |
45 |
Correct |
2 ms |
8540 KB |
Output is correct |
46 |
Correct |
3 ms |
8540 KB |
Output is correct |
47 |
Correct |
2 ms |
8540 KB |
Output is correct |
48 |
Correct |
2 ms |
8796 KB |
Output is correct |
49 |
Correct |
2 ms |
8796 KB |
Output is correct |
50 |
Correct |
2 ms |
8748 KB |
Output is correct |
51 |
Correct |
2 ms |
8540 KB |
Output is correct |
52 |
Correct |
2 ms |
8540 KB |
Output is correct |
53 |
Correct |
2 ms |
8540 KB |
Output is correct |
54 |
Correct |
2 ms |
8536 KB |
Output is correct |
55 |
Correct |
2 ms |
8540 KB |
Output is correct |
56 |
Correct |
2 ms |
8540 KB |
Output is correct |
57 |
Correct |
2 ms |
8796 KB |
Output is correct |
58 |
Correct |
2 ms |
8540 KB |
Output is correct |
59 |
Correct |
2 ms |
8540 KB |
Output is correct |
60 |
Correct |
3 ms |
9044 KB |
Output is correct |
61 |
Correct |
2 ms |
8540 KB |
Output is correct |
62 |
Correct |
2 ms |
8796 KB |
Output is correct |
63 |
Correct |
2 ms |
8540 KB |
Output is correct |
64 |
Correct |
71 ms |
15264 KB |
Output is correct |
65 |
Correct |
81 ms |
15184 KB |
Output is correct |
66 |
Correct |
83 ms |
15248 KB |
Output is correct |
67 |
Correct |
77 ms |
15204 KB |
Output is correct |
68 |
Correct |
72 ms |
15236 KB |
Output is correct |
69 |
Correct |
67 ms |
15248 KB |
Output is correct |
70 |
Correct |
73 ms |
15092 KB |
Output is correct |
71 |
Correct |
85 ms |
15016 KB |
Output is correct |
72 |
Correct |
87 ms |
15084 KB |
Output is correct |
73 |
Correct |
94 ms |
30272 KB |
Output is correct |
74 |
Correct |
92 ms |
30340 KB |
Output is correct |
75 |
Correct |
72 ms |
15096 KB |
Output is correct |
76 |
Correct |
67 ms |
15004 KB |
Output is correct |
77 |
Correct |
75 ms |
15140 KB |
Output is correct |
78 |
Correct |
75 ms |
15144 KB |
Output is correct |
79 |
Correct |
70 ms |
14984 KB |
Output is correct |
80 |
Correct |
74 ms |
15104 KB |
Output is correct |
81 |
Correct |
74 ms |
16268 KB |
Output is correct |
82 |
Correct |
69 ms |
15696 KB |
Output is correct |
83 |
Correct |
86 ms |
21668 KB |
Output is correct |
84 |
Correct |
89 ms |
24660 KB |
Output is correct |
85 |
Correct |
154 ms |
24144 KB |
Output is correct |
86 |
Correct |
78 ms |
17112 KB |
Output is correct |
87 |
Correct |
77 ms |
16140 KB |
Output is correct |
88 |
Correct |
74 ms |
16612 KB |
Output is correct |
89 |
Correct |
69 ms |
16580 KB |
Output is correct |
90 |
Correct |
72 ms |
16208 KB |
Output is correct |
91 |
Correct |
71 ms |
16468 KB |
Output is correct |
92 |
Correct |
78 ms |
17068 KB |
Output is correct |
93 |
Correct |
73 ms |
20384 KB |
Output is correct |
94 |
Correct |
69 ms |
22140 KB |
Output is correct |
95 |
Correct |
77 ms |
20712 KB |
Output is correct |
96 |
Correct |
70 ms |
18408 KB |
Output is correct |
97 |
Correct |
64 ms |
16464 KB |
Output is correct |
98 |
Correct |
70 ms |
15696 KB |
Output is correct |
99 |
Correct |
90 ms |
15600 KB |
Output is correct |
100 |
Correct |
73 ms |
19540 KB |
Output is correct |
101 |
Correct |
75 ms |
16980 KB |
Output is correct |
102 |
Correct |
63 ms |
15964 KB |
Output is correct |
103 |
Correct |
99 ms |
15844 KB |
Output is correct |
104 |
Correct |
51 ms |
17328 KB |
Output is correct |
105 |
Correct |
98 ms |
26584 KB |
Output is correct |
106 |
Correct |
48 ms |
17388 KB |
Output is correct |
107 |
Correct |
45 ms |
17348 KB |
Output is correct |
108 |
Correct |
94 ms |
31088 KB |
Output is correct |
109 |
Correct |
62 ms |
17360 KB |
Output is correct |
110 |
Correct |
91 ms |
31568 KB |
Output is correct |
111 |
Correct |
89 ms |
15120 KB |
Output is correct |