1854: [Scoi2010]游戏[并查集]
1854: [Scoi2010]游戏
Time Limit: 5 Sec Memory Limit: 162 MB
Submit: 4938 Solved: 1948
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2
3 2
4 5
Sample Output
HINT
【数据范围】
对于30%的数据,保证N < =1000
对于100%的数据,保证N < =1000000
Source
/*
将每个装备看做一条边,将装备的属性看做点。将每个装备的两个属性连接成一个集合,依次连成一条链。
最后用并查集处理找出根最大的链,输出。。。
注意每次并查集合并时要把属性大的作为根。
*/
#include<cstdio>
#include<algorithm>
#define set(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
using namespace std;
const int N=1e4+;
int n,fa[N];bool vis[N];
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
set(game);
for(int i=;i<=;i++) fa[i]=i;
n=read();
for(int i=,x,y;i<=n;i++){
x=read();y=read();
x=find(x);y=find(y);
if(x==y) vis[x]=;
if(x<y) swap(x,y);
fa[y]=x;
}
for(int i=;i<=;i++){
if(find(i)==i&&!vis[i]){
printf("%d",i-);return ;
}
}
puts("");
return ;
}
/*
orz zjk
乱搞100分:
b[]为第一优先
a[],num[b[]]为第二优先
#include<cstdio>
#include<iostream>
#define set(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
using namespace std;
const int N=1e6+5;
int n,f,a[N],b[N],num[N];bool vis[N];
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main(){
set(game);
n=read();
for(int i=1;i<=n;i++){
a[i]=read();b[i]=read();
if(a[i]>b[i]) swap(a[i],b[i]);
num[a[i]]++;num[b[i]]++;
}
for(int i=1,pos;i<=10000;i++){
pos=0;
for(int j=1;j<=n;j++){
if(!vis[j]&&b[j]==i){
pos=j;break;
}
}
if(pos){
vis[pos]=1;
num[a[pos]]--;num[b[pos]]--;
continue;
}
pos=0;
for(int j=1;j<=n;j++){
if(!vis[j]&&a[j]==i&&num[b[j]]>num[b[pos]]){
pos=j;
}
}
if(pos){
vis[pos]=1;
num[a[pos]]--;num[b[pos]]--;
}
else{
f=1;
printf("%d\n",i-1);
break;
}
}
if(!f) puts("10000");
return 0;
}
/*
in:
6
1 3
2 4
3 4
4 5
4 6
5 6
out:
6
*/
1854: [Scoi2010]游戏[并查集]的更多相关文章
- BZOJ 1854: [Scoi2010]游戏 并查集
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 2672 Solved: 958[Submit][Status][ ...
- bzoj 1854: [Scoi2010]游戏 (并查集||二分图最大匹配)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1854 写法1: 二分图最大匹配 思路: 将武器的属性对武器编号建边,因为只有10000种 ...
- 【bzoj1854】[Scoi2010]游戏 - 并查集
lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备最多只能使 ...
- BZOJ 1854: [Scoi2010]游戏 无向图判环
题目链接: 题目 1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB 问题描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装 ...
- BZOJ 1854: [Scoi2010]游戏( 二分图最大匹配 )
匈牙利算法..从1~10000依次找增广路, 找不到就停止, 输出答案. --------------------------------------------------------------- ...
- 1854: [Scoi2010]游戏
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 2538 Solved: 905[Submit][Status] ...
- ●BZOJ 1854 [Scoi2010]游戏
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1854 题解: 并查集(还可以用匈牙利算法进行单路增广的二分图匹配) 把每个武器看成是一条边, ...
- 【BZOJ】1854: [Scoi2010]游戏【二分图】
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 6759 Solved: 2812[Submit][Status] ...
- BZOJ 1854: [Scoi2010]游戏(二分图匹配/并查集)
题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1854 题解: 1.二分图匹配: 首先我们发现每件装备只能在两种属性中选一种.因此,我们以每 ...
随机推荐
- 【web开发学习笔记】Struts-Tags学习笔记1 - 通用标签和控制标签
通用标签和控制标签 第一部分.代码 //前端 <html> <head> <meta http-equiv="Content-Type" conten ...
- MySQL EXPLAIN 命令详解
MySQL EXPLAIN 命令详解 MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提 ...
- PHP is_callable 方法
is_callable (PHP 4 >= 4.0.6, PHP 5) is_callable — 验证变量的内容是否能够进行函数调用 Description bool is_callable ...
- 点滴积累【other】---HTTP 错误 404.13 - Not Found,请求筛选模块被配置为拒绝超过请求内容长度的请求(转载)
此文参考来源:http://blog.csdn.net/tiantian1980/article/details/6577499 问题:HTTP 错误 404.13 - Not Found,请求筛选模 ...
- PHP——数组中的each(),list()和while循环遍历数组
.while遍历 在while()语句每次循环中,each()语句将当前数组元素的键,赋给list()函数的第一个参数变量$key.并将当前数组元素中的值,赋给list()函数中的第二个参数变量$va ...
- 2015 Multi-University Training Contest 3 1006 Beautiful Set
Beautiful Set Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5321 Mean: 给出一个集合,有两种计算集合的值的 ...
- C++ 类的继承三(继承中的构造与析构)
//继承中的构造与析构 #include<iostream> using namespace std; /* 继承中的构造析构调用原则 1.子类对象在创建时会首先调用父类的构造函数 2.父 ...
- 在函数体的“出口处”,对 return 语句的正确性和效率进行检查
在函数体的“出口处”,对 return 语句的正确性和效率进行检查. 如果函数有返回值,那么函数的“出口处”是 return 语句. 我们不要轻视 return 语 句.如果 return 语句写得不 ...
- Angular入门篇高速开发导航网
简单介绍 AngularJS 是一个为动态WEB应用设计的结构框架,提供给大家一种新的开发应用方式.这样的方式能够让你扩展HTML的语法.以弥补在构建动态WEB应用时静态文本的不足.从而在web应用程 ...
- PHPMailer发送邮箱
1.可以参考的链接.http://www.helloweba.com/view-blog-205.html 2.下载最新的PHPMailer文件库 3.主要代码 class.phpmailer.php ...