lightoj 1009 - Back to Underworld(二分图染色)
来源:互联网 发布:艺术网站源码 编辑:程序博客网 时间:2024/05/17 07:21
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
Output for Sample Input
2
2
1 2
2 3
3
1 2
2 3
4 2
Case 1: 2
Case 2: 3
题意是给你n组关系,代表这两个数是对立关系,然后让你判断最多的一组里最多有可能有多少个人。
思路就是用二分图染色,然而这题的坑点是下标不是连续的,图可能不是联通的,所以做了一些处理。
#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn = 20005;int a,b;int v[200005];int color[maxn];vector<int> e[maxn];void dfs(int x,int c){ color[x] = c; if(c == 1) a++; if(c == -1) b++; for(int i=0;i<e[x].size();i++) { int t = e[x][i]; if(!color[t]) dfs(t,-c); } return;}int main(void){ int T,n,i,j; scanf("%d",&T); int cas = 1; while(T--) { scanf("%d",&n); memset(color,0,sizeof(color)); for(i=0;i<=maxn;i++) e[i].clear(); int cnt = 0; for(i=1;i<=n;i++) { int x,y; scanf("%d%d",&x,&y); e[x].push_back(y); e[y].push_back(x); v[cnt++] = x; v[cnt++] = y; } sort(v,v+cnt); cnt = unique(v,v+cnt) - v; int ans = 0; for(i=0;i<cnt;i++) if(!color[v[i]]) { a = b = 0; dfs(v[i],1); ans += max(a,b); } printf("Case %d: %d\n",cas++,ans); } return 0;}
- lightoj 1009 - Back to Underworld(二分图染色)
- LightOJ - 1009 Back to Underworld(二分图染色)
- 【Lightoj 1009 Back to Underworld】+ 二分图染色
- LightOJ-1009-Back to Underworld [二分图染色]
- 1009 - Back to Underworld(二分图染色)
- LightOJ1009-Back to Underworld -二分图染色
- LOJ 1009 - Back to Underworld(二分图染色)
- LightOJ 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 (种类并查集)
- 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
- MSP430定时器的使用
- C语言的图形界面?——简单外挂程序
- caffe的python接口学习(13):调试
- 从尾到头打印链表
- jQuery的ajax方法
- lightoj 1009 - Back to Underworld(二分图染色)
- bin文件转化成hex文件的实现
- Spring中java.lang.NoClassDefFoundError: org/springframework/core/MethodIntrospector
- 统计单词(分割字符串,字符串函数的应用)
- Linux服务器部署Java Web项目笔记
- 利用github webhook 结合openresty自动更新静态博客
- Matlab导出eps或jpg图片的四种方法
- antd design 大小优化
- 把米转换成公里