Submission #539088

#TimeUsernameProblemLanguageResultExecution timeMemory
539088huB1erTi2Tug of War (BOI15_tug)C++17
71 / 100
3069 ms4436 KiB
// _| _| // _| _| _| _| _|_| _|_|_| _|_|_| _| _| _|_| _|_| // _|_| _| _| _|_|_|_| _| _| _|_| _|_| _|_|_|_| _|_|_|_| // _| _| _| _| _| _| _| _|_| _| _| _| _| // _| _| _|_|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_| // _| _| // _|_| _| #include <iostream> #include <cassert> #include <vector> #include <algorithm> using namespace std; // #define DEBUG #ifdef DEBUG #define dassert(x) assert(x); #define df(...) printf(__VA_ARGS__) #else #define dassert(x) #define df(...) #endif #define x first #define y second #define mp make_pair #define pb push_back #define ir(x, a, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define sz(x) (ll)x.size() #define foru(i, n) for (int i = 0; (i) < (n); ++(i)) #define all(x) (x).begin(), (x).end() typedef int64_t ll; int read() { int n = 0; bool b = 0; char c = getchar(); for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-'); for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0'); if (b) return -n; return n; } vec<vec<pair<int, int>>> g; vec<int> ct; vec<char> used; // edges, vertices pair<int, int> count(int v) { used[v] = 1; int se = ct[v], sv = 1; for (auto [c, _] : g[v]) { if (used[c]) continue; auto [ce, cv] = count(c); se += ce, sv += cv; } return {se, sv}; } int cycle(int v, int p) { // df("visiting %d\n", v); used[v] = 1; int ret = -1; for (auto [c, _] : g[v]) { if (c == p) continue; if (used[c]) { ret = v; continue; } int res = cycle(c, v); if (res != -1) ret = res; } return ret; } pair<int, int> dfs(int v, int p, int val, int costdown) { used[v] = 1; int sum = 0, cycle = 1e9; bool covered = 0; // df("costdown: %d\n", costdown); for (auto [c, cost] : g[v]) { if (c == p && cost == -costdown && !covered) { covered = 1; continue; } if (used[c]) { if (v == p) continue; // cout << "set cycle " << v << " " << c << " " << cost << endl; cycle = val + cost; sum += cost; } else { auto [ss, cyc] = dfs(c, v, val + cost, cost); if (cyc != 1e9) cycle = cyc; sum += ss + cost; } } return {sum, cycle}; } void fail() { cout << "NO\n"; exit(0); } void succeed() { cout << "YES\n"; exit(0); } int main() { df("debug mode\n"); #ifndef DEBUG ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif int n, k; cin >> n >> k; g.resize(2*n); ct.resize(2*n); used.resize(2*n); foru (i, 2*n) { int v, u, s; cin >> v >> u >> s; u += n; --v, --u; df("%d--%d\n", v, u); g[v].pb({u, s}); g[u].pb({v, -s}); ++ct[u], ++ct[v]; } foru (v, 2*n) { if (used[v]) continue; auto [ce, cv] = count(v); ce /= 2; // in and out counted twice if (ce != cv) fail(); } used.assign(2*n, 0); vec<int> roots; foru (v, 2*n) { if (used[v]) continue; roots.pb(cycle(v, v)); df("added root %d\n", roots.back()); dassert(roots.back() != -1); } used.assign(2*n, 0); int zero = 0; vec<int> ns; for (auto root : roots) { auto [sum, cycle] = dfs(root, root, 0, 0); int a = sum, b = sum - 2*cycle; if (b < a) swap(a, b); zero -= a, b -= a, a -= a; // moving everything by -a if (b) ns.pb(b); } df("zero: %d\n", zero); // we want a number between zero - k, zero + k if (zero - k <= 0 && zero + k >= 0) succeed(); if (zero < 0) fail(); vec<char> dp(zero + k + 1); dp[0] = 1; int hi = 0; sort(all(ns)); for (auto x : ns) { // cout << x << endl; for (int i = min(hi, zero+k-x); i >= 0; --i) { if (dp[i]) { dp[i+x] = 1; if (ir(i+x, zero-k, zero+k)) succeed(); } } hi += x; } for (int i = zero - k; i <= zero + k; ++i) { if (dp[i]) succeed(); } fail(); 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...