Submission #294067

#TimeUsernameProblemLanguageResultExecution timeMemory
294067BTheroPower Plant (JOI20_power)C++17
6 / 100
1593 ms16120 KiB
// 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)

power.cpp: In function 'void solve()':
power.cpp:39:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   39 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
power.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   43 |     scanf("%d %d", &u, &v);
      |     ~~~~~^~~~~~~~~~~~~~~~~
power.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   50 |     scanf(" %c", &c);
      |     ~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...