# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
79807 |
2018-10-16T13:53:02 Z |
Vinhspm |
Tug of War (BOI15_tug) |
C++14 |
|
1470 ms |
16700 KB |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
typedef pair<int, int> ii;
const int N = 2e5 + 5;
const int M = 20 * 30000;
const int oo = 1e9;
ii a[N];
int n, maxn, sum, suma, cnt, curval, total, las, nex = 1;
int cost[N], deg[N], f[2][40 * 30005];
bool alive[N];
vector<int> vi[N];
map<int, int> val;
void bad() { cout << "NO"; exit(0); }
void upd(int &x, int y) { x = min(x, y); }
void addeg(int u, int v) {
deg[u] ++; deg[v] ++;
vi[u].pb(v); vi[v].pb(u);
}
void kill(int u) {
int u1 = 0, tmp = 0;
for(int v: vi[u]) if(!alive[v]) {
u1 = v; tmp ++;
}
if(tmp > 1) return;
if(u1 == 0) bad();
if(u <= 3*n) curval += cost[u1];
alive[u] = alive[u1] = 1;
for(int v: vi[u1]) if(!alive[v])
kill(v); // :D
}
void dfs(int u, bool spm) {
if(spm == 0) suma += cost[u];
sum += cost[u];
alive[u] = true;
int nex = -1;
for(int v: vi[u]) if(!alive[v]) {
nex = v; break;
}
alive[nex] = true;
for(int v: vi[nex]) if(!alive[v])
dfs(v, 1 - spm);
}
signed main() {
//freopen("test.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
scanf("%d%d", &n, &maxn);
for(int i = 1; i <= 2*n; ++i) {
int l, r, c; scanf("%d%d%d", &l, &r, &c);
total += c;
addeg(i, 2*n + l);
addeg(3*n + r, i);
cost[i] = c;
}
for(int i = 2*n + 1; i <= 4*n; ++i) if(deg[i] == 1)
kill(i);
for(int i = 1; i <= 4*n; ++i) if(!alive[i]) {
int tmp = 0;
for(int v: vi[i]) if(!alive[v]) tmp ++;
if(tmp > 2) bad();
}
// now we have all the circle
for(int i = 1; i <= 2*n; ++i) if(!alive[i]) {
sum = suma = 0;
dfs(i, 0);
curval += min(suma, sum - suma);
int x = max(suma, sum - suma) - min(suma, sum - suma);
val[x] ++;
}
for(auto it = val.begin(); it != val.end(); ++it)
a[++cnt] = ii(it->fi, it->se); // :D
a[0].se = 0;
//for(int i = 1; i <= n; ++i) cout << a[i].fi << ' ' << a[i].se << '\n';
//cout << curval << ' ' << total << '\n';
for(int i = 0; i <= 1; ++i) for(int j = 0; j <= M; ++j) f[i][j] = oo;
f[las][0] = 0;
for(int i = 0; i <= cnt; ++i) {
for(int j = 0; j <= M; ++j) if(f[las][j] <= a[i].se) {
if(f[las][j] < a[i].se) upd(f[las][j + a[i].fi], f[las][j] + 1);
upd(f[nex][j], 0);
}
if(i < cnt) {
swap(las, nex);
for(int j = 0; j <= M; ++j) f[nex][j] = oo;
}
}
for(int i = 0; i <= M; ++i) if(f[las][i] <= a[cnt].se) {
if(abs(2*(curval + i) - total) <= maxn)
return cout << "YES", 0;
}
return cout << "NO", 0;
}
Compilation message
tug.cpp: In function 'int main()':
tug.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &maxn);
~~~~~^~~~~~~~~~~~~~~~~~~
tug.cpp:58:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int l, r, c; scanf("%d%d%d", &l, &r, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
9720 KB |
Output is correct |
2 |
Correct |
20 ms |
9720 KB |
Output is correct |
3 |
Correct |
20 ms |
9736 KB |
Output is correct |
4 |
Correct |
24 ms |
9880 KB |
Output is correct |
5 |
Correct |
17 ms |
9940 KB |
Output is correct |
6 |
Correct |
17 ms |
9940 KB |
Output is correct |
7 |
Correct |
21 ms |
9940 KB |
Output is correct |
8 |
Correct |
19 ms |
9940 KB |
Output is correct |
9 |
Correct |
21 ms |
10032 KB |
Output is correct |
10 |
Correct |
18 ms |
10032 KB |
Output is correct |
11 |
Correct |
19 ms |
10048 KB |
Output is correct |
12 |
Correct |
18 ms |
10160 KB |
Output is correct |
13 |
Correct |
20 ms |
10160 KB |
Output is correct |
14 |
Correct |
20 ms |
10160 KB |
Output is correct |
15 |
Correct |
20 ms |
10160 KB |
Output is correct |
16 |
Correct |
20 ms |
10160 KB |
Output is correct |
17 |
Correct |
20 ms |
10160 KB |
Output is correct |
18 |
Correct |
16 ms |
10160 KB |
Output is correct |
19 |
Correct |
20 ms |
10160 KB |
Output is correct |
20 |
Correct |
16 ms |
10160 KB |
Output is correct |
21 |
Correct |
6 ms |
10160 KB |
Output is correct |
22 |
Correct |
15 ms |
10160 KB |
Output is correct |
23 |
Correct |
16 ms |
10160 KB |
Output is correct |
24 |
Correct |
20 ms |
10160 KB |
Output is correct |
25 |
Correct |
20 ms |
10160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
9720 KB |
Output is correct |
2 |
Correct |
20 ms |
9720 KB |
Output is correct |
3 |
Correct |
20 ms |
9736 KB |
Output is correct |
4 |
Correct |
24 ms |
9880 KB |
Output is correct |
5 |
Correct |
17 ms |
9940 KB |
Output is correct |
6 |
Correct |
17 ms |
9940 KB |
Output is correct |
7 |
Correct |
21 ms |
9940 KB |
Output is correct |
8 |
Correct |
19 ms |
9940 KB |
Output is correct |
9 |
Correct |
21 ms |
10032 KB |
Output is correct |
10 |
Correct |
18 ms |
10032 KB |
Output is correct |
11 |
Correct |
19 ms |
10048 KB |
Output is correct |
12 |
Correct |
18 ms |
10160 KB |
Output is correct |
13 |
Correct |
20 ms |
10160 KB |
Output is correct |
14 |
Correct |
20 ms |
10160 KB |
Output is correct |
15 |
Correct |
20 ms |
10160 KB |
Output is correct |
16 |
Correct |
20 ms |
10160 KB |
Output is correct |
17 |
Correct |
20 ms |
10160 KB |
Output is correct |
18 |
Correct |
16 ms |
10160 KB |
Output is correct |
19 |
Correct |
20 ms |
10160 KB |
Output is correct |
20 |
Correct |
16 ms |
10160 KB |
Output is correct |
21 |
Correct |
6 ms |
10160 KB |
Output is correct |
22 |
Correct |
15 ms |
10160 KB |
Output is correct |
23 |
Correct |
16 ms |
10160 KB |
Output is correct |
24 |
Correct |
20 ms |
10160 KB |
Output is correct |
25 |
Correct |
20 ms |
10160 KB |
Output is correct |
26 |
Correct |
33 ms |
10376 KB |
Output is correct |
27 |
Correct |
267 ms |
10500 KB |
Output is correct |
28 |
Correct |
33 ms |
10500 KB |
Output is correct |
29 |
Correct |
388 ms |
10648 KB |
Output is correct |
30 |
Correct |
33 ms |
10648 KB |
Output is correct |
31 |
Correct |
253 ms |
10648 KB |
Output is correct |
32 |
Correct |
33 ms |
10656 KB |
Output is correct |
33 |
Correct |
254 ms |
10836 KB |
Output is correct |
34 |
Correct |
27 ms |
10836 KB |
Output is correct |
35 |
Correct |
246 ms |
10836 KB |
Output is correct |
36 |
Correct |
32 ms |
10972 KB |
Output is correct |
37 |
Correct |
244 ms |
10972 KB |
Output is correct |
38 |
Correct |
35 ms |
11068 KB |
Output is correct |
39 |
Correct |
339 ms |
11068 KB |
Output is correct |
40 |
Correct |
33 ms |
11164 KB |
Output is correct |
41 |
Correct |
251 ms |
11212 KB |
Output is correct |
42 |
Correct |
32 ms |
11212 KB |
Output is correct |
43 |
Correct |
256 ms |
11212 KB |
Output is correct |
44 |
Correct |
33 ms |
11232 KB |
Output is correct |
45 |
Correct |
263 ms |
11404 KB |
Output is correct |
46 |
Correct |
32 ms |
11404 KB |
Output is correct |
47 |
Correct |
9 ms |
11404 KB |
Output is correct |
48 |
Correct |
34 ms |
11420 KB |
Output is correct |
49 |
Correct |
33 ms |
11520 KB |
Output is correct |
50 |
Correct |
32 ms |
11636 KB |
Output is correct |
51 |
Correct |
31 ms |
11636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
12756 KB |
Output is correct |
2 |
Correct |
23 ms |
12756 KB |
Output is correct |
3 |
Correct |
29 ms |
13244 KB |
Output is correct |
4 |
Correct |
20 ms |
13244 KB |
Output is correct |
5 |
Correct |
28 ms |
13244 KB |
Output is correct |
6 |
Correct |
20 ms |
13244 KB |
Output is correct |
7 |
Correct |
28 ms |
13244 KB |
Output is correct |
8 |
Correct |
21 ms |
13244 KB |
Output is correct |
9 |
Correct |
30 ms |
13244 KB |
Output is correct |
10 |
Correct |
20 ms |
13244 KB |
Output is correct |
11 |
Correct |
40 ms |
13440 KB |
Output is correct |
12 |
Correct |
22 ms |
13440 KB |
Output is correct |
13 |
Correct |
28 ms |
13568 KB |
Output is correct |
14 |
Correct |
39 ms |
13568 KB |
Output is correct |
15 |
Correct |
21 ms |
13568 KB |
Output is correct |
16 |
Correct |
30 ms |
13568 KB |
Output is correct |
17 |
Correct |
22 ms |
13568 KB |
Output is correct |
18 |
Correct |
27 ms |
13568 KB |
Output is correct |
19 |
Correct |
24 ms |
13568 KB |
Output is correct |
20 |
Correct |
27 ms |
13568 KB |
Output is correct |
21 |
Correct |
31 ms |
13568 KB |
Output is correct |
22 |
Correct |
40 ms |
13568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
9720 KB |
Output is correct |
2 |
Correct |
20 ms |
9720 KB |
Output is correct |
3 |
Correct |
20 ms |
9736 KB |
Output is correct |
4 |
Correct |
24 ms |
9880 KB |
Output is correct |
5 |
Correct |
17 ms |
9940 KB |
Output is correct |
6 |
Correct |
17 ms |
9940 KB |
Output is correct |
7 |
Correct |
21 ms |
9940 KB |
Output is correct |
8 |
Correct |
19 ms |
9940 KB |
Output is correct |
9 |
Correct |
21 ms |
10032 KB |
Output is correct |
10 |
Correct |
18 ms |
10032 KB |
Output is correct |
11 |
Correct |
19 ms |
10048 KB |
Output is correct |
12 |
Correct |
18 ms |
10160 KB |
Output is correct |
13 |
Correct |
20 ms |
10160 KB |
Output is correct |
14 |
Correct |
20 ms |
10160 KB |
Output is correct |
15 |
Correct |
20 ms |
10160 KB |
Output is correct |
16 |
Correct |
20 ms |
10160 KB |
Output is correct |
17 |
Correct |
20 ms |
10160 KB |
Output is correct |
18 |
Correct |
16 ms |
10160 KB |
Output is correct |
19 |
Correct |
20 ms |
10160 KB |
Output is correct |
20 |
Correct |
16 ms |
10160 KB |
Output is correct |
21 |
Correct |
6 ms |
10160 KB |
Output is correct |
22 |
Correct |
15 ms |
10160 KB |
Output is correct |
23 |
Correct |
16 ms |
10160 KB |
Output is correct |
24 |
Correct |
20 ms |
10160 KB |
Output is correct |
25 |
Correct |
20 ms |
10160 KB |
Output is correct |
26 |
Correct |
33 ms |
10376 KB |
Output is correct |
27 |
Correct |
267 ms |
10500 KB |
Output is correct |
28 |
Correct |
33 ms |
10500 KB |
Output is correct |
29 |
Correct |
388 ms |
10648 KB |
Output is correct |
30 |
Correct |
33 ms |
10648 KB |
Output is correct |
31 |
Correct |
253 ms |
10648 KB |
Output is correct |
32 |
Correct |
33 ms |
10656 KB |
Output is correct |
33 |
Correct |
254 ms |
10836 KB |
Output is correct |
34 |
Correct |
27 ms |
10836 KB |
Output is correct |
35 |
Correct |
246 ms |
10836 KB |
Output is correct |
36 |
Correct |
32 ms |
10972 KB |
Output is correct |
37 |
Correct |
244 ms |
10972 KB |
Output is correct |
38 |
Correct |
35 ms |
11068 KB |
Output is correct |
39 |
Correct |
339 ms |
11068 KB |
Output is correct |
40 |
Correct |
33 ms |
11164 KB |
Output is correct |
41 |
Correct |
251 ms |
11212 KB |
Output is correct |
42 |
Correct |
32 ms |
11212 KB |
Output is correct |
43 |
Correct |
256 ms |
11212 KB |
Output is correct |
44 |
Correct |
33 ms |
11232 KB |
Output is correct |
45 |
Correct |
263 ms |
11404 KB |
Output is correct |
46 |
Correct |
32 ms |
11404 KB |
Output is correct |
47 |
Correct |
9 ms |
11404 KB |
Output is correct |
48 |
Correct |
34 ms |
11420 KB |
Output is correct |
49 |
Correct |
33 ms |
11520 KB |
Output is correct |
50 |
Correct |
32 ms |
11636 KB |
Output is correct |
51 |
Correct |
31 ms |
11636 KB |
Output is correct |
52 |
Correct |
41 ms |
12756 KB |
Output is correct |
53 |
Correct |
23 ms |
12756 KB |
Output is correct |
54 |
Correct |
29 ms |
13244 KB |
Output is correct |
55 |
Correct |
20 ms |
13244 KB |
Output is correct |
56 |
Correct |
28 ms |
13244 KB |
Output is correct |
57 |
Correct |
20 ms |
13244 KB |
Output is correct |
58 |
Correct |
28 ms |
13244 KB |
Output is correct |
59 |
Correct |
21 ms |
13244 KB |
Output is correct |
60 |
Correct |
30 ms |
13244 KB |
Output is correct |
61 |
Correct |
20 ms |
13244 KB |
Output is correct |
62 |
Correct |
40 ms |
13440 KB |
Output is correct |
63 |
Correct |
22 ms |
13440 KB |
Output is correct |
64 |
Correct |
28 ms |
13568 KB |
Output is correct |
65 |
Correct |
39 ms |
13568 KB |
Output is correct |
66 |
Correct |
21 ms |
13568 KB |
Output is correct |
67 |
Correct |
30 ms |
13568 KB |
Output is correct |
68 |
Correct |
22 ms |
13568 KB |
Output is correct |
69 |
Correct |
27 ms |
13568 KB |
Output is correct |
70 |
Correct |
24 ms |
13568 KB |
Output is correct |
71 |
Correct |
27 ms |
13568 KB |
Output is correct |
72 |
Correct |
31 ms |
13568 KB |
Output is correct |
73 |
Correct |
40 ms |
13568 KB |
Output is correct |
74 |
Correct |
93 ms |
16572 KB |
Output is correct |
75 |
Correct |
1357 ms |
16636 KB |
Output is correct |
76 |
Correct |
93 ms |
16636 KB |
Output is correct |
77 |
Correct |
1328 ms |
16636 KB |
Output is correct |
78 |
Correct |
91 ms |
16636 KB |
Output is correct |
79 |
Correct |
93 ms |
16640 KB |
Output is correct |
80 |
Correct |
1470 ms |
16640 KB |
Output is correct |
81 |
Correct |
97 ms |
16640 KB |
Output is correct |
82 |
Correct |
55 ms |
16640 KB |
Output is correct |
83 |
Correct |
101 ms |
16700 KB |
Output is correct |
84 |
Correct |
1459 ms |
16700 KB |
Output is correct |
85 |
Correct |
86 ms |
16700 KB |
Output is correct |
86 |
Correct |
1385 ms |
16700 KB |
Output is correct |
87 |
Correct |
86 ms |
16700 KB |
Output is correct |
88 |
Correct |
1318 ms |
16700 KB |
Output is correct |
89 |
Correct |
92 ms |
16700 KB |
Output is correct |
90 |
Correct |
1429 ms |
16700 KB |
Output is correct |
91 |
Correct |
88 ms |
16700 KB |
Output is correct |
92 |
Correct |
1331 ms |
16700 KB |
Output is correct |
93 |
Correct |
169 ms |
16700 KB |
Output is correct |
94 |
Correct |
63 ms |
16700 KB |
Output is correct |
95 |
Correct |
105 ms |
16700 KB |
Output is correct |
96 |
Correct |
92 ms |
16700 KB |
Output is correct |
97 |
Correct |
113 ms |
16700 KB |
Output is correct |
98 |
Correct |
91 ms |
16700 KB |
Output is correct |