LightOJ 1009 二分图染色+BFS/种类并查集
**题意:**有两个阵营的人,他们互相敌对,给出互相敌对的人,问同个阵营的人最多有多少个。
**思路:**可以使用种类并查集写。也可以使用使用二分图染色的写法,由于给定的点并不是连续的,所以排序离散化一下,再进行BFS染色。
二分图:
/** @Date : 2016-11-19-21.46
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
//#include<bits/stdc++.h>
#define LL long long
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e4+20;
int vis[N];
bool mp[N];
vector<int> ed[N*10];
int c[N][2];
int s[N*10];
int a[N*10];
int b[N*10];
void bfs(int s, int v)
{
queue<int>q;
vis[s] = 1;
q.push(s);
while(!q.empty())
{
int nw = q.front();
q.pop();
c[v][vis[nw]]++;
for(int i = 0; i < ed[nw].size(); i++)
{
int nx = ed[nw][i];
if(vis[nx] == -1)
{
vis[nx] = vis[nw]^1;
q.push(nx);
}
}
}
}
int main()
{
int T;
int cnt = 0;
cin >> T;
while(T--)
{
int n;
scanf("%d", &n);
MMF(mp);
MMF(s);
int ct = 0;
for(int i = 0; i < n; i++)
{
scanf("%d%d", a + i, b + i);
if(!mp[a[i]])
mp[a[i]] = 1, s[++ct] = a[i];
if(!mp[b[i]])
mp[b[i]] = 1, s[++ct] = b[i];
}
for(int i = 1; i <= ct; i++)
{
ed[i].clear();
vis[i] = -1;
c[i][0] = c[i][1] = 0;
}
sort(s + 1, s + ct + 1);
for(int i = 0; i < n; i++)
{
int x = upper_bound(s + 1, s + 1 + ct, a[i]) - s - 1;
int y = upper_bound(s + 1, s + 1 + ct, b[i]) - s - 1;
ed[x].push_back(y);
ed[y].push_back(x);
}
int vol = 0;
for(int i = 1; i <= ct; i++)
{
if(vis[i]==-1)
bfs(i, ++vol);
}
int ans = 0;
for(int i = 1; i <= vol; i++)
ans+=max(c[i][0],c[i][1]);
printf("Case %d: %d\n", ++cnt, ans);
}
return 0;
}
LightOJ 1009 二分图染色+BFS/种类并查集的更多相关文章
- leetcode 886. 可能的二分法(DFS,染色,种类并查集)
题目链接 886. 可能的二分法 题意: 给定一组 N 人(编号为 1, 2, ..., N), 我们想把每个人分进任意大小的两组. 每个人都可能不喜欢其他人,那么他们不应该属于同一组. 形式上,如果 ...
- NOIP2010关押罪犯[并查集|二分答案+二分图染色 | 种类并查集]
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示 ...
- poj 2492 A Bug's Life 二分图染色 || 种类并查集
题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...
- HDU 5285 wyh2000 and pupil(dfs或种类并查集)
wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Other ...
- 洛谷 P1525 关押罪犯 & [NOIP2010提高组](贪心,种类并查集)
传送门 解题思路 很显然,为了让最大值最小,肯定就是从大到小枚举,让他们分在两个监狱中,第一个不符合的就是答案. 怎样判断是否在一个监狱中呢? 很显然,就是用种类并查集. 种类并查集的讲解——团伙(很 ...
- NOI2001|POJ1182食物链[种类并查集 向量]
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65430 Accepted: 19283 Description ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- poj1417(种类并查集+dp)
题目:http://poj.org/problem?id=1417 题意:输入三个数m, p, q 分别表示接下来的输入行数,天使数目,恶魔数目: 接下来m行输入形如x, y, ch,ch为yes表示 ...
- poj1733(种类并查集+离散化)
题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...
随机推荐
- HADOOP docker(六):hive简易使用指南
前言1.hive简介1.1 hive组件与相应功能:1.2 hive的表类型1.3 分区表1.3 分隔符1.4 hive的数据存储2.数据类型2.1 基本数据类型2.1 复杂数据类型2.3 NULL3 ...
- 搭建备份到业务迁移---mysql
mysql安装启动以及配置 使用到阿里云主机直接yum安装以及配置 [root@yunwei-169 mysql]# yum install mysql mysql-server [root@yunw ...
- Spring Security 快速了解
在Spring Security之前 我曾经使用 Interceptor 实现了一个简单网站Demo的登录拦截和Session处理工作,虽然能够实现相应的功能,但是无疑Spring Security提 ...
- Android屏幕适配解析 - 详解像素,设备独立像素,归一化密度,精确密度及各种资源对应的尺寸密度分辨率适配问题
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19698511 . 最近遇到了一系列的屏幕适配问题, 以及 ...
- node.js安装部署
node js 安装部署学习 CentOS 下安装 Node.js 1.下载源码,你需要在https://nodejs.org/en/download/下载最新的Nodejs版本,链接: http ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie
题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...
- LintCode-165.合并两个排序链表
合并两个排序链表 将两个排序链表合并为一个新的排序链表 样例 给出 1->3->8->11->15->null,2->null, 返回 1->2->3- ...
- iOS- Swift实现UITableView的常见操作
1.前言 Swift在这就不多介绍了,想必大家都已皆知. 离Swift面世也过了有一个多月的时间. 在闲暇时间我用Swift实现了UITableView的一些常见操作. 基本都是可以用上的,今天在 ...
- ios::sync_with_stdio(false)提高C++读写速度
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:ios::sync_with_stdio(false)提高C++读写速度 本文地址:h ...
- Jenkins系列-Jenkins邮件通知
一.安装邮件插件 由于Jenkins自带的邮件功能比较鸡肋,因此这里推荐安装专门的邮件插件,不过下面也会顺带介绍如何配置Jenkins自带的邮件功能作用. 可以通过系统管理→管理插件→可选插件,选择E ...