Submission #221640

# Submission time Handle Problem Language Result Execution time Memory
221640 2020-04-10T17:04:31 Z Haunted_Cpp Džumbus (COCI19_dzumbus) C++17
0 / 110
1000 ms 18216 KB
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <cassert>
#include <string>
#include <cstring>
#include <bitset>
#include <random>
#include <chrono>
#include <iomanip>
 
#pragma GCC optimize ("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
 
#define FOR(i, a, b) for(int i = a; i < (int) b; i++)
#define F0R(i, a) FOR (i, 0, a)
#define ROF(i, a, b) for(int i = a; i >= (int) b; i--)
#define R0F(i, a) ROF(i, a, 0)
#define GO(i, a) for (auto i : a)
 
#define rsz resize
#define eb emplace_back
#define pb push_back
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define f first
#define s second

#define TRACE(x) cerr << #x << " " << x << '\n'
 
using namespace std;
 
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vpii> vvpii;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef vector<pi64> vpi64;
 
const int dr[] = {+1, -1, +0, +0, +1, -1, +1, -1};
const int dc[] = {+0, +0, +1, -1, +1, -1, -1, +1};
const int ms[] = {+31, +29, +31, 30, +31, +30, +31, +31, +30, +31, +30, +31};

const int N = 1e3 + 5;
const int S = 1e3 + 5;
const int B = 2;
const int DUMMY = 0;

i64 dp [N][S][B];
i64 best_way [S][B];
i64 cost [N];

vvi g (N);

void dfs (int node = DUMMY, int p = -1) {
  GO (to, g[node]) {
    if (to != p) {
      dfs (to, node);
    }
  }
  F0R (i, S) F0R (j, B) best_way[i][j] = 1e18;
  dp[node][0][0] = best_way[0][0] = 0;
  dp[node][0][1] = best_way[0][1] = cost[node];
  // Do not include the current node
  GO (to, g[node]) {  
    if (to == p) continue;
    F0R (current_score, S) {
      FOR (bridge_score, 2, current_score + 1) {
        const int L = current_score - bridge_score;
        const int R = bridge_score;      
        best_way[L + R][0] = min (best_way[L + R][0], dp[to][R][0] + dp[node][L][0]);
        best_way[L + R][0] = min (best_way[L + R][0], dp[to][R][1] + dp[node][L][0]);   
      }
    }
    F0R (i, S) {
      assert (best_way[i][0] <= dp[node][i][0]);
      dp[node][i][0] = best_way[i][0];
    }
  }
  // Include the current node
  GO (to, g[node]) { 
    if (to == p) continue;
    best_way[2][1] = min (best_way[2][1], cost[node] + cost[to]);
    FOR (current_score, 3, S) {
      F0R (bridge_score, current_score + 1) {
        const int L = current_score - bridge_score;
        const int R = bridge_score;    
        if (R == 0) {
          best_way[L + R][1] = min (best_way[L + R][1], dp[node][L + R - 1][1] + cost[to]);
          continue;
        }
        if (L == 0) {
          best_way[L + R][1] = min (best_way[L + R][1], dp[to][L + R - 1][1] + cost[node]);
          continue;
        }
        best_way[L + R][1] = min (best_way[L + R][1], dp[to][R][0] + dp[node][L][1]);
        best_way[L + R][1] = min (best_way[L + R][1], dp[to][R][1] + dp[node][L][1]);  
      }
    }
    F0R (i, S) {
      assert (best_way[i][1] <= dp[node][i][1]);
      dp[node][i][1] = best_way[i][1];
    }  
  }
}



// Make sure the given graph is a tree and not a forest
bool vis [N];
void connect (int node) {
  vis[node] = true;
  GO (to, g[node]) if (!vis[to]) {
    vis[to] = true;
    connect (to);
  }
}
 
int main () {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  cost[0] = 1e18;
  F0R (i, n) cin >> cost[i + 1];
  F0R (i, m) {
    int st, et;
    cin >> st >> et;
    g[st].eb(et);
    g[et].eb(st);
  }
  FOR (i, 1, n + 1) {
    if (!vis[i]) {
      g[DUMMY].eb(i);
      connect (i);
    }
  }
  /*
   DP[i][j][x]
   Lowest cost of being in the ith node with j nodes in a component so far. x is set to true if i belongs to the component or not
  */
  F0R (i, N) {
    F0R (j, N) {
      dp[i][j][0] = dp[i][j][1] = 1e18;
    }
  }
  dfs ();
  /*
  F0R (i, n + 1) {
    cout << "NODE: " << i << '\n';
    F0R (j, n + 1) {
      cout << dp[i][j][0] << ' ' << dp[i][j][1] << '\n';
    }
    cout << '\n';
  }
  */
  int q;
  cin >> q;
  while (q--) {
    i64 budget;
    cin >> budget;
    int mx = 0;
    F0R (i, S) {
      if (dp[0][i][0] <= budget || dp[0][i][1] <= budget) mx = max (mx, i);
    }
    cout << mx << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 1090 ms 16384 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1090 ms 16384 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 397 ms 18216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1090 ms 16384 KB Time limit exceeded
2 Halted 0 ms 0 KB -