#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 6e4 + 10;
int N, K;
set<pair<int, int>> adj[MAX];
int s[MAX], L, R;
bool vis[MAX];
void dfs(int u, int p, vector<int> &val){
// cout << u << ' ';
vis[u] = true;
for(auto it: adj[u]){
int v = it.fi;
int cost = s[it.se];
if(it.se != p) val.push_back(cost);
if(vis[v]) continue;
dfs(v, it.se, val);
}
}
void solve(){
//input
cin >> N >> K;
for(int i=1; i<=N*2; i++){
int l, r;
cin >> l >> r >> s[i];
adj[l].insert(make_pair(r + N, i));
adj[r + N].insert(make_pair(l, i));
}
queue<int> q;
for(int i=1; i<=N*2; i++){
if(sz(adj[i]) == 0){
cout << "NO\n";
return;
}
if(sz(adj[i]) == 1){
q.push(i);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
if(sz(adj[u]) == 0) continue;
auto it = adj[u].begin();
int v = (*it).fi, cost = s[(*it).se];
if(u <= N){
L += cost;
}
else{
R += cost;
}
adj[u].erase(it);
adj[v].erase(make_pair(u, cost));
if(sz(adj[v]) == 1){
q.push(v);
}
}
bitset<1200003> bs;
bs[0] = true;
int S = 0;
for(int i=1; i<=N*2; i++){
if(sz(adj[i]) > 2){
cout << -1;
return;
}
if(sz(adj[i]) == 2 && !vis[i]){
vector<int> val;
dfs(i, 0, val);
int c1 = 0, c2 = 0;
for(int i=0; i+1<sz(val); i++){
if(i & 1) c2 += val[i];
else c1 += val[i];
}
bs = (bs << c1) | (bs << c2);
S += c1 + c2;
}
}
for(int i=0; i<1200001; i++){
if(bs[i]){
int j = S - i;
if(abs(L + i - (R + j)) <= K){
cout << "YES\n";
return;
}
}
}
cout << "NO\n";
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("netw.inp", "r", stdin);
// freopen("netw.out", "w", stdout);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3932 KB |
Output is correct |
2 |
Correct |
2 ms |
3932 KB |
Output is correct |
3 |
Correct |
2 ms |
3928 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
3 ms |
3764 KB |
Output is correct |
6 |
Correct |
2 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3932 KB |
Output is correct |
8 |
Correct |
2 ms |
3932 KB |
Output is correct |
9 |
Correct |
2 ms |
3932 KB |
Output is correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3932 KB |
Output is correct |
14 |
Correct |
1 ms |
3984 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
3932 KB |
Output is correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
18 |
Correct |
1 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
3932 KB |
Output is correct |
20 |
Correct |
2 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Incorrect |
1 ms |
3932 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3932 KB |
Output is correct |
2 |
Correct |
2 ms |
3932 KB |
Output is correct |
3 |
Correct |
2 ms |
3928 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
3 ms |
3764 KB |
Output is correct |
6 |
Correct |
2 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3932 KB |
Output is correct |
8 |
Correct |
2 ms |
3932 KB |
Output is correct |
9 |
Correct |
2 ms |
3932 KB |
Output is correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3932 KB |
Output is correct |
14 |
Correct |
1 ms |
3984 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
3932 KB |
Output is correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
18 |
Correct |
1 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
3932 KB |
Output is correct |
20 |
Correct |
2 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Incorrect |
1 ms |
3932 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
540 ms |
6152 KB |
Output is correct |
2 |
Correct |
8 ms |
5976 KB |
Output is correct |
3 |
Correct |
557 ms |
6152 KB |
Output is correct |
4 |
Correct |
8 ms |
5976 KB |
Output is correct |
5 |
Correct |
513 ms |
5976 KB |
Output is correct |
6 |
Correct |
8 ms |
5980 KB |
Output is correct |
7 |
Correct |
569 ms |
5980 KB |
Output is correct |
8 |
Correct |
7 ms |
5976 KB |
Output is correct |
9 |
Correct |
518 ms |
5980 KB |
Output is correct |
10 |
Correct |
7 ms |
5980 KB |
Output is correct |
11 |
Correct |
531 ms |
6152 KB |
Output is correct |
12 |
Correct |
7 ms |
5976 KB |
Output is correct |
13 |
Correct |
617 ms |
6168 KB |
Output is correct |
14 |
Correct |
511 ms |
6152 KB |
Output is correct |
15 |
Correct |
8 ms |
5980 KB |
Output is correct |
16 |
Correct |
525 ms |
6148 KB |
Output is correct |
17 |
Correct |
7 ms |
5976 KB |
Output is correct |
18 |
Correct |
516 ms |
6148 KB |
Output is correct |
19 |
Correct |
7 ms |
5980 KB |
Output is correct |
20 |
Correct |
516 ms |
6032 KB |
Output is correct |
21 |
Correct |
7 ms |
5980 KB |
Output is correct |
22 |
Incorrect |
7 ms |
5980 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3932 KB |
Output is correct |
2 |
Correct |
2 ms |
3932 KB |
Output is correct |
3 |
Correct |
2 ms |
3928 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
3 ms |
3764 KB |
Output is correct |
6 |
Correct |
2 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3932 KB |
Output is correct |
8 |
Correct |
2 ms |
3932 KB |
Output is correct |
9 |
Correct |
2 ms |
3932 KB |
Output is correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3932 KB |
Output is correct |
14 |
Correct |
1 ms |
3984 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
3932 KB |
Output is correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
18 |
Correct |
1 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
3932 KB |
Output is correct |
20 |
Correct |
2 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Incorrect |
1 ms |
3932 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |