UVa10158 War

        题意:有n个人,彼此间是朋友或敌对关系。有4种操作:1设置a,b为朋友 2设置a,b为敌对 3询问a,b是不是朋友 4询问a,b是不是敌对。对于询问操作,如果是输出1,否则输出0。对于设置操作,如果与已知关系矛盾,输出-1,操作无效;有效的设置不输出。

        思路:有相互关系的并查集。如果已知两个人的关系,不管是朋友还是敌对,就放到一个集合里。用一个数组表示元素与父元素间的关系。不管是查找和合并都涉及到关系数组的改变,写的时候头脑一定要清醒,了解它是怎么运作的。

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <ctype.h>
#define INF 1000000
using namespace std;

int fa[10010];
bool rel[10010];

int find(int i){
	if(i==fa[i])return i;
	int t=fa[i];
	fa[i]=find(fa[i]);
	rel[i]=!(rel[i]^rel[t]);
	return fa[i];
}

void uni(int a,int b,bool f){
	int t=find(a);
	fa[find(a)]=find(b);
	rel[t]=!(!(rel[a]^rel[b])^f);
	find(a);
	find(b);
}

int main(){
	int n;
	while(cin>>n){
		for(int i=0;i<=n;i++){
			fa[i]=i;
		}
		for(int i=0;i<=n;i++){
			rel[i]=1;
		}
		
		int c,x,y;
		while(cin>>c>>x>>y){
			if(!(c||x||y))break;
			switch(c){
				case 1:
					if(find(x)!=find(y)){
						uni(x,y,true);
					}else if(rel[x]!=rel[y]){
						cout<<"-1"<<endl;
					}
					break;
				case 2:
					if(find(x)!=find(y)){
						uni(x,y,false);
					}else if(rel[x]==rel[y]){
						cout<<"-1"<<endl;
					}
					break;
				case 3:
					if(find(x)==find(y)){
						if(rel[x]==rel[y]){
							cout<<"1"<<endl;
						}else{
							cout<<"0"<<endl;
						}
					}else{
						cout<<"0"<<endl;
					}
					break;
				case 4:
					if(find(x)==find(y)){
						if(rel[x]==rel[y]){
							cout<<"0"<<endl;
						}else{
							cout<<"1"<<endl;
						}
					}else{
						cout<<"0"<<endl;
					}
					break;			
			}
		}
	}
	return 0;
} 


There were different types of fish in your aquarium. But they did not go along well with each other. So there had been Fish-War-1 among them. It was a complete mess. Lot of fishes died, many of them hid in some mountain, some were eaten by other fishes and so on. So you decided to compartmentalize your aquarium. You divided your aquarium into R x C grids, that is R rows and C columns. Then you inserted walls into each cell. The walls are slanted, that is it goes from north-east corner to south-west corner or north-west corner to south-east corner. They look like “/” or “\” respectively. Many years passed since the war. Now the fishes want to unite again. They want to bring down the walls. They measured the strength of each of the walls. What is the minimum amount of strength they need to spend to unite all the compartments? For example, in the following 2 x 2 grid, they can spend 7 + 9 + 12 = 28 unit strength to unite the 4 compartments. And this is the minimum. First line of the input contains number of test case T (<= 20). Hence follows T test cases. First line of the test case describes number of row R and number of columns C (1 <= R, C <= 100). Next R lines describe the walls. Each of these lines contains C characters and the characters are either “/” or “\”. Next R lines contain C positive integers, each describes the strength of the wall at the corresponding cell. The strength of a wall would be at most 10,000. For each test case output the case number and the minimum amount of strength to unite all the compartments in the aquarium. 输入样例 2 2 2 \/ \\ 7 10 12 9 1 3 /\\ 3 4 5 输出样例 Case 1: 28 Case 2: 12
最新发布
12-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值