Submission #1096069

# Submission time Handle Problem Language Result Execution time Memory
1096069 2024-10-03T16:59:09 Z cpptowin Tug of War (BOI15_tug) C++17
100 / 100
587 ms 36248 KB
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 600010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
    x += k;
    x %= mod;
    if (x < 0)
        x += mod;
}
void del(int &x, int k)
{
    x -= k;
    x %= mod;
    if (x < 0)
        x += mod;
}
int cnt[maxn];
bool ok[maxn];
array<int, 3> edge[maxn];
int n, k;
set<int> ke[maxn];
vi things;
int total = 0;
int sum;
void dfs(int u, int val)
{
    if(ke[u].empty()) 
    {
        if(sum != 0)
        things.pb(abs(sum));
        return;
    }
    auto [i,j,w] = edge[*ke[u].begin()];
    int v = i + j - u;
    sum += w * val;
    ke[v].erase(ke[v].find(*ke[u].begin()));
    ke[u].erase(ke[u].begin());
    dfs(v,-val);
}
bitset<600010> f;
main()
{
#define name "TASK"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    fo(i, 1, 2 * n)
    {
        int u, v, w;
        cin >> u >> v >> w;
        edge[i] = {u, v + n, w};
        ke[u].insert(i);
        ke[v + n].insert(i);
        ok[i] = 1;
    }
    queue<int> q;
    fo(i,1,2 * n) 
    {
        if(ss(ke[i]) == 0) return cout << "NO",0;
        if(ss(ke[i]) == 1) q.push(i);
    }
    while(ss(q))
    {
        int u = q.front();
        q.pop();
        if(ss(ke[u]) == 0) return cout << "NO",0;
        int it = *ke[u].begin();
        int v = edge[it][0] + edge[it][1] - u;
        if(v > n) sum -= edge[it][2];
        else sum += edge[it][2];
        ke[u].clear();
        ke[v].erase(ke[v].find(it));
        if(ss(ke[v]) == 1) q.push(v);
    }
    if(sum != 0)
    things.pb(abs(sum));
    fo(i, 1, 2 * n) if (ss(ke[i]))
    {
        sum = 0;
        dfs(i, 1);
    }
    total = accumulate(all(things),0);
    f[0] = 1;
    for (int it : things)
        f |= f << it;
    fo(i, 0, 600000) if (f[i] and abs(total - 2 * i) <= k) return cout << "YES", 0;
    cout << "NO";
}

Compilation message

tug.cpp:81:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   81 | main()
      | ^~~~
