# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
294067 | BThero | Power Plant (JOI20_power) | C++17 | 1593 ms | 16120 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " = " << x << endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = (int)2e3 + 5;
int dist[MAXN][MAXN];
vector<int> adj[MAXN];
int state[MAXN], broken[MAXN];
int can[MAXN];
int n;
bool bit(int x, int p) {
return (x >> p) & 1;
}
void dfs(int d[], int v, int pr = -1) {
for (int to : adj[v]) {
if (to != pr) {
d[to] = d[v] + 1;
dfs(d, to, v);
}
}
}
void solve() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d %d", &u, &v);
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 1; i <= n; ++i) {
char c;
scanf(" %c", &c);
can[i] = c - '0';
}
for (int i = 1; i <= n; ++i) {
dfs(dist[i], i);
}
int ans = 0;
for (int mask = 0; mask < (1 << n); ++mask) {
bool ok = 1;
for (int i = 1; i <= n; ++i) {
state[i] = bit(mask, i - 1);
broken[i] = 0;
if (state[i] > can[i]) {
ok = 0;
}
}
if (!ok) {
continue;
}
for (int x = 1; x <= n; ++x) {
if (!state[x]) {
continue;
}
for (int y = 1; y <= n; ++y) {
if (!can[y] || x == y) {
continue;
}
for (int z = 1; z <= n; ++z) {
if (!state[z]) {
continue;
}
if (y != z && x != z && dist[x][y] + dist[y][z] == dist[x][z]) {
broken[y] = 1;
}
}
}
}
int cur = 0;
for (int i = 1; i <= n; ++i) {
if (state[i] == 1 && !broken[i]) {
++cur;
}
cur -= broken[i];
}
ans = max(ans, cur);
}
printf("%d\n", ans);
}
int main() {
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |