LightOJ - 1009 Back to Underworld (vector&dfs)好题 判断两个阵营中最多人数

47 篇文章 1 订阅
39 篇文章 0 订阅
LightOJ - 1009
Time Limit: 4000MSMemory Limit: 32768KB64bit IO Format: %lld & %llu

Submit Status

Description

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

Source

Problem Setter: Jane Alam Jan
//题意:
有两个阵营,两个阵营各派一人对战,问对战n次后,人数更多的那个阵营的人数是多少?
开始用暴力写一直WA。。。下面的是参考大神的。
//思路:输入一个n,接下来n对人的对战情况
先用vector建图,将这n对对战的人存入图中,再用dfs查找赋值。因为可能有很多块,所以要
每次都加上每个块的最多的那个人数。最终所得即为所求
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ullunsigned long long
#define ll long long
#define IN __int64
#define N 20010
#define M 1000000007
using namespace std;
vector<int>v[N];
bool vis[N],use[N];
int sum,sum1;
void dfs(int s,bool flag)
{
	sum++;
	if(flag)
		sum1++;
	vis[s]=true;
	for(int i=0;i<v[s].size();i++)
	{
		int e=v[s][i];
		if(vis[e])
			continue;
		dfs(e,!flag);//!flag表示阵营(大神的机智所在) 
	}
}
int main()
{
	int T=1,t,n,m;
	int i,j,k;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=1;i<N;i++)
		{
			vis[i]=false;
			use[i]=false;
			v[i].clear();
		}
		int s,e,mm=0;
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&s,&e);
			use[s]=true;use[e]=true;
			v[s].push_back(e);
			v[e].push_back(s);
			mm=max(max(s,e),mm);
		}
		int ans=0;
		for(i=1;i<=mm;i++)
		{
			if(!use[i])	continue;
			if(vis[i]) continue;			
			sum=sum1=0;
			dfs(i,false);
			ans+=max(sum1,sum-sum1);
		}
		printf("Case %d: %d\n",T++,ans);
	}
	return 0;
}

//这是我开始用暴力模拟写的,感觉对,但却WA到死,就是过不了。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<stack>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 100010
#define M 1000000007
using namespace std;
int vis[N];
struct zz
{
	int x;
	int y;
}p[N];
bool cmp(zz a,zz b)
{
	if(a.x==b.x)
		return a.y<b.y;
	return a.x<b.x;
}
int main()
{
	int T=1,t,n,i,k;
	scanf("%d",&t);
	while(t--)
	{
		memset(p,0,sizeof(p));
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&p[i].x,&p[i].y);
			if(p[i].x>p[i].y)
			{
				k=p[i].x;
				p[i].x=p[i].y;
				p[i].y=k;
			}
		}
		sort(p,p+n,cmp);
		memset(vis,0,sizeof(vis));
		int a=0,b=0,sum=0;
		for(i=0;i<n;i++)
		{
			if(!vis[p[i].x]&&!vis[p[i].y])
			{				
				sum+=max(a,b);
				vis[p[i].x]=1;vis[p[i].y]=2;
				a=1;b=1;
			}
			else if(vis[p[i].x]&&!vis[p[i].y])
			{
				if(vis[p[i].x]==1)
				{
					vis[p[i].y]=2;
					b++;
				}
				else
				{
					vis[p[i].y]=1;
					a++;
				}
			}
			else if(vis[p[i].y]&&!vis[p[i].x])
			{
				if(vis[p[i].y]==1)
				{
					vis[p[i].x]=2;
					b++;
				}
				else
				{
					vis[p[i].x]=1;
					a++;
				}
			}
		}
		sum+=max(a,b);
		printf("Case %d: %d\n",T++,sum);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值