#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <bitset>
#include <vector>
#include <set>
using namespace std;
const int maxN = 30'000;
struct Jmp
{
int to, wei;
operator int&() { return to; }
};
vector<Jmp> jmp[2 * maxN];
Jmp par[2 * maxN];
bool used[2 * maxN], cyc[2 * maxN];
pair<int, Jmp> DFS(int v, int& nodes, int& edges)
{
static unordered_set<int> st;
st.insert(v);
++nodes;
used[v] = true;
pair<int, Jmp> ret = {-1, {-1, -1}};
edges += jmp[v].size();
for (int i = 0; i < jmp[v].size(); ++i)
{
Jmp to = jmp[v][i];
if (!used[to])
{
par[to] = {v, to.wei};
pair<int, Jmp> x = DFS(to, nodes, edges);
if (x.first != -1)
ret = x;
if (i + 1 < jmp[v].size() && jmp[v][i + 1] == to)
ret = {to, {v, jmp[v][i + 1].wei}};
}
else if (to != par[v] && st.count(to))
ret = {v, to};
}
st.erase(v);
return ret;
}
void SolveSub(int v, int p, int& ans)
{
for (Jmp to: jmp[v])
if (to != p && !cyc[to])
{
ans += to.wei * (to % 2 ? 1 : -1);
SolveSub(to, v, ans);
}
}
pair<int, int> Solve(int v)
{
int nodes = 0, edges = 0;
par[v] = {-1, -1};
pair<int, Jmp> p = DFS(v, nodes, edges);
edges /= 2;
if (edges != nodes)
{
cout << "NO";
exit(0);
}
pair<int, int> ans;
for (int u = p.first; u != p.second; u = par[u])
{
ans.first += par[u].wei * (u % 2 ? 1 : -1);
ans.second += par[u].wei * (par[u] % 2 ? 1 : -1);
}
ans.first += p.second.wei * (p.second % 2 ? 1 : -1);
ans.second += p.second.wei * (p.first % 2 ? 1 : -1);
vector<int> cycle;
while (true)
{
cycle.push_back(p.first);
if (p.first == p.second) break;
p.first = par[p.first];
}
for (int u: cycle)
cyc[u] = true;
int ans0 = 0;
for (int u: cycle)
SolveSub(u, -1, ans0);
ans.second += ans0;
ans.first += ans0;
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < 2 * n; ++i)
{
int l, r, s;
cin >> l >> r >> s;
--l; --r;
jmp[2 * l].push_back({2 * r + 1, s});
jmp[2 * r + 1].push_back({2 * l, s});
#ifdef MANSON
cout << "(" << 2 * l << ", " << 2 * r + 1 << ": " << s << ")\n";
#endif
}
for (int i = 0; i < 2 * n; ++i)
sort(jmp[i].begin(), jmp[i].end());
vector<pair<int, int>> a;
for (int i = 0; i < 2 * n; ++i)
if (!used[i])
{
a.push_back(Solve(i));
#ifdef MANSON
cerr << a.back().first << ", " << a.back().second << '\n';
#endif
}
bitset<2 * 20 * maxN + 1> dp;
dp.set(20 * n);
for (int i = 0; i < a.size(); ++i)
{
int x = a[i].first, y = a[i].second;
dp = (x > 0 ? dp << x : dp >> -x) | (y > 0 ? dp << y : dp >> -y);
}
int ans = 20 * n;
for (int i = 0; i <= 2 * 20 * n; ++i)
if (dp[i])
ans = min(ans, abs(20 * n - i));
cout << (ans <= k ? "YES" : "NO");
}
Compilation message
tug.cpp: In function 'std::pair<int, Jmp> DFS(int, int&, int&)':
tug.cpp:27:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < jmp[v].size(); ++i)
~~^~~~~~~~~~~~~~~
tug.cpp:36:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i + 1 < jmp[v].size() && jmp[v][i + 1] == to)
~~~~~~^~~~~~~~~~~~~~~
tug.cpp: In function 'int main()':
tug.cpp:119:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a.size(); ++i)
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2432 KB |
Output is correct |
2 |
Correct |
7 ms |
2432 KB |
Output is correct |
3 |
Correct |
7 ms |
2560 KB |
Output is correct |
4 |
Correct |
7 ms |
2560 KB |
Output is correct |
5 |
Correct |
7 ms |
2560 KB |
Output is correct |
6 |
Correct |
7 ms |
2432 KB |
Output is correct |
7 |
Correct |
7 ms |
2432 KB |
Output is correct |
8 |
Correct |
7 ms |
2560 KB |
Output is correct |
9 |
Correct |
7 ms |
2560 KB |
Output is correct |
10 |
Correct |
7 ms |
2432 KB |
Output is correct |
11 |
Correct |
7 ms |
2432 KB |
Output is correct |
12 |
Correct |
7 ms |
2432 KB |
Output is correct |
13 |
Correct |
7 ms |
2432 KB |
Output is correct |
14 |
Correct |
7 ms |
2432 KB |
Output is correct |
15 |
Correct |
7 ms |
2560 KB |
Output is correct |
16 |
Correct |
7 ms |
2432 KB |
Output is correct |
17 |
Correct |
7 ms |
2432 KB |
Output is correct |
18 |
Correct |
7 ms |
2432 KB |
Output is correct |
19 |
Correct |
7 ms |
2432 KB |
Output is correct |
20 |
Correct |
7 ms |
2432 KB |
Output is correct |
21 |
Correct |
5 ms |
1792 KB |
Output is correct |
22 |
Correct |
6 ms |
2432 KB |
Output is correct |
23 |
Correct |
7 ms |
2432 KB |
Output is correct |
24 |
Correct |
7 ms |
2432 KB |
Output is correct |
25 |
Correct |
7 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2432 KB |
Output is correct |
2 |
Correct |
7 ms |
2432 KB |
Output is correct |
3 |
Correct |
7 ms |
2560 KB |
Output is correct |
4 |
Correct |
7 ms |
2560 KB |
Output is correct |
5 |
Correct |
7 ms |
2560 KB |
Output is correct |
6 |
Correct |
7 ms |
2432 KB |
Output is correct |
7 |
Correct |
7 ms |
2432 KB |
Output is correct |
8 |
Correct |
7 ms |
2560 KB |
Output is correct |
9 |
Correct |
7 ms |
2560 KB |
Output is correct |
10 |
Correct |
7 ms |
2432 KB |
Output is correct |
11 |
Correct |
7 ms |
2432 KB |
Output is correct |
12 |
Correct |
7 ms |
2432 KB |
Output is correct |
13 |
Correct |
7 ms |
2432 KB |
Output is correct |
14 |
Correct |
7 ms |
2432 KB |
Output is correct |
15 |
Correct |
7 ms |
2560 KB |
Output is correct |
16 |
Correct |
7 ms |
2432 KB |
Output is correct |
17 |
Correct |
7 ms |
2432 KB |
Output is correct |
18 |
Correct |
7 ms |
2432 KB |
Output is correct |
19 |
Correct |
7 ms |
2432 KB |
Output is correct |
20 |
Correct |
7 ms |
2432 KB |
Output is correct |
21 |
Correct |
5 ms |
1792 KB |
Output is correct |
22 |
Correct |
6 ms |
2432 KB |
Output is correct |
23 |
Correct |
7 ms |
2432 KB |
Output is correct |
24 |
Correct |
7 ms |
2432 KB |
Output is correct |
25 |
Correct |
7 ms |
2432 KB |
Output is correct |
26 |
Correct |
202 ms |
2808 KB |
Output is correct |
27 |
Correct |
34 ms |
2688 KB |
Output is correct |
28 |
Correct |
211 ms |
2688 KB |
Output is correct |
29 |
Correct |
35 ms |
2688 KB |
Output is correct |
30 |
Correct |
208 ms |
2808 KB |
Output is correct |
31 |
Correct |
34 ms |
2688 KB |
Output is correct |
32 |
Correct |
204 ms |
2772 KB |
Output is correct |
33 |
Correct |
34 ms |
2688 KB |
Output is correct |
34 |
Correct |
21 ms |
2688 KB |
Output is correct |
35 |
Correct |
35 ms |
2688 KB |
Output is correct |
36 |
Correct |
201 ms |
2812 KB |
Output is correct |
37 |
Correct |
34 ms |
2688 KB |
Output is correct |
38 |
Correct |
205 ms |
2808 KB |
Output is correct |
39 |
Correct |
34 ms |
2688 KB |
Output is correct |
40 |
Correct |
203 ms |
2808 KB |
Output is correct |
41 |
Correct |
35 ms |
2688 KB |
Output is correct |
42 |
Correct |
201 ms |
2688 KB |
Output is correct |
43 |
Correct |
34 ms |
2688 KB |
Output is correct |
44 |
Correct |
206 ms |
2688 KB |
Output is correct |
45 |
Correct |
35 ms |
2688 KB |
Output is correct |
46 |
Correct |
208 ms |
2788 KB |
Output is correct |
47 |
Correct |
7 ms |
2048 KB |
Output is correct |
48 |
Correct |
106 ms |
2688 KB |
Output is correct |
49 |
Correct |
108 ms |
2808 KB |
Output is correct |
50 |
Correct |
209 ms |
2688 KB |
Output is correct |
51 |
Correct |
203 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
650 ms |
3752 KB |
Output is correct |
2 |
Correct |
14 ms |
2816 KB |
Output is correct |
3 |
Correct |
696 ms |
3724 KB |
Output is correct |
4 |
Correct |
14 ms |
2688 KB |
Output is correct |
5 |
Correct |
661 ms |
3728 KB |
Output is correct |
6 |
Correct |
14 ms |
2816 KB |
Output is correct |
7 |
Correct |
654 ms |
3752 KB |
Output is correct |
8 |
Correct |
14 ms |
2816 KB |
Output is correct |
9 |
Correct |
657 ms |
3752 KB |
Output is correct |
10 |
Correct |
14 ms |
2816 KB |
Output is correct |
11 |
Correct |
667 ms |
3752 KB |
Output is correct |
12 |
Correct |
14 ms |
2816 KB |
Output is correct |
13 |
Correct |
654 ms |
3752 KB |
Output is correct |
14 |
Correct |
640 ms |
3840 KB |
Output is correct |
15 |
Correct |
14 ms |
2816 KB |
Output is correct |
16 |
Correct |
662 ms |
3724 KB |
Output is correct |
17 |
Correct |
15 ms |
2816 KB |
Output is correct |
18 |
Correct |
661 ms |
3724 KB |
Output is correct |
19 |
Correct |
14 ms |
2816 KB |
Output is correct |
20 |
Correct |
661 ms |
3752 KB |
Output is correct |
21 |
Correct |
16 ms |
3328 KB |
Output is correct |
22 |
Correct |
431 ms |
3832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2432 KB |
Output is correct |
2 |
Correct |
7 ms |
2432 KB |
Output is correct |
3 |
Correct |
7 ms |
2560 KB |
Output is correct |
4 |
Correct |
7 ms |
2560 KB |
Output is correct |
5 |
Correct |
7 ms |
2560 KB |
Output is correct |
6 |
Correct |
7 ms |
2432 KB |
Output is correct |
7 |
Correct |
7 ms |
2432 KB |
Output is correct |
8 |
Correct |
7 ms |
2560 KB |
Output is correct |
9 |
Correct |
7 ms |
2560 KB |
Output is correct |
10 |
Correct |
7 ms |
2432 KB |
Output is correct |
11 |
Correct |
7 ms |
2432 KB |
Output is correct |
12 |
Correct |
7 ms |
2432 KB |
Output is correct |
13 |
Correct |
7 ms |
2432 KB |
Output is correct |
14 |
Correct |
7 ms |
2432 KB |
Output is correct |
15 |
Correct |
7 ms |
2560 KB |
Output is correct |
16 |
Correct |
7 ms |
2432 KB |
Output is correct |
17 |
Correct |
7 ms |
2432 KB |
Output is correct |
18 |
Correct |
7 ms |
2432 KB |
Output is correct |
19 |
Correct |
7 ms |
2432 KB |
Output is correct |
20 |
Correct |
7 ms |
2432 KB |
Output is correct |
21 |
Correct |
5 ms |
1792 KB |
Output is correct |
22 |
Correct |
6 ms |
2432 KB |
Output is correct |
23 |
Correct |
7 ms |
2432 KB |
Output is correct |
24 |
Correct |
7 ms |
2432 KB |
Output is correct |
25 |
Correct |
7 ms |
2432 KB |
Output is correct |
26 |
Correct |
202 ms |
2808 KB |
Output is correct |
27 |
Correct |
34 ms |
2688 KB |
Output is correct |
28 |
Correct |
211 ms |
2688 KB |
Output is correct |
29 |
Correct |
35 ms |
2688 KB |
Output is correct |
30 |
Correct |
208 ms |
2808 KB |
Output is correct |
31 |
Correct |
34 ms |
2688 KB |
Output is correct |
32 |
Correct |
204 ms |
2772 KB |
Output is correct |
33 |
Correct |
34 ms |
2688 KB |
Output is correct |
34 |
Correct |
21 ms |
2688 KB |
Output is correct |
35 |
Correct |
35 ms |
2688 KB |
Output is correct |
36 |
Correct |
201 ms |
2812 KB |
Output is correct |
37 |
Correct |
34 ms |
2688 KB |
Output is correct |
38 |
Correct |
205 ms |
2808 KB |
Output is correct |
39 |
Correct |
34 ms |
2688 KB |
Output is correct |
40 |
Correct |
203 ms |
2808 KB |
Output is correct |
41 |
Correct |
35 ms |
2688 KB |
Output is correct |
42 |
Correct |
201 ms |
2688 KB |
Output is correct |
43 |
Correct |
34 ms |
2688 KB |
Output is correct |
44 |
Correct |
206 ms |
2688 KB |
Output is correct |
45 |
Correct |
35 ms |
2688 KB |
Output is correct |
46 |
Correct |
208 ms |
2788 KB |
Output is correct |
47 |
Correct |
7 ms |
2048 KB |
Output is correct |
48 |
Correct |
106 ms |
2688 KB |
Output is correct |
49 |
Correct |
108 ms |
2808 KB |
Output is correct |
50 |
Correct |
209 ms |
2688 KB |
Output is correct |
51 |
Correct |
203 ms |
2688 KB |
Output is correct |
52 |
Correct |
650 ms |
3752 KB |
Output is correct |
53 |
Correct |
14 ms |
2816 KB |
Output is correct |
54 |
Correct |
696 ms |
3724 KB |
Output is correct |
55 |
Correct |
14 ms |
2688 KB |
Output is correct |
56 |
Correct |
661 ms |
3728 KB |
Output is correct |
57 |
Correct |
14 ms |
2816 KB |
Output is correct |
58 |
Correct |
654 ms |
3752 KB |
Output is correct |
59 |
Correct |
14 ms |
2816 KB |
Output is correct |
60 |
Correct |
657 ms |
3752 KB |
Output is correct |
61 |
Correct |
14 ms |
2816 KB |
Output is correct |
62 |
Correct |
667 ms |
3752 KB |
Output is correct |
63 |
Correct |
14 ms |
2816 KB |
Output is correct |
64 |
Correct |
654 ms |
3752 KB |
Output is correct |
65 |
Correct |
640 ms |
3840 KB |
Output is correct |
66 |
Correct |
14 ms |
2816 KB |
Output is correct |
67 |
Correct |
662 ms |
3724 KB |
Output is correct |
68 |
Correct |
15 ms |
2816 KB |
Output is correct |
69 |
Correct |
661 ms |
3724 KB |
Output is correct |
70 |
Correct |
14 ms |
2816 KB |
Output is correct |
71 |
Correct |
661 ms |
3752 KB |
Output is correct |
72 |
Correct |
16 ms |
3328 KB |
Output is correct |
73 |
Correct |
431 ms |
3832 KB |
Output is correct |
74 |
Correct |
2948 ms |
6136 KB |
Output is correct |
75 |
Correct |
166 ms |
5756 KB |
Output is correct |
76 |
Correct |
2960 ms |
6136 KB |
Output is correct |
77 |
Correct |
156 ms |
5752 KB |
Output is correct |
78 |
Correct |
2891 ms |
6160 KB |
Output is correct |
79 |
Correct |
2864 ms |
6140 KB |
Output is correct |
80 |
Correct |
163 ms |
5880 KB |
Output is correct |
81 |
Correct |
159 ms |
5880 KB |
Output is correct |
82 |
Correct |
2891 ms |
6136 KB |
Output is correct |
83 |
Correct |
2857 ms |
6136 KB |
Output is correct |
84 |
Correct |
146 ms |
5752 KB |
Output is correct |
85 |
Correct |
2921 ms |
6136 KB |
Output is correct |
86 |
Correct |
152 ms |
5752 KB |
Output is correct |
87 |
Correct |
2843 ms |
6136 KB |
Output is correct |
88 |
Correct |
149 ms |
6008 KB |
Output is correct |
89 |
Correct |
2869 ms |
6136 KB |
Output is correct |
90 |
Correct |
151 ms |
5752 KB |
Output is correct |
91 |
Correct |
2934 ms |
6136 KB |
Output is correct |
92 |
Correct |
149 ms |
5752 KB |
Output is correct |
93 |
Correct |
2922 ms |
6136 KB |
Output is correct |
94 |
Correct |
38 ms |
4728 KB |
Output is correct |
95 |
Correct |
1541 ms |
6520 KB |
Output is correct |
96 |
Correct |
1474 ms |
6272 KB |
Output is correct |
97 |
Correct |
2910 ms |
6136 KB |
Output is correct |
98 |
Correct |
2960 ms |
6136 KB |
Output is correct |