UVA1623-Enter The Dragon(并查集)
Accept: 108 Submit: 689
Time Limit: 3000 mSec
Problem Description
The capital of Ardenia is surrounded by several lakes, and each of them is initially full of water. Currently, heavy rainfalls are expected over the land. Such a rain falls to one of the lakes: if the lake is dry and empty, then it will be filled with water; if the lake is already full, then it will overflow, which will result in a natural disaster. Fortunately, the citizens have a dragon at their disposal (and they will not hesitate to use it). The dragon may drink the whole water from a lake in one sitting. Also, the mages of Ardenia already predicted the weather conditions for the next couple of years. The only question is: from which lake and when should the dragon drink to prevent a catastrophe?
Input
The input contains several test cases. The first line of the input contains a positive integer Z ≤ 40, denoting the number of test cases. Then Z test cases follow, each conforming to the format described below. The first line of the input instance contains two space-separated positive integers n ≤ 106 andm ≤ 106 , where n is the number of lakes. (There are at most 10 input instances for which n ≥ 105or m ≥ 105.) The second line contains the weather forecast for the next m days: m space-separated integers t1,t2,...,tm (ti ∈ [0,n]). If ti ∈ [1,n], it means a heavy rainfall over lake ti at day i. If ti = 0, there is no rain at day i, and the dragon has the time to drink the water from one lake of your choice. Note that the dragon does not drink on a rainy day.
Output
Sample Input
2 4
0 0 1 1
2 4
0 1 0 2
2 3
0 1 2
2 4
0 0 0 1
Sample Output
NO
YES
1 2
NO
YES
0 1 0
题解:这个题应该算是并查集的应用吧,贪心虽然有,但是很明显,没什么可说的。并查集维护某个位置往后第一个晴天,从前往后扫,遇到一个下雨天,就看所下到的那个湖x前一次被灌满时什么时候,查询对应位置之后的第一个晴天,喝了湖x的水即可。
#include <bits/stdc++.h>
using namespace std;
const int maxn = + ;
int read() {
int q = ; char ch = ' ';
while (ch<'' || ch>'') ch = getchar();
while ('' <= ch && ch <= '') {
q = q * + ch - '';
ch = getchar();
}
return q;
}
int n, m;
int rain[maxn], pre[maxn], fa[maxn];
int ans[maxn];
int findn(int x) {
return x == fa[x] ? x : fa[x] = findn(fa[x]);
}
bool solve() {
memset(pre, , sizeof(pre));
memset(ans, , sizeof(ans));
for (int i = ; i <= m; i++) {
if (!rain[i]) continue;
int x = findn(pre[rain[i]]);
if (x < i) {
fa[x] = findn(x + );
ans[x] = rain[i];
}
else return false;
pre[rain[i]] = i;
}
printf("YES\n");
for (int i = ; i <= m; i++) {
if (!rain[i]) {
printf("%d ", ans[i]);
}
}
printf("\n");
return true;
}
int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
iCase = read();
while (iCase--) {
//n = read(), m = read();
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++) {
//rain[i] = read();
scanf("%d", &rain[i]);
}
int last = m + ;
fa[m + ] = m + ;
for (int i = m; i >= ; i--) {
if (!rain[i] && i) last = i;
fa[i] = last;
}
if (!solve()) {
printf("NO\n");
}
}
return ;
}
UVA1623-Enter The Dragon(并查集)的更多相关文章
- UVA-1623 Enter The Dragon (贪心)
题目大意:有n个装满水的湖,m天.每天可能下雨也可能晴天,只要下雨就会把湖填满,若已满,则发洪水.有一台只能在晴天使用的抽水机,每次抽水只能抽一个湖,并且全部抽光.问是否存在一种使得不发洪水的抽水方案 ...
- UVA1623 Enter The Dragon (贪心)
题意: m个坑,n天,起初每个坑都是满的,每天至多有一个坑会下雨,下雨就会满,满了再下就输出no. 在没有雨的时候可以安排龙来喝水,把坑喝空,可行的话输出龙喝水的方案 思路: 边读入边操作,set保存 ...
- hdu 3635 Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- hdu3635 Dragon Balls(带权并查集)
/* 题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作 T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 思路:并查集 (压缩路径的时候将龙珠移动 ...
- [HDOJ3635]Dragon Balls(并查集,路径压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意:有n个龙珠,n个城市.初始状态第i个龙珠在第i个城市里.接下来有两个操作: T A B:把 ...
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
随机推荐
- springMVC_04controller四种配置总结
一.通过url对应bean,加粗部分为必须有的 <bean class=" org.springframework.web.servlet.handler.BeanNameUrlHan ...
- Spring Security Oauth2 示例
所有示例的依赖如下(均是SpringBoot项目) pom.xml <dependencies> <dependency> <groupId>org.springf ...
- 【转】AtomicReference与volatile的区别
来源:AtomicReference与volatile的区别 AtomicReference与volatile的在可见性上的意义是一致的. volatile不能保证原子性,AutomicReferen ...
- Android LiveData使用
LiveData是一个可观察的数据持有者类. 与常规observable不同,LiveData是生命周期感知的,当生命周期处于STARTED或RESUMED状态,则LiveData会将其视为活动状态, ...
- 用GitHub Issue取代多说,是不是很厉害?
摘要: 别了,多说,拥抱Gitment. 2017年6月1日,多说正式下线,这多少让人感觉有些遗憾.在比较了多个博客评论系统,我最终选择了Gitment作为本站的博客评论系统: UI简洁,适合我的博客 ...
- idea代码提示
idea代码提示:Keymap-->Main menu-->Code-->Completion去掉Cyclic Expand Word的快捷键将Basic的快捷键更改为Alt+/
- JUnit4注解
今天学习了下,mybatis中开发dao的方法,用到了JUnit4进行单元测试, 将JUnit4中的注解总结了下,供大家参考学习: JUnit 4 开始使用 Java 5 中的注解(annotatio ...
- Android 常用数据操作封装类案例
1.DbHelper类 继承自SQLiteOpenHelper类,实现对数据库的基本操作 package com.example.utils; import android.content.Conte ...
- JS检测浏览器Adobe Reader插件
Web应用中当我们希望向用户显示pdf文档时候,如果用户安装了Adobe Reader之类的pdf阅读器,就可以直接打开文档在浏览器中显示, 但是,当用户没有安装这类软件的时候,自然是打不开的,为了系 ...
- Space Time Varying Color Palette
PDF Space Time Varying Color Palettes from Bo Zhou