tug.cpp: In function 'int main()':
tug.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
tug.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28760 KB Output is correct
2 Correct 12 ms 28760 KB Output is correct
3 Correct 13 ms 28760 KB Output is correct
4 Correct 11 ms 28752 KB Output is correct
5 Correct 13 ms 28972 KB Output is correct
6 Correct 12 ms 28760 KB Output is correct
7 Correct 19 ms 28760 KB Output is correct
8 Correct 11 ms 28792 KB Output is correct
9 Correct 12 ms 28732 KB Output is correct
10 Correct 12 ms 28764 KB Output is correct
11 Correct 12 ms 28676 KB Output is correct
12 Correct 12 ms 28776 KB Output is correct
13 Correct 12 ms 28764 KB Output is correct
14 Correct 11 ms 28760 KB Output is correct
15 Correct 11 ms 28764 KB Output is correct
16 Correct 12 ms 28876 KB Output is correct
17 Correct 11 ms 28764 KB Output is correct
18 Correct 11 ms 28760 KB Output is correct
19 Correct 11 ms 28764 KB Output is correct
20 Correct 11 ms 28764 KB Output is correct
21 Correct 12 ms 28608 KB Output is correct
22 Correct 13 ms 28764 KB Output is correct
23 Correct 12 ms 28764 KB Output is correct
24 Correct 11 ms 28764 KB Output is correct
25 Correct 12 ms 28764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28760 KB Output is correct
2 Correct 12 ms 28760 KB Output is correct
3 Correct 13 ms 28760 KB Output is correct
4 Correct 11 ms 28752 KB Output is correct
5 Correct 13 ms 28972 KB Output is correct
6 Correct 12 ms 28760 KB Output is correct
7 Correct 19 ms 28760 KB Output is correct
8 Correct 11 ms 28792 KB Output is correct
9 Correct 12 ms 28732 KB Output is correct
10 Correct 12 ms 28764 KB Output is correct
11 Correct 12 ms 28676 KB Output is correct
12 Correct 12 ms 28776 KB Output is correct
13 Correct 12 ms 28764 KB Output is correct
14 Correct 11 ms 28760 KB Output is correct
15 Correct 11 ms 28764 KB Output is correct
16 Correct 12 ms 28876 KB Output is correct
17 Correct 11 ms 28764 KB Output is correct
18 Correct 11 ms 28760 KB Output is correct
19 Correct 11 ms 28764 KB Output is correct
20 Correct 11 ms 28764 KB Output is correct
21 Correct 12 ms 28608 KB Output is correct
22 Correct 13 ms 28764 KB Output is correct
23 Correct 12 ms 28764 KB Output is correct
24 Correct 11 ms 28764 KB Output is correct
25 Correct 12 ms 28764 KB Output is correct
26 Correct 42 ms 29272 KB Output is correct
27 Correct 20 ms 29276 KB Output is correct
28 Correct 42 ms 29316 KB Output is correct
29 Correct 18 ms 29272 KB Output is correct
30 Correct 43 ms 29272 KB Output is correct
31 Correct 17 ms 29272 KB Output is correct
32 Correct 42 ms 29276 KB Output is correct
33 Correct 17 ms 29276 KB Output is correct
34 Correct 15 ms 29276 KB Output is correct
35 Correct 17 ms 29276 KB Output is correct
36 Correct 42 ms 29276 KB Output is correct
37 Correct 17 ms 29272 KB Output is correct
38 Correct 45 ms 29276 KB Output is correct
39 Correct 17 ms 29272 KB Output is correct
40 Correct 42 ms 29276 KB Output is correct
41 Correct 18 ms 29272 KB Output is correct
42 Correct 42 ms 29276 KB Output is correct
43 Correct 18 ms 29140 KB Output is correct
44 Correct 42 ms 29340 KB Output is correct
45 Correct 18 ms 29272 KB Output is correct
46 Correct 44 ms 29272 KB Output is correct
47 Correct 15 ms 29272 KB Output is correct
48 Correct 29 ms 29528 KB Output is correct
49 Correct 30 ms 29272 KB Output is correct
50 Correct 45 ms 29276 KB Output is correct
51 Correct 57 ms 29264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 31068 KB Output is correct
2 Correct 22 ms 31064 KB Output is correct
3 Correct 18 ms 31068 KB Output is correct
4 Correct 21 ms 31024 KB Output is correct
5 Correct 18 ms 31320 KB Output is correct
6 Correct 20 ms 31068 KB Output is correct
7 Correct 20 ms 30976 KB Output is correct
8 Correct 20 ms 31068 KB Output is correct
9 Correct 19 ms 31320 KB Output is correct
10 Correct 19 ms 31064 KB Output is correct
11 Correct 19 ms 31068 KB Output is correct
12 Correct 18 ms 31028 KB Output is correct
13 Correct 18 ms 31064 KB Output is correct
14 Correct 17 ms 31108 KB Output is correct
15 Correct 17 ms 31064 KB Output is correct
16 Correct 18 ms 31068 KB Output is correct
17 Correct 18 ms 31068 KB Output is correct
18 Correct 18 ms 31164 KB Output is correct
19 Correct 18 ms 31064 KB Output is correct
20 Correct 19 ms 31068 KB Output is correct
21 Correct 19 ms 30944 KB Output is correct
22 Correct 21 ms 31064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28760 KB Output is correct
2 Correct 12 ms 28760 KB Output is correct
3 Correct 13 ms 28760 KB Output is correct
4 Correct 11 ms 28752 KB Output is correct
5 Correct 13 ms 28972 KB Output is correct
6 Correct 12 ms 28760 KB Output is correct
7 Correct 19 ms 28760 KB Output is correct
8 Correct 11 ms 28792 KB Output is correct
9 Correct 12 ms 28732 KB Output is correct
10 Correct 12 ms 28764 KB Output is correct
11 Correct 12 ms 28676 KB Output is correct
12 Correct 12 ms 28776 KB Output is correct
13 Correct 12 ms 28764 KB Output is correct
14 Correct 11 ms 28760 KB Output is correct
15 Correct 11 ms 28764 KB Output is correct
16 Correct 12 ms 28876 KB Output is correct
17 Correct 11 ms 28764 KB Output is correct
18 Correct 11 ms 28760 KB Output is correct
19 Correct 11 ms 28764 KB Output is correct
20 Correct 11 ms 28764 KB Output is correct
21 Correct 12 ms 28608 KB Output is correct
22 Correct 13 ms 28764 KB Output is correct
23 Correct 12 ms 28764 KB Output is correct
24 Correct 11 ms 28764 KB Output is correct
25 Correct 12 ms 28764 KB Output is correct
26 Correct 42 ms 29272 KB Output is correct
27 Correct 20 ms 29276 KB Output is correct
28 Correct 42 ms 29316 KB Output is correct
29 Correct 18 ms 29272 KB Output is correct
30 Correct 43 ms 29272 KB Output is correct
31 Correct 17 ms 29272 KB Output is correct
32 Correct 42 ms 29276 KB Output is correct
33 Correct 17 ms 29276 KB Output is correct
34 Correct 15 ms 29276 KB Output is correct
35 Correct 17 ms 29276 KB Output is correct
36 Correct 42 ms 29276 KB Output is correct
37 Correct 17 ms 29272 KB Output is correct
38 Correct 45 ms 29276 KB Output is correct
39 Correct 17 ms 29272 KB Output is correct
40 Correct 42 ms 29276 KB Output is correct
41 Correct 18 ms 29272 KB Output is correct
42 Correct 42 ms 29276 KB Output is correct
43 Correct 18 ms 29140 KB Output is correct
44 Correct 42 ms 29340 KB Output is correct
45 Correct 18 ms 29272 KB Output is correct
46 Correct 44 ms 29272 KB Output is correct
47 Correct 15 ms 29272 KB Output is correct
48 Correct 29 ms 29528 KB Output is correct
49 Correct 30 ms 29272 KB Output is correct
50 Correct 45 ms 29276 KB Output is correct
51 Correct 57 ms 29264 KB Output is correct
52 Correct 22 ms 31068 KB Output is correct
53 Correct 22 ms 31064 KB Output is correct
54 Correct 18 ms 31068 KB Output is correct
55 Correct 21 ms 31024 KB Output is correct
56 Correct 18 ms 31320 KB Output is correct
57 Correct 20 ms 31068 KB Output is correct
58 Correct 20 ms 30976 KB Output is correct
59 Correct 20 ms 31068 KB Output is correct
60 Correct 19 ms 31320 KB Output is correct
61 Correct 19 ms 31064 KB Output is correct
62 Correct 19 ms 31068 KB Output is correct
63 Correct 18 ms 31028 KB Output is correct
64 Correct 18 ms 31064 KB Output is correct
65 Correct 17 ms 31108 KB Output is correct
66 Correct 17 ms 31064 KB Output is correct
67 Correct 18 ms 31068 KB Output is correct
68 Correct 18 ms 31068 KB Output is correct
69 Correct 18 ms 31164 KB Output is correct
70 Correct 18 ms 31064 KB Output is correct
71 Correct 19 ms 31068 KB Output is correct
72 Correct 19 ms 30944 KB Output is correct
73 Correct 21 ms 31064 KB Output is correct
74 Correct 479 ms 36172 KB Output is correct
75 Correct 53 ms 36008 KB Output is correct
76 Correct 474 ms 36180 KB Output is correct
77 Correct 58 ms 36068 KB Output is correct
78 Correct 480 ms 36216 KB Output is correct
79 Correct 484 ms 36240 KB Output is correct
80 Correct 53 ms 35932 KB Output is correct
81 Correct 54 ms 36184 KB Output is correct
82 Correct 530 ms 35928 KB Output is correct
83 Correct 494 ms 36204 KB Output is correct
84 Correct 59 ms 35924 KB Output is correct
85 Correct 493 ms 36208 KB Output is correct
86 Correct 68 ms 36000 KB Output is correct
87 Correct 494 ms 36204 KB Output is correct
88 Correct 55 ms 36072 KB Output is correct
89 Correct 479 ms 36204 KB Output is correct
90 Correct 60 ms 35924 KB Output is correct
91 Correct 482 ms 36188 KB Output is correct
92 Correct 56 ms 35924 KB Output is correct
93 Correct 487 ms 36248 KB Output is correct
94 Correct 33 ms 35924 KB Output is correct
95 Correct 255 ms 36224 KB Output is correct
96 Correct 271 ms 36220 KB Output is correct
97 Correct 525 ms 36176 KB Output is correct
98 Correct 587 ms 36176 KB Output is correct