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>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 1e18;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 5e5;
int l[mxn], r[mxn], s[mxn];
int n, k;
vector<int> g[mxn];
int used[mxn];
int vis[mxn];
int tempa, tempb;
int trans(int u, int ed){
if(u == l[ed]){
tempb += s[ed];
return r[ed];
}
else{
tempa += s[ed];
return l[ed];
}
}
bool OK;
void dfs(int u, int flag){
for(auto e : g[u]){
if(used[e] == flag) continue;
used[e] = flag;
int nx = trans(u, e);
if(vis[nx] == flag){
OK = false;
}
vis[nx] = flag;
dfs(nx, flag);
}
}
void solve(){
cin >> n>> k;
n *= 2;
bool sub3 = true;
fr(i, 0, n){
cin >> l[i] >> r[i] >> s[i];
r[i] += n/2;
--l[i], --r[i];
if(s[i] != 1) sub3 = false;
}
int suma = 0;
int sumb = 0;
int deg[n];
memset(deg, 0, sizeof(deg));
fr(i, 0, n){
deg[l[i]]++;
deg[r[i]]++;
}
fr(i, 0, n){
if(deg[l[i]] == 1 && deg[r[i]] == 1){
cout<<"NO"<<endl;
return;
}
if(deg[l[i]] == 1){
suma += s[i];
}
else if(deg[r[i]] == 1){
sumb += s[i];
}
else{
g[l[i]].pb(i);
g[r[i]].pb(i);
}
}
vector<pii> diff;
fr(i, 0, n){
if(used[i] == 2) continue;
vis[l[i]] = 1;
used[i] = 1;
OK = true;
tempa = s[i], tempb = 0;
dfs(l[i], 1);
bool ok1 = OK;
int val1 = tempa-tempb;
vis[r[i]] = 2;
used[i] = 2;
OK = true;
tempa = 0, tempb = s[i];
dfs(r[i], 2);
bool ok2 = OK;
int val2 = tempa-tempb;
diff.pb({val1, val2});
if(!ok1 && !ok2){
cout<<"NO"<<endl;
return;
}
}
if(sub3){
cout<<"YES"<<endl;
return;
}
int base = 20*n;
int len = diff.size();
bool dp[len+1][40*n];
memset(dp, false, sizeof(dp));
dp[0][base + suma - sumb] = true;
fr(i, 1, len+1){
fr(pr, 0, 40*n){
if(!dp[i-1][pr]) continue;
dp[i][pr+diff[i-1].st] = true;
dp[i][pr+diff[i-1].nd] = true;
}
}
fr(i, 0, k+1){
if(dp[len][base+i]||dp[len][base-i]){
cout<<"YES"<<endl;
return;
}
}
cout<<"NO"<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | 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... |