codeforces723 D. Lakes in Berland(并查集)
题目链接:codeforces723 D. Lakes in Berland
参考博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
const int N = ;
const int M = ;
int n, m, k;
char ch[N][N];
int val[N][N];
int fa[M], num[M];
bool vis[M];
int dx[] = {,,,-};
int dy[] = {,-,,};
void init(){
CLR(ch,'.');
int cnt = ;
for(int i = ; i <= n+; ++i)
for(int j = ; j <= m+; ++j)
val[i][j] = ++cnt;
for(int i = ; i <= cnt; ++i){
fa[i] = i;
num[i] = ;
}
}
int fin(int x){
if(x != fa[x])
fa[x] = fin(fa[x]);
return fa[x];
}
void uni(int x, int y){
if((x = fin(x)) == (y = fin(y))) return;
else {
fa[x] = y;
num[y] += num[x];
}
}
void make(int i, int j){
for(int k = ; k < ; ++k){
if(ch[dx[k]+i][dy[k]+j] == '.')
uni(val[i][j], val[dx[k]+i][dy[k]+j]);
}
}
bool cmp(int x, int y){
return num[x] < num[y];
}
vector<int>v;
int main(){
int i, j, kk, rt, t;
scanf("%d%d%d", &n, &m, &k);
init();
for(i = ; i <= n+; ++i){
uni(val[][], val[i][]);
uni(val[][], val[i][m+]);
}
for(i = ; i <= m+; ++i){
uni(val[][], val[][i]);
uni(val[][], val[n+][i]);
}
for(i = ; i <= n; ++i){
scanf("%s", ch[i]+);
ch[i][m+] = '.';//第二遍改时把这句漏了继续WA
}
for(i = ; i <= n; ++i)
for(j = ; j <= m; ++j)
if(ch[i][j] == '.')
make(i, j);
for(i = ; i <= n; ++i){
for(j = ; j <= m; ++j){
rt = fin(val[i][j]);
if(ch[i][j] == '.' && fin(val[][]) != rt){
if(vis[rt]) continue;
else vis[rt] = true;
v.push_back(rt);
}
}
}
sort(v.begin(), v.end(), cmp);
int len = v.size();
t = len;
int ans = ;
if(t > k){
for(i = ; i < len; ++i){
rt = fin(v[i]);
if(vis[rt]){
vis[rt] = false;
t--;
for(j = ; j <= n; ++j)
for(kk = ; kk <= m; ++kk)//我第一遍把这里写成k导致WA死
if(fin(val[j][kk]) == rt) {ch[j][kk] = '*'; ans++;}
}
if(t == k) break;
}
}
printf("%d\n", ans);
for(i = ; i <= n; ++i){
for(j = ; j <= m; ++j)
printf("%c", ch[i][j]);
puts("");
}
return ;
}
codeforces723 D. Lakes in Berland(并查集)的更多相关文章
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
- CodeForces Roads not only in Berland(并查集)
H - Roads not only in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland (DFS或并查集)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Beta Round #5 E. Bindian Signalizing 并查集
E. Bindian Signalizing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- UVA1623-Enter The Dragon(并查集)
Problem UVA1623-Enter The Dragon Accept: 108 Submit: 689Time Limit: 3000 mSec Problem Description T ...
- CodeForces - 357C Knight Tournament 伪并查集(区间合并)
Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...
- Codeforces Round #346 (Div. 2) E题 并查集找环
E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to it ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- 【转】在网页中运行VB6程序
用VB6做的程序在网页里运行, 需要把程序做成OCX格式,下面简单做一介绍: 首先新建一个工程, 选择ActivX控件: 然后添加控件和代码: 然后F5运行 然后按下图设置,去掉弹出消息阻止 这样 ...
- Nessus基本命令
/etc/init.d/nessusd start 启动nessusd服务 默认端口8834 添加用户(未知是什么用户) sudo /opt/nessus/sbin/nessus-adduser
- python_way ,day23 API
python_way ,day23 1.api认证 .api加密动态请求 2.自定义session 一.api认证 首先提供api的公司,如支付宝,微信,都会给你一个用户id,然后还会让你下一个SD ...
- python_way ,day22 tonardo
python_way day22 1.tonardo 2.cookie 3.api认证 一.tonardo: a.tonardo 初识 #!/usr/bin/env python3# Created ...
- 一个不错的能将HTML表格导出为excel,pdf等的jquery插件
https://github.com/kayalshri/tableExport.jquery.plugin https://github.com/kayalshri/ tableExport.jqu ...
- c point
a[i] 与 *(a+i) 是等价的. 事实上在计算a[i]的值时,c语言首先将前者转换为后者形式, 而且,通常而言,用指针编写的程序要比用数组下标编写的程序执行速度快,(为什么?) 因此,应该尽量用 ...
- poj1673EXOCENTER OF A TRIANGLE
链接 据说这题是垂心..数学太弱没有看出来,写了分朴实无华的代码.. 旋转三边得到图中的外顶点,然后连接三角形顶点求交点,交上WA..觉得没什么错误就去看了下discuss,发现都在说精度问题,果断开 ...
- mysql 占用的内存大小
1.mysql执行查询计划,key_len表示索引使用的字节数,这个字节数和三个条件有关.mysql> create table t1(v1 char(10));Query OK, 0 rows ...
- SDL2.0的加载图片贴图
加载图片贴图,采用了SDL_Window.SDL_Renderer.SDL_Texture和SDL_Image库 实例: #include <stdio.h> #include <m ...
- Oracle SQL 调优之 sqlhc
SQL 执行慢,如何 快速准确的优化. sqlhc 就是其中最好工具之一 通过获得sql所有的执行计划,列出实际的性能的瓶颈点,列出 sql 所在的表上的行数,每一列的数据和分布,现有的索引,sql ...