Lightoj1009——Back to Underworld(DFS)
来源:互联网 发布:凸优化 中文版 pdf 编辑:程序博客网 时间:2024/06/05 16:01
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
Output for Sample Input
Case 1: 2
Case 2: 3
两个国家打仗,给出一些两方交战的数据,求两个国家可能的最大军队数,比如样例2:1、3、4就可能是某个国家最大的军队数,所以按照dfs遍历图,每隔一个点标记一个军队,因为敌人的敌人就是自己人。这个图可能不是连通图,所以需要全部遍历,把每个连通块的最大军队加起来
#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 200005#define Mod 10001using namespace std;int n,m,use[MAXN],vis[MAXN],sum,sum1;vector<int> mp[MAXN];void dfs(int x,bool flag){ sum++; if(flag) sum1++; vis[x]=1; for(int i=0;i<mp[x].size();++i) { int y=mp[x][i]; if(!vis[y]) { vis[y]=1; dfs(y,!flag); } }}int main(){ int t,cnt=1; scanf("%d",&t); while(t--) { scanf("%d",&m); int u,v; memset(use,0,sizeof(use)); memset(vis,0,sizeof(vis)); for(int i=0;i<MAXN;++i) mp[i].clear(); for(int i=0;i<m;++i) { scanf("%d%d",&u,&v); use[u]=use[v]=1; mp[v].push_back(u); mp[u].push_back(v); n=max(n,max(u,v)); } int ans=0; for(int i=1;i<=n;++i) { if(!use[i]) continue; if(vis[i]) continue; sum=sum1=0; dfs(i,0); ans+=max(sum1,sum-sum1); } printf("Case %d: %d\n",cnt++,ans); } return 0;}
- Lightoj1009——Back to Underworld(DFS)
- lightoj1009 - Back to Underworld【dfs】
- LightOJ1009---Back to Underworld (bfs染色)
- LightOJ1009-Back to Underworld -二分图染色
- Light 1009 - Back to Underworld (dfs)
- Lightoj--1009--Back to Underworld(dfs)
- lightoj 1009 - Back to Underworld 【DFS】
- Back to Underworld
- Back to Underworld
- LightOJ - 1009 Back to Underworld (vector&dfs)好题 判断两个阵营中最多人数
- LightOJ 1009 Back to Underworld
- 1009 - Back to Underworld(二分图染色)
- lightoj 1009 - Back to Underworld(二分图染色)
- lightoj 1009 Back to Underworld (种类并查集)
- lightoj1009【DFS】
- Light OJ 1009 - Back to Underworld
- LightOJ 1009 Back to Underworld 搜索
- LightOJ 1009 Back to Underworld (种类并查集)
- CSS居中和多列布局
- 文章标题
- 909422229__利用dom4j修改XML数据_进行增删改查【最新】
- 【UVa】10298 - Power Strings
- 2017滴滴出行面试题 n 张桌子吃饭的最大利润
- Lightoj1009——Back to Underworld(DFS)
- [Android开发]ScrollView中嵌套ListView
- Anadroid 之使用ImageLoader 默认配置和手动配置参数
- 千里之行,始于足下
- Linux-C基础知识学习:C语言作业-输入三角形底和高,输出三角形面积
- swift youtube player 使用心得
- JVM原理优化和GC的处理
- Linux tcpdump抓组播包
- Android_碎片