#include<bits/stdc++.h>
using namespace std;
int n,k;
vector<pair<pair<int,int>,int>>all;
vector<set<int>>fa,fb;
int res=0,now=0;
int unnow=0;
vector<int>vis;
vector<int>allp;
vector<int>bf;
set<int>alla,allb;
void del(int u,int z){
if(u==-1||vis[u]==1){
return ;
}
vis[u]=1;
int next=-1;
if(z==1){
allb.insert(all[u].first.second);
unnow-=all[u].second;
fb[all[u].first.second].erase(u);
fa[all[u].first.first].erase(u);
if((int)fa[all[u].first.first].size()==1){
next=*(fa[all[u].first.first].begin());
}
}
else{
unnow+=all[u].second;
alla.insert(all[u].first.first);
fb[all[u].first.second].erase(u);
fa[all[u].first.first].erase(u);
if((int)fb[all[u].first.second].size()==1){
next=*(fb[all[u].first.second].begin());
}
}
del(next,(z^1));
}
void bfs(int z){
if(bf.size()==0){
return ;
}
// cout<<bf[0]<<" "<<z<<" "<<allp.size()<<endl;
vis[bf[0]]=1;
for(auto x:bf){
if(z==1){
res-=all[x].second;
allb.insert(all[x].first.second);
fb[all[x].first.second].erase(x);
}
else{
res+=all[x].second;
alla.insert(all[x].first.first);
fa[all[x].first.first].erase(x);
}
}
vector<int>fake;
for(auto x:bf){
if(z==1){
for(auto y:fb[all[x].first.second]){
int v=y;
if(vis[v]==0){
vis[v]=1;
fake.push_back(v);
}
}
}
else{
for(auto y:fa[all[x].first.first]){
int v=y;
if(vis[v]==0){
vis[v]=1;
fake.push_back(v);
}
}
}
}
bf.swap(fake);
bfs((z^1));
}
const int mid=20*30000+10;
void solve(){
bitset<mid*2>bt;
bt[mid]=1;
for(int i=0;i<(int)allp.size();i++){
int x=allp[i];
bitset<mid*2>fake,fakea;
for(int i=0;i<x;i++){
fake[i]=0;
}
for(int i=x;i<mid*2;i++){
fake[i]=bt[i-x];
}
for(int i=0;i<mid*2-x;i++){
fakea[i]=bt[i+x];
}
for(int i=mid*2-x;i<mid*2;i++){
fakea[i]=0;
}
for(int i=0;i<mid*2;i++){
if(fake[i]==1||fakea[i]==1){
bt[i]=1;
}
else{
bt[i]=0;
}
}
}
for(int i=mid-k;i<=mid+k;i++){
if(bt[i]==1){
cout<<"YES\n";
exit(0);
}
}
cout<<"NO\n";
exit(0);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
vis.resize(n*2+1);
fa.resize(n+1),fb.resize(n+1);
all.resize(n*2);
for(int i=0;i<n*2;i++){
cin>>all[i].first.first>>all[i].first.second>>all[i].second;
}
for(int i=0;i<2*n;i++){
fa[all[i].first.first].insert(i);
fb[all[i].first.second].insert(i);
}
for(int i=1;i<=n;i++){
if((int)fa[i].size()==1){
auto x=*(fa[i].begin());
del(x,0);
}
if((int)fb[i].size()==1){
auto x=*(fb[i].begin());
del(x,1);
}
}
for(int i=0;i<n*2;i++){
if(vis[i]==0){
bf.push_back(i);
res=0;
bfs(1);
allp.push_back(abs(res));
}
}
if((int)alla.size()!=n||(int)allb.size()!=n){
cout<<"NO\n";
exit(0);
}
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
740 KB |
Output is correct |
2 |
Correct |
96 ms |
748 KB |
Output is correct |
3 |
Correct |
95 ms |
736 KB |
Output is correct |
4 |
Correct |
96 ms |
724 KB |
Output is correct |
5 |
Correct |
96 ms |
724 KB |
Output is correct |
6 |
Correct |
98 ms |
740 KB |
Output is correct |
7 |
Correct |
104 ms |
724 KB |
Output is correct |
8 |
Correct |
94 ms |
724 KB |
Output is correct |
9 |
Correct |
98 ms |
740 KB |
Output is correct |
10 |
Correct |
96 ms |
740 KB |
Output is correct |
11 |
Correct |
97 ms |
744 KB |
Output is correct |
12 |
Correct |
96 ms |
744 KB |
Output is correct |
13 |
Correct |
96 ms |
736 KB |
Output is correct |
14 |
Correct |
103 ms |
736 KB |
Output is correct |
15 |
Correct |
102 ms |
736 KB |
Output is correct |
16 |
Correct |
98 ms |
740 KB |
Output is correct |
17 |
Correct |
98 ms |
740 KB |
Output is correct |
18 |
Correct |
104 ms |
740 KB |
Output is correct |
19 |
Correct |
102 ms |
724 KB |
Output is correct |
20 |
Correct |
97 ms |
736 KB |
Output is correct |
21 |
Correct |
0 ms |
316 KB |
Output is correct |
22 |
Correct |
49 ms |
724 KB |
Output is correct |
23 |
Incorrect |
48 ms |
724 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
740 KB |
Output is correct |
2 |
Correct |
96 ms |
748 KB |
Output is correct |
3 |
Correct |
95 ms |
736 KB |
Output is correct |
4 |
Correct |
96 ms |
724 KB |
Output is correct |
5 |
Correct |
96 ms |
724 KB |
Output is correct |
6 |
Correct |
98 ms |
740 KB |
Output is correct |
7 |
Correct |
104 ms |
724 KB |
Output is correct |
8 |
Correct |
94 ms |
724 KB |
Output is correct |
9 |
Correct |
98 ms |
740 KB |
Output is correct |
10 |
Correct |
96 ms |
740 KB |
Output is correct |
11 |
Correct |
97 ms |
744 KB |
Output is correct |
12 |
Correct |
96 ms |
744 KB |
Output is correct |
13 |
Correct |
96 ms |
736 KB |
Output is correct |
14 |
Correct |
103 ms |
736 KB |
Output is correct |
15 |
Correct |
102 ms |
736 KB |
Output is correct |
16 |
Correct |
98 ms |
740 KB |
Output is correct |
17 |
Correct |
98 ms |
740 KB |
Output is correct |
18 |
Correct |
104 ms |
740 KB |
Output is correct |
19 |
Correct |
102 ms |
724 KB |
Output is correct |
20 |
Correct |
97 ms |
736 KB |
Output is correct |
21 |
Correct |
0 ms |
316 KB |
Output is correct |
22 |
Correct |
49 ms |
724 KB |
Output is correct |
23 |
Incorrect |
48 ms |
724 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3084 ms |
4228 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
740 KB |
Output is correct |
2 |
Correct |
96 ms |
748 KB |
Output is correct |
3 |
Correct |
95 ms |
736 KB |
Output is correct |
4 |
Correct |
96 ms |
724 KB |
Output is correct |
5 |
Correct |
96 ms |
724 KB |
Output is correct |
6 |
Correct |
98 ms |
740 KB |
Output is correct |
7 |
Correct |
104 ms |
724 KB |
Output is correct |
8 |
Correct |
94 ms |
724 KB |
Output is correct |
9 |
Correct |
98 ms |
740 KB |
Output is correct |
10 |
Correct |
96 ms |
740 KB |
Output is correct |
11 |
Correct |
97 ms |
744 KB |
Output is correct |
12 |
Correct |
96 ms |
744 KB |
Output is correct |
13 |
Correct |
96 ms |
736 KB |
Output is correct |
14 |
Correct |
103 ms |
736 KB |
Output is correct |
15 |
Correct |
102 ms |
736 KB |
Output is correct |
16 |
Correct |
98 ms |
740 KB |
Output is correct |
17 |
Correct |
98 ms |
740 KB |
Output is correct |
18 |
Correct |
104 ms |
740 KB |
Output is correct |
19 |
Correct |
102 ms |
724 KB |
Output is correct |
20 |
Correct |
97 ms |
736 KB |
Output is correct |
21 |
Correct |
0 ms |
316 KB |
Output is correct |
22 |
Correct |
49 ms |
724 KB |
Output is correct |
23 |
Incorrect |
48 ms |
724 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |