Lightoj 1009 - Back to Underworld
Time Limit: 4 second(s) | Memory Limit: 32 MB |
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 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 |
二分图染色
dfs
/* ***********************************************
Author :guanjun
Created Time :2016-6-12 15:28:00
File Name :1009.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 20100
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std; vector<int>edge[maxn];
int vis[maxn];
int a,b;
void init(){
cle(vis);
a=b=;
for(int i=;i<maxn;i++)edge[i].clear();
}
void dfs(int u,int k){
vis[u]=k;
if(k==)a++;
if(k==)b++;
for(int i=;i<edge[u].size();i++){
int v=edge[u][i];
if(!vis[v]){
dfs(v,-k);
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int T,n,x,y;
cin>>T;
for(int t=;t<=T;t++){
init();
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&x,&y);
edge[x].push_back(y);
edge[y].push_back(x);
}
int ans=;
for(int i=;i<maxn;i++){
if(vis[i]==&&edge[i].size()){
a=;b=;
dfs(i,);
ans+=max(a,b);
}
} printf("Case %d: %d\n",t,ans);
}
return ;
}
Lightoj 1009 - Back to Underworld的更多相关文章
- Back to Underworld(lightoj 1009)
1009 - Back to Underworld PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 32 ...
- LightOJ 1009 二分图染色+BFS/种类并查集
题意:有两个阵营的人,他们互相敌对,给出互相敌对的人,问同个阵营的人最多有多少个. 思路:可以使用种类并查集写.也可以使用使用二分图染色的写法,由于给定的点并不是连续的,所以排序离散化一下,再进行BF ...
- Back to Underworld(搜索)
Back to Underworld Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Su ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 1009: [HNOI2008]GT考试
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MB Description 阿申准备报名参加GT考试,准考证号为N位数\(X_1X_ ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- LightOj 1298 - One Theorem, One Year(DP + 欧拉)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...
- 1214 - Large Division -- LightOj(大数取余)
http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...
- LightOJ Beginners Problems 部分题解
相关代码请戳 https://coding.net/u/tiny656/p/LightOJ/git 1006 Hex-a-bonacci. 用数组模拟记录结果,注意取模 1008 Fibsieve's ...
随机推荐
- SpringMVC修改功能
articleList.jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...
- ES6(字符串)
ES6新增字符串特性 一.Unicode的表示法 当码值>2个字节(0xff) 即第一个数字未处理,不显示 处理这种超过2字节的情况,用{}包起来即可 二.API 1.ES5中 码值>2个 ...
- 配置工程文件dll编译后copy路径
放到工程文件的最后面的配置节点: 下面的配置节点中生成路径换成实际的相对路径就可以了 修改:Prject.csproj 文件里面的配置节点 project配置节点里面的最后面 <Target ...
- JavaScript变量声明提升
JavaScript代码在被解析引擎执行前,会被“编译”把变量声明等放在合适的作用域中,如果不了解这一点,会让人产生很多疑惑. 文章:详解js变量声明提升
- hdu1160简单dp最长下降子序列
/* 简单dp,要记录顺序 解:先排序,然后是一个最长下降子序列 ,中间需记录顺序 dp[i]=Max(dp[i],dp[j]+1); */ #include<stdio.h> #incl ...
- CF830B:Cards Sorting
对叠放着的n张牌,第i张牌写有数字Ai,进行操作:将牌堆顶的牌取出,若是当前牌堆最小值就扔掉,否则放到牌堆底,求牌堆空时操作次数. 怎么看怎么像约瑟夫..不过约瑟夫DP我不太熟,于是就yy了一下 “当 ...
- linux下部署一个JavaEE项目的简单步骤
部署项目的前提是准备好环境,包括:Java运行环境(JDK),Tomcat,Mysql数据库 1.首先将东西上传到服务器,我是在windows安装ssh工具(下载安装git即可http://gitfo ...
- eclispe使用
eclipse 快捷键 ctrl+shif+o :去除多余引用 ctrl+shift+x :转大写 ctrl+shift+y :转小写 ctrl+o :查找方法 Alt+ ← :回 ...
- 洛谷——P2068 统计和
P2068 统计和 题目描述 给定一个长度为n(n<=100000),初始值都为0的序列,x(x<=10000)次的修改某些位置上的数字,每次加上一个数,然后提出y (y<=1000 ...
- codeforces 301 E. Infinite Inversions
题目: time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...