# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
117149 |
2019-06-15T02:08:33 Z |
luciocf |
Tug of War (BOI15_tug) |
C++14 |
|
273 ms |
91640 KB |
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
const int maxn = 4e3+10;
typedef pair<int, int> pii;
int n, k;
int sa, sb;
int pai[maxn], edge[maxn];
int firstCycle[maxn], lastCycle[maxn], pesoCycle[2][maxn], inCycle[maxn];
int comp[maxn], back[maxn];
bool dp[maxn][20*maxn];
bool mark[maxn];
vector<int> V;
vector<pii> grafo[maxn];
void dfs(int u, int p, int cc)
{
comp[u] = cc;
for (auto pp: grafo[u])
{
int v = pp.ff, w = pp.ss;
if (comp[v])
{
if (v != p)
{
firstCycle[cc] = u, lastCycle[cc] = v;
back[cc] = w;
}
continue;
}
pai[v] = u, edge[v] = w;
dfs(v, u, cc);
}
}
void markCycle(int cc)
{
int u = firstCycle[cc], v = lastCycle[cc];
int s0 = 0, s1 = back[cc];
bool turn = 0;
while (true)
{
inCycle[v] = true;
if (!turn) s0 += edge[v];
else s1 += edge[v];
if (u == v) break;
v = pai[v];
turn = !turn;
}
pesoCycle[0][cc] = s0, pesoCycle[1][cc] = s1;
}
void dfs2(int u)
{
mark[u] = true;
for (auto pp: grafo[u])
{
int v = pp.ff, w = pp.ss;
if (!mark[v])
dfs2(v);
if (!inCycle[v])
{
if (v <= n) sa += w;
else sb += w;
}
}
}
int main(void)
{
memset(firstCycle, -1, sizeof firstCycle);
scanf("%d %d", &n, &k);
int S = 0;
for (int i = 1; i <= 2*n; i++)
{
int a, b, w;
scanf("%d %d %d", &a, &b, &w);
b += n;
S += w;
grafo[a].push_back({b, w});
grafo[b].push_back({a, w});
}
int cc = 0;
for (int i = 1; i <= 2*n; i++)
{
if (comp[i]) continue;
dfs(i, 0, ++cc);
if (firstCycle[cc] == -1)
{
printf("NO\n");
return 0;
}
}
if (n > 2000)
{
printf("YES\n");
return 0;
}
for (int i = 1; i <= cc; i++)
{
markCycle(i);
dfs2(firstCycle[i]);
}
dp[0][0] = 1;
for (int i = 1; i <= cc; i++)
{
for (int j = 1; j <= S; j++)
{
if (pesoCycle[0][i] <= j)
dp[i][j] = dp[i][j]|dp[i-1][j-pesoCycle[0][i]];
if (pesoCycle[1][i] <= j)
dp[i][j] = dp[i][j]|dp[i-1][j-pesoCycle[1][i]];
}
}
for (int i = 0; i <= S; i++)
{
if (dp[cc][i])
{
int a = i+sa, b = S-i-sa;
if (abs(a-b) <= k)
{
printf("YES\n");
return 0;
}
a = S-i-sb, b = i+sb;
if (abs(a-b) <= k)
{
printf("YES\n");
return 0;
}
}
}
printf("NO\n");
}
Compilation message
tug.cpp: In function 'int main()':
tug.cpp:99:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
tug.cpp:106:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &a, &b, &w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
512 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
512 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
229 ms |
90616 KB |
Output is correct |
27 |
Correct |
35 ms |
12792 KB |
Output is correct |
28 |
Correct |
227 ms |
89912 KB |
Output is correct |
29 |
Correct |
35 ms |
12664 KB |
Output is correct |
30 |
Correct |
236 ms |
90160 KB |
Output is correct |
31 |
Correct |
36 ms |
12664 KB |
Output is correct |
32 |
Correct |
239 ms |
91364 KB |
Output is correct |
33 |
Correct |
37 ms |
12664 KB |
Output is correct |
34 |
Correct |
20 ms |
6912 KB |
Output is correct |
35 |
Correct |
34 ms |
12664 KB |
Output is correct |
36 |
Correct |
230 ms |
91128 KB |
Output is correct |
37 |
Correct |
35 ms |
12664 KB |
Output is correct |
38 |
Correct |
233 ms |
91640 KB |
Output is correct |
39 |
Correct |
36 ms |
12664 KB |
Output is correct |
40 |
Correct |
234 ms |
90760 KB |
Output is correct |
41 |
Correct |
77 ms |
12664 KB |
Output is correct |
42 |
Correct |
273 ms |
89880 KB |
Output is correct |
43 |
Correct |
37 ms |
12716 KB |
Output is correct |
44 |
Correct |
237 ms |
90136 KB |
Output is correct |
45 |
Correct |
35 ms |
12664 KB |
Output is correct |
46 |
Correct |
249 ms |
91128 KB |
Output is correct |
47 |
Correct |
4 ms |
640 KB |
Output is correct |
48 |
Correct |
101 ms |
37344 KB |
Output is correct |
49 |
Incorrect |
102 ms |
37368 KB |
Output isn't correct |
50 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
18 ms |
2688 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
512 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
229 ms |
90616 KB |
Output is correct |
27 |
Correct |
35 ms |
12792 KB |
Output is correct |
28 |
Correct |
227 ms |
89912 KB |
Output is correct |
29 |
Correct |
35 ms |
12664 KB |
Output is correct |
30 |
Correct |
236 ms |
90160 KB |
Output is correct |
31 |
Correct |
36 ms |
12664 KB |
Output is correct |
32 |
Correct |
239 ms |
91364 KB |
Output is correct |
33 |
Correct |
37 ms |
12664 KB |
Output is correct |
34 |
Correct |
20 ms |
6912 KB |
Output is correct |
35 |
Correct |
34 ms |
12664 KB |
Output is correct |
36 |
Correct |
230 ms |
91128 KB |
Output is correct |
37 |
Correct |
35 ms |
12664 KB |
Output is correct |
38 |
Correct |
233 ms |
91640 KB |
Output is correct |
39 |
Correct |
36 ms |
12664 KB |
Output is correct |
40 |
Correct |
234 ms |
90760 KB |
Output is correct |
41 |
Correct |
77 ms |
12664 KB |
Output is correct |
42 |
Correct |
273 ms |
89880 KB |
Output is correct |
43 |
Correct |
37 ms |
12716 KB |
Output is correct |
44 |
Correct |
237 ms |
90136 KB |
Output is correct |
45 |
Correct |
35 ms |
12664 KB |
Output is correct |
46 |
Correct |
249 ms |
91128 KB |
Output is correct |
47 |
Correct |
4 ms |
640 KB |
Output is correct |
48 |
Correct |
101 ms |
37344 KB |
Output is correct |
49 |
Incorrect |
102 ms |
37368 KB |
Output isn't correct |
50 |
Halted |
0 ms |
0 KB |
- |