Problem 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

For each test case, your program has to write an output conforming to the format described below. In the first line your program should output word ‘YES’ if it is possible to prevent a catastrophic overflow and ‘NO’ otherwise. In the former case, you should output the second line containing l integers from the range [0,n], where l is the number of zeros in the weather forecast description, i.e., the number of non-rainy days. Each of these integers denotes the number of the lake from which the dragon should drink; zero means the dragon should not drink from any lake (this might be necessary, as even the dragon cannot drink from an empty lake).
 

 Sample Input

4
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(并查集)的更多相关文章

  1. UVA-1623 Enter The Dragon (贪心)

    题目大意:有n个装满水的湖,m天.每天可能下雨也可能晴天,只要下雨就会把湖填满,若已满,则发洪水.有一台只能在晴天使用的抽水机,每次抽水只能抽一个湖,并且全部抽光.问是否存在一种使得不发洪水的抽水方案 ...

  2. UVA1623 Enter The Dragon (贪心)

    题意: m个坑,n天,起初每个坑都是满的,每天至多有一个坑会下雨,下雨就会满,满了再下就输出no. 在没有雨的时候可以安排龙来喝水,把坑喝空,可行的话输出龙喝水的方案 思路: 边读入边操作,set保存 ...

  3. hdu 3635 Dragon Balls(并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

  5. hdu3635 Dragon Balls(带权并查集)

    /* 题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作 T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 思路:并查集 (压缩路径的时候将龙珠移动 ...

  6. [HDOJ3635]Dragon Balls(并查集,路径压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意:有n个龙珠,n个城市.初始状态第i个龙珠在第i个城市里.接下来有两个操作: T A B:把 ...

  7. hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

    这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...

  8. POJ1703Find them, Catch them[种类并查集]

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42416   Accepted: ...

  9. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

随机推荐

  1. Java 学习笔记 泛型

    泛型 上界匹配 ? extends Number 下界匹配 ? super Number getSimpleName 不包括包名 getName 会包括包名 常和反射联合使用,做框架 Type是一个标 ...

  2. linux的 .bashrc文件是干什么的?

    使用man bash命令查看到的联机帮助文件中的相关解释如下: .bashrc - The individual per-interactive-shell startup file. 这个文件主要保 ...

  3. mysql给root开启远程访问权限

    MySql-Server 出于安全方面考虑默认只允许本机(localhost, 127.0.0.1)来连接访问. !!!所以必须给root修改可以远程访问的权限 1.在连接服务器后,操作mysql系统 ...

  4. 亲测:LNMP环境下,解决项目缓冲慢、502以及配置https的问题

    在做的项目在nginx下访问缓冲时间过长,明显比apache下访问蛮11倍有余, 解决办法: 1增加nginx的upstream,其中upstream中为php-cgi的地址: 2利用nginx作为反 ...

  5. 洛谷P3246 [HNOI2016]序列(离线 差分 树状数组)

    题意 题目链接 Sol 好像搞出了一个和题解不一样的做法(然而我考场上没写出来还是爆零0) 一个很显然的思路是考虑每个最小值的贡献. 预处理出每个数左边第一个比他小的数,右边第一个比他大的数. 那么\ ...

  6. Activiti实现流程自由跳转

    import org.activiti.engine.ProcessEngine; import org.activiti.engine.TaskService; import org.activit ...

  7. meta、link、script的常用写法

    meta 设置编码格式 <meta charset='utf-8'> 设置关键字 <meta name="keywords" content="音乐 播 ...

  8. Mysql 常用数据类型

    double:浮点型,double(5,2) 表示最多5位,必须包含两位小数,最大值是 999.99 char:定长字符串类型,char(10) 表示必须放 10 的字节,没有就用空格补充 varch ...

  9. LVS + HAProxy实现跨网负载均衡

  10. WannaCry勒索比特币蠕虫病毒解决方案

    WannaCry ransomware used in widespread attacks all over the world Customer Guidance for WannaCrypt a ...