Submission #51972

# Submission time Handle Problem Language Result Execution time Memory
51972 2018-06-22T19:28:37 Z mareksom Tug of War (BOI15_tug) C++17
100 / 100
1052 ms 24516 KB
#ifndef LOCAL
#pragma GCC optimize ("O3")
#endif

#include <bits/stdc++.h>

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (c it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(x...) " [" #x ": " << (x) << "] "

using ld = long double;
using ll = long long;

constexpr int mod = 1000 * 1000 * 1000 + 7;
constexpr int odw2 = (mod + 1) / 2;

void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; }
int Odejmij(int a, int b) { OdejmijOd(a, b); return a; }
void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; }
int Dodaj(int a, int b) { DodajDo(a, b); return a; }
int Mnoz(int a, int b) { return (ll) a * b % mod; }
void MnozDo(int& a, int b) { a = Mnoz(a, b); }
int Pot(int a, int b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; }
int Odw(int a) { return Pot(a, mod - 2); }
void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); }
int Podziel(int a, int b) { return Mnoz(a, Odw(b)); }
int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; }

template <typename T> T Maxi(T& a, T b) { return a = max(a, b); }
template <typename T> T Mini(T& a, T b) { return a = min(a, b); }

void Odp(bool ok) {
  if (ok) cout << "YES" << endl;
  else cout << "NO" << endl;
  exit(0);
}

int diff, k;
vector<int> roznice;
bitset<600 * 1000 + 105> ok;

void Podzial() {
  const int a = -k - diff;
  const int b = k - diff;
  debug() << imie(a) imie(b) imie(roznice);
  if (b < 0) Odp(false);
  if (a <= 0) Odp(true);
  assert(a <= b);
  if (b >= (int) ok.size()) Odp(false);
  for (int i = a; i <= b; i++) {
    ok[i] = 1;
  }
  for (int r : roznice) {
    ok |= (ok >> r);
    if (ok[0]) Odp(true);
  }
  Odp(false);
}

constexpr int nax = 60 * 1000 + 105;

int n;
vector<pair<int, int>> graf[nax];
bool odw[nax];
bool nacyk[nax];
bool nacyklu[nax];
vector<pair<int, int>> cykl;

bool Cykl(int w, int o) {
  if (nacyk[w]) {
    reverse(cykl.begin(), cykl.end());
    while (cykl.back().first != w) {
      cykl.pop_back();
    }
    reverse(cykl.begin(), cykl.end());
    return true;
  }
  if (odw[w]) return false;
  odw[w] = true;
  nacyk[w] = true;
  for (auto& it : graf[w]) {
    if (it.first == o) {
      o = -1;
      continue;
    }
    cykl.emplace_back(w, it.second);
    if (Cykl(it.first, w)) {
      return true;
    }
    cykl.pop_back();
  }
  nacyk[w] = false;
  return false;
}

void Dfs(int w, int o, int kraw) {
  if (1 <= w and w <= n) {
    diff += kraw;
  } else if (n + 1 <= w and w <= 2 * n) {
    diff -= kraw;
  } else {
    assert(false);
  }
  odw[w] = true;
  for (auto& it : graf[w]) {
    if (it.first != o) {
      Dfs(it.first, w, it.second);
    }
  }
}

void OdpalZ(int w) {
  debug() << "OdpalZ(" imie(w) ")";
  if (!Cykl(w, -1)) assert(false);
  debug() << imie(cykl);
  for (auto& nac : cykl) {
    nacyklu[nac.first] = true;
    odw[nac.first] = true;
  }
  for (auto& nac : cykl) {
    for (auto& it : graf[nac.first]) {
      if (!nacyklu[it.first]) {
        Dfs(it.first, nac.first, it.second);
      }
    }
  }
  assert((int) cykl.size() % 2 == 0);
  int roznica = 0;
  for (int i = 0; i < (int) cykl.size(); i++) {
    const int val = cykl[i].second;
    if (i % 2 == 0) roznica += val;
    else roznica -= val;
  }
  roznica = abs(roznica);
  diff -= roznica;
  roznica *= 2;
  roznice.push_back(roznica);
}

void Grafuj() {
  for (int i = 1; i <= n; i++) {
    if (!odw[i]) {
      OdpalZ(i);
    }
  }
}

int rozmiar[nax];
bool jest_cykl[nax];
int fut[nax];
int fuf(int w) { if (fut[w] == w) return w; return fut[w] = fuf(fut[w]); }
void fuu(int u, int v) {
  u = fuf(u);
  v = fuf(v);
  if (u == v) {
    if (jest_cykl[u]) {
      Odp(false);
    }
    jest_cykl[u] = true;
    return;
  }
  rozmiar[u] += rozmiar[v];
  if (jest_cykl[v] and jest_cykl[u]) {
    Odp(false);
  }
  if (jest_cykl[v]) jest_cykl[u] = true;
  fut[v] = u;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= 2 * n; i++) {
    fut[i] = i;
    rozmiar[i] = 1;
  }
  for (int i = 1; i <= 2 * n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    b += n;
    graf[a].emplace_back(b, c);
    graf[b].emplace_back(a, c);
    fuu(a, b);
  }
  Grafuj();
  Podzial();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2040 KB Output is correct
2 Correct 3 ms 2040 KB Output is correct
3 Correct 3 ms 2040 KB Output is correct
4 Correct 3 ms 2092 KB Output is correct
5 Correct 3 ms 2152 KB Output is correct
6 Correct 3 ms 2152 KB Output is correct
7 Correct 4 ms 2200 KB Output is correct
8 Correct 3 ms 2232 KB Output is correct
9 Correct 4 ms 2232 KB Output is correct
10 Correct 3 ms 2232 KB Output is correct
11 Correct 3 ms 2252 KB Output is correct
12 Correct 4 ms 2252 KB Output is correct
13 Correct 3 ms 2252 KB Output is correct
14 Correct 4 ms 2260 KB Output is correct
15 Correct 3 ms 2260 KB Output is correct
16 Correct 3 ms 2260 KB Output is correct
17 Correct 3 ms 2260 KB Output is correct
18 Correct 3 ms 2260 KB Output is correct
19 Correct 3 ms 2260 KB Output is correct
20 Correct 3 ms 2260 KB Output is correct
21 Correct 3 ms 2260 KB Output is correct
22 Correct 3 ms 2260 KB Output is correct
23 Correct 3 ms 2260 KB Output is correct
24 Correct 3 ms 2260 KB Output is correct
25 Correct 4 ms 2276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2040 KB Output is correct
2 Correct 3 ms 2040 KB Output is correct
3 Correct 3 ms 2040 KB Output is correct
4 Correct 3 ms 2092 KB Output is correct
5 Correct 3 ms 2152 KB Output is correct
6 Correct 3 ms 2152 KB Output is correct
7 Correct 4 ms 2200 KB Output is correct
8 Correct 3 ms 2232 KB Output is correct
9 Correct 4 ms 2232 KB Output is correct
10 Correct 3 ms 2232 KB Output is correct
11 Correct 3 ms 2252 KB Output is correct
12 Correct 4 ms 2252 KB Output is correct
13 Correct 3 ms 2252 KB Output is correct
14 Correct 4 ms 2260 KB Output is correct
15 Correct 3 ms 2260 KB Output is correct
16 Correct 3 ms 2260 KB Output is correct
17 Correct 3 ms 2260 KB Output is correct
18 Correct 3 ms 2260 KB Output is correct
19 Correct 3 ms 2260 KB Output is correct
20 Correct 3 ms 2260 KB Output is correct
21 Correct 3 ms 2260 KB Output is correct
22 Correct 3 ms 2260 KB Output is correct
23 Correct 3 ms 2260 KB Output is correct
24 Correct 3 ms 2260 KB Output is correct
25 Correct 4 ms 2276 KB Output is correct
26 Correct 36 ms 2404 KB Output is correct
27 Correct 12 ms 2404 KB Output is correct
28 Correct 37 ms 2404 KB Output is correct
29 Correct 13 ms 2404 KB Output is correct
30 Correct 64 ms 2412 KB Output is correct
31 Correct 12 ms 2428 KB Output is correct
32 Correct 36 ms 2428 KB Output is correct
33 Correct 12 ms 2428 KB Output is correct
34 Correct 8 ms 2428 KB Output is correct
35 Correct 12 ms 2432 KB Output is correct
36 Correct 42 ms 2432 KB Output is correct
37 Correct 17 ms 2432 KB Output is correct
38 Correct 64 ms 2432 KB Output is correct
39 Correct 12 ms 2432 KB Output is correct
40 Correct 36 ms 2432 KB Output is correct
41 Correct 12 ms 2432 KB Output is correct
42 Correct 67 ms 2432 KB Output is correct
43 Correct 12 ms 2432 KB Output is correct
44 Correct 34 ms 2432 KB Output is correct
45 Correct 12 ms 2500 KB Output is correct
46 Correct 39 ms 2500 KB Output is correct
47 Correct 5 ms 2500 KB Output is correct
48 Correct 38 ms 2524 KB Output is correct
49 Correct 36 ms 2524 KB Output is correct
50 Correct 40 ms 2524 KB Output is correct
51 Correct 66 ms 2524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2940 KB Output is correct
2 Correct 6 ms 2940 KB Output is correct
3 Correct 13 ms 2940 KB Output is correct
4 Correct 6 ms 2940 KB Output is correct
5 Correct 13 ms 2944 KB Output is correct
6 Correct 6 ms 2944 KB Output is correct
7 Correct 14 ms 2944 KB Output is correct
8 Correct 9 ms 2944 KB Output is correct
9 Correct 13 ms 2944 KB Output is correct
10 Correct 7 ms 2944 KB Output is correct
11 Correct 15 ms 2944 KB Output is correct
12 Correct 6 ms 2944 KB Output is correct
13 Correct 13 ms 2944 KB Output is correct
14 Correct 13 ms 2944 KB Output is correct
15 Correct 7 ms 2944 KB Output is correct
16 Correct 13 ms 2944 KB Output is correct
17 Correct 10 ms 2944 KB Output is correct
18 Correct 13 ms 3036 KB Output is correct
19 Correct 8 ms 3036 KB Output is correct
20 Correct 13 ms 3036 KB Output is correct
21 Correct 8 ms 3036 KB Output is correct
22 Correct 14 ms 3068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2040 KB Output is correct
2 Correct 3 ms 2040 KB Output is correct
3 Correct 3 ms 2040 KB Output is correct
4 Correct 3 ms 2092 KB Output is correct
5 Correct 3 ms 2152 KB Output is correct
6 Correct 3 ms 2152 KB Output is correct
7 Correct 4 ms 2200 KB Output is correct
8 Correct 3 ms 2232 KB Output is correct
9 Correct 4 ms 2232 KB Output is correct
10 Correct 3 ms 2232 KB Output is correct
11 Correct 3 ms 2252 KB Output is correct
12 Correct 4 ms 2252 KB Output is correct
13 Correct 3 ms 2252 KB Output is correct
14 Correct 4 ms 2260 KB Output is correct
15 Correct 3 ms 2260 KB Output is correct
16 Correct 3 ms 2260 KB Output is correct
17 Correct 3 ms 2260 KB Output is correct
18 Correct 3 ms 2260 KB Output is correct
19 Correct 3 ms 2260 KB Output is correct
20 Correct 3 ms 2260 KB Output is correct
21 Correct 3 ms 2260 KB Output is correct
22 Correct 3 ms 2260 KB Output is correct
23 Correct 3 ms 2260 KB Output is correct
24 Correct 3 ms 2260 KB Output is correct
25 Correct 4 ms 2276 KB Output is correct
26 Correct 36 ms 2404 KB Output is correct
27 Correct 12 ms 2404 KB Output is correct
28 Correct 37 ms 2404 KB Output is correct
29 Correct 13 ms 2404 KB Output is correct
30 Correct 64 ms 2412 KB Output is correct
31 Correct 12 ms 2428 KB Output is correct
32 Correct 36 ms 2428 KB Output is correct
33 Correct 12 ms 2428 KB Output is correct
34 Correct 8 ms 2428 KB Output is correct
35 Correct 12 ms 2432 KB Output is correct
36 Correct 42 ms 2432 KB Output is correct
37 Correct 17 ms 2432 KB Output is correct
38 Correct 64 ms 2432 KB Output is correct
39 Correct 12 ms 2432 KB Output is correct
40 Correct 36 ms 2432 KB Output is correct
41 Correct 12 ms 2432 KB Output is correct
42 Correct 67 ms 2432 KB Output is correct
43 Correct 12 ms 2432 KB Output is correct
44 Correct 34 ms 2432 KB Output is correct
45 Correct 12 ms 2500 KB Output is correct
46 Correct 39 ms 2500 KB Output is correct
47 Correct 5 ms 2500 KB Output is correct
48 Correct 38 ms 2524 KB Output is correct
49 Correct 36 ms 2524 KB Output is correct
50 Correct 40 ms 2524 KB Output is correct
51 Correct 66 ms 2524 KB Output is correct
52 Correct 13 ms 2940 KB Output is correct
53 Correct 6 ms 2940 KB Output is correct
54 Correct 13 ms 2940 KB Output is correct
55 Correct 6 ms 2940 KB Output is correct
56 Correct 13 ms 2944 KB Output is correct
57 Correct 6 ms 2944 KB Output is correct
58 Correct 14 ms 2944 KB Output is correct
59 Correct 9 ms 2944 KB Output is correct
60 Correct 13 ms 2944 KB Output is correct
61 Correct 7 ms 2944 KB Output is correct
62 Correct 15 ms 2944 KB Output is correct
63 Correct 6 ms 2944 KB Output is correct
64 Correct 13 ms 2944 KB Output is correct
65 Correct 13 ms 2944 KB Output is correct
66 Correct 7 ms 2944 KB Output is correct
67 Correct 13 ms 2944 KB Output is correct
68 Correct 10 ms 2944 KB Output is correct
69 Correct 13 ms 3036 KB Output is correct
70 Correct 8 ms 3036 KB Output is correct
71 Correct 13 ms 3036 KB Output is correct
72 Correct 8 ms 3036 KB Output is correct
73 Correct 14 ms 3068 KB Output is correct
74 Correct 1002 ms 5072 KB Output is correct
75 Correct 66 ms 5692 KB Output is correct
76 Correct 930 ms 6884 KB Output is correct
77 Correct 69 ms 7328 KB Output is correct
78 Correct 493 ms 8432 KB Output is correct
79 Correct 516 ms 9236 KB Output is correct
80 Correct 101 ms 9792 KB Output is correct
81 Correct 61 ms 10600 KB Output is correct
82 Correct 540 ms 11788 KB Output is correct
83 Correct 1052 ms 12612 KB Output is correct
84 Correct 69 ms 13040 KB Output is correct
85 Correct 936 ms 14140 KB Output is correct
86 Correct 75 ms 14692 KB Output is correct
87 Correct 519 ms 15772 KB Output is correct
88 Correct 102 ms 16332 KB Output is correct
89 Correct 604 ms 17500 KB Output is correct
90 Correct 68 ms 17960 KB Output is correct
91 Correct 928 ms 19028 KB Output is correct
92 Correct 71 ms 19584 KB Output is correct
93 Correct 531 ms 20748 KB Output is correct
94 Correct 20 ms 20748 KB Output is correct
95 Correct 587 ms 22212 KB Output is correct
96 Correct 514 ms 23144 KB Output is correct
97 Correct 515 ms 23792 KB Output is correct
98 Correct 960 ms 24516 KB Output is correct