#pragma GCC optimize("O4,unroll-loops,no-stack-protector")
#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
vector<vector<int>> adj,adjr;
vector<bool> vis;
vector<int> ord;
vector<int> scc;
vector<int> l,r,S;
bool comp(int a,int b){
return S[a]<S[b];
}
void dfs(int v){
vis[v]=true;
for(auto u:adj[v]){
if(vis[u]) continue;
dfs(u);
}
ord.push_back(v);
}
void dfs2(int v,int id){
scc[v]=id;
vis[v]=true;
for(auto u:adjr[v]){
if(vis[u]) continue;
dfs2(u,id);
}
}
void addedge(int a,int b){
adj[a].push_back(b);
adjr[b].push_back(a);
}
int main() {_
auto st=clock();
int n,k;
cin>>n>>k;
l.resize(2*n);
r.resize(2*n);
S.resize(2*n);
int all=0;
vector<vector<int>> ls(n),rs(n);
for(int i=0;i<2*n;i++){
cin>>l[i]>>r[i]>>S[i];
l[i]--;
r[i]--;
ls[l[i]].push_back(i);
rs[r[i]].push_back(i);
all+=S[i];
}
adj.resize(4*n),adjr.resize(4*n);
vis.resize(4*n);
scc.resize(4*n);
for(int i=0;i<n;i++){
if(ls[i].empty() or rs[i].empty()){
cout<<"NO"<<'\n';
return 0;
}
for(auto a:ls[i]){
for(auto b:ls[i]){
if(a!=b){
addedge(2*a,2*b+1);
}
}
}
for(auto a:rs[i]){
for(auto b:rs[i]){
if(a!=b){
addedge(2*a+1,2*b);
}
}
}
}
vector<int> ord1(4*n);
vector<int> tmp(2*n);
for(int i=0;i<2*n;i++){
tmp[i]=i;
}
sort(all(tmp),comp);
int pos=0;
int pos2=4*(n/2)-1;
for(int i=0;i<n/2;i++){
ord1[pos++]=2*tmp[i];
ord1[pos++]=2*tmp[2*n-i-1];
ord1[pos2--]=2*tmp[i]+1;
ord1[pos2--]=2*tmp[2*n-i-1]+1;
}
pos=4*(n/2);
pos2=4*n;
for(int i=n/2;i<n;i++){
ord1[pos++]=2*tmp[i];
ord1[pos++]=2*tmp[2*n-i-1];
ord1[pos2--]=2*tmp[i]+1;
ord1[pos2--]=2*tmp[2*n-i-1]+1;
}
mt19937 rng(time(NULL));
while(clock()-st<2.99*CLOCKS_PER_SEC){
fill(all(vis),false);
ord.clear();
for(int i=0;i<4*n;i++){
if(!vis[ord1[i]]){
dfs(ord1[i]);
}
}
shuffle(all(ord1),rng);
reverse(all(ord));
fill(all(vis),false);
int cnt=0;
for(int i=0;i<4*n;i++){
if(!vis[ord[i]]){
dfs2(ord[i],cnt++);
}
}
int sum=0;
for(int i=0;i<2*n;i++){
if(scc[i*2]==scc[i*2+1]){
cout<<"NO"<<'\n';
return 0;
}
else{
if(scc[i*2]<scc[i*2+1]){
sum+=S[i];
}
}
}
if(abs(sum-(all-sum))<=k){
cout<<"YES"<<'\n';
return 0;
}
}
cout<<"NO"<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message
tug.cpp: In function 'void setIO(std::string)':
tug.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tug.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2990 ms |
440 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2991 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2990 ms |
348 KB |
Output is correct |
6 |
Correct |
2991 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2990 ms |
436 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2990 ms |
436 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
2991 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
2990 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2990 ms |
440 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2991 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2990 ms |
348 KB |
Output is correct |
6 |
Correct |
2991 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2990 ms |
436 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2990 ms |
436 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
2991 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
2990 ms |
348 KB |
Output is correct |
26 |
Correct |
129 ms |
1880 KB |
Output is correct |
27 |
Correct |
2991 ms |
1624 KB |
Output is correct |
28 |
Correct |
294 ms |
1624 KB |
Output is correct |
29 |
Correct |
2991 ms |
1772 KB |
Output is correct |
30 |
Correct |
2991 ms |
1772 KB |
Output is correct |
31 |
Correct |
2992 ms |
1780 KB |
Output is correct |
32 |
Correct |
71 ms |
1628 KB |
Output is correct |
33 |
Correct |
2991 ms |
1764 KB |
Output is correct |
34 |
Incorrect |
2991 ms |
1756 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
7000 KB |
Output is correct |
2 |
Correct |
6 ms |
3816 KB |
Output is correct |
3 |
Correct |
14 ms |
6992 KB |
Output is correct |
4 |
Correct |
7 ms |
3932 KB |
Output is correct |
5 |
Correct |
14 ms |
7160 KB |
Output is correct |
6 |
Correct |
8 ms |
3932 KB |
Output is correct |
7 |
Correct |
15 ms |
7072 KB |
Output is correct |
8 |
Correct |
7 ms |
3932 KB |
Output is correct |
9 |
Correct |
13 ms |
7004 KB |
Output is correct |
10 |
Correct |
6 ms |
3932 KB |
Output is correct |
11 |
Correct |
18 ms |
7004 KB |
Output is correct |
12 |
Correct |
6 ms |
3932 KB |
Output is correct |
13 |
Correct |
18 ms |
7004 KB |
Output is correct |
14 |
Correct |
18 ms |
7120 KB |
Output is correct |
15 |
Correct |
6 ms |
3928 KB |
Output is correct |
16 |
Correct |
18 ms |
7028 KB |
Output is correct |
17 |
Correct |
12 ms |
4088 KB |
Output is correct |
18 |
Correct |
18 ms |
7004 KB |
Output is correct |
19 |
Correct |
7 ms |
3928 KB |
Output is correct |
20 |
Correct |
17 ms |
7028 KB |
Output is correct |
21 |
Correct |
6 ms |
3932 KB |
Output is correct |
22 |
Correct |
16 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2990 ms |
440 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2991 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2990 ms |
348 KB |
Output is correct |
6 |
Correct |
2991 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2990 ms |
436 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2990 ms |
436 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
2991 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
2990 ms |
348 KB |
Output is correct |
26 |
Correct |
129 ms |
1880 KB |
Output is correct |
27 |
Correct |
2991 ms |
1624 KB |
Output is correct |
28 |
Correct |
294 ms |
1624 KB |
Output is correct |
29 |
Correct |
2991 ms |
1772 KB |
Output is correct |
30 |
Correct |
2991 ms |
1772 KB |
Output is correct |
31 |
Correct |
2992 ms |
1780 KB |
Output is correct |
32 |
Correct |
71 ms |
1628 KB |
Output is correct |
33 |
Correct |
2991 ms |
1764 KB |
Output is correct |
34 |
Incorrect |
2991 ms |
1756 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |