#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const int inf = 0x3f3f3f3f;
const ll linf = 123456789012345678;
const ll mod1 = 1000000007;
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl
#define sz(x) ((int)(x).size())
int n, k;
vector<vpi> adj;
vi visited;
vi starts;
int constup = 0, constdown = 0;
bool findcycle(int node, int parent, bool found){
if(visited[node]){
if(!found) starts.push_back(node);
return true;
}
visited[node] = 1;
for(auto x : adj[node]){
if(x.first != parent)
found |= findcycle(x.first, node, found);
}
return found;
}
pair<bool, pi> dfs(int node, int parent){
visited[node] = 1;
int sumup = 0;
int sumdown = 0;
bool toparent = false;
bool cycle = false;
for(auto x : adj[node]){
if(visited[x.first]){
if(parent == -1){
if(node >= n) sumup += x.second;
else sumdown += x.second;
}
if(parent != x.first) cycle = true;
else if(toparent) cycle = true;
else toparent = true;
continue;
}
pair<bool, pi> r = dfs(x.first, node);
if(!r.first){
if(node < n) constup += x.second;
else constdown += x.second;
continue;
}
cycle = true;
sumup += r.second.first;
sumdown += r.second.second;
if(node < n) sumup += x.second;
else sumdown += x.second;
}
return {cycle, {sumup, sumdown}};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
adj = vector<vpi>(2*n);
int strengthsum = 0;
for(int i = 0; i < 2*n; i++){
int l, r, s;
cin >> l >> r >> s;
l--,r--;
r+=n;
adj[l].emplace_back(r, s);
adj[r].emplace_back(l, s);
strengthsum += s;
}
visited = vi(2*n);
for(int i = 0; i < 2*n; i++){
if(!visited[i] && !findcycle(i, -1, false)){
cout << "NO\n";
return 0;
}
}
visited = vi(2*n);
int diff = 0;
vi alts;
for(int x : starts){
pair<bool, pi> r = dfs(x, -1);
alts.push_back(2*(r.second.second - r.second.first));
diff += r.second.first - r.second.second;
}
diff += constup-constdown;
int from = -k-diff;
int to = k-diff;
gp_hash_table<int, int> p;
p[0] = 1;
for(int i = 0; i < sz(alts); i++){
vi a;
for(auto x : p){
a.push_back(x.first);
}
for(int x : a){
p[x+alts[i]] = 1;
}
}
bool ans = false;
for(auto x : p){
if(x.first >= from && x.first <= to) ans = true;
}
if(ans) cout << "YES\n";
else cout << "NO\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
380 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
380 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
172 ms |
1476 KB |
Output is correct |
27 |
Correct |
79 ms |
3244 KB |
Output is correct |
28 |
Correct |
170 ms |
1348 KB |
Output is correct |
29 |
Correct |
80 ms |
3324 KB |
Output is correct |
30 |
Correct |
173 ms |
1348 KB |
Output is correct |
31 |
Correct |
76 ms |
3192 KB |
Output is correct |
32 |
Correct |
172 ms |
1348 KB |
Output is correct |
33 |
Correct |
77 ms |
3200 KB |
Output is correct |
34 |
Correct |
21 ms |
1976 KB |
Output is correct |
35 |
Correct |
78 ms |
3156 KB |
Output is correct |
36 |
Correct |
174 ms |
1348 KB |
Output is correct |
37 |
Correct |
77 ms |
3196 KB |
Output is correct |
38 |
Correct |
176 ms |
1396 KB |
Output is correct |
39 |
Correct |
77 ms |
3196 KB |
Output is correct |
40 |
Correct |
189 ms |
1348 KB |
Output is correct |
41 |
Correct |
78 ms |
3196 KB |
Output is correct |
42 |
Correct |
176 ms |
1320 KB |
Output is correct |
43 |
Correct |
81 ms |
3324 KB |
Output is correct |
44 |
Correct |
175 ms |
1348 KB |
Output is correct |
45 |
Correct |
77 ms |
3196 KB |
Output is correct |
46 |
Correct |
170 ms |
1348 KB |
Output is correct |
47 |
Correct |
4 ms |
632 KB |
Output is correct |
48 |
Correct |
47 ms |
1016 KB |
Output is correct |
49 |
Correct |
48 ms |
1144 KB |
Output is correct |
50 |
Correct |
489 ms |
3284 KB |
Output is correct |
51 |
Correct |
486 ms |
3572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1912 KB |
Output is correct |
2 |
Correct |
15 ms |
2012 KB |
Output is correct |
3 |
Correct |
13 ms |
1912 KB |
Output is correct |
4 |
Correct |
12 ms |
1912 KB |
Output is correct |
5 |
Correct |
13 ms |
1912 KB |
Output is correct |
6 |
Correct |
12 ms |
1912 KB |
Output is correct |
7 |
Correct |
13 ms |
1912 KB |
Output is correct |
8 |
Correct |
12 ms |
1912 KB |
Output is correct |
9 |
Correct |
15 ms |
1912 KB |
Output is correct |
10 |
Correct |
12 ms |
1912 KB |
Output is correct |
11 |
Correct |
13 ms |
1912 KB |
Output is correct |
12 |
Correct |
12 ms |
1912 KB |
Output is correct |
13 |
Correct |
13 ms |
2040 KB |
Output is correct |
14 |
Correct |
14 ms |
1912 KB |
Output is correct |
15 |
Correct |
12 ms |
1912 KB |
Output is correct |
16 |
Correct |
13 ms |
1912 KB |
Output is correct |
17 |
Correct |
12 ms |
1912 KB |
Output is correct |
18 |
Correct |
13 ms |
1912 KB |
Output is correct |
19 |
Correct |
12 ms |
1912 KB |
Output is correct |
20 |
Correct |
13 ms |
1912 KB |
Output is correct |
21 |
Correct |
13 ms |
2040 KB |
Output is correct |
22 |
Correct |
14 ms |
2040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
380 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
172 ms |
1476 KB |
Output is correct |
27 |
Correct |
79 ms |
3244 KB |
Output is correct |
28 |
Correct |
170 ms |
1348 KB |
Output is correct |
29 |
Correct |
80 ms |
3324 KB |
Output is correct |
30 |
Correct |
173 ms |
1348 KB |
Output is correct |
31 |
Correct |
76 ms |
3192 KB |
Output is correct |
32 |
Correct |
172 ms |
1348 KB |
Output is correct |
33 |
Correct |
77 ms |
3200 KB |
Output is correct |
34 |
Correct |
21 ms |
1976 KB |
Output is correct |
35 |
Correct |
78 ms |
3156 KB |
Output is correct |
36 |
Correct |
174 ms |
1348 KB |
Output is correct |
37 |
Correct |
77 ms |
3196 KB |
Output is correct |
38 |
Correct |
176 ms |
1396 KB |
Output is correct |
39 |
Correct |
77 ms |
3196 KB |
Output is correct |
40 |
Correct |
189 ms |
1348 KB |
Output is correct |
41 |
Correct |
78 ms |
3196 KB |
Output is correct |
42 |
Correct |
176 ms |
1320 KB |
Output is correct |
43 |
Correct |
81 ms |
3324 KB |
Output is correct |
44 |
Correct |
175 ms |
1348 KB |
Output is correct |
45 |
Correct |
77 ms |
3196 KB |
Output is correct |
46 |
Correct |
170 ms |
1348 KB |
Output is correct |
47 |
Correct |
4 ms |
632 KB |
Output is correct |
48 |
Correct |
47 ms |
1016 KB |
Output is correct |
49 |
Correct |
48 ms |
1144 KB |
Output is correct |
50 |
Correct |
489 ms |
3284 KB |
Output is correct |
51 |
Correct |
486 ms |
3572 KB |
Output is correct |
52 |
Correct |
13 ms |
1912 KB |
Output is correct |
53 |
Correct |
15 ms |
2012 KB |
Output is correct |
54 |
Correct |
13 ms |
1912 KB |
Output is correct |
55 |
Correct |
12 ms |
1912 KB |
Output is correct |
56 |
Correct |
13 ms |
1912 KB |
Output is correct |
57 |
Correct |
12 ms |
1912 KB |
Output is correct |
58 |
Correct |
13 ms |
1912 KB |
Output is correct |
59 |
Correct |
12 ms |
1912 KB |
Output is correct |
60 |
Correct |
15 ms |
1912 KB |
Output is correct |
61 |
Correct |
12 ms |
1912 KB |
Output is correct |
62 |
Correct |
13 ms |
1912 KB |
Output is correct |
63 |
Correct |
12 ms |
1912 KB |
Output is correct |
64 |
Correct |
13 ms |
2040 KB |
Output is correct |
65 |
Correct |
14 ms |
1912 KB |
Output is correct |
66 |
Correct |
12 ms |
1912 KB |
Output is correct |
67 |
Correct |
13 ms |
1912 KB |
Output is correct |
68 |
Correct |
12 ms |
1912 KB |
Output is correct |
69 |
Correct |
13 ms |
1912 KB |
Output is correct |
70 |
Correct |
12 ms |
1912 KB |
Output is correct |
71 |
Correct |
13 ms |
1912 KB |
Output is correct |
72 |
Correct |
13 ms |
2040 KB |
Output is correct |
73 |
Correct |
14 ms |
2040 KB |
Output is correct |
74 |
Execution timed out |
3045 ms |
7872 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |