# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
494899 | 2021-12-17T10:57:29 Z | fcmalkcin | Tug of War (BOI15_tug) | C++17 | 442 ms | 13400 KB |
#pragma GCC optimize("Ofast") #pragma GCC optimization("unroll-loops, no-stack-protector") #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> using namespace std; #define ll long long #define pll pair<ll,ll> #define ff first #define ss second #define pb push_back #define endl "\n" mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); const ll maxn=3e5+21; const ll mod=998244353 ; const ll base=1e9+1000; /// you will be the best but now you just are trash /// goal 2/7 ll par[maxn]; ll cnt[maxn]; ll find_par(ll u) { if (par[u]<0) return u; return par[u]=find_par(par[u]); } void dsu(ll x,ll y) { x=find_par(x); y=find_par(y); if (x==y) return ; if (y>x) swap(x,y); cnt[y]+=cnt[x]; par[y]+=par[x]; par[x]=y; } vector<ll> adj[maxn]; bool dd[maxn]; ll n; ll x[maxn]; ll y[maxn]; ll w[maxn]; ll pos; ll cnta=0; vector<ll> vt; ll ed; void dfs(ll u,ll par) { ll nw=par; dd[u]=1; vt.pb(u); for (auto id:adj[u]) { ll to=x[id]+y[id]-u; if (to==nw) { nw=-1; continue; } if (dd[to]) { ed=id; } else { dfs(to,u); } } } void dfs1(ll u,ll par) { for (auto id:adj[u]) { if (id==ed) continue; ll to=x[id]+y[id]-u; if (to==par) continue; if (to<=n) cnta+=w[id]; dfs1(to,u); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (fopen("t.inp", "r")) { freopen("test.inp", "r", stdin); freopen("test.out", "w", stdout); } ll k; cin>> n>> k; for (int i=1;i<=2*n;i++) { par[i]=-1; } ll all=0; for (int i=1;i<=2*n;i++) { cin>>x[i]>>y[i]>>w[i]; all+=w[i]; y[i]+=n; adj[x[i]].pb(i); adj[y[i]].pb(i); dsu(x[i],y[i]); cnt[find_par(x[i])]++; } vector<ll> vt; ll nw=0; bitset<600002> bs; bs[0]=1; for (int i=1;i<=2*n;i++) { if (i==find_par(i)) { if (cnt[i]!=-par[i]) { cout <<"NO"; return 0; } cnta=0; ed=-1; dfs(i,0); assert(ed!=-1); dfs1(x[ed],0); ll f=cnta+w[ed]; cnta=0; dfs1(y[ed],0); ll l=cnta; if (f>l) swap(l,f); nw+=f; bs|=(bs<<(l-f)); } } for (int i=0;i<=600000;i++) { if (bs[i]) { ll vala=nw+i; if (abs(vala-(all-vala))<=k) { cout <<"YES"; return 0; } } } cout <<"NO"; } /*11 1 4 2 14 10 3 15 6 1 2 9 10 9 1 9*/
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 7 ms | 7504 KB | Output is correct |
2 | Correct | 4 ms | 7632 KB | Output is correct |
3 | Correct | 5 ms | 7592 KB | Output is correct |
4 | Correct | 4 ms | 7504 KB | Output is correct |
5 | Correct | 5 ms | 7504 KB | Output is correct |
6 | Correct | 5 ms | 7632 KB | Output is correct |
7 | Correct | 5 ms | 7504 KB | Output is correct |
8 | Correct | 5 ms | 7504 KB | Output is correct |
9 | Correct | 5 ms | 7632 KB | Output is correct |
10 | Correct | 5 ms | 7504 KB | Output is correct |
11 | Correct | 4 ms | 7600 KB | Output is correct |
12 | Correct | 4 ms | 7504 KB | Output is correct |
13 | Correct | 5 ms | 7504 KB | Output is correct |
14 | Correct | 5 ms | 7504 KB | Output is correct |
15 | Correct | 4 ms | 7600 KB | Output is correct |
16 | Correct | 5 ms | 7504 KB | Output is correct |
17 | Correct | 5 ms | 7608 KB | Output is correct |
18 | Correct | 5 ms | 7504 KB | Output is correct |
19 | Correct | 4 ms | 7632 KB | Output is correct |
20 | Correct | 5 ms | 7592 KB | Output is correct |
21 | Correct | 5 ms | 7504 KB | Output is correct |
22 | Correct | 5 ms | 7588 KB | Output is correct |
23 | Correct | 5 ms | 7632 KB | Output is correct |
24 | Correct | 5 ms | 7600 KB | Output is correct |
25 | Correct | 5 ms | 7596 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 7 ms | 7504 KB | Output is correct |
2 | Correct | 4 ms | 7632 KB | Output is correct |
3 | Correct | 5 ms | 7592 KB | Output is correct |
4 | Correct | 4 ms | 7504 KB | Output is correct |
5 | Correct | 5 ms | 7504 KB | Output is correct |
6 | Correct | 5 ms | 7632 KB | Output is correct |
7 | Correct | 5 ms | 7504 KB | Output is correct |
8 | Correct | 5 ms | 7504 KB | Output is correct |
9 | Correct | 5 ms | 7632 KB | Output is correct |
10 | Correct | 5 ms | 7504 KB | Output is correct |
11 | Correct | 4 ms | 7600 KB | Output is correct |
12 | Correct | 4 ms | 7504 KB | Output is correct |
13 | Correct | 5 ms | 7504 KB | Output is correct |
14 | Correct | 5 ms | 7504 KB | Output is correct |
15 | Correct | 4 ms | 7600 KB | Output is correct |
16 | Correct | 5 ms | 7504 KB | Output is correct |
17 | Correct | 5 ms | 7608 KB | Output is correct |
18 | Correct | 5 ms | 7504 KB | Output is correct |
19 | Correct | 4 ms | 7632 KB | Output is correct |
20 | Correct | 5 ms | 7592 KB | Output is correct |
21 | Correct | 5 ms | 7504 KB | Output is correct |
22 | Correct | 5 ms | 7588 KB | Output is correct |
23 | Correct | 5 ms | 7632 KB | Output is correct |
24 | Correct | 5 ms | 7600 KB | Output is correct |
25 | Correct | 5 ms | 7596 KB | Output is correct |
26 | Correct | 30 ms | 7996 KB | Output is correct |
27 | Correct | 10 ms | 7980 KB | Output is correct |
28 | Correct | 32 ms | 7972 KB | Output is correct |
29 | Correct | 10 ms | 8016 KB | Output is correct |
30 | Correct | 34 ms | 7944 KB | Output is correct |
31 | Correct | 11 ms | 7896 KB | Output is correct |
32 | Correct | 30 ms | 7996 KB | Output is correct |
33 | Correct | 10 ms | 8016 KB | Output is correct |
34 | Correct | 8 ms | 7888 KB | Output is correct |
35 | Correct | 13 ms | 7988 KB | Output is correct |
36 | Correct | 31 ms | 7972 KB | Output is correct |
37 | Correct | 11 ms | 7992 KB | Output is correct |
38 | Correct | 31 ms | 7988 KB | Output is correct |
39 | Correct | 10 ms | 7888 KB | Output is correct |
40 | Correct | 31 ms | 7972 KB | Output is correct |
41 | Correct | 11 ms | 7892 KB | Output is correct |
42 | Correct | 32 ms | 7992 KB | Output is correct |
43 | Correct | 10 ms | 7972 KB | Output is correct |
44 | Correct | 35 ms | 7988 KB | Output is correct |
45 | Correct | 11 ms | 7888 KB | Output is correct |
46 | Correct | 31 ms | 7964 KB | Output is correct |
47 | Correct | 7 ms | 7888 KB | Output is correct |
48 | Correct | 18 ms | 8020 KB | Output is correct |
49 | Correct | 21 ms | 8032 KB | Output is correct |
50 | Correct | 30 ms | 7880 KB | Output is correct |
51 | Correct | 30 ms | 7888 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 82 ms | 9508 KB | Output is correct |
2 | Correct | 13 ms | 9228 KB | Output is correct |
3 | Correct | 78 ms | 9500 KB | Output is correct |
4 | Correct | 12 ms | 9288 KB | Output is correct |
5 | Correct | 79 ms | 9540 KB | Output is correct |
6 | Correct | 12 ms | 9264 KB | Output is correct |
7 | Correct | 85 ms | 9576 KB | Output is correct |
8 | Correct | 13 ms | 9300 KB | Output is correct |
9 | Correct | 79 ms | 9528 KB | Output is correct |
10 | Correct | 12 ms | 9288 KB | Output is correct |
11 | Correct | 83 ms | 9564 KB | Output is correct |
12 | Correct | 13 ms | 9280 KB | Output is correct |
13 | Correct | 78 ms | 9512 KB | Output is correct |
14 | Correct | 83 ms | 9512 KB | Output is correct |
15 | Correct | 12 ms | 9288 KB | Output is correct |
16 | Correct | 90 ms | 9548 KB | Output is correct |
17 | Correct | 13 ms | 9296 KB | Output is correct |
18 | Correct | 80 ms | 9560 KB | Output is correct |
19 | Correct | 13 ms | 9276 KB | Output is correct |
20 | Correct | 80 ms | 9512 KB | Output is correct |
21 | Correct | 13 ms | 9208 KB | Output is correct |
22 | Correct | 54 ms | 9628 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 7 ms | 7504 KB | Output is correct |
2 | Correct | 4 ms | 7632 KB | Output is correct |
3 | Correct | 5 ms | 7592 KB | Output is correct |
4 | Correct | 4 ms | 7504 KB | Output is correct |
5 | Correct | 5 ms | 7504 KB | Output is correct |
6 | Correct | 5 ms | 7632 KB | Output is correct |
7 | Correct | 5 ms | 7504 KB | Output is correct |
8 | Correct | 5 ms | 7504 KB | Output is correct |
9 | Correct | 5 ms | 7632 KB | Output is correct |
10 | Correct | 5 ms | 7504 KB | Output is correct |
11 | Correct | 4 ms | 7600 KB | Output is correct |
12 | Correct | 4 ms | 7504 KB | Output is correct |
13 | Correct | 5 ms | 7504 KB | Output is correct |
14 | Correct | 5 ms | 7504 KB | Output is correct |
15 | Correct | 4 ms | 7600 KB | Output is correct |
16 | Correct | 5 ms | 7504 KB | Output is correct |
17 | Correct | 5 ms | 7608 KB | Output is correct |
18 | Correct | 5 ms | 7504 KB | Output is correct |
19 | Correct | 4 ms | 7632 KB | Output is correct |
20 | Correct | 5 ms | 7592 KB | Output is correct |
21 | Correct | 5 ms | 7504 KB | Output is correct |
22 | Correct | 5 ms | 7588 KB | Output is correct |
23 | Correct | 5 ms | 7632 KB | Output is correct |
24 | Correct | 5 ms | 7600 KB | Output is correct |
25 | Correct | 5 ms | 7596 KB | Output is correct |
26 | Correct | 30 ms | 7996 KB | Output is correct |
27 | Correct | 10 ms | 7980 KB | Output is correct |
28 | Correct | 32 ms | 7972 KB | Output is correct |
29 | Correct | 10 ms | 8016 KB | Output is correct |
30 | Correct | 34 ms | 7944 KB | Output is correct |
31 | Correct | 11 ms | 7896 KB | Output is correct |
32 | Correct | 30 ms | 7996 KB | Output is correct |
33 | Correct | 10 ms | 8016 KB | Output is correct |
34 | Correct | 8 ms | 7888 KB | Output is correct |
35 | Correct | 13 ms | 7988 KB | Output is correct |
36 | Correct | 31 ms | 7972 KB | Output is correct |
37 | Correct | 11 ms | 7992 KB | Output is correct |
38 | Correct | 31 ms | 7988 KB | Output is correct |
39 | Correct | 10 ms | 7888 KB | Output is correct |
40 | Correct | 31 ms | 7972 KB | Output is correct |
41 | Correct | 11 ms | 7892 KB | Output is correct |
42 | Correct | 32 ms | 7992 KB | Output is correct |
43 | Correct | 10 ms | 7972 KB | Output is correct |
44 | Correct | 35 ms | 7988 KB | Output is correct |
45 | Correct | 11 ms | 7888 KB | Output is correct |
46 | Correct | 31 ms | 7964 KB | Output is correct |
47 | Correct | 7 ms | 7888 KB | Output is correct |
48 | Correct | 18 ms | 8020 KB | Output is correct |
49 | Correct | 21 ms | 8032 KB | Output is correct |
50 | Correct | 30 ms | 7880 KB | Output is correct |
51 | Correct | 30 ms | 7888 KB | Output is correct |
52 | Correct | 82 ms | 9508 KB | Output is correct |
53 | Correct | 13 ms | 9228 KB | Output is correct |
54 | Correct | 78 ms | 9500 KB | Output is correct |
55 | Correct | 12 ms | 9288 KB | Output is correct |
56 | Correct | 79 ms | 9540 KB | Output is correct |
57 | Correct | 12 ms | 9264 KB | Output is correct |
58 | Correct | 85 ms | 9576 KB | Output is correct |
59 | Correct | 13 ms | 9300 KB | Output is correct |
60 | Correct | 79 ms | 9528 KB | Output is correct |
61 | Correct | 12 ms | 9288 KB | Output is correct |
62 | Correct | 83 ms | 9564 KB | Output is correct |
63 | Correct | 13 ms | 9280 KB | Output is correct |
64 | Correct | 78 ms | 9512 KB | Output is correct |
65 | Correct | 83 ms | 9512 KB | Output is correct |
66 | Correct | 12 ms | 9288 KB | Output is correct |
67 | Correct | 90 ms | 9548 KB | Output is correct |
68 | Correct | 13 ms | 9296 KB | Output is correct |
69 | Correct | 80 ms | 9560 KB | Output is correct |
70 | Correct | 13 ms | 9276 KB | Output is correct |
71 | Correct | 80 ms | 9512 KB | Output is correct |
72 | Correct | 13 ms | 9208 KB | Output is correct |
73 | Correct | 54 ms | 9628 KB | Output is correct |
74 | Correct | 406 ms | 13144 KB | Output is correct |
75 | Correct | 63 ms | 13020 KB | Output is correct |
76 | Correct | 418 ms | 13060 KB | Output is correct |
77 | Correct | 63 ms | 13104 KB | Output is correct |
78 | Correct | 431 ms | 13224 KB | Output is correct |
79 | Correct | 397 ms | 13100 KB | Output is correct |
80 | Correct | 61 ms | 13108 KB | Output is correct |
81 | Correct | 67 ms | 13036 KB | Output is correct |
82 | Correct | 403 ms | 13028 KB | Output is correct |
83 | Correct | 431 ms | 13160 KB | Output is correct |
84 | Correct | 58 ms | 13124 KB | Output is correct |
85 | Correct | 424 ms | 13096 KB | Output is correct |
86 | Correct | 68 ms | 13128 KB | Output is correct |
87 | Correct | 421 ms | 13044 KB | Output is correct |
88 | Correct | 72 ms | 13128 KB | Output is correct |
89 | Correct | 403 ms | 13028 KB | Output is correct |
90 | Correct | 62 ms | 13108 KB | Output is correct |
91 | Correct | 427 ms | 13112 KB | Output is correct |
92 | Correct | 61 ms | 13044 KB | Output is correct |
93 | Correct | 425 ms | 13108 KB | Output is correct |
94 | Correct | 34 ms | 12616 KB | Output is correct |
95 | Correct | 248 ms | 13356 KB | Output is correct |
96 | Correct | 236 ms | 13400 KB | Output is correct |
97 | Correct | 418 ms | 13060 KB | Output is correct |
98 | Correct | 442 ms | 13284 KB | Output is correct |