#include<bits/stdc++.h>
using namespace std;
int n,k;
vector<pair<pair<int,int>,int>>all;
vector<vector<int>>fa,fb;
int res=0,now=0;
vector<int>vis;
vector<pair<int,int>>allp;
vector<int>bf;
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;
}
else{
res+=all[x].second;
}
}
vector<int>fake;
for(auto x:bf){
if(z==1){
int v;
if(fb[all[x].first.second][0]==x){
v=fb[all[x].first.second][1];
}
else{
v=fb[all[x].first.second][0];
}
if(vis[v]==0){
vis[v]=1;
fake.push_back(v);
}
}
else{
int v;
if(fa[all[x].first.first][0]==x){
v=fa[all[x].first.first][1];
}
else{
v=fa[all[x].first.first][0];
}
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>bt;
bt[0]=1;
for(int i=0;i<(int)allp.size();i++){
int x=allp[i].first;
x=abs(x);
bitset<mid>fake,fakea;
fake=(bt>>x);
fakea=(bt<<x);
bt=(fake|fakea);
}
for(int i=0;i<=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].push_back(i);
fb[all[i].first.second].push_back(i);
}
for(int i=1;i<=n;i++){
if((int)fa[i].size()>2||(int)fb[i].size()>2){
cout<<"NO\n";
return 0;
}
}
for(int i=0;i<n*2;i++){
if(vis[i]==0){
bf.push_back(i);
res=0;
bfs(1);
allp.push_back(make_pair(res,-res));
}
}
// for(auto x:allp){
// cout<<x.first<<endl;
// }
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
2216 KB |
Output is correct |
2 |
Correct |
6 ms |
1620 KB |
Output is correct |
3 |
Correct |
179 ms |
2228 KB |
Output is correct |
4 |
Correct |
6 ms |
1620 KB |
Output is correct |
5 |
Correct |
180 ms |
2228 KB |
Output is correct |
6 |
Correct |
6 ms |
1628 KB |
Output is correct |
7 |
Correct |
173 ms |
2220 KB |
Output is correct |
8 |
Correct |
8 ms |
1636 KB |
Output is correct |
9 |
Correct |
176 ms |
2224 KB |
Output is correct |
10 |
Correct |
7 ms |
1620 KB |
Output is correct |
11 |
Correct |
177 ms |
2228 KB |
Output is correct |
12 |
Correct |
6 ms |
1620 KB |
Output is correct |
13 |
Correct |
174 ms |
2224 KB |
Output is correct |
14 |
Correct |
173 ms |
2132 KB |
Output is correct |
15 |
Correct |
6 ms |
1620 KB |
Output is correct |
16 |
Correct |
181 ms |
2252 KB |
Output is correct |
17 |
Correct |
7 ms |
1620 KB |
Output is correct |
18 |
Correct |
177 ms |
2224 KB |
Output is correct |
19 |
Correct |
6 ms |
1620 KB |
Output is correct |
20 |
Correct |
173 ms |
2228 KB |
Output is correct |
21 |
Correct |
6 ms |
1620 KB |
Output is correct |
22 |
Incorrect |
6 ms |
1620 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |