【Lightoj 1009 Back to Underworld】+ 二分图染色
来源:互联网 发布:ftp怎么绑定域名 编辑:程序博客网 时间:2024/06/05 03:31
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
把可以确定的对立军队二分图染色,依次找出每个对立阵营的最多军队相加即可 :
下面是AC代码 :
#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int KL = 20011;vector <int> v[KL];int vis[KL],na,nb,ans;void init(){ memset(vis,0,sizeof(vis)); for(int i = 0 ; i <= KL ; i++) v[i].clear();}void DFS(int x,int y){ vis[x] = 1; if(y == 1) na++; else nb++; for(int i = 0 ; i < v[x].size(); i ++){ int u = v[x][i]; if(!vis[u]) DFS(u,3 - y); //者 u 为 x 的对立阵营 }}int main(){ int T,nl = 0,N,a,b,i; scanf("%d",&T); while(T--){ init(); scanf("%d",&N); while(N--){ scanf("%d%d",&a,&b); v[a].push_back(b); v[b].push_back(a); } ans = 0; for(i = 1 ; i <= KL ; i++) if(!vis[i] && v[i].size()){ na = nb = 0; DFS(i,1); ans += max(na,nb); // 每次让军队最多的阵营为同一阵营 } printf("Case %d: %d\n",++nl,ans); } return 0;}
- LightOJ - 1009 Back to Underworld(二分图染色)
- 【Lightoj 1009 Back to Underworld】+ 二分图染色
- lightoj 1009 - Back to Underworld(二分图染色)
- LightOJ-1009-Back to Underworld [二分图染色]
- LightOJ1009-Back to Underworld -二分图染色
- 1009 - Back to Underworld(二分图染色)
- LOJ 1009 - Back to Underworld(二分图染色)
- LightOJ 1009 Back to Underworld
- Lightoj--1009--Back to Underworld(dfs)
- lightoj 1009 - Back to Underworld 【DFS】
- LightOJ 1009 Back to Underworld 搜索
- LightOJ 1009 Back to Underworld (种类并查集)
- lightoj 1009 Back to Underworld (种类并查集)
- LightOJ1009---Back to Underworld (bfs染色)
- LightOJ - 1009 Back to Underworld (vector&dfs)好题 判断两个阵营中最多人数
- Light OJ 1009 - Back to Underworld
- Light 1009 - Back to Underworld (dfs)
- Back to Underworld
- android adt download for all platforms
- 域名访问指定项目文件
- 操作系统实验-系统调用
- 【分享】那些年我们用过的外贸工具
- 找不同
- 【Lightoj 1009 Back to Underworld】+ 二分图染色
- HashMap和HashTable的区别
- AMD和CMD的区别
- java基础--19 Date类中的日期和毫秒值之间的转化
- 二进制中1的个数
- 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm
- Scikit-learn Quick Start
- 虚拟磁盘制作和快照
- 欢迎使用CSDN-markdown编辑器