Lightoj--1009--Back to Underworld(dfs)
2016-04-13 19:16
459 查看
Back to Underworld
Submit Status
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
Source
Problem Setter: Jane Alam Jan
题意:有两个帮派打架,判断两个帮派最多的人数有多少
搜索,因为每次给的都是两个帮派打架的编号,并且同一个战斗不会输出两次,我们可以把每个连通块画出来,然后可以发现,相邻的两层一定不是一个帮派的,所以记录下每个连通块奇数层还有偶数层的人数,每次都取最多
Time Limit: 4000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
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
Source
Problem Setter: Jane Alam Jan
题意:有两个帮派打架,判断两个帮派最多的人数有多少
搜索,因为每次给的都是两个帮派打架的编号,并且同一个战斗不会输出两次,我们可以把每个连通块画出来,然后可以发现,相邻的两层一定不是一个帮派的,所以记录下每个连通块奇数层还有偶数层的人数,每次都取最多
#include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; int a,b; int n,v[200000+10],vis[200000+10]; vector<int>G[200000+10]; void dfs(int s,int now) { for(int i=0;i<G[s].size();i++) { if(G[s][i]!=s) if(vis[G[s][i]]==0) { if(now==1) { b++; vis[G[s][i]]=1; dfs(G[s][i],0); } else { a++; vis[G[s][i]]=1; dfs(G[s][i],1); } } } } int main() { int t,k=1; scanf("%d",&t); while(t--) { int mm=0; int x,y; memset(v,0,sizeof(v)); memset(vis,0,sizeof(vis)); scanf("%d",&n); for(int i=0;i<200000;i++) G[i].clear(); for(int i=0;i<n;i++) { scanf("%d%d",&x,&y); if(x>y) swap(x,y); G[x].push_back(y); G[y].push_back(x); v[x]=v[y]=1; mm=max(mm,x); mm=max(mm,y); } int sum=0; for(int i=1;i<=mm;i++) { a=b=0; if(v[i]&&!vis[i]) { a++; vis[i]=1; dfs(i,1); sum+=max(a,b); } } printf("Case %d: %d\n",k++,sum); } return 0; }
相关文章推荐
- TextView动态设置字体
- 最新版 CocoaPods 的安装步骤
- VB.net版机房收费系统——属性过程
- 自定义ANDROID中EDITTEXT中的HINT文本的大小
- Unity3D架构设计NavMesh寻路(未完待续)
- Codeforces India Hack2016 653ABCED
- Prim算法求权数和,POJ(1258)
- 在Ubuntu14.04上快速部署OpenStack
- 读书笔记—View的事件体系(1)
- Redis入门(一)
- jvm的内存构成
- Xcode7--免证书真机调试步骤
- ios 嵌套 H5 页面 返回缓存页面
- 图特云平台前端试手遇到的问题
- 关于括号匹配的问题及其源码
- 使用Dreamweaver格式化HTML代码
- Android 从启动到程序运行发生的事情
- 高斯模糊效果实现方案及性能对比
- Android 事件流详解之View事件分发
- jboss jndi配置部分参数详解