답안 #387293

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
387293 2021-04-08T08:46:21 Z kshitij_sodani Tug of War (BOI15_tug) C++14
23 / 100
63 ms 36076 KB
//#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);
	}

	/*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;
}
 
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 28524 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 28524 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 34412 KB Output is correct
2 Correct 63 ms 34412 KB Output is correct
3 Correct 50 ms 34560 KB Output is correct
4 Correct 60 ms 34540 KB Output is correct
5 Correct 49 ms 34540 KB Output is correct
6 Correct 60 ms 34412 KB Output is correct
7 Correct 49 ms 34412 KB Output is correct
8 Correct 59 ms 34412 KB Output is correct
9 Correct 53 ms 34412 KB Output is correct
10 Correct 62 ms 34412 KB Output is correct
11 Correct 50 ms 34412 KB Output is correct
12 Correct 57 ms 34412 KB Output is correct
13 Correct 53 ms 34412 KB Output is correct
14 Correct 52 ms 34412 KB Output is correct
15 Correct 60 ms 34412 KB Output is correct
16 Correct 50 ms 34412 KB Output is correct
17 Correct 58 ms 34412 KB Output is correct
18 Correct 51 ms 34300 KB Output is correct
19 Correct 60 ms 34412 KB Output is correct
20 Correct 61 ms 34412 KB Output is correct
21 Correct 63 ms 36076 KB Output is correct
22 Correct 54 ms 34412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 28524 KB Output isn't correct
2 Halted 0 ms 0 KB -