#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 << "NO\n";
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 |
3928 KB |
Output is correct |
2 |
Correct |
2 ms |
3932 KB |
Output is correct |
3 |
Correct |
2 ms |
3992 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
2 ms |
3932 KB |
Output is correct |
6 |
Correct |
3 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3932 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3928 KB |
Output is correct |
14 |
Correct |
2 ms |
3932 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
3984 KB |
Output is correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
18 |
Correct |
2 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 |
3928 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3928 KB |
Output is correct |
2 |
Correct |
2 ms |
3932 KB |
Output is correct |
3 |
Correct |
2 ms |
3992 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
2 ms |
3932 KB |
Output is correct |
6 |
Correct |
3 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3932 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3928 KB |
Output is correct |
14 |
Correct |
2 ms |
3932 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
3984 KB |
Output is correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
18 |
Correct |
2 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 |
3928 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
546 ms |
5936 KB |
Output is correct |
2 |
Correct |
7 ms |
5720 KB |
Output is correct |
3 |
Correct |
573 ms |
5948 KB |
Output is correct |
4 |
Correct |
6 ms |
5724 KB |
Output is correct |
5 |
Correct |
555 ms |
5728 KB |
Output is correct |
6 |
Correct |
7 ms |
5720 KB |
Output is correct |
7 |
Correct |
524 ms |
5940 KB |
Output is correct |
8 |
Correct |
7 ms |
5720 KB |
Output is correct |
9 |
Correct |
551 ms |
5952 KB |
Output is correct |
10 |
Correct |
7 ms |
5724 KB |
Output is correct |
11 |
Correct |
543 ms |
5972 KB |
Output is correct |
12 |
Correct |
6 ms |
5724 KB |
Output is correct |
13 |
Correct |
538 ms |
5724 KB |
Output is correct |
14 |
Correct |
511 ms |
5972 KB |
Output is correct |
15 |
Correct |
7 ms |
5720 KB |
Output is correct |
16 |
Correct |
526 ms |
5936 KB |
Output is correct |
17 |
Correct |
7 ms |
5724 KB |
Output is correct |
18 |
Correct |
496 ms |
5724 KB |
Output is correct |
19 |
Correct |
9 ms |
5724 KB |
Output is correct |
20 |
Correct |
530 ms |
5724 KB |
Output is correct |
21 |
Correct |
10 ms |
5720 KB |
Output is correct |
22 |
Incorrect |
7 ms |
5892 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3928 KB |
Output is correct |
2 |
Correct |
2 ms |
3932 KB |
Output is correct |
3 |
Correct |
2 ms |
3992 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
2 ms |
3932 KB |
Output is correct |
6 |
Correct |
3 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3932 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3928 KB |
Output is correct |
14 |
Correct |
2 ms |
3932 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
3984 KB |
Output is correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
18 |
Correct |
2 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 |
3928 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |