Friends(并查集)
2016-04-15 00:10
316 查看
F - Friends
Crawling in process...
Crawling failed
Time Limit:3000MS
Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit
Status
Practice
UVA 10608
Description
There is a town with N citizens. It is known that some pairs of people are friends. According to the famous saying that “The friends of my friends are my friends, too” it follows that if A and B are friends and B and C are friends then A and C are friends,
too.
Your task is to count how many people there are in the largest group of friends.
Input
Input consists of several datasets. The first line of the input consists of a line with the number of test cases to follow. The first line of each dataset contains tho numbers N and M, where N is the number of town’s citizens (1 ≤ N ≤ 30000) and M is the number
of pairs of people (0 ≤ M ≤ 500000), which are known to be friends. Each of the following M lines consists of two integers A and B (1 ≤ A ≤ N, 1 ≤ B ≤ N, A ̸= B) which describe that A and B are friends. There could be repetitions among the given pairs.
Output
The output for each test case should contain (on a line by itself) one number denoting how many people there are in the largest group of friends on a line by itself.
Sample Input
2 3 2 1 2 2 3 10 12 1 2 3 1 3 4 5 4 3 5 4 6 5 2 2 1 7 1 1 2 9 10 8 9
Sample Output
3 7
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 30030;
struct Fri
{
int f, num;
} father[MAXN];
int ran[MAXN];
int fin(int v)
{
return father[v].f = father[v].f == v ? v : fin(father[v].f);
}
void merg(int x, int y)
{
int a = fin(x), b = fin(y);
if( a == b )
return;
if( ran[a] < ran[b])
{
father[a].f = b;
father[b].num += father[a].num;
}
else
{
father[b].f = a;
father[a].num += father[b].num;
if(ran[b] == ran[a])
{
++ran[a];
}
}
}
int main()
{
int t, x, y, a, b, ans = 0, n, m;
cin >> t;
while(t--)
{
ans = 0;
memset(father, 0, sizeof(father));
memset(ran, 0, sizeof(ran));
cin >> n >> m;
for( int i=0; i<n; i++ )
{
father[i].f = i;
father[i].num = 1;
}
for( int i = 0; i<m; i++ )
{
cin >> a >> b;
merg(a, b);
x = fin(a);
y = fin(b);
ans = max(ans, max(father[x].num, father[y].num));
}
cout << ans << endl;
}
return 0;
}
最后我才发现我是看错题了,citizens都会默了///
Crawling in process...
Crawling failed
Time Limit:3000MS
Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit
Status
Practice
UVA 10608
Description
There is a town with N citizens. It is known that some pairs of people are friends. According to the famous saying that “The friends of my friends are my friends, too” it follows that if A and B are friends and B and C are friends then A and C are friends,
too.
Your task is to count how many people there are in the largest group of friends.
Input
Input consists of several datasets. The first line of the input consists of a line with the number of test cases to follow. The first line of each dataset contains tho numbers N and M, where N is the number of town’s citizens (1 ≤ N ≤ 30000) and M is the number
of pairs of people (0 ≤ M ≤ 500000), which are known to be friends. Each of the following M lines consists of two integers A and B (1 ≤ A ≤ N, 1 ≤ B ≤ N, A ̸= B) which describe that A and B are friends. There could be repetitions among the given pairs.
Output
The output for each test case should contain (on a line by itself) one number denoting how many people there are in the largest group of friends on a line by itself.
Sample Input
2 3 2 1 2 2 3 10 12 1 2 3 1 3 4 5 4 3 5 4 6 5 2 2 1 7 1 1 2 9 10 8 9
Sample Output
3 7
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 30030;
struct Fri
{
int f, num;
} father[MAXN];
int ran[MAXN];
int fin(int v)
{
return father[v].f = father[v].f == v ? v : fin(father[v].f);
}
void merg(int x, int y)
{
int a = fin(x), b = fin(y);
if( a == b )
return;
if( ran[a] < ran[b])
{
father[a].f = b;
father[b].num += father[a].num;
}
else
{
father[b].f = a;
father[a].num += father[b].num;
if(ran[b] == ran[a])
{
++ran[a];
}
}
}
int main()
{
int t, x, y, a, b, ans = 0, n, m;
cin >> t;
while(t--)
{
ans = 0;
memset(father, 0, sizeof(father));
memset(ran, 0, sizeof(ran));
cin >> n >> m;
for( int i=0; i<n; i++ )
{
father[i].f = i;
father[i].num = 1;
}
for( int i = 0; i<m; i++ )
{
cin >> a >> b;
merg(a, b);
x = fin(a);
y = fin(b);
ans = max(ans, max(father[x].num, father[y].num));
}
cout << ans << endl;
}
return 0;
}
最后我才发现我是看错题了,citizens都会默了///
相关文章推荐
- ORB_SLAM2在Android上的移植过程
- 实验5 数独游戏界面设计
- 实验5 数独游戏界面设计
- AS下NDK开发(一)
- 任天堂平台开号注册+Q【7133V5583】
- 云鼎娱乐平台注册+Q【7133V5583】
- 银雀娱乐平台客服+Q【7133V5583】
- 银雀娱乐开户注册+Q【7133V5583】
- 361娱乐彩票平台招商+Q【7133V5583】
- 金砖娱乐平台客服+Q【7133V5583】
- 名人娱乐平台怎么申请做代理+Q【7133V5583】
- 游艇会娱乐平台客服+Q【7133V5583】
- A6娱乐网上购彩平台代理+Q【7133V5583】
- 天天彩娱乐网上购彩平台代理+Q【7133V5583】
- 云鼎娱乐平台客服+Q【7133V5583】
- 三国彩娱乐开户注册+Q【7133V5583】
- 久游娱乐平台购彩官网+Q【7133V5583】
- 如意娱乐平台怎么申请做代理+Q【7133V5583】
- 星娱娱乐平台官方网站+Q【7133V5583】
- 大唐娱乐平台购彩官网+Q【7133V5583】