1009 - Back to Underworld(二分图染色)
2016-03-28 15:57
597 查看
http://www.lightoj.com/volume_showproblem.php?problem=1009
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 u and v. No rival will be reported more than once.
1009 - Back to Underworld
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 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 |
Note
Dataset is huge, use faster I/O methods.#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <string> #define SI(T)int T;scanf("%d",&T) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long using namespace std; const int SIZE=2e4+10; const int maxn=1<<30; int vis[SIZE]; int node[SIZE]; int sum_tot[SIZE][2]; vector<vector<int> >G; void bfs(int s,int tot){ queue<int> q; q.push(s); vis[s]=1; while(!q.empty()){ int u=q.front(); q.pop(); sum_tot[tot][vis[u]]++; for(int i=0;i<G[u].size();i++){ int v=G[u][i]; if(vis[v]!=-1)continue;//匹配过了return false; q.push(v); vis[v]=vis[u]^1; } } return; } int main() { SI(T); for(int cas=1;cas<=T;cas++){ G.clear(); G.resize(SIZE); memset(vis,0,sizeof(vis)); int n,u,v,cnt=0; scanf("%d",&n); while(n--){ scanf("%d%d",&u,&v); if(!vis[u]){ node[cnt++]=u; vis[u]=1; } if(!vis[v]){ node[cnt++]=v; vis[v]=1; } G[u].push_back(v); G[v].push_back(u); } int tot=0; memset(vis,-1,sizeof(vis)); memset(sum_tot,0,sizeof(sum_tot)); for(int i=0;i<cnt;i++){ int x=node[i]; if(vis[x]==-1){ bfs(x,tot++); } } int ans=0; for(int i=0;i<tot;i++){ ans+=max(sum_tot[i][0],sum_tot[i][1]); } printf("Case %d: %d\n",cas,ans); } return 0; }
相关文章推荐
- 统计在线登陆用户数
- 函数调用模式
- 自定义实现FlawLayout
- linux7 防火墙,firewall的说明及相关配置注释
- 6.1 持久化技术简介 & 6.2 文件存储&6.3 SharedPreferences
- 深入探索 Kdump,第 1 部分:带你走进 Kdump 的世界
- c3p0的配置方式
- CleanMyMac3 苹果系统清理软件首选
- 关于JavaScript中apply与call的用法意义及区别
- sqlite的callback
- Android 疑难杂症之获取listView Item上面组件的值
- Ajax
- C++的函数重载
- Yii中使用gii遇到“unable to write the file”
- Webstorm汉化方法
- JavaScript 字符串操作函数
- bzoj 2440 莫比乌斯反演应用
- 归并排序
- ASP.NET MVC 表单提交多层子级实体集合数据到控制器中
- java的自己进行输入输出