题目链接: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(并查集)的更多相关文章

  1. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  2. CodeForces Roads not only in Berland(并查集)

    H - Roads not only in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Beta Round #5 E. Bindian Signalizing 并查集

    E. Bindian Signalizing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  6. UVA1623-Enter The Dragon(并查集)

    Problem UVA1623-Enter The Dragon Accept: 108  Submit: 689Time Limit: 3000 mSec Problem Description T ...

  7. CodeForces - 357C Knight Tournament 伪并查集(区间合并)

    Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...

  8. 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 ...

  9. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

随机推荐

  1. java 导出Excel 大数据量,自己经验总结!

    出处: http://lyjilu.iteye.com/ 分析导出实现代码,XLSX支持: /** * 生成<span style="white-space: normal; back ...

  2. LINUX RPM卸载

    1. which httpd /usr/bin/httpd 2.rpm -q -f /usr/bin/httpd name 3.rpm -e name 如果有依赖: rpm -e --allmatch ...

  3. Codeforces Round #259 (Div. 2)AB

    链接:http://codeforces.com/contest/454/problem/A A. Little Pony and Crystal Mine time limit per test 1 ...

  4. windows下使用xShell远程连接virtualbox里面的linux

    第一阶段:基本安装 安装virtual box 在virtualbox里面安装xubuntu:是ubuntu+xfce桌面环境的一个linux的发行版本 在windows下安装Xmanager Ent ...

  5. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. CSS笔记(三)背景

    CSS 允许应用纯色作为背景,也允许使用背景图像创建相当复杂的效果. 参考:http://www.w3school.com.cn/css/css_background.asp 背景色 p {backg ...

  7. 转 C编译: 使用gdb调试

    C编译: 使用gdb调试   作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! gdb是the GNU Debugger的简称.它是 ...

  8. 对List中对象的去重

    今天项目中遇到了一个对List中对象去重的问题. 首先对于我们自己系统中的对象我们只要重写该对象的 equal 和 hashcode 即可(利用对象中的能够唯一确定对象的属性). 但是我遇到的不是本系 ...

  9. 写XML配置文件时的一个乱码问题

    首先我们用文本文档写一个xml文件如下: <?xml version="1.0" encoding="gb2312"?> <a> < ...

  10. mysql 事务隔离级别

    read-uncommitted(未提交读)测试流程:1.A设置read-uncommitted, start transaction2.B执行start transaction,修改一条记录,3.A ...