Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs

2024-06-11 05:08

本文主要是介绍Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

老规矩,先抄一波qsc的,自己的写在后面https://www.cnblogs.com/qscqesze/p/5342366.html

 

F. Polycarp and Hay

题目连接:

http://www.codeforces.com/contest/659/problem/F

Description

The farmer Polycarp has a warehouse with hay, which can be represented as an n × m rectangular table, where n is the number of rows, and m is the number of columns in the table. Each cell of the table contains a haystack. The height in meters of the hay located in the i-th row and the j-th column is equal to an integer ai, j and coincides with the number of cubic meters of hay in the haystack, because all cells have the size of the base 1 × 1. Polycarp has decided to tidy up in the warehouse by removing an arbitrary integer amount of cubic meters of hay from the top of each stack. You can take different amounts of hay from different haystacks. Besides, it is allowed not to touch a stack at all, or, on the contrary, to remove it completely. If a stack is completely removed, the corresponding cell becomes empty and no longer contains the stack.

Polycarp wants the following requirements to hold after the reorganization:

the total amount of hay remaining in the warehouse must be equal to k,
the heights of all stacks (i.e., cells containing a non-zero amount of hay) should be the same,
the height of at least one stack must remain the same as it was,
for the stability of the remaining structure all the stacks should form one connected region.
The two stacks are considered adjacent if they share a side in the table. The area is called connected if from any of the stack in the area you can get to any other stack in this area, moving only to adjacent stacks. In this case two adjacent stacks necessarily belong to the same area.

Help Polycarp complete this challenging task or inform that it is impossible.

Input

The first line of the input contains three integers n, m (1 ≤ n, m ≤ 1000) and k (1 ≤ k ≤ 1018) — the number of rows and columns of the rectangular table where heaps of hay are lain and the required total number cubic meters of hay after the reorganization.

Then n lines follow, each containing m positive integers ai, j (1 ≤ ai, j ≤ 109), where ai, j is equal to the number of cubic meters of hay making the hay stack on the i-th row and j-th column of the table.

Output

In the first line print "YES" (without quotes), if Polycarpus can perform the reorganisation and "NO" (without quotes) otherwise. If the answer is "YES" (without quotes), then in next n lines print m numbers — the heights of the remaining hay stacks. All the remaining non-zero values should be equal, represent a connected area and at least one of these values shouldn't be altered.

If there are multiple answers, print any of them.

Sample Input

2 3 35
10 4 9
9 9 7

Sample Output

YES
7 0 7
7 7 7

Hint

题意

给你一个n*m的矩阵,然后给你一个k

这个矩阵里面的数,只能减小,不能增加。

然后你要是的矩阵最后只剩下一个连通块,且连通块里面有一个位置的数没有改变。

连通块的权值和恰好等于k

让你输出一个解。

题解:

把所有数,从大到小排序,然后用并查集去维护

只要当前这个连通块的大小大于等于k/a[i][j]就好了

然后输出的时候用bfs去输出,去维护这个连通块的大小

然后就完了……

代码

#include<bits/stdc++.h>
using namespace std;const int maxn = 1e6+7;
int a[1003][1003],p[maxn],num[maxn],n,m,vis[1003][1003];
long long k;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int fi(int x)
{return p[x]==x?x:p[x]=fi(p[x]);
}
void uni(int x,int y)
{int p1=fi(x),p2=fi(y);if(p1==p2)return;p[p1]=p2;num[p2]+=num[p1];num[p1]=0;
}
struct node
{int x,y,z;node(int x1,int y1,int z1){x=x1,y=y1,z=z1;}
};
bool cmp(node a,node b)
{return a.x>b.x;
}
vector<node>Q;
void solve(int x,int y,int z,int v)
{queue<node>Q;Q.push(node(x,y,0));vis[x][y]=1;z--;while(!Q.empty()){node now = Q.front();Q.pop();for(int i=0;i<4;i++){int xx=now.x+dx[i];int yy=now.y+dy[i];if(z==0)continue;if(xx<=0||xx>n)continue;if(yy<=0||yy>m)continue;if(vis[xx][yy])continue;if(a[xx][yy]<v)continue;vis[xx][yy]=1,z--;Q.push(node(xx,yy,0));}}for(int i=1;i<=n;i++,cout<<endl)for(int j=1;j<=m;j++)if(vis[i][j])printf("%d ",v);else printf("0 ");
}
int main()
{scanf("%d%d%lld",&n,&m,&k);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&a[i][j]),Q.push_back(node(a[i][j],i,j));for(int i=0;i<maxn;i++)p[i]=i,num[i]=1;sort(Q.begin(),Q.end(),cmp);for(int i=0;i<Q.size();i++){int v = Q[i].x;if(v==0)break;int x = Q[i].y;int y = Q[i].z;for(int j=0;j<4;j++){int xx = x+dx[j];int yy = y+dy[j];if(a[xx][yy]>=v)uni((xx-1)*m+yy,(x-1)*m+y);}long long Num = k/v;if(k%v)continue;int fa=fi((x-1)*m+y);if(Num<=num[fa]){printf("YES\n");solve(x,y,Num,v);return 0;}}printf("NO\n");
}

这个题目就是把无序化的bfs变为 有序化的。怎么有序化?

可以发现,枚举一个节点的值为基准时,他的联通部分权值比他小的点是无用的!所以只需知道比他权值大的且和他联通的点有多少个就行了。

如果把所有点按权值由高到低排序,那么我们就可以用并查集维护了。每次枚举一个点时,判断与他相邻的点是否比他大,如果比他大就并查集unite一下。

很不错的题目。

最后注意一下并查集的初始化和getid的计算。如果getid充分利用了(0,m*n)的话,初始化还好。如果没有充分利用,初始化就应该考虑到多出来的一些点!

 

#include<bits/stdc++.h>
#define rep(i, j, k) for (int i=j; i<k; i++)
#define ll long long 
#define dprintf if (debug) printf
using namespace std; 
const int maxn = 1005;
const int debug = 0;
int n, m, cnt;
ll k, a[maxn][maxn];
int vis[maxn][maxn], par[maxn*maxn],high[maxn*maxn];
ll num[maxn*maxn];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
struct Node{int x, y;ll w;
}st[maxn * maxn], ans[maxn * maxn];queue<Node> Q;
int findfather(int x)
{return par[x]=par[x]==x?x:findfather(par[x]);
}bool unite(int a, int b)
{int fa=findfather(a), fb=findfather(b);if(fa==fb)return false;if(high[fa]>high[fb]){par[fb]=fa;num[fa] += num[fb];}else{par[fa]=fb;num[fb] += num[fa];if(high[fa]==high[fb])++high[fb];}return true;
}//初始化int bfs(Node now){ll goal = k/a[now.x][now.y]; ll val = a[now.x][now.y];if (goal > m*n) return 0;ll cnt = 1;ans[0] = {now.x, now.y};while (!Q.empty()) Q.pop();memset(vis, 0, sizeof(vis));Q.push(now); vis[now.x][now.y] = 1;while (!Q.empty()){if (cnt == goal) break;now = Q.front(); Q.pop();dprintf("bfs %d %d\n", now.x, now.y);int x = now.x; int y = now.y;rep(i, 0, 4){int nx = x + dx[i]; int ny = y + dy[i];if (nx<=0 || nx>n || ny<=0 || ny>m || a[nx][ny] < val || vis[nx][ny]) continue;vis[nx][ny] = 1;Q.push({nx, ny, a[nx][ny]});ans[cnt++] = {nx, ny};if (cnt == goal) break;}}//dprintf("cnt = %d\n", cnt);if (cnt == goal){rep(i, 0, cnt){int x = ans[i].x; int y = ans[i].y;a[x][y] = -1;}puts("YES");for (int i=1; i<=n; i++){for (int j=1; j<=m; j++){if (a[i][j] != -1)printf("0 ");elseprintf("%lld ", val);}puts("");}return 1;}return 0;
}bool cmp(Node n1, Node n2){return n1.w > n2.w;
}
int id(int x, int y){///!!!return (x-1) * m + y-1;
}
int main(){scanf("%d%d%lld", &n, &m, &k);for (int i=1; i<=n; i++){for (int j=1; j<=m; j++){scanf("%lld", &a[i][j]);st[cnt++] = {i, j, a[i][j]};}}rep(i, 0, cnt){par[i] = i;num[i] = 1;}sort(st, st+cnt, cmp);rep(i, 0, cnt){int x = st[i].x; int y = st[i].y; ll w = st[i].w;dprintf("\n\n x = %d y = %d w = %lld\n", x, y, st[i].w);rep(j, 0, 4){int nx = x + dx[j]; int ny = y + dy[j];if (nx <=0 || nx > n || ny <=0 || ny>m) continue;if (a[nx][ny] >= w) {dprintf("unite %d %d %d %d\n", x, y, nx, ny);unite(id(x, y), id(nx, ny));dprintf("numfa = %lld\n", num[findfather(id(x, y))]);}}if (k%w) continue;if (num[findfather(id(x, y))] >= k/w){bfs(st[i]);return 0;}//dprintf("k = %lld, w = %lld, k/w = %lld\n", k, w, k/w);}puts("NO");return 0;
}

 

这篇关于Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1050272

相关文章

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

poj 1182 并查集 食物链类

题意: 有n只动物,分别编号1....n。所有动物都属于A,B,C中的一种,已知A吃B,B吃C,C吃A。 按顺序给出下面两种共K条信息: 1. x 和 y 属于同一类。 2. x 吃 y 。 然而这些信息可能会出错,有可能有的信息和之前给出的信息矛盾,也有的信息可能给出的 x 和 y 不在n的范围内。 求k条信息中有多少条是不正确的。 解析: 对于每只动物,创建3个元素 i

poj 2195 bfs+有流量限制的最小费用流

题意: 给一张n * m(100 * 100)的图,图中” . " 代表空地, “ M ” 代表人, “ H ” 代表家。 现在,要你安排每个人从他所在的地方移动到家里,每移动一格的消耗是1,求最小的消耗。 人可以移动到家的那一格但是不进去。 解析: 先用bfs搞出每个M与每个H的距离。 然后就是网络流的建图过程了,先抽象出源点s和汇点t。 令源点与每个人相连,容量为1,费用为

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

POJ 3057 最大二分匹配+bfs + 二分

SampleInput35 5XXDXXX...XD...XX...DXXXXX5 12XXXXXXXXXXXXX..........DX.XXXXXXXXXXX..........XXXXXXXXXXXXX5 5XDXXXX.X.DXX.XXD.X.XXXXDXSampleOutput321impossible

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

POJ1988带权并查集

M u v 将u所在的堆移动到v所在的堆的上面 C u 求u的下面有多少块 带权并查集 import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;i