제출 #285142

#제출 시각아이디문제언어결과실행 시간메모리
285142AlexLuchianovTug of War (BOI15_tug)C++14
71 / 100
3065 ms3064 KiB
#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
#include <bitset>

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 30000;
int const sigma = 20;
int strength[1 + 2 * nmax];
int l[1 + 2 * nmax], r[1 + 2 * nmax];
int count[1 + nmax * 2], weight[1 + 2 * nmax], seen[1 + 2 * nmax];
int dif = 0, n;

void dfs(int node) {
  if(count[node] != 1)
    return ;
  int id = weight[node];
  if(node <= n)
    dif += strength[id];
  else
    dif -= strength[id];
  seen[id] = 1;
  count[l[id]]--;
  count[r[id]]--;
  weight[l[id]] -= id;
  weight[r[id]] -= id;
  dfs(l[id]);
  dfs(r[id]);
}

int oth(int id, int p) {
  if(p == 1)
    return weight[r[id]] - id;
  else
    return weight[l[id]] - id;
}

int explore(int id){
  if(seen[id] == 1)
    return 0;
  int result = strength[id];
  int node = oth(id, 1);
  int last = 1, coef = -1;
  seen[id] = 1;
  while(node != id) {
    result += strength[node] * coef;
    coef *= -1;
    last ^= 1;
    seen[node] = 1;
    node = oth(node, last);
  }
  return std::max(result, -result);
}

std::bitset<5 + nmax * sigma * 2> sol;

int main() {
  int k;
  std::cin >> n >> k;
  for(int i = 1;i <= 2 * n; i++) {
    std::cin >> l[i] >> r[i] >> strength[i];
    r[i] += n;
   count[l[i]]++;
   count[r[i]]++;
   weight[l[i]] += i;
   weight[r[i]] += i;
  }
  
  for(int i = 1;i <= 2 * n; i++)  
    dfs(i);
  for(int i = 1; i <= 2 * n; i++)
    if(2 < count[i]) {
      std::cout << "NO";
      return 0;
    }
  
  sol[nmax * sigma + dif] = 1;

  for(int i = 1;i <= 2 * n; i++) {
    if(seen[i] == 0) {
      int val = explore(i);
      sol = (sol << val) | (sol >> val);
    }
  }
  for(int i = -k; i <= k; i++)
    if(sol[nmax * sigma + i] == 1) {
      std::cout << "YES";
      return 0;
    }
  std::cout << "NO";
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...