Submission #418076

# Submission time Handle Problem Language Result Execution time Memory
418076 2021-06-05T03:53:35 Z JerryLiu06 Tug of War (BOI15_tug) C++17
100 / 100
2834 ms 10500 KB
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
using ld = long double;
using db = double;
using str = string;
 
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
 
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
 
#define mp make_pair
#define f first
#define s second
 
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
 
#define lb lower_bound
#define ub upper_bound
 
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define EACH(a,x) for (auto& a: x)
 
const int MOD = 1e9 + 7;
const int MX = 3e4 + 10;
const ll INF = 1e18;
 
int N, K; multiset<pi> adj[2 * MX]; int tot; bool vis[2 * MX]; vi todo;
 
void DFS(int X) {
    vis[X] = true; pi E = *adj[X].bg();
 
    tot += E.s; if (!vis[E.f]) { adj[X].clear(); adj[E.f].erase(adj[E.f].find({X, -E.s})); DFS(E.f); }
}
 
int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
 
    cin >> N >> K; F0R(i, 2 * N) { 
        int A, B, S; cin >> A >> B >> S; 
        
        adj[A].insert({N + B, S}), adj[N + B].insert({A, -S});
    }
    queue<int> Q; FOR(i, 1, 2 * N + 1) { 
        if (sz(adj[i]) == 0) { cout << "NO"; return 0; } 
 
        if (sz(adj[i]) == 1) Q.push(i);
    }
    while (!Q.empty()) {
        int cur = Q.front(); Q.pop(); if (sz(adj[cur]) == 0) { cout << "NO"; return 0; }
 
        pi E = *adj[cur].bg(); tot += E.s; adj[cur].clear(); adj[E.f].erase(adj[E.f].find({cur, -E.s}));
 
        if (sz(adj[E.f]) == 1) Q.push(E.f);
    }
    if (tot) todo.pb(abs(tot)); FOR(i, 1, 2 * N + 1) if (!vis[i] && sz(adj[i])) {
        tot = 0; adj[i].erase(adj[i].bg()); DFS(i); if (tot) todo.pb(abs(tot));
    }
    bitset<40 * MX> DP; DP[20 * MX] = 1;EACH(C, todo) DP = (DP << C) | (DP >> C); 
 
    FOR(i, 20 * MX - K, 20 * MX + K + 1) if (DP[i]) { cout << "YES"; return 0; } cout << "NO";
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:39:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   39 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
      |                    ^~~
tug.cpp:82:5: note: in expansion of macro 'FOR'
   82 |     FOR(i, 20 * MX - K, 20 * MX + K + 1) if (DP[i]) { cout << "YES"; return 0; } cout << "NO";
      |     ^~~
tug.cpp:82:82: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   82 |     FOR(i, 20 * MX - K, 20 * MX + K + 1) if (DP[i]) { cout << "YES"; return 0; } cout << "NO";
      |                                                                                  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3660 KB Output is correct
2 Correct 3 ms 3660 KB Output is correct
3 Correct 3 ms 3660 KB Output is correct
4 Correct 4 ms 3660 KB Output is correct
5 Correct 3 ms 3716 KB Output is correct
6 Correct 3 ms 3660 KB Output is correct
7 Correct 3 ms 3660 KB Output is correct
8 Correct 3 ms 3660 KB Output is correct
9 Correct 3 ms 3716 KB Output is correct
10 Correct 4 ms 3660 KB Output is correct
11 Correct 3 ms 3720 KB Output is correct
12 Correct 3 ms 3660 KB Output is correct
13 Correct 3 ms 3660 KB Output is correct
14 Correct 3 ms 3660 KB Output is correct
15 Correct 3 ms 3724 KB Output is correct
16 Correct 3 ms 3660 KB Output is correct
17 Correct 3 ms 3716 KB Output is correct
18 Correct 3 ms 3660 KB Output is correct
19 Correct 3 ms 3660 KB Output is correct
20 Correct 3 ms 3720 KB Output is correct
21 Correct 2 ms 3660 KB Output is correct
22 Correct 3 ms 3724 KB Output is correct
23 Correct 3 ms 3660 KB Output is correct
24 Correct 3 ms 3660 KB Output is correct
25 Correct 3 ms 3660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3660 KB Output is correct
2 Correct 3 ms 3660 KB Output is correct
3 Correct 3 ms 3660 KB Output is correct
4 Correct 4 ms 3660 KB Output is correct
5 Correct 3 ms 3716 KB Output is correct
6 Correct 3 ms 3660 KB Output is correct
7 Correct 3 ms 3660 KB Output is correct
8 Correct 3 ms 3660 KB Output is correct
9 Correct 3 ms 3716 KB Output is correct
10 Correct 4 ms 3660 KB Output is correct
11 Correct 3 ms 3720 KB Output is correct
12 Correct 3 ms 3660 KB Output is correct
13 Correct 3 ms 3660 KB Output is correct
14 Correct 3 ms 3660 KB Output is correct
15 Correct 3 ms 3724 KB Output is correct
16 Correct 3 ms 3660 KB Output is correct
17 Correct 3 ms 3716 KB Output is correct
18 Correct 3 ms 3660 KB Output is correct
19 Correct 3 ms 3660 KB Output is correct
20 Correct 3 ms 3720 KB Output is correct
21 Correct 2 ms 3660 KB Output is correct
22 Correct 3 ms 3724 KB Output is correct
23 Correct 3 ms 3660 KB Output is correct
24 Correct 3 ms 3660 KB Output is correct
25 Correct 3 ms 3660 KB Output is correct
26 Correct 179 ms 4144 KB Output is correct
27 Correct 29 ms 4044 KB Output is correct
28 Correct 177 ms 4044 KB Output is correct
29 Correct 29 ms 4116 KB Output is correct
30 Correct 178 ms 4152 KB Output is correct
31 Correct 28 ms 4044 KB Output is correct
32 Correct 175 ms 4140 KB Output is correct
33 Correct 29 ms 4044 KB Output is correct
34 Correct 17 ms 4136 KB Output is correct
35 Correct 29 ms 4044 KB Output is correct
36 Correct 177 ms 4232 KB Output is correct
37 Correct 29 ms 4044 KB Output is correct
38 Correct 182 ms 4140 KB Output is correct
39 Correct 29 ms 4044 KB Output is correct
40 Correct 176 ms 4044 KB Output is correct
41 Correct 29 ms 4044 KB Output is correct
42 Correct 184 ms 4148 KB Output is correct
43 Correct 29 ms 4044 KB Output is correct
44 Correct 176 ms 4092 KB Output is correct
45 Correct 29 ms 4148 KB Output is correct
46 Correct 176 ms 4044 KB Output is correct
47 Correct 5 ms 4044 KB Output is correct
48 Correct 95 ms 4136 KB Output is correct
49 Correct 91 ms 4136 KB Output is correct
50 Correct 184 ms 4120 KB Output is correct
51 Correct 185 ms 4044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 5744 KB Output is correct
2 Correct 13 ms 5720 KB Output is correct
3 Correct 13 ms 5836 KB Output is correct
4 Correct 14 ms 5784 KB Output is correct
5 Correct 12 ms 5836 KB Output is correct
6 Correct 13 ms 5816 KB Output is correct
7 Correct 13 ms 5776 KB Output is correct
8 Correct 13 ms 5748 KB Output is correct
9 Correct 13 ms 5836 KB Output is correct
10 Correct 13 ms 5744 KB Output is correct
11 Correct 13 ms 5784 KB Output is correct
12 Correct 13 ms 5792 KB Output is correct
13 Correct 13 ms 5776 KB Output is correct
14 Correct 13 ms 5836 KB Output is correct
15 Correct 12 ms 5812 KB Output is correct
16 Correct 12 ms 5780 KB Output is correct
17 Correct 12 ms 5796 KB Output is correct
18 Correct 13 ms 5744 KB Output is correct
19 Correct 13 ms 5836 KB Output is correct
20 Correct 14 ms 5964 KB Output is correct
21 Correct 13 ms 5836 KB Output is correct
22 Correct 13 ms 5828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3660 KB Output is correct
2 Correct 3 ms 3660 KB Output is correct
3 Correct 3 ms 3660 KB Output is correct
4 Correct 4 ms 3660 KB Output is correct
5 Correct 3 ms 3716 KB Output is correct
6 Correct 3 ms 3660 KB Output is correct
7 Correct 3 ms 3660 KB Output is correct
8 Correct 3 ms 3660 KB Output is correct
9 Correct 3 ms 3716 KB Output is correct
10 Correct 4 ms 3660 KB Output is correct
11 Correct 3 ms 3720 KB Output is correct
12 Correct 3 ms 3660 KB Output is correct
13 Correct 3 ms 3660 KB Output is correct
14 Correct 3 ms 3660 KB Output is correct
15 Correct 3 ms 3724 KB Output is correct
16 Correct 3 ms 3660 KB Output is correct
17 Correct 3 ms 3716 KB Output is correct
18 Correct 3 ms 3660 KB Output is correct
19 Correct 3 ms 3660 KB Output is correct
20 Correct 3 ms 3720 KB Output is correct
21 Correct 2 ms 3660 KB Output is correct
22 Correct 3 ms 3724 KB Output is correct
23 Correct 3 ms 3660 KB Output is correct
24 Correct 3 ms 3660 KB Output is correct
25 Correct 3 ms 3660 KB Output is correct
26 Correct 179 ms 4144 KB Output is correct
27 Correct 29 ms 4044 KB Output is correct
28 Correct 177 ms 4044 KB Output is correct
29 Correct 29 ms 4116 KB Output is correct
30 Correct 178 ms 4152 KB Output is correct
31 Correct 28 ms 4044 KB Output is correct
32 Correct 175 ms 4140 KB Output is correct
33 Correct 29 ms 4044 KB Output is correct
34 Correct 17 ms 4136 KB Output is correct
35 Correct 29 ms 4044 KB Output is correct
36 Correct 177 ms 4232 KB Output is correct
37 Correct 29 ms 4044 KB Output is correct
38 Correct 182 ms 4140 KB Output is correct
39 Correct 29 ms 4044 KB Output is correct
40 Correct 176 ms 4044 KB Output is correct
41 Correct 29 ms 4044 KB Output is correct
42 Correct 184 ms 4148 KB Output is correct
43 Correct 29 ms 4044 KB Output is correct
44 Correct 176 ms 4092 KB Output is correct
45 Correct 29 ms 4148 KB Output is correct
46 Correct 176 ms 4044 KB Output is correct
47 Correct 5 ms 4044 KB Output is correct
48 Correct 95 ms 4136 KB Output is correct
49 Correct 91 ms 4136 KB Output is correct
50 Correct 184 ms 4120 KB Output is correct
51 Correct 185 ms 4044 KB Output is correct
52 Correct 12 ms 5744 KB Output is correct
53 Correct 13 ms 5720 KB Output is correct
54 Correct 13 ms 5836 KB Output is correct
55 Correct 14 ms 5784 KB Output is correct
56 Correct 12 ms 5836 KB Output is correct
57 Correct 13 ms 5816 KB Output is correct
58 Correct 13 ms 5776 KB Output is correct
59 Correct 13 ms 5748 KB Output is correct
60 Correct 13 ms 5836 KB Output is correct
61 Correct 13 ms 5744 KB Output is correct
62 Correct 13 ms 5784 KB Output is correct
63 Correct 13 ms 5792 KB Output is correct
64 Correct 13 ms 5776 KB Output is correct
65 Correct 13 ms 5836 KB Output is correct
66 Correct 12 ms 5812 KB Output is correct
67 Correct 12 ms 5780 KB Output is correct
68 Correct 12 ms 5796 KB Output is correct
69 Correct 13 ms 5744 KB Output is correct
70 Correct 13 ms 5836 KB Output is correct
71 Correct 14 ms 5964 KB Output is correct
72 Correct 13 ms 5836 KB Output is correct
73 Correct 13 ms 5828 KB Output is correct
74 Correct 2657 ms 10456 KB Output is correct
75 Correct 145 ms 10128 KB Output is correct
76 Correct 2606 ms 10384 KB Output is correct
77 Correct 146 ms 10180 KB Output is correct
78 Correct 2609 ms 10416 KB Output is correct
79 Correct 2623 ms 10436 KB Output is correct
80 Correct 142 ms 10212 KB Output is correct
81 Correct 146 ms 10180 KB Output is correct
82 Correct 2750 ms 10424 KB Output is correct
83 Correct 2647 ms 10500 KB Output is correct
84 Correct 181 ms 10152 KB Output is correct
85 Correct 2630 ms 10412 KB Output is correct
86 Correct 142 ms 10180 KB Output is correct
87 Correct 2634 ms 10436 KB Output is correct
88 Correct 146 ms 10208 KB Output is correct
89 Correct 2639 ms 10424 KB Output is correct
90 Correct 157 ms 10304 KB Output is correct
91 Correct 2647 ms 10400 KB Output is correct
92 Correct 150 ms 10316 KB Output is correct
93 Correct 2631 ms 10436 KB Output is correct
94 Correct 41 ms 10100 KB Output is correct
95 Correct 1349 ms 10480 KB Output is correct
96 Correct 1335 ms 10368 KB Output is correct
97 Correct 2834 ms 10436 KB Output is correct
98 Correct 2777 ms 10444 KB Output is correct