Submission #387296

#TimeUsernameProblemLanguageResultExecution timeMemory
387296kshitij_sodaniTug of War (BOI15_tug)C++14
41 / 100
662 ms35948 KiB
//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second
//#define endl '\n'

int n,k;
int aa[300001];
int bb[300001];
int it[300001];
set<int> pre[300001];

vector<int> adj[300001];
vector<int> adj2[300001];
int vis[300001];
void add(int i,int j){
	adj[2*i+1].pb(2*j);
	adj[2*j+1].pb(2*i);
	/*cout<<2*i+1<<"<<"<<2*j<<endl;
	cout<<2*j+1<<"<<"<<2*i<<endl;
	cout<<endl;*/
}
void add2(int i,int j){
	adj[2*i].pb(2*j+1);
	adj[2*j].pb(2*i+1);
	/*cout<<2*i+1<<"<<"<<2*j<<endl;
	cout<<2*j+1<<"<<"<<2*i<<endl;
	cout<<endl;*/
}
vector<int> ord;
void dfs(int no){
	vis[no]=1;
	pair<int,int> cur2;
	
	cur2.b=(no%2);
	cur2.a=no/2;

	int ind=cur2.a/2;

	if(cur2.b==1){
		if(cur2.a%2==0){
			pre[aa[ind]].erase(cur2.a);
		}
		else{
			pre[bb[ind]].erase(cur2.a);
		}	
	}
	for(auto j:adj[no]){
		if(vis[j]==0){
			dfs(j);
		}
	}
	if(cur2.b==0){
		if(cur2.a%2==0){
			while(pre[aa[ind]].size()){
				int st=0;
				for(auto j:pre[aa[ind]]){
					if(j!=cur2.a){
						//cout<<no<<":"<<j*2+1<<endl;
						dfs(j*2+1);
						st=1;
						break;
					}
				}
				if(st==0){
					break;
				}
			}
		}
		else{
			while(pre[bb[ind]].size()){
				int st=0;
				for(auto j:pre[bb[ind]]){
					if(j!=cur2.a){
					//	cout<<no<<":"<<j*2+1<<endl;
						dfs(j*2+1);
						st=1;
						break;
					}
				}
				if(st==0){
					break;
				}
			}
		}	
	}
	



	ord.pb(no);
}
int cur=0;
void dfs2(int no){
	vis[no]=cur;
	pair<int,int> cur;
	
	cur.b=(no%2);
	cur.a=no/2;

	int ind=cur.a/2;

	if(cur.b==0){
		if(cur.a%2==0){
			pre[aa[ind]].erase(cur.a);
		}
		else{
			pre[bb[ind]].erase(cur.a);
		}	
	}
	for(auto j:adj2[no]){
		if(vis[j]==0){
			dfs2(j);
		}
	}
	if(cur.b==1){
		if(cur.a%2==0){
			while(pre[aa[ind]].size()){
				int st=0;
				for(auto j:pre[aa[ind]]){
					if(j!=cur.a){
						dfs2(j*2);
						st=1;
						break;
					}
				}
				if(st==0){
					break;
				}
			}
		}
		else{
			while(pre[bb[ind]].size()){
				int st=0;
				for(auto j:pre[bb[ind]]){
					if(j!=cur.a){
						dfs2(j*2);
						st=1;
						break;
					}
				}
				if(st==0){
					break;
				}
			}
		}	
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n>>k;
	for(int i=0;i<2*n;i++){
		cin>>aa[i]>>bb[i]>>it[i];
		aa[i]--;
		bb[i]--;
		bb[i]+=n;
		pre[aa[i]].insert(2*i);
		pre[bb[i]].insert(2*i+1);
		add(i*2,i*2+1);
	}
	if(n<=10){
		int ans=0;
		for(int i=0;i<(1<<(2*n));i++){
			vector<int> ss;
			vector<int> tt;
			for(int j=0;j<2*n;j++){
				if((1<<j)&i){
					ss.pb(j);
				}
				else{
					tt.pb(j);
				}
			}
			if(ss.size()==n){
				set<int> xx;
				set<int> yy;
				int su=0;
				for(auto j:ss){
					su+=it[j];
					xx.insert(aa[j]);
				}
				for(auto j:tt){
					su-=it[j];
					yy.insert(bb[j]);
				}
				if(abs(su)<=k and xx.size()==n and yy.size()==n){
					ans=1;
				}

			}
		}

		if(ans){
			cout<<"YES"<<endl;
		}
		else{
			cout<<"NO"<<endl;
		}
		return 0;
	}

	/*for(int i=0;i<2*n;i++){
		for(auto jj:pre[aa[i]]){
			int j=jj/2;
			if(j!=i){
			//	cout<<i<<":"<<j<<endl;
				//not(j*2) or not(i*2)
			//	add2(i*2,j*2);
			//	continue;
				adj[(j*2)*2].pb(2*(2*i)+1);
				adj[(i*2)*2].pb((j*2)*2+1);
			}
		}
		for(auto jj:pre[bb[i]]){
			int j=jj/2;
			if(j!=i){
			//cout<<i<<","<<j<<endl;
			//	add2(i*2+1,j*2+1);
				//continue;
				adj[(j*2+1)*2].pb(2*(2*i+1)+1);
				adj[(i*2+1)*2].pb((j*2+1)*2+1);
			}
		}
	}
	*/
	for(int i=0;i<8*n;i++){
		for(auto j:adj[i]){
			adj2[j].pb(i);
			//cout<<j<<".."<<i<<endl;
		}
	}


	for(int i=0;i<8*n;i++){
		if(vis[i]==0){
			dfs(i);
		}
	}
	reverse(ord.begin(),ord.end());
	/*for(auto i:ord){
		cout<<i<<":";
	}
	cout<<endl;*/
	for(int i=0;i<2*n;i++){
		pre[aa[i]].insert(i*2);
		pre[bb[i]].insert(i*2+1);
	}
	for(int j=0;j<8*n;j++){
		vis[j]=0;
	}
	for(auto j:ord){
		if(vis[j]==0){
			cur++;
			dfs2(j);
		}
	}
	int st=1;
	for(int i=0;i<8*n;i+=2){
		if(vis[i]==vis[i+1]){
			st=0;
		}
	}
	/*for(int i=0;i<8*n;i++){
		cout<<vis[i]<<",";
	}
	cout<<endl;*/
	if(st){
		cout<<"YES"<<endl;
	}
	else{
		cout<<"NO"<<endl;
	}











 
 
	return 0;
}
 

Compilation message (stderr)

tug.cpp: In function 'int main()':
tug.cpp:180:16: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  180 |    if(ss.size()==n){
      |       ~~~~~~~~~^~~
tug.cpp:192:32: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  192 |     if(abs(su)<=k and xx.size()==n and yy.size()==n){
      |                       ~~~~~~~~~^~~
tug.cpp:192:49: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  192 |     if(abs(su)<=k and xx.size()==n and yy.size()==n){
      |                                        ~~~~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...