链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1854

写法1: 二分图最大匹配

思路:  将武器的属性对武器编号建边,因为只有10000种属性,我们直接对1-10000跑二分图匹配,同时用时间戳优化匹配。

实现代码:

#include<bits/stdc++.h>
using namespace std;
const int M = 1e6+;
vector<int>g[M];
int vis[M],pre[M],t,n;
bool dfs(int u){
for(int i = ;i < g[u].size();i ++){
int v = g[u][i];
if(vis[v] ^ t){
vis[v] = t;
if(!pre[v]||dfs(pre[v])){
pre[v] = u;
return ;
}
}
}
return ;
} int main()
{
int u,v;
scanf("%d",&n);
for(int i = ;i <= n;i ++){
scanf("%d%d",&u,&v);
g[u].push_back(i);
g[v].push_back(i);
}
for(int i = ;i <= ;i ++){
++t;
if(!dfs(i)){
printf("%d\n",i-);
break;
}
}
return ;
}

思路2: 并查集

把每个武器的属性a,b连成一条边,这样就会形成一幅图,对于图中的每个联通块来说,如果当前联通块没有环,那么一定会有任意p-1个点满足条件(p为联通块大小),如果联通块中存在环的话,那么这p个点都满足条件,我们将p个点都标记下,如果没有环的话,我们不选择最大的那个点,因为属性是从小到大选的,优先选择小的,维护的话,并查集合并的时候将顶点小的并查集并到顶点大的并查集,并且标记掉小的并查集的顶点。

实现代码;

#include<bits/stdc++.h>
using namespace std;
const int M = 1e6+;
int f[M],vis[M],n;
int Find(int x){
if(x == f[x]) return x;
return f[x] = Find(f[x]);
} void mix(int x,int y){
int fx = Find(x),fy = Find(y);
if(fx == fy){
vis[fx] = ;
}
else {
if(fx < fy) swap(fx,fy);
vis[fy] = ;
f[fy] = fx;
}
} int main()
{
int u,v;
scanf("%d",&n);
for(int i = ;i <= ;i ++) f[i] = i;
for(int i = ;i <= n;i ++){
scanf("%d%d",&u,&v);
mix(u,v);
}
for(int i = ;i <= ;i ++){
if(!vis[i]){
printf("%d\n",i-);
break;
}
}
return ;
}

bzoj 1854: [Scoi2010]游戏 (并查集||二分图最大匹配)的更多相关文章

  1. BZOJ 1854: [Scoi2010]游戏 并查集

    1854: [Scoi2010]游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 2672  Solved: 958[Submit][Status][ ...

  2. 1854: [Scoi2010]游戏[并查集]

    1854: [Scoi2010]游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4938  Solved: 1948[Submit][Status] ...

  3. BZOJ 1854: [Scoi2010]游戏( 二分图最大匹配 )

    匈牙利算法..从1~10000依次找增广路, 找不到就停止, 输出答案. --------------------------------------------------------------- ...

  4. ●BZOJ 1854 [Scoi2010]游戏

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1854 题解: 并查集(还可以用匈牙利算法进行单路增广的二分图匹配) 把每个武器看成是一条边, ...

  5. BZOJ 1854: [Scoi2010]游戏 无向图判环

    题目链接: 题目 1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB 问题描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装 ...

  6. BZOJ 1854: [Scoi2010]游戏(二分图匹配/并查集)

    题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1854 题解: 1.二分图匹配: 首先我们发现每件装备只能在两种属性中选一种.因此,我们以每 ...

  7. BZOJ 1854: [Scoi2010]游戏 [连通分量 | 并查集 | 二分图匹配]

    题意: 有$n \le 10^6$中物品,每种两个权值$\le 10^4$只能选一个,使得选出的所有权值从1递增,最大递增到多少 一开始想了一个奇怪的规定流量网络流+二分答案做法...然而我还不知道怎 ...

  8. bzoj 1854: [Scoi2010]游戏

    #include<cstdio> #include<iostream> #include<cstring> #define M 2000008 using name ...

  9. 【bzoj1854】[Scoi2010]游戏 - 并查集

    lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备最多只能使 ...

随机推荐

  1. koa服务器搭建基础

    之前我一直使用rails搭建网站.rails与koa的基本理念很相似,都是基于中间件提供一层层的服务.所不同的是,rails有很多内置的中间件,这使得开发者只需要关注MVC模块以及页面路由.而Koa这 ...

  2. Python练习-2

    #1.使用while循环输入 1 2 3 4 5 6 8 9 10 count = 0 while count < 10: count += 1 # count = count + 1 if c ...

  3. HDU - 1166 - 敌兵布阵 线段树的单点修改,区间求和

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  4. Five Dimensional Points CodeForces - 851C (计算几何+暴力)

      C. Five Dimensional Points time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. 2019省赛训练组队赛3.26周二---FJUT 2016

    A.Minimum’s Revenge There is a graph of n vertices which are indexed from 1 to n. For any pair of di ...

  6. 【转】实现Nginx代理WSS协议

    https://blog.csdn.net/chopin407/article/details/52937645 后来看到了官网的教程(http://nginx.org/en/docs/http/we ...

  7. array_column函数

    <?php $arr = [ [ 'id'=>1, 'name'=>'wang', 'age'=>10 ], [ 'id'=>2, 'name'=>'yong', ...

  8. 1 Expression of Possiblity

    Expression of possibility Probably     Perhaps There's a change(that) It's very likly(that) It's pos ...

  9. 【转】Java基础——容器分类

    Java容器可以说是增强程序员编程能力的基本工具,本系列将带您深入理解容器类. 容器的用途 如果对象的数量与生命周期都是固定的,自然我们也就不需要很复杂的数据结构. 我们可以通过创建引用来持有对象,如 ...

  10. Maven 项目 查找指定包的引用位置