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. 向Spring容器中注册组件的方法汇总小结

    1.通过xml定义 <bean class=""> <property name="" value=""></ ...

  2. learnVUE-note

    title: learnVUE-note date: 2018-02-27 15:57:37 tags: categories: 前端技术 --- 本文是自己在学习Vue中的 VUE事件处理 在事件处 ...

  3. hive函数应用之操作json

    1.创建表 createtable.sql中存放的创建表语句如下 create external table adt.jsontest ( appKey string comment "AP ...

  4. angular ztree 梯形结构json配置、点击节点事件、默认展开所有

    // 获取树数据 $scope.initZtreeData = function () { var url = '/bpopstation/func/queryAll.do'; $http.post( ...

  5. K8S 部署 ingress-nginx (一) 原理及搭建

    Kubernetes 暴露服务的有三种方式,分别为 LoadBlancer Service.NodePort Service.Ingress.官网对 Ingress 的定义为管理对外服务到集群内服务之 ...

  6. css小知识

    7. span { display:inline-block; width:70px; /* 超出长度以...显示 */ text-overflow: ellipsis; white-space: n ...

  7. Ubuntu、deepin 安装 mysql

    在 Ubuntu 和 deepin 安装 mysql 是很简单的,只需要几条简单的命令即可   1. sudo apt-get install mysql-server 2. sudo apt-get ...

  8. C# 利用SharpPcap实现网络包捕获嗅探

    本文是利用SharpPcap实现网络包的捕获的小例子,实现了端口监控,数据包捕获等功能,主要用于学习分享. 什么是SharpPcap? SharpPcap 是一个.NET 环境下的网络包捕获框架,基于 ...

  9. Implemented the “Importance Sampling of Reflections from Hair Fibers”

      Just the indirect specular pass by importance sampling. With all layers. Manually traced by 3D Ham ...

  10. WPF:Hyperlink如何绑定数据

    <TextBlock> <Hyperlink> <Run Text="{Binding PCFolderPath, Mode=OneWay}"/> ...