lightoj 1009 Back to Underworld (种类并查集)
2017-08-22 15:58
459 查看
The Vampires and Lykans are fighting each other to death. The war has become so fierce that, none knows who will win. The humans want to know who will survive finally. But humans are afraid of going to the battlefield.
So, they made a plan. They collected the information from the newspapers of Vampires and Lykans. They found the information about all the dual fights. Dual fight means a fight between a Lykan and a Vampire. They know the name of the dual fighters, but don't
know which one of them is a Vampire or a Lykan.
So, the humans listed all the rivals. They want to find the maximum possible number of Vampires or Lykans.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case contains an integer n (1 ≤ n ≤ 105), denoting the number of dual fights. Each of the next
n lines will contain two different integers u v (1 ≤ u, v ≤ 20000) denoting there was a fight between
u and v. No rival will be reported more than once.
Output
For each case, print the case number and the maximum possible members of any race.
Sample Input
2
2
1 2
2 3
3
1 2
2 3
4 2
Sample Output
Case 1: 2
Case 2: 3
Hint
Dataset is huge, use faster I/O methods.
种类并查集。
group[i]代表i和他的祖先的关系,group[i]=1代表是同类,=0代表不是同类。
#include<bits/stdc++.h>
#define N 20000+10
using namespace std;
int f
,group
;
int in
,num
,flag
;
int Find(int x)
{
if(x==f[x])
return x;
int tx=f[x];
f[x]=Find(f[x]);
group[x]=(group[x]+group[tx]+1)%2;
return f[x];
}
int main()
{
int t,m,n,a,b;
int cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&m);
for(int i=1; i<N; i++)
{
f[i]=i;
group[i]=1;
in[i]=0;
flag[i]=0;
num[i]=1;
}
n=-1;
for(int i=1; i<=m; i++)
{
scanf("%d%d",&a,&b);
n=max(a,n);
n=max(b,n);
in[a]=1,in[b]=1;
int ta=Find(a);
int tb=Find(b);
if(ta!=tb)
{
f[ta]=tb;
num[tb]+=num[ta];//集合里的元素整体要归到祖先下面
group[ta]=(group[b]-group[a]+2)%2;
}
}
for(int i=1;i<=n;i++)
{
if(in[i])
{
int ti=Find(i);
if(group[i]==0)
flag[ti]++;
}
}
int ans=0;
for(int i=1;i<=n;i++)//计算每一个祖先下面的儿子们与祖先的关系,累加
{
if(in[i]&&f[i]==i)
ans+=max(flag[i],num[i]-flag[i]);
}
printf("Case %d: %d\n",cas++,ans);
}
return 0;
}
So, they made a plan. They collected the information from the newspapers of Vampires and Lykans. They found the information about all the dual fights. Dual fight means a fight between a Lykan and a Vampire. They know the name of the dual fighters, but don't
know which one of them is a Vampire or a Lykan.
So, the humans listed all the rivals. They want to find the maximum possible number of Vampires or Lykans.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case contains an integer n (1 ≤ n ≤ 105), denoting the number of dual fights. Each of the next
n lines will contain two different integers u v (1 ≤ u, v ≤ 20000) denoting there was a fight between
u and v. No rival will be reported more than once.
Output
For each case, print the case number and the maximum possible members of any race.
Sample Input
2
2
1 2
2 3
3
1 2
2 3
4 2
Sample Output
Case 1: 2
Case 2: 3
Hint
Dataset is huge, use faster I/O methods.
种类并查集。
group[i]代表i和他的祖先的关系,group[i]=1代表是同类,=0代表不是同类。
#include<bits/stdc++.h>
#define N 20000+10
using namespace std;
int f
,group
;
int in
,num
,flag
;
int Find(int x)
{
if(x==f[x])
return x;
int tx=f[x];
f[x]=Find(f[x]);
group[x]=(group[x]+group[tx]+1)%2;
return f[x];
}
int main()
{
int t,m,n,a,b;
int cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&m);
for(int i=1; i<N; i++)
{
f[i]=i;
group[i]=1;
in[i]=0;
flag[i]=0;
num[i]=1;
}
n=-1;
for(int i=1; i<=m; i++)
{
scanf("%d%d",&a,&b);
n=max(a,n);
n=max(b,n);
in[a]=1,in[b]=1;
int ta=Find(a);
int tb=Find(b);
if(ta!=tb)
{
f[ta]=tb;
num[tb]+=num[ta];//集合里的元素整体要归到祖先下面
group[ta]=(group[b]-group[a]+2)%2;
}
}
for(int i=1;i<=n;i++)
{
if(in[i])
{
int ti=Find(i);
if(group[i]==0)
flag[ti]++;
}
}
int ans=0;
for(int i=1;i<=n;i++)//计算每一个祖先下面的儿子们与祖先的关系,累加
{
if(in[i]&&f[i]==i)
ans+=max(flag[i],num[i]-flag[i]);
}
printf("Case %d: %d\n",cas++,ans);
}
return 0;
}
相关文章推荐
- LightOJ 1009 Back to Underworld (种类并查集)
- Lightoj1009 Back to Underworld(带权并查集)
- LightOJ1009---Back to Underworld (bfs染色)
- lightoj1009 - Back to Underworld【dfs】
- 【lightoj1009 - Back to Underworld】二分图染色dfs
- LightOJ - 1009 Back to Underworld (vector&dfs)好题 判断两个阵营中最多人数
- LightOJ1009-Back to Underworld -二分图染色
- LightOJ 1009 Back to Underworld 搜索
- lightoj 1009 - Back to Underworld 【DFS】
- lightoj 1009 - Back to Underworld(二分图染色)
- 【Lightoj 1009 Back to Underworld】+ 二分图染色
- LightOJ - 1009 Back to Underworld(二分图染色)
- Lightoj--1009--Back to Underworld(dfs)
- Back to Underworld(lightoj 1009)
- LightOJ 1009 Back to Underworld
- LightOJ-1009-Back to Underworld [二分图染色]
- Lightoj 1009 - Back to Underworld
- Lightoj1009——Back to Underworld(DFS)
- 1009 - Back to Underworld(二分图染色)
- Light OJ 1009 - Back to Underworld