This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 6e4 + 50;
const ll Mod = 1e9 + 7;
ll n, k, s[N], mark[N], par[N], h[N], f[N], sum, cnt;
pair<pii, int> edge;
vector<pii> adj[N], vec;
void dfs(int col, int v){
mark[v] = col;
for(auto [u, w]: adj[v]){
if(u == par[v]) continue;
else if(mark[u]){
edge = Mp(Mp(v, u), w);
continue;
}
par[u] = v; h[u] = h[v] + 1; f[u] = w - f[v];
dfs(col, u);
if(h[v] % 2) sum += w;
else sum -= w;
}
}
const ll M = 10 * N;
bitset<2*M> diff;
void check(int v){
mark[v] = 1;
for(auto [u, w]: adj[v]){
if(!mark[u]) check(u);
}
cnt++; sum += adj[v].size();
}
int main(){
fast_io;
cin >> n >> k;
int l, r;
for(int i = 1; i <= 2*n; i++){
cin >> l >> r >> s[i]; r += n;
adj[l].pb(Mp(r, s[i]));
adj[r].pb(Mp(l, s[i]));
}
for(int i = 1; i <= 2*n; i++){
if(!mark[i]){
sum = 0; cnt = 0; check(i);
if(sum != cnt * 2) kill("NO");
}
}
fill(mark, mark + N, 0);
for(int i = 1; i <= 2*n; i++){
if(!mark[i]){
sum = 0; dfs(i, i);
int v = edge.F.F;
int u = edge.F.S;
int w = edge.S;
if(h[v] % 2) sum -= w;
else sum += w;
ll sum2 = sum; // f[u] + f[v] - w
if(h[v] % 2) sum2 -= 2 * (f[u] + f[v] - w);
else sum2 += 2 * (f[u] + f[v] - w);
vec.pb(Mp(sum, sum2));
//cout << sum << sep << sum2 << endl;
}
}
diff[M] = 1;
for(auto [x, y]: vec){
if(x >= 0 && y >= 0) diff = (diff << x) | (diff << y);
else if(x >= 0) diff = (diff << x) | (diff >> -y);
else if(y >= 0) diff = (diff >> -x) | (diff << y);
else diff = (diff >> -x) | (diff >> -y);
}
ll res = Mod;
for(int i = 0; i < 2*M; i++) if(diff[i]) res = min(res, abs(M-i));
//cout << res << endl;
if(res <= k) cout << "YES";
else cout << "NO";
}
Compilation message (stderr)
tug.cpp: In function 'void dfs(int, int)':
tug.cpp:36:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | for(auto [u, w]: adj[v]){
| ^
tug.cpp: In function 'void check(int)':
tug.cpp:56:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
56 | for(auto [u, w]: adj[v]){
| ^
tug.cpp: In function 'int main()':
tug.cpp:103:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
103 | for(auto [x, y]: vec){
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |