Submission #285145

# Submission time Handle Problem Language Result Execution time Memory
285145 2020-08-28T11:13:39 Z AlexLuchianov Tug of War (BOI15_tug) C++14
100 / 100
1928 ms 3192 KB
#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;
  
  int base = 0;

  for(int i = 1;i <= 2 * n; i++) {
    if(seen[i] == 0) {
      int val = explore(i);
      base -= val;
      sol |= (sol << (2 * val));
    }
  }

  for(int i = -k; i <= k; i++)
    if(sol[nmax * sigma + i - base] == 1) {
      std::cout << "YES";
      return 0;
    }
  std::cout << "NO";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Correct 1 ms 768 KB Output is correct
4 Correct 2 ms 768 KB Output is correct
5 Correct 1 ms 768 KB Output is correct
6 Correct 1 ms 768 KB Output is correct
7 Correct 1 ms 768 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 2 ms 768 KB Output is correct
10 Correct 1 ms 768 KB Output is correct
11 Correct 1 ms 768 KB Output is correct
12 Correct 1 ms 768 KB Output is correct
13 Correct 1 ms 768 KB Output is correct
14 Correct 1 ms 768 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 768 KB Output is correct
17 Correct 1 ms 768 KB Output is correct
18 Correct 2 ms 896 KB Output is correct
19 Correct 1 ms 768 KB Output is correct
20 Correct 1 ms 768 KB Output is correct
21 Correct 0 ms 384 KB Output is correct
22 Correct 1 ms 768 KB Output is correct
23 Correct 1 ms 768 KB Output is correct
24 Correct 1 ms 768 KB Output is correct
25 Correct 1 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Correct 1 ms 768 KB Output is correct
4 Correct 2 ms 768 KB Output is correct
5 Correct 1 ms 768 KB Output is correct
6 Correct 1 ms 768 KB Output is correct
7 Correct 1 ms 768 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 2 ms 768 KB Output is correct
10 Correct 1 ms 768 KB Output is correct
11 Correct 1 ms 768 KB Output is correct
12 Correct 1 ms 768 KB Output is correct
13 Correct 1 ms 768 KB Output is correct
14 Correct 1 ms 768 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 768 KB Output is correct
17 Correct 1 ms 768 KB Output is correct
18 Correct 2 ms 896 KB Output is correct
19 Correct 1 ms 768 KB Output is correct
20 Correct 1 ms 768 KB Output is correct
21 Correct 0 ms 384 KB Output is correct
22 Correct 1 ms 768 KB Output is correct
23 Correct 1 ms 768 KB Output is correct
24 Correct 1 ms 768 KB Output is correct
25 Correct 1 ms 768 KB Output is correct
26 Correct 125 ms 916 KB Output is correct
27 Correct 23 ms 896 KB Output is correct
28 Correct 124 ms 916 KB Output is correct
29 Correct 22 ms 896 KB Output is correct
30 Correct 123 ms 896 KB Output is correct
31 Correct 22 ms 896 KB Output is correct
32 Correct 123 ms 896 KB Output is correct
33 Correct 22 ms 896 KB Output is correct
34 Correct 14 ms 896 KB Output is correct
35 Correct 21 ms 896 KB Output is correct
36 Correct 137 ms 1016 KB Output is correct
37 Correct 22 ms 896 KB Output is correct
38 Correct 124 ms 896 KB Output is correct
39 Correct 23 ms 896 KB Output is correct
40 Correct 125 ms 896 KB Output is correct
41 Correct 22 ms 896 KB Output is correct
42 Correct 124 ms 896 KB Output is correct
43 Correct 26 ms 896 KB Output is correct
44 Correct 125 ms 896 KB Output is correct
45 Correct 25 ms 896 KB Output is correct
46 Correct 128 ms 896 KB Output is correct
47 Correct 6 ms 384 KB Output is correct
48 Correct 68 ms 896 KB Output is correct
49 Correct 68 ms 896 KB Output is correct
50 Correct 126 ms 1016 KB Output is correct
51 Correct 127 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 441 ms 1272 KB Output is correct
2 Correct 25 ms 760 KB Output is correct
3 Correct 454 ms 1280 KB Output is correct
4 Correct 27 ms 760 KB Output is correct
5 Correct 428 ms 1280 KB Output is correct
6 Correct 25 ms 760 KB Output is correct
7 Correct 431 ms 1284 KB Output is correct
8 Correct 25 ms 760 KB Output is correct
9 Correct 424 ms 1400 KB Output is correct
10 Correct 25 ms 760 KB Output is correct
11 Correct 433 ms 1288 KB Output is correct
12 Correct 26 ms 760 KB Output is correct
13 Correct 449 ms 1284 KB Output is correct
14 Correct 431 ms 1400 KB Output is correct
15 Correct 26 ms 768 KB Output is correct
16 Correct 444 ms 1284 KB Output is correct
17 Correct 26 ms 760 KB Output is correct
18 Correct 437 ms 1280 KB Output is correct
19 Correct 29 ms 760 KB Output is correct
20 Correct 442 ms 1376 KB Output is correct
21 Correct 26 ms 760 KB Output is correct
22 Correct 261 ms 1272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Correct 1 ms 768 KB Output is correct
4 Correct 2 ms 768 KB Output is correct
5 Correct 1 ms 768 KB Output is correct
6 Correct 1 ms 768 KB Output is correct
7 Correct 1 ms 768 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 2 ms 768 KB Output is correct
10 Correct 1 ms 768 KB Output is correct
11 Correct 1 ms 768 KB Output is correct
12 Correct 1 ms 768 KB Output is correct
13 Correct 1 ms 768 KB Output is correct
14 Correct 1 ms 768 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 768 KB Output is correct
17 Correct 1 ms 768 KB Output is correct
18 Correct 2 ms 896 KB Output is correct
19 Correct 1 ms 768 KB Output is correct
20 Correct 1 ms 768 KB Output is correct
21 Correct 0 ms 384 KB Output is correct
22 Correct 1 ms 768 KB Output is correct
23 Correct 1 ms 768 KB Output is correct
24 Correct 1 ms 768 KB Output is correct
25 Correct 1 ms 768 KB Output is correct
26 Correct 125 ms 916 KB Output is correct
27 Correct 23 ms 896 KB Output is correct
28 Correct 124 ms 916 KB Output is correct
29 Correct 22 ms 896 KB Output is correct
30 Correct 123 ms 896 KB Output is correct
31 Correct 22 ms 896 KB Output is correct
32 Correct 123 ms 896 KB Output is correct
33 Correct 22 ms 896 KB Output is correct
34 Correct 14 ms 896 KB Output is correct
35 Correct 21 ms 896 KB Output is correct
36 Correct 137 ms 1016 KB Output is correct
37 Correct 22 ms 896 KB Output is correct
38 Correct 124 ms 896 KB Output is correct
39 Correct 23 ms 896 KB Output is correct
40 Correct 125 ms 896 KB Output is correct
41 Correct 22 ms 896 KB Output is correct
42 Correct 124 ms 896 KB Output is correct
43 Correct 26 ms 896 KB Output is correct
44 Correct 125 ms 896 KB Output is correct
45 Correct 25 ms 896 KB Output is correct
46 Correct 128 ms 896 KB Output is correct
47 Correct 6 ms 384 KB Output is correct
48 Correct 68 ms 896 KB Output is correct
49 Correct 68 ms 896 KB Output is correct
50 Correct 126 ms 1016 KB Output is correct
51 Correct 127 ms 1016 KB Output is correct
52 Correct 441 ms 1272 KB Output is correct
53 Correct 25 ms 760 KB Output is correct
54 Correct 454 ms 1280 KB Output is correct
55 Correct 27 ms 760 KB Output is correct
56 Correct 428 ms 1280 KB Output is correct
57 Correct 25 ms 760 KB Output is correct
58 Correct 431 ms 1284 KB Output is correct
59 Correct 25 ms 760 KB Output is correct
60 Correct 424 ms 1400 KB Output is correct
61 Correct 25 ms 760 KB Output is correct
62 Correct 433 ms 1288 KB Output is correct
63 Correct 26 ms 760 KB Output is correct
64 Correct 449 ms 1284 KB Output is correct
65 Correct 431 ms 1400 KB Output is correct
66 Correct 26 ms 768 KB Output is correct
67 Correct 444 ms 1284 KB Output is correct
68 Correct 26 ms 760 KB Output is correct
69 Correct 437 ms 1280 KB Output is correct
70 Correct 29 ms 760 KB Output is correct
71 Correct 442 ms 1376 KB Output is correct
72 Correct 26 ms 760 KB Output is correct
73 Correct 261 ms 1272 KB Output is correct
74 Correct 1893 ms 2296 KB Output is correct
75 Correct 170 ms 2948 KB Output is correct
76 Correct 1919 ms 3192 KB Output is correct
77 Correct 166 ms 3064 KB Output is correct
78 Correct 1901 ms 3068 KB Output is correct
79 Correct 1869 ms 3120 KB Output is correct
80 Correct 154 ms 3064 KB Output is correct
81 Correct 153 ms 3028 KB Output is correct
82 Correct 1913 ms 3156 KB Output is correct
83 Correct 1867 ms 3068 KB Output is correct
84 Correct 152 ms 3064 KB Output is correct
85 Correct 1857 ms 3064 KB Output is correct
86 Correct 151 ms 2936 KB Output is correct
87 Correct 1869 ms 3120 KB Output is correct
88 Correct 151 ms 3044 KB Output is correct
89 Correct 1871 ms 3036 KB Output is correct
90 Correct 166 ms 3000 KB Output is correct
91 Correct 1928 ms 3064 KB Output is correct
92 Correct 160 ms 2940 KB Output is correct
93 Correct 1911 ms 3028 KB Output is correct
94 Correct 91 ms 2552 KB Output is correct
95 Correct 1033 ms 2936 KB Output is correct
96 Correct 1028 ms 3036 KB Output is correct
97 Correct 1906 ms 3032 KB Output is correct
98 Correct 1906 ms 3028 KB Output is correct