训练19 加权并查集

做事情要有始有终。昨天下午暑期集训画上了句号,我整个人也就随着懈怠了下来。这篇题解是我最后的惯性了吧。之前拉下的题我是不打算继续写了。
下一阶段依然是刷题,准备回洛谷去。白天学习正经东西,晚上研究副业。


Virtual Friends

A Bug’s Life

Zjnu Stadium


Virtual Friends

Problem Description
These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends’ friends, their friends’ friends’ friends, and so on), has become an addictive hobby. Just as some people collect stamps, other people collect virtual friends.

Your task is to observe the interactions on such a website and keep track of the size of each person’s network.

Assume that every friendship is mutual. If Fred is Barney’s friend, then Barney is also Fred’s friend.
Input
Input file contains multiple test cases.
The first line of each case indicates the number of test friendship nest.
each friendship nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, which is no more than 100 000. Each of the following F lines contains the names of two people who have just become friends, separated by a space. A name is a string of 1 to 20 letters (uppercase or lowercase).
Output
Whenever a friendship is formed, print a line containing one integer, the number of people in the social network of the two people who have just become friends.
Sample Input
1
3
Fred Barney
Barney Betty
Betty Wilma
Sample Output
2
3
4
题目大意给你建立的关系,询问每次建立关系后这个并查集的人数。
算是一道很基础的模板题,在结构体内多定义一个变量记录自己的儿子有几个(包括自己)。合并的时候把这个量给祖宗加上就行。另外用map存储字符串就不说了。
这个题有个很坑的点,从来没见过要写while(cin>>T) while(T–)

//You has the final say in what kind of life you want.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define MAX 200000 +10
using namespace std;
map<string,int>name;
int cnt;
struct People
{
	int fa;
	int q;
}people[MAX];

int findfa(int x)
{
	if(people[x].fa==-1)
		return x;
	people[x].fa=findfa(people[x].fa);
	return people[x].fa;
}

void Initial()
{
	for(int i=0;i<MAX;i++)
	{
		people[i].fa=-1;
		people[i].q=1;
	}
	name.clear();
	cnt=0;
	return;
}

int merge(int a,int b)
{
	int afa=findfa(a);
	int bfa=findfa(b);
	if(afa==bfa)
		return people[afa].q;
	people[afa].q+=people[bfa].q;
	people[bfa].fa=afa;
	return people[afa].q;		
}

int main()
{
	ios::sync_with_stdio(false);
	int n,T;
	string a,b;
	while(cin>>T)
	{
		while(T--)
		{
			Initial();
			cin>>n;			
			for(int i=0;i<n;i++)
			{
				cin>>a>>b;
				if(name[a]==0)
					name[a]=++cnt;
				if(name[b]==0)
					name[b]=++cnt;
				printf("%d\n",merge(name[a],name[b]));
			} 
		}
		
	}		
	return 0;
}

A Bug’s Life

Problem Description
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.

Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Input
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
The output for every scenario is a line containing “Scenario #i:”, where i is the number of the scenario starting at 1, followed by one line saying either “No suspicious bugs found!” if the experiment is consistent with his assumption about the bugs’ sexual behavior, or “Suspicious bugs found!” if Professor Hopper’s assumption is definitely wrong.
Sample Input
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Sample Output
Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!
题目大意有n条虫子,m次交配关系,每次都要是不同性别的。问是否存在同性恋。
网上说用二分图匹配法,我早忘了这玩意是啥了。后来又想起来老师上课讲过一种题就是这样的,具体做法是,用一个数组a记录某一条虫子x的异性配偶编号y,然后每当有一条虫子z和x交配的时候就把z和y放到一个并查集里。
这个方法比我的简单不知多少了。
我用了类似食物链那个题的做法,将他们都放到一个并查集里,在子节点记录边的权重,找规律。1代表和父节点性别相反,0代表相同,各种亦或。然后向上查找祖宗的时候用的循环,顺便处理边的权重。这样效率上可能比递归慢点,因为递归的话能把父节点一并处理。但是循环我认为比较好写。
然后这个题我T了好几次,因为寻找父节点的函数没有-1的直接返回值,导致将某个点的-1赋值成了自己,进入了死循环。以后写程序没把握就一步步输出来看看。
然后T完后终于。。WA了。没记错的话应该是merge某个变量名字写错了。主要还是问题没有想清楚。日了狗了。

#include<iostream>
#include<cstdio>
#define MAX 2000+10
using namespace std;
int n;
struct Bug
{
	int fa;
	int gen;
}bug[MAX];
void Initial()
{
	for(int i=0;i<MAX;i++)
	{
		bug[i].fa=-1;
		bug[i].gen=0;
	}
	return;
}
int findfa(int x)
{
	if(bug[x].fa==-1)
		return x;
	int y=x;
	while(bug[y].fa!=-1)
	{
		y=bug[y].fa;
		bug[x].gen=bug[x].gen^bug[y].gen;
	}
	bug[x].fa=y;
	
	return y;
}
bool merge(int a,int b)
{
	int afa=findfa(a);
	int bfa=findfa(b);
	if(afa==bfa&&!(bug[a].gen^bug[b].gen))
		return 0;
	if(afa!=bfa)
	{
		bug[bfa].fa=afa;
		bug[bfa].gen=(bug[b].gen==bug[a].gen?1:0);
	}
	return 1;
}

int main()
{
	int T,m;
	int a,b;
	bool flag;
	scanf("%d",&T);
	for(int cnt=1;cnt<=T;cnt++)
	{
		Initial();
		flag=1;
		scanf("%d%d",&n,&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d%d",&a,&b);
			if(!flag)
				continue;
			flag=merge(a,b);
//			for(int i=1;i<=n;i++)	cout<<bug[i].fa<<", ";cout<<endl;
//			for(int i=1;i<=n;i++)	cout<<bug[i].gen<<",  ";cout<<endl;
		}
		printf("Scenario #%d:\n",cnt);
		if(flag)
			printf("No suspicious bugs found!\n");
		else printf("Suspicious bugs found!\n");
		printf("\n");
	}
	return 0;
}

Zjnu Stadium

Font Size: ← →
Problem Description
In 12th Zhejiang College Students Games 2007, there was a new stadium built in Zhejiang Normal University. It was a modern stadium which could hold thousands of people. The audience Seats made a circle. The total number of columns were 300 numbered 1–300, counted clockwise, we assume the number of rows were infinite.
These days, Busoniya want to hold a large-scale theatrical performance in this stadium. There will be N people go there numbered 1–N. Busoniya has Reserved several seats. To make it funny, he makes M requests for these seats: A B X, which means people numbered B must seat clockwise X distance from people numbered A. For example: A is in column 4th and X is 2, then B must in column 6th (6=4+2).
Now your task is to judge weather the request is correct or not. The rule of your judgement is easy: when a new request has conflicts against the foregoing ones then we define it as incorrect, otherwise it is correct. Please find out all the incorrect requests and count them as R.
Input
There are many test cases:
For every case:
The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.
Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B), separated by a space.

Output
For every case:
Output R, represents the number of incorrect request.
Sample Input
10 10
1 2 150
3 4 200
1 5 270
2 6 200
6 5 80
4 7 150
8 9 100
4 8 50
1 7 100
9 2 100
Sample Output
2
问题大意有个总长为600圈,告诉你a在b顺时针方向相隔多少。问你有没有矛盾的说法。
并查集加一个边权记录与父节点相隔多少,把距离什么的公式写对然后记得对600取余就行。寻找祖宗的时候这个距离要一并处理,永远记录的是与父节点顺时针方向的距离。然后如果两个在一个并查集里,通过与父节点距离判断他俩是不是如题目所说的相距d。
距离公式没想清楚,merge函数某个数少了个负号,WA了一次。避免方法还是以后复杂一点的程序数出来看一下。

#include<iostream>
#include<cstdio>
#define MAX 50000+10
using namespace std;
int n;
struct People
{
	int fa;
	int dis;
}people[MAX];

void Initial()
{
	for(int i=0;i<MAX;i++)
		people[i].fa=-1,people[i].dis=0;
}

int findfa(int x)
{
	if(people[x].fa==-1)
		return x;
	int y=x;
	while(people[y].fa!=-1)
	{
		y=people[y].fa;
		people[x].dis=(people[x].dis+people[y].dis+600)%600;
	}
	people[x].fa=y;
	return y;
}


bool merge(int a,int b,int d)
{
	int afa=findfa(a);
	int bfa=findfa(b);
	if(afa==bfa)
	{
		if((people[b].dis-people[a].dis+600)%600==d)
			return 1;
		else return 0;
	}
	people[bfa].fa=afa;
	people[bfa].dis=(-people[b].dis+d+people[a].dis+600)%600;
	return 1;
}

int main()
{
	int m,a,b,d,ans;
	while(~scanf("%d%d",&n,&m))
	{
		Initial();
		ans=0;
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&d);
			if(!merge(a,b,d))
				++ans;
//			for(int i=1;i<=n;i++)	cout<<people[i].fa<<",";cout<<endl;
//			for(int i=1;i<=n;i++)cout<<people[i].dis<<",";cout<<endl;		
		}
		printf("%d\n",ans);
	}
	return 0;
}


最后一篇题解写的略有含糊。算是给暑假集训画个句号吧。二十多天不算长,也算过得充实了。以后的路长的很,可能这个开头是我最轻松的时候吧。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值