CodingTrip - 携程编程大赛 (预赛第二场):1001 剪刀石头布



剪刀石头布

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
  
  
现有M个人一起玩剪刀石头布,以1-M编号,每人出一种,出过不再改变,但是我们并不知道它到底是哪一种。 (其中石头赢剪刀,剪刀赢布,布赢石头,一样则平) 裁判用两种说法对这M个人所构成的输赢关系进行描述: 一:"1 A B",表示第A个人和第B个人出的一样。 二:"2 A B",表示第A个人赢第B个人。 裁判对M个人,用以上两种说法,连说N句话,其中有真的、也有假的。 一句话出现以下情况,就是假话,否则就是真话。 1) 该句话与之前的某些真话冲突; 2) 该句话中A或B比M大; 3) 该句话表示A赢A。 请根据给定的M和N,输出假话数。 其中(1 <= M <= 10,000),(0 <= N <= 10,000)
 

Input
  
  
第1行是一个自然数K,代表有K组数据。 每组数据以一个空行分隔,其中每组数据的第1行是两个自然数M、N,以空格分开。 每组数据的第2行至N+1行,每行是三个自然数X,A,B,三个数之间用空格分开,X(1或2)表示说法的种类。
 

Output
  
  
每组数据对应一行,每行有一个整数,代表假话数。
 

Sample Input
  
  
3 43 11 1 4 3 2 3 3 1 4 1 1 4 4 2 3 3 1 2 2 2 1 4 1 1 1 2 1 4 2 3 4 2 3 2 66 9 2 3 1 2 4 4 2 1 2 2 4 3 2 4 2 2 2 3 1 3 2 1 2 1 1 1 1 6 7 2 3 7 2 1 2 2 4 4 1 2 1 1 3 2 1 2 3 2 1 3
 

Sample Output
  
  
5 4 3
 

Statistic |  Submit |  Clarifications |  Back


思路:

关键看当前这句话有木有跟以前的真话冲突。
二维数组记录任意两个人pk的结果,eg. win[A][B],表示A对阵B的结果,==2,表A赢B,记做A>B;==1,表平局,记做A=B;==0,表示A被B干掉了,记做A<B;

遍历每句话,对于当前的(X, A, B),若win[A][B]还没赋值过,就令win[A][B] = X,由win[A][B]推出的其他关系也立刻赋值;
如果win[A][B]已经被赋值,看看当前X==win[A][B]是否成立,成立就是真话,不成立就是假话,就把当前的这个假话废掉,继续看下一句话吧。。。

有木有好心人帮我找找这思路有木有漏洞呀。。。

第一个代码不但wa了,而且很丑,题目给的测试数据都对,不知道错哪里了。。。
第二个代码是可以ac的~

#include <iostream>
#include <cstdlib> 
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;
const int MAXSIZE = 10010;


struct edge{
	int A, B;
	int X;
	bool isTrue;
};

class B{
private:
	int M, N;
	int ans;
	vector<int> win[MAXSIZE];
	edge e[MAXSIZE];

public:
	void initialize();
	void readCase();
	void computing();
	void outResult();
};

void B::initialize(){
	
	ans = 0;
	M = N = 0;
	for(int i = 0; i<=MAXSIZE; i++){//清空 
		win[i].clear();
	}
}

void B::readCase(){
	
	getchar();
	cin>>M>>N;
	for(int i = 0; i<N; i++){
		int xx, aa, bb;
		cin >>xx>>aa>>bb;
		e[i].X = xx;
		e[i].A = aa;
		e[i].B = bb;
	}
	
	for(int i = 0; i<=M; i++){
		for(int j = 0; j<=M; j++){
			win[i].push_back(-2);
		}
	}

}

void B::computing(){
	
	for(int i = 0; i<N; i++){
		int A = e[i].A;
		int B = e[i].B;
		int X = e[i].X;
		
		if(A > M || B > M){
			ans++;
		
		}
		else if(X == 2 && A == B){
			ans++;
		
		}
		else{
			if(win[A][B] == -2){
				win[A][B] = X;
				win[B][A] = 2 - win[A][B];
				
				if(X == 1){
					for(int K = 1; K<=M; K++){
						if(win[A][K]!=-2){
							win[B][K] = win[A][K];
							win[K][B] = 2 - win[B][K];
						}
						else if(win[B][K]!=-2){
							win[A][K] = win[B][K];
							win[K][A] = 2 - win[A][K];
						}
					}
				} 
				else if(X == 2){
					for(int K = 1; K<=M; K++){
						
						if(win[K][B]!=-2){
							if(win[K][B] == 2){
								win[A][K] = 1;
							}
							else if(win[K][B] == 0){
								win[A][K] = 0;
							}
							else if(win[K][B] == 1){
								win[A][K] = 2;
							}
							win[K][A] = 2 - win[A][K];
							
						}
						if(win[K][A]!=-2){
							if(win[K][A] == 2){
								win[B][K] = 2;
							}
							else if(win[K][A] == 0){
								win[B][K] = 1;
							}
							else if(win[K][A] == 1){
								win[B][K] = 0;
							}
							win[K][B] = 2 - win[B][K];
						}
					}
				}
			}
			else{//已填 
				if(win[A][B] != X){
					ans++;
				}
			}
		}	
	}
}

void B::outResult(){
	cout<<ans<<endl;
} 

int main(){
	
	int T;
	cin >> T;
	while(T--){
		B b;
		b.initialize();
		b.readCase();
		b.computing();
		//cout<<"Case #"<<i++<<": ";
		b.outResult();	
	} 
	return 0;
}


下面的ac的代码,并查集(合并子树)做的,参考 http://www.cnblogs.com/asif/p/3659995.html
#include <iostream>
#include <cstdlib> 
#include <cstdio>
#include <cstring>
#include <vector>


using namespace std;
const int MAXSIZE=100010;
//结构体数组这么大的时候程序会崩 

class B{
private:
	int M, N;
	int ans;
	int f[MAXSIZE];
	int p[MAXSIZE];
	
public:
	void initialize();
	void readCase();
	int find(int);
	bool unin(int a, int x, int y);
	void computing();
	void outResult();
};

void B::initialize(){
	
	ans = 0;
	
	for(int i=0;i<=M;i++)
	{
		f[i]=i;//根节点:自身 
		p[i]=0;//关系:同类 
	}
}

void B::readCase(){
	//getchar();
	cin>>M>>N;
	initialize();
}

int B::find(int x)
{
	if(x==f[x])
		return x;
		
	int fx=find(f[x]);
	p[x]=(p[x]+p[f[x]])%3;
	f[x]=fx;
    return f[x];
}

bool B::unin(int a, int x, int y){
	
	a--;
	int fx = find(x);
	int fy = find(y);
	if(fx == fy){
		if((3-p[x]+p[y])%3 != a){
			return true;
		}
		return false;
	}
	else{
		f[fy] = fx;
		p[fy] = (p[x]-p[y]+3+a)%3;
		return false;
	}
}

void B::computing(){
	for(int i = 0; i<N; i++){
		int A, B, X;
		cin>>X>>A>>B;
	
		if(A > M || B > M || (X == 2 && A == B)){
			ans++;
		
		}
		else{
			if(unin(X, A, B)){
				ans++;
			}
		}	
	}
}

void B::outResult(){
	
	cout<<ans<<endl;
} 

int main(){
	
	B b;
	int T;
	cin >> T;
	while(T--){
		b.readCase();
		b.computing();	
		b.outResult();	
	} 
	return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值