#include <bits/stdc++.h>
using namespace std;
vector<multiset<pair<int,int> > > adj;
vector<bool> hasVisited;
int ans = 0;
void dfs (int curNode, int prevNode) {
if (hasVisited[curNode]) {
return;
}
hasVisited[curNode] = true;
for (pair<int,int> p: adj[curNode]) {
if (p.first != prevNode) {
ans += p.second;
dfs (p.first, curNode);
}
}
}
int c = 0;
void apply () {
queue<int> q;
for (int i = 0; i < adj.size(); i++) {
if (adj[i].size() == 1) {
q.push(i);
}
}
while (!q.empty()) {
int x = q.front();
q.pop();
pair<int,int> p = *adj[x].begin();
c += p.second;
adj[x].clear();
adj[p.first].erase(adj[p.first].find(make_pair(x, -p.second)));
if (adj[p.first].size() == 1) {
q.push(p.first);
}
if (adj[p.first].empty()) {
cout << "NO\n";
exit(0);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
adj.resize(2 * n);
hasVisited.assign(2 * n, false);
map<pair<int,int>,vector<int> > myMap;
for (int i = 0; i < 2 * n; i++) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
v += n;
myMap[make_pair(u, v)].push_back(w);
adj[u].insert(make_pair(v, w));
adj[v].insert(make_pair(u, -w));
}
for (int i = 0; i < 2 * n; i++) {
if (adj[i].empty()) {
cout << "NO\n";
exit(0);
}
}
apply();
vector<int> tot;
for (int i = 0; i < n; i++) {
if (!hasVisited[i] && !adj[i].empty()) {
ans = 0;
dfs (i, (*adj[i].rbegin()).first);
tot.push_back(abs(ans));
}
}
for (auto& p: myMap) {
if (p.second.size() <= 1) {
continue;
}
int a = 0;
for (int j = 0; j < p.second.size(); j++) {
if (j % 2) a += p.second[j];
else a -= p.second[j];
}
tot.push_back(abs(a));
}
bitset<600000> bs;
bs.set(c + 300000);
for (int j: tot) {
bs = (bs >> j) | (bs << j);
}
for (int i = -k; i <= k; i++) {
if (bs[300000 + i]) {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
}
Compilation message
tug.cpp: In function 'void apply()':
tug.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::multiset<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < adj.size(); i++) {
| ~~^~~~~~~~~~~~
tug.cpp: In function 'int main()':
tug.cpp:79:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int j = 0; j < p.second.size(); j++) {
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
544 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
608 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
544 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
608 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
720 KB |
Output is correct |
26 |
Correct |
116 ms |
1364 KB |
Output is correct |
27 |
Correct |
13 ms |
1620 KB |
Output is correct |
28 |
Correct |
130 ms |
1424 KB |
Output is correct |
29 |
Correct |
15 ms |
1620 KB |
Output is correct |
30 |
Correct |
163 ms |
1420 KB |
Output is correct |
31 |
Correct |
13 ms |
1620 KB |
Output is correct |
32 |
Correct |
117 ms |
1388 KB |
Output is correct |
33 |
Correct |
15 ms |
1620 KB |
Output is correct |
34 |
Correct |
9 ms |
1492 KB |
Output is correct |
35 |
Correct |
18 ms |
1620 KB |
Output is correct |
36 |
Correct |
124 ms |
1416 KB |
Output is correct |
37 |
Correct |
15 ms |
1620 KB |
Output is correct |
38 |
Correct |
123 ms |
1364 KB |
Output is correct |
39 |
Correct |
14 ms |
1492 KB |
Output is correct |
40 |
Correct |
112 ms |
1424 KB |
Output is correct |
41 |
Correct |
15 ms |
1620 KB |
Output is correct |
42 |
Correct |
140 ms |
1364 KB |
Output is correct |
43 |
Correct |
15 ms |
1620 KB |
Output is correct |
44 |
Correct |
112 ms |
1364 KB |
Output is correct |
45 |
Correct |
15 ms |
1620 KB |
Output is correct |
46 |
Correct |
122 ms |
1416 KB |
Output is correct |
47 |
Correct |
3 ms |
1492 KB |
Output is correct |
48 |
Correct |
64 ms |
1412 KB |
Output is correct |
49 |
Correct |
68 ms |
1492 KB |
Output is correct |
50 |
Correct |
119 ms |
1428 KB |
Output is correct |
51 |
Correct |
118 ms |
1416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
414 ms |
4740 KB |
Output is correct |
2 |
Correct |
19 ms |
5624 KB |
Output is correct |
3 |
Correct |
485 ms |
4732 KB |
Output is correct |
4 |
Correct |
17 ms |
5588 KB |
Output is correct |
5 |
Correct |
428 ms |
4736 KB |
Output is correct |
6 |
Correct |
19 ms |
5560 KB |
Output is correct |
7 |
Correct |
451 ms |
4724 KB |
Output is correct |
8 |
Correct |
14 ms |
5588 KB |
Output is correct |
9 |
Correct |
494 ms |
4736 KB |
Output is correct |
10 |
Correct |
14 ms |
5588 KB |
Output is correct |
11 |
Correct |
407 ms |
4728 KB |
Output is correct |
12 |
Correct |
18 ms |
5588 KB |
Output is correct |
13 |
Correct |
423 ms |
4720 KB |
Output is correct |
14 |
Correct |
437 ms |
4732 KB |
Output is correct |
15 |
Correct |
15 ms |
5588 KB |
Output is correct |
16 |
Correct |
412 ms |
4736 KB |
Output is correct |
17 |
Correct |
18 ms |
5600 KB |
Output is correct |
18 |
Correct |
411 ms |
4848 KB |
Output is correct |
19 |
Correct |
17 ms |
5588 KB |
Output is correct |
20 |
Correct |
476 ms |
4748 KB |
Output is correct |
21 |
Correct |
15 ms |
5332 KB |
Output is correct |
22 |
Correct |
225 ms |
5180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
544 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
608 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
720 KB |
Output is correct |
26 |
Correct |
116 ms |
1364 KB |
Output is correct |
27 |
Correct |
13 ms |
1620 KB |
Output is correct |
28 |
Correct |
130 ms |
1424 KB |
Output is correct |
29 |
Correct |
15 ms |
1620 KB |
Output is correct |
30 |
Correct |
163 ms |
1420 KB |
Output is correct |
31 |
Correct |
13 ms |
1620 KB |
Output is correct |
32 |
Correct |
117 ms |
1388 KB |
Output is correct |
33 |
Correct |
15 ms |
1620 KB |
Output is correct |
34 |
Correct |
9 ms |
1492 KB |
Output is correct |
35 |
Correct |
18 ms |
1620 KB |
Output is correct |
36 |
Correct |
124 ms |
1416 KB |
Output is correct |
37 |
Correct |
15 ms |
1620 KB |
Output is correct |
38 |
Correct |
123 ms |
1364 KB |
Output is correct |
39 |
Correct |
14 ms |
1492 KB |
Output is correct |
40 |
Correct |
112 ms |
1424 KB |
Output is correct |
41 |
Correct |
15 ms |
1620 KB |
Output is correct |
42 |
Correct |
140 ms |
1364 KB |
Output is correct |
43 |
Correct |
15 ms |
1620 KB |
Output is correct |
44 |
Correct |
112 ms |
1364 KB |
Output is correct |
45 |
Correct |
15 ms |
1620 KB |
Output is correct |
46 |
Correct |
122 ms |
1416 KB |
Output is correct |
47 |
Correct |
3 ms |
1492 KB |
Output is correct |
48 |
Correct |
64 ms |
1412 KB |
Output is correct |
49 |
Correct |
68 ms |
1492 KB |
Output is correct |
50 |
Correct |
119 ms |
1428 KB |
Output is correct |
51 |
Correct |
118 ms |
1416 KB |
Output is correct |
52 |
Correct |
414 ms |
4740 KB |
Output is correct |
53 |
Correct |
19 ms |
5624 KB |
Output is correct |
54 |
Correct |
485 ms |
4732 KB |
Output is correct |
55 |
Correct |
17 ms |
5588 KB |
Output is correct |
56 |
Correct |
428 ms |
4736 KB |
Output is correct |
57 |
Correct |
19 ms |
5560 KB |
Output is correct |
58 |
Correct |
451 ms |
4724 KB |
Output is correct |
59 |
Correct |
14 ms |
5588 KB |
Output is correct |
60 |
Correct |
494 ms |
4736 KB |
Output is correct |
61 |
Correct |
14 ms |
5588 KB |
Output is correct |
62 |
Correct |
407 ms |
4728 KB |
Output is correct |
63 |
Correct |
18 ms |
5588 KB |
Output is correct |
64 |
Correct |
423 ms |
4720 KB |
Output is correct |
65 |
Correct |
437 ms |
4732 KB |
Output is correct |
66 |
Correct |
15 ms |
5588 KB |
Output is correct |
67 |
Correct |
412 ms |
4736 KB |
Output is correct |
68 |
Correct |
18 ms |
5600 KB |
Output is correct |
69 |
Correct |
411 ms |
4848 KB |
Output is correct |
70 |
Correct |
17 ms |
5588 KB |
Output is correct |
71 |
Correct |
476 ms |
4748 KB |
Output is correct |
72 |
Correct |
15 ms |
5332 KB |
Output is correct |
73 |
Correct |
225 ms |
5180 KB |
Output is correct |
74 |
Correct |
1792 ms |
12728 KB |
Output is correct |
75 |
Correct |
157 ms |
15632 KB |
Output is correct |
76 |
Correct |
1864 ms |
12712 KB |
Output is correct |
77 |
Correct |
133 ms |
15560 KB |
Output is correct |
78 |
Correct |
1826 ms |
12680 KB |
Output is correct |
79 |
Correct |
1836 ms |
12620 KB |
Output is correct |
80 |
Correct |
149 ms |
15564 KB |
Output is correct |
81 |
Correct |
154 ms |
15576 KB |
Output is correct |
82 |
Correct |
1743 ms |
12724 KB |
Output is correct |
83 |
Correct |
1764 ms |
12648 KB |
Output is correct |
84 |
Correct |
147 ms |
15564 KB |
Output is correct |
85 |
Correct |
1852 ms |
12708 KB |
Output is correct |
86 |
Correct |
141 ms |
15656 KB |
Output is correct |
87 |
Correct |
1801 ms |
12620 KB |
Output is correct |
88 |
Correct |
143 ms |
15644 KB |
Output is correct |
89 |
Correct |
1826 ms |
12728 KB |
Output is correct |
90 |
Correct |
130 ms |
15576 KB |
Output is correct |
91 |
Correct |
1776 ms |
12728 KB |
Output is correct |
92 |
Correct |
140 ms |
15624 KB |
Output is correct |
93 |
Correct |
1736 ms |
12624 KB |
Output is correct |
94 |
Correct |
85 ms |
14796 KB |
Output is correct |
95 |
Correct |
917 ms |
14412 KB |
Output is correct |
96 |
Correct |
1024 ms |
14264 KB |
Output is correct |
97 |
Correct |
1920 ms |
12676 KB |
Output is correct |
98 |
Correct |
2050 ms |
12724 KB |
Output is correct |