# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1070582 |
2024-08-22T15:38:11 Z |
AdamGS |
Tug of War (BOI15_tug) |
C++17 |
|
2260 ms |
6844 KB |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=3e4+7;
vector<int>P;
bitset<LIM*80>B;
vector<pair<int,int>>V[2*LIM];
int F[2*LIM], ile[2*LIM], usun[2*LIM], odw[2*LIM], n, sum, akt;
int fnd(int x) {
if(F[x]==x) return x;
return F[x]=fnd(F[x]);
}
void uni(int a, int b) {
a=fnd(a); b=fnd(b);
if(a!=b) {
ile[a]+=ile[b];
F[b]=a;
}
--ile[a];
}
void DFS(int x) {
odw[x]=1;
int p=0;
for(auto i : V[x]) if(!odw[i.st] && !usun[i.st]) {
++p;
if(p==2) {
if(i.st<n) akt+=i.nd; else akt-=i.nd;
}
}
for(auto i : V[x]) if(!odw[i.st] && !usun[i.st]) {
if(x<n) akt+=i.nd; else akt-=i.nd;
DFS(i.st);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int k;
cin >> n >> k;
rep(i, 2*n) {
F[i]=i;
ile[i]=1;
}
rep(i, 2*n) {
int a, b, c;
cin >> a >> b >> c; --a; --b; b+=n;
V[a].pb({b, c});
V[b].pb({a, c});
uni(a, b);
}
rep(i, 2*n) if(ile[fnd(i)]<0) {
cout << "NO\n";
return 0;
}
rep(i, 2*n) ile[i]=V[i].size();
queue<int>q;
rep(i, 2*n) if(ile[i]==1) q.push(i);
while(!q.empty()) {
int p=q.front(); q.pop();
usun[p]=1;
for(auto i : V[p]) if(!usun[i.st]) {
--ile[i.st];
if(ile[i.st]==1) q.push(i.st);
if(p<n) sum+=i.nd; else sum-=i.nd;
}
}
rep(i, 2*n) if(!usun[i] && !odw[i]) {
akt=0;
DFS(i);
P.pb(abs(akt));
}
sum=abs(sum);
int S=0;
B[0]=1;
for(auto i : P) {
S+=i;
B|=B<<i;
}
rep(i, 80*LIM) if(B[i]) {
int a=sum+i, b=S-i;
if(abs(a-b)<=k) {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
4 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
4 ms |
2716 KB |
Output is correct |
6 |
Correct |
3 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
2 ms |
3420 KB |
Output is correct |
9 |
Correct |
3 ms |
3420 KB |
Output is correct |
10 |
Correct |
3 ms |
3420 KB |
Output is correct |
11 |
Correct |
2 ms |
3416 KB |
Output is correct |
12 |
Correct |
2 ms |
3416 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Correct |
2 ms |
3420 KB |
Output is correct |
15 |
Correct |
2 ms |
3420 KB |
Output is correct |
16 |
Correct |
3 ms |
3420 KB |
Output is correct |
17 |
Correct |
2 ms |
3420 KB |
Output is correct |
18 |
Correct |
2 ms |
3420 KB |
Output is correct |
19 |
Correct |
2 ms |
3512 KB |
Output is correct |
20 |
Correct |
1 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
3164 KB |
Output is correct |
22 |
Correct |
2 ms |
3420 KB |
Output is correct |
23 |
Correct |
3 ms |
3420 KB |
Output is correct |
24 |
Correct |
2 ms |
3352 KB |
Output is correct |
25 |
Correct |
4 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
4 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
4 ms |
2716 KB |
Output is correct |
6 |
Correct |
3 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
2 ms |
3420 KB |
Output is correct |
9 |
Correct |
3 ms |
3420 KB |
Output is correct |
10 |
Correct |
3 ms |
3420 KB |
Output is correct |
11 |
Correct |
2 ms |
3416 KB |
Output is correct |
12 |
Correct |
2 ms |
3416 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Correct |
2 ms |
3420 KB |
Output is correct |
15 |
Correct |
2 ms |
3420 KB |
Output is correct |
16 |
Correct |
3 ms |
3420 KB |
Output is correct |
17 |
Correct |
2 ms |
3420 KB |
Output is correct |
18 |
Correct |
2 ms |
3420 KB |
Output is correct |
19 |
Correct |
2 ms |
3512 KB |
Output is correct |
20 |
Correct |
1 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
3164 KB |
Output is correct |
22 |
Correct |
2 ms |
3420 KB |
Output is correct |
23 |
Correct |
3 ms |
3420 KB |
Output is correct |
24 |
Correct |
2 ms |
3352 KB |
Output is correct |
25 |
Correct |
4 ms |
3420 KB |
Output is correct |
26 |
Correct |
116 ms |
3672 KB |
Output is correct |
27 |
Correct |
20 ms |
3676 KB |
Output is correct |
28 |
Correct |
117 ms |
3676 KB |
Output is correct |
29 |
Correct |
19 ms |
3672 KB |
Output is correct |
30 |
Correct |
123 ms |
3692 KB |
Output is correct |
31 |
Correct |
20 ms |
2904 KB |
Output is correct |
32 |
Correct |
123 ms |
2908 KB |
Output is correct |
33 |
Correct |
20 ms |
2908 KB |
Output is correct |
34 |
Correct |
11 ms |
2908 KB |
Output is correct |
35 |
Correct |
21 ms |
2908 KB |
Output is correct |
36 |
Correct |
128 ms |
2904 KB |
Output is correct |
37 |
Correct |
22 ms |
2908 KB |
Output is correct |
38 |
Correct |
139 ms |
2908 KB |
Output is correct |
39 |
Correct |
21 ms |
2908 KB |
Output is correct |
40 |
Correct |
132 ms |
2908 KB |
Output is correct |
41 |
Correct |
20 ms |
3676 KB |
Output is correct |
42 |
Correct |
124 ms |
3692 KB |
Output is correct |
43 |
Correct |
27 ms |
2984 KB |
Output is correct |
44 |
Correct |
127 ms |
2972 KB |
Output is correct |
45 |
Correct |
23 ms |
2984 KB |
Output is correct |
46 |
Correct |
156 ms |
2908 KB |
Output is correct |
47 |
Correct |
2 ms |
2652 KB |
Output is correct |
48 |
Correct |
64 ms |
3016 KB |
Output is correct |
49 |
Correct |
74 ms |
3016 KB |
Output is correct |
50 |
Correct |
149 ms |
2908 KB |
Output is correct |
51 |
Correct |
135 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
376 ms |
3932 KB |
Output is correct |
2 |
Correct |
10 ms |
3416 KB |
Output is correct |
3 |
Correct |
379 ms |
3852 KB |
Output is correct |
4 |
Correct |
8 ms |
3428 KB |
Output is correct |
5 |
Correct |
384 ms |
3940 KB |
Output is correct |
6 |
Correct |
6 ms |
3420 KB |
Output is correct |
7 |
Correct |
450 ms |
3928 KB |
Output is correct |
8 |
Correct |
9 ms |
3416 KB |
Output is correct |
9 |
Correct |
404 ms |
3940 KB |
Output is correct |
10 |
Correct |
11 ms |
3500 KB |
Output is correct |
11 |
Correct |
426 ms |
3932 KB |
Output is correct |
12 |
Correct |
7 ms |
3420 KB |
Output is correct |
13 |
Correct |
400 ms |
3852 KB |
Output is correct |
14 |
Correct |
378 ms |
3932 KB |
Output is correct |
15 |
Correct |
6 ms |
3416 KB |
Output is correct |
16 |
Correct |
394 ms |
3928 KB |
Output is correct |
17 |
Correct |
11 ms |
3420 KB |
Output is correct |
18 |
Correct |
378 ms |
3932 KB |
Output is correct |
19 |
Correct |
6 ms |
3416 KB |
Output is correct |
20 |
Correct |
366 ms |
3932 KB |
Output is correct |
21 |
Correct |
9 ms |
3332 KB |
Output is correct |
22 |
Correct |
217 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
4 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
4 ms |
2716 KB |
Output is correct |
6 |
Correct |
3 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
2 ms |
3420 KB |
Output is correct |
9 |
Correct |
3 ms |
3420 KB |
Output is correct |
10 |
Correct |
3 ms |
3420 KB |
Output is correct |
11 |
Correct |
2 ms |
3416 KB |
Output is correct |
12 |
Correct |
2 ms |
3416 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Correct |
2 ms |
3420 KB |
Output is correct |
15 |
Correct |
2 ms |
3420 KB |
Output is correct |
16 |
Correct |
3 ms |
3420 KB |
Output is correct |
17 |
Correct |
2 ms |
3420 KB |
Output is correct |
18 |
Correct |
2 ms |
3420 KB |
Output is correct |
19 |
Correct |
2 ms |
3512 KB |
Output is correct |
20 |
Correct |
1 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
3164 KB |
Output is correct |
22 |
Correct |
2 ms |
3420 KB |
Output is correct |
23 |
Correct |
3 ms |
3420 KB |
Output is correct |
24 |
Correct |
2 ms |
3352 KB |
Output is correct |
25 |
Correct |
4 ms |
3420 KB |
Output is correct |
26 |
Correct |
116 ms |
3672 KB |
Output is correct |
27 |
Correct |
20 ms |
3676 KB |
Output is correct |
28 |
Correct |
117 ms |
3676 KB |
Output is correct |
29 |
Correct |
19 ms |
3672 KB |
Output is correct |
30 |
Correct |
123 ms |
3692 KB |
Output is correct |
31 |
Correct |
20 ms |
2904 KB |
Output is correct |
32 |
Correct |
123 ms |
2908 KB |
Output is correct |
33 |
Correct |
20 ms |
2908 KB |
Output is correct |
34 |
Correct |
11 ms |
2908 KB |
Output is correct |
35 |
Correct |
21 ms |
2908 KB |
Output is correct |
36 |
Correct |
128 ms |
2904 KB |
Output is correct |
37 |
Correct |
22 ms |
2908 KB |
Output is correct |
38 |
Correct |
139 ms |
2908 KB |
Output is correct |
39 |
Correct |
21 ms |
2908 KB |
Output is correct |
40 |
Correct |
132 ms |
2908 KB |
Output is correct |
41 |
Correct |
20 ms |
3676 KB |
Output is correct |
42 |
Correct |
124 ms |
3692 KB |
Output is correct |
43 |
Correct |
27 ms |
2984 KB |
Output is correct |
44 |
Correct |
127 ms |
2972 KB |
Output is correct |
45 |
Correct |
23 ms |
2984 KB |
Output is correct |
46 |
Correct |
156 ms |
2908 KB |
Output is correct |
47 |
Correct |
2 ms |
2652 KB |
Output is correct |
48 |
Correct |
64 ms |
3016 KB |
Output is correct |
49 |
Correct |
74 ms |
3016 KB |
Output is correct |
50 |
Correct |
149 ms |
2908 KB |
Output is correct |
51 |
Correct |
135 ms |
2908 KB |
Output is correct |
52 |
Correct |
376 ms |
3932 KB |
Output is correct |
53 |
Correct |
10 ms |
3416 KB |
Output is correct |
54 |
Correct |
379 ms |
3852 KB |
Output is correct |
55 |
Correct |
8 ms |
3428 KB |
Output is correct |
56 |
Correct |
384 ms |
3940 KB |
Output is correct |
57 |
Correct |
6 ms |
3420 KB |
Output is correct |
58 |
Correct |
450 ms |
3928 KB |
Output is correct |
59 |
Correct |
9 ms |
3416 KB |
Output is correct |
60 |
Correct |
404 ms |
3940 KB |
Output is correct |
61 |
Correct |
11 ms |
3500 KB |
Output is correct |
62 |
Correct |
426 ms |
3932 KB |
Output is correct |
63 |
Correct |
7 ms |
3420 KB |
Output is correct |
64 |
Correct |
400 ms |
3852 KB |
Output is correct |
65 |
Correct |
378 ms |
3932 KB |
Output is correct |
66 |
Correct |
6 ms |
3416 KB |
Output is correct |
67 |
Correct |
394 ms |
3928 KB |
Output is correct |
68 |
Correct |
11 ms |
3420 KB |
Output is correct |
69 |
Correct |
378 ms |
3932 KB |
Output is correct |
70 |
Correct |
6 ms |
3416 KB |
Output is correct |
71 |
Correct |
366 ms |
3932 KB |
Output is correct |
72 |
Correct |
9 ms |
3332 KB |
Output is correct |
73 |
Correct |
217 ms |
3932 KB |
Output is correct |
74 |
Correct |
1769 ms |
6232 KB |
Output is correct |
75 |
Correct |
87 ms |
5988 KB |
Output is correct |
76 |
Correct |
1905 ms |
6380 KB |
Output is correct |
77 |
Correct |
89 ms |
6088 KB |
Output is correct |
78 |
Correct |
1847 ms |
6376 KB |
Output is correct |
79 |
Correct |
1960 ms |
6376 KB |
Output is correct |
80 |
Correct |
88 ms |
5976 KB |
Output is correct |
81 |
Correct |
92 ms |
6128 KB |
Output is correct |
82 |
Correct |
1814 ms |
6376 KB |
Output is correct |
83 |
Correct |
1958 ms |
6376 KB |
Output is correct |
84 |
Correct |
96 ms |
5976 KB |
Output is correct |
85 |
Correct |
1919 ms |
6376 KB |
Output is correct |
86 |
Correct |
93 ms |
5980 KB |
Output is correct |
87 |
Correct |
1983 ms |
6376 KB |
Output is correct |
88 |
Correct |
88 ms |
5980 KB |
Output is correct |
89 |
Correct |
2054 ms |
6236 KB |
Output is correct |
90 |
Correct |
161 ms |
6144 KB |
Output is correct |
91 |
Correct |
2260 ms |
6380 KB |
Output is correct |
92 |
Correct |
167 ms |
5972 KB |
Output is correct |
93 |
Correct |
2053 ms |
6376 KB |
Output is correct |
94 |
Correct |
26 ms |
5720 KB |
Output is correct |
95 |
Correct |
1084 ms |
6836 KB |
Output is correct |
96 |
Correct |
996 ms |
6844 KB |
Output is correct |
97 |
Correct |
1989 ms |
6384 KB |
Output is correct |
98 |
Correct |
1987 ms |
6368 KB |
Output is correct |