# 训练19 加权并查集

1 篇文章 0 订阅

## A Bug’s Life

### 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

//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!

#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;
}


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

#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
评论
02-05 281
05-07 332
10-05 1248
05-10 4048
09-02 476
04-05 142

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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