Lightoj 1009 - Back to Underworld
2016-06-12 16:30
471 查看
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.
二分图染色
dfs
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 |
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 << 61; const double eps=1e-5; using namespace std; vector<int>edge[maxn]; int vis[maxn]; int a,b; void init(){ cle(vis); a=b=0; for(int i=0;i<maxn;i++)edge[i].clear(); } void dfs(int u,int k){ vis[u]=k; if(k==1)a++; if(k==2)b++; for(int i=0;i<edge[u].size();i++){ int v=edge[u][i]; if(!vis[v]){ dfs(v,3-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=1;t<=T;t++){ init(); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&x,&y); edge[x].push_back(y); edge[y].push_back(x); } int ans=0; for(int i=1;i<maxn;i++){ if(vis[i]==0&&edge[i].size()){ a=0;b=0; dfs(i,1); ans+=max(a,b); } } printf("Case %d: %d\n",t,ans); } return 0; }
相关文章推荐
- CuteFTP下载大文件时报错
- linux的进程与线程
- 各种键盘布局及用处
- 内存为王:DBIM RAC Share Nothing架构的挑战和解决方案
- 【bzoj1263】【scoi2006】【整数划分】【高精度+贪心】
- Linux中mysql的操作
- javascript模拟sleep
- 部署多个tomcat
- CentOS 6.5/6.6 安装mysql 5.7
- OpenStack 学习笔记(一):OpenStack 环境介绍
- Navicat导入数据到数据库中碰到的问题
- 使用haproxy做tcp 层的负载均衡
- 设置RecyclerView 移动到底部
- Android类似QQ弹窗效果
- Cookie容易忽略的坑
- 【代码安全】ARM平台代码保护之虚拟化
- 文档驱动式超敏捷开发
- Coping with the TCP TIME-WAIT state on busy Linux servers
- HttpURLConnection用法详解
- Kali 2.0 采用ssh连接登陆