Codeforces 659F Polycarp and Hay 并查集
链接
题意
一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变
思路
将数值从小到大排序,按顺序把与其相邻的加到并查集中。记录当前并查集中的个数,如果当前值能被K整除且总和超过了K,那么就可以以该点为中心输出了。
代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <string>
#define LL long long
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
struct node{
int x, y;
LL val;
};
node b[1000005];
int father[1000005];
int a[1005][1005];
bool vis[1005][1005];
LL num[1000005];
const int step[4][2] = { 1, 0, 0, 1, -1, 0, 0, -1 };
int n, m;
int get_father(int x){
if (father[x] == x) return x;
return father[x] = get_father(father[x]);
}
bool compare(node a, node b){
return a.val > b.val;
}
void bfs(node u, LL k){
queue<node> Q;
int val = u.val;
u.val = 1;
Q.push(u);
memset(vis, 0, sizeof(vis));
vis[u.x][u.y] = true;
int tag = k / (LL)val;
int cnt = 1;
bool flag = false;
while (!Q.empty()){
u = Q.front(); Q.pop();
for (int i = 0; i < 4; ++i){
int x = u.x + step[i][0];
int y = u.y + step[i][1];
if (x < 1 || x > n || y < 1 || y > m || a[x][y] < val || vis[x][y] || cnt >= tag) continue;
node t = u;
t.x = x, t.y = y;
++cnt;
vis[x][y] = true;
if (cnt == tag){
flag = true;
break;
}
Q.push(t);
}
if (flag) break;
}
printf("YES\n");
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
if (vis[i][j]){
printf("%d ", val);
}
else{
printf("0 ");
}
}
printf("\n");
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
LL k;
while (~scanf("%d%d%I64d", &n, &m, &k)){
int t = 0;
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
scanf("%d", &a[i][j]);
b[t].y = j;
b[t].val = a[i][j];
++t;
}
}
for (int i = 0; i < n * m; ++i){
father[i] = i;
num[i] = 1;
}
sort(b, b + n * m, compare);
for (int i = 0; i < n * m; ++i){
int x, y;
int p = (b[i].x - 1) * m + b[i].y;
for (int j = 0; j < 4; ++j){
x = b[i].x + step[j][0];
y = b[i].y + step[j][1];
if (x < 1 || x > n || y < 1 || y > m || a[x][y] < b[i].val) continue;
int q = (x - 1) * m + y;
int xf = get_father(p), yf = get_father(q);
if (xf != yf){
father[xf] = yf;
num[yf] += num[xf];
num[xf] = 0;
}
}
int yf = get_father(p);
if (k % b[i].val == 0 && num[yf] * b[i].val >= k){
bfs(b[i], k);
return 0;
}
}
printf("NO\n");
}
}
Codeforces 659F Polycarp and Hay 并查集的更多相关文章
- codeforces 659F F. Polycarp and Hay(并查集+bfs)
题目链接: F. Polycarp and Hay time limit per test 4 seconds memory limit per test 512 megabytes input st ...
- Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs
F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...
- Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集
题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...
- CodeForces 659F Polycarp and Hay
并查集,$dfs$. 从大的数字往里加,每加一个数字合并一下连通块,判断连通块内数字个数是否够,以及k能不能被当前加入的数字整除.然后$dfs$一下构造答案. #pragma comment(link ...
- Codeforces 659F Polycarp and Hay【BFS】
有毒,自从上次选拔赛(哭哭)一个垃圾bfs写错之后,每次写bfs都要WA几发...好吧,其实也就这一次... 小白说的对,还是代码能力不足... 非常不足... 题目链接: http://codefo ...
- codeforces 659F . Polycarp and Hay 搜索
题目链接 遍历每个点, 如果这个点的值能被k整除并且k/a[i][j]后小于等于n*m, 那么就对这个点进行搜索. 将这个点加入队列, 将周围的所有大于等于这个点的值的点也加入队列. 不断重复, 直到 ...
- Codeforces 699D Fix a Tree 并查集
原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- codeforces 400D Dima and Bacteria 并查集+floyd
题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...
随机推荐
- 通过配置host,自定义域名让本地访问
最近服务器这块一直由我来维护,我们开发的项目很多域名根本没有解析,而是仅仅配置了host,就可以本地访问服务器了.感觉很有意思,于是乎,打算试一试.结果弄了许久,最后第二天才解决了.把这个艰辛的旅程记 ...
- ifame子页实现父页面刷新(或跳转到指定页面)
<script>parent.location.replace('../D_DailyManager/Add.aspx?id=" + x + "');</scri ...
- Codeforces Round #284 (Div. 2) A
解题思路:给出 n个电影的精彩时段(a[i],b[i]),和每次可以跳过的时间x,问要看完所有的精彩时刻,至少需要看多长时间的电影. 因为要时间最少,所有除了精彩时刻的电影则能跳过就跳过(用取余来算) ...
- 使用LayUI在页面实现加载层(图标)代码:
实现代码: var index = layer.load({ shade: [0.4,'#def'], icon :' 实现效果: 可以使用 layer.close(index); 来控制其在什么时 ...
- luogu P5290 [十二省联考2019]春节十二响 优先队列_启发式合并
思维难度不大,在考上上写的启发式合并写错了,只拿了 60 pts,好难过QAQ 没什么太难的,在考场上想出链的部分分之后很容易就能想到正解.没错,就是非常短的启发式合并.注意一下,写的要漂亮一点,否则 ...
- Pyhton学习——Day24
# #面向对象设计:# def dog(name,gender,type):# def jiao(dog):# print('One Dog[%s],wfwfwf'%dog['name'])# def ...
- 池(Pool)
#1 就是一个资源的集合,用的时候按照你的需要去取,用完了给人家放回去 #2 学编程的时候,老师给我们的解释过池的意思,大概是: 如果你喝水,你可以拿杯子去水龙头接.如果很多人喝水,那就只能排队去接. ...
- sql limit 的用法
sql语句里的limit使用方法 . SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset 在我们使用查询语句的时候 ...
- python 多列表对应的位置的值形成一个新的列表
list1 = [1, 2, 3, 4, 5] list2 = ['a','b', 'c', 'd', 'e'] list3 = [1, 2, 3, 4, 5] multi_list = map(li ...
- sqrt开平方算法的尝试,是的看了卡马克大叔的代码,我来试试用C#写个0x5f3759df和0x5f375a86跟System.Math.Sqrt到底哪个更强
今天笔试遇到一个代码题,要求写一个开平方算法,回来发现了雷神之锤里的一段神代码: float Q_rsqrt( float number ) { long i; float x2, y; const ...