lightoj1009 - Back to Underworld【dfs】
2016-04-13 21:23
309 查看
1009 - Back to Underworld
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.
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 uand v. No rival will be reported more than once.
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
const int maxn=20010;
vector<int>vec[maxn];
bool use[maxn];
int vis[maxn],num[maxn];
void init(){
for(int i=1;i<=20000;++i){
vis[i]=0;use[i]=false;
vec[i].clear();
}
}
int temp,sum;
void dfs(int n,int sign){//当sign为1时标记当前点为友方
if(sign==1)temp++;sum++;
for(int i=0;i<vec
.size();++i){
if(!use[vec
[i]]){
use[vec
[i]]=true;
dfs(vec
[i],-1*sign);//变号
}
}
}
int main()
{
int t,i,j,k,n,test=1;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int a,b,cnt=0;init();
for(i=0;i<n;++i){
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vec[b].push_back(a);
if(vis[a]==0){
num[cnt]=a;cnt++;vis[a]=1;
}
if(vis[b]==0){
num[cnt]=b;cnt++;vis[b]=1;
}
}
int ans=0;
for(i=0;i<cnt;++i){
if(!use[num[i]]){
temp=sum=0;use[num[i]]=true;
dfs(num[i],1);
ans=ans+max(temp,sum-temp);
}
}
printf("Case %d: %d\n",test++,ans);
}
return 0;
}
PDF (English) | Statistics | Forum |
Time Limit: 4 second(s) | Memory Limit: 32 MB |
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 uand 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 | Output for Sample Input |
2 2 1 2 2 3 3 1 2 2 3 4 2 | Case 1: 2 Case 2: 3 |
Note
Dataset is huge, use faster I/O methods.#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
const int maxn=20010;
vector<int>vec[maxn];
bool use[maxn];
int vis[maxn],num[maxn];
void init(){
for(int i=1;i<=20000;++i){
vis[i]=0;use[i]=false;
vec[i].clear();
}
}
int temp,sum;
void dfs(int n,int sign){//当sign为1时标记当前点为友方
if(sign==1)temp++;sum++;
for(int i=0;i<vec
.size();++i){
if(!use[vec
[i]]){
use[vec
[i]]=true;
dfs(vec
[i],-1*sign);//变号
}
}
}
int main()
{
int t,i,j,k,n,test=1;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int a,b,cnt=0;init();
for(i=0;i<n;++i){
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vec[b].push_back(a);
if(vis[a]==0){
num[cnt]=a;cnt++;vis[a]=1;
}
if(vis[b]==0){
num[cnt]=b;cnt++;vis[b]=1;
}
}
int ans=0;
for(i=0;i<cnt;++i){
if(!use[num[i]]){
temp=sum=0;use[num[i]]=true;
dfs(num[i],1);
ans=ans+max(temp,sum-temp);
}
}
printf("Case %d: %d\n",test++,ans);
}
return 0;
}
相关文章推荐
- 人生的高境界 - 无所谓
- 个人家园 - 永久免费ASP个人主页空间
- L.WuBi - 发布个最好用的在线五笔编码查询、五笔拆字图解
- 用hta实现制作的无殇 - 快书V1.1打包下载了第1/2页
- Windows SP2 免激活安装版 - DeepinXP PLUS V2 测试版 下载
- 超星图书浏览器(SSReader) v4.00 Bulid 070511 - 数字图书阅览器 下载
- Convenientfox v0.0.1.3 - 基于FireFox的浏览器 下载
- GX::Transcoder v3.20.51.3687 - 音频视频文件转换全能王 下载
- 某市大型汽车网 - vip代码提供下载了
- CSS属性 - white-space 空白属性使用说明
- 测试模式 - XSL教程 - 5
- 选择模式 - XSL教程 - 2
- 匹配模式 - XSL教程 - 4
- XPath入门 - XSL教程 - 3
- 强烈推荐 - Ajax 技术资源中心
- filewipe - 商用级文件擦除工具 提供下载了
- Javascript - HTML的request类
- 关于XSL - XSL教程
- JS创建优美的页面滑动块效果 - Glider.js
- 脚本中出现 window.open() access is denied - 拒绝访问 情况一则及分析