#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 rec (int x) {
pair<int,int> p = *adj[x].begin();
c += p.second;
adj[x].clear();
assert(adj[p.first].count(make_pair(x, -p.second)));
adj[p.first].erase(adj[p.first].find(make_pair(x, -p.second)));
if (adj[p.first].size() == 1) {
rec(p.first);
}
if (adj[p.first].empty()) {
cout << "NO\n";
exit(0);
}
}
void apply () {
for (int i = 0; i < adj.size(); i++) {
if (adj[i].size() == 1) {
rec(i);
}
}
}
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;
tot.push_back(c);
for (int i = 0; i < n; i++) {
if (!hasVisited[i] && !adj[i].empty()) {
ans = 0;
assert(adj[i].size() == 2);
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(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:34: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]
34 | 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 |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 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 |
596 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 |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
2 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 |
612 KB |
Output is correct |
17 |
Correct |
2 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 |
0 ms |
596 KB |
Output is correct |
22 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 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 |
596 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 |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
2 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 |
612 KB |
Output is correct |
17 |
Correct |
2 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 |
0 ms |
596 KB |
Output is correct |
22 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
415 ms |
4732 KB |
Output is correct |
2 |
Correct |
17 ms |
5588 KB |
Output is correct |
3 |
Correct |
364 ms |
4820 KB |
Output is correct |
4 |
Correct |
16 ms |
5612 KB |
Output is correct |
5 |
Correct |
414 ms |
4732 KB |
Output is correct |
6 |
Correct |
16 ms |
5588 KB |
Output is correct |
7 |
Correct |
366 ms |
4692 KB |
Output is correct |
8 |
Correct |
19 ms |
5588 KB |
Output is correct |
9 |
Correct |
396 ms |
4732 KB |
Output is correct |
10 |
Correct |
15 ms |
5588 KB |
Output is correct |
11 |
Correct |
370 ms |
4732 KB |
Output is correct |
12 |
Correct |
18 ms |
5588 KB |
Output is correct |
13 |
Correct |
390 ms |
4724 KB |
Output is correct |
14 |
Correct |
414 ms |
4728 KB |
Output is correct |
15 |
Correct |
16 ms |
5588 KB |
Output is correct |
16 |
Correct |
362 ms |
4732 KB |
Output is correct |
17 |
Correct |
19 ms |
5588 KB |
Output is correct |
18 |
Correct |
372 ms |
4740 KB |
Output is correct |
19 |
Correct |
15 ms |
5588 KB |
Output is correct |
20 |
Correct |
368 ms |
4732 KB |
Output is correct |
21 |
Correct |
24 ms |
5332 KB |
Output is correct |
22 |
Incorrect |
18 ms |
5076 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 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 |
596 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 |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
2 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 |
612 KB |
Output is correct |
17 |
Correct |
2 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 |
0 ms |
596 KB |
Output is correct |
22 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |