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 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- ajax接收返回值获取不到问题
function testAsync() { //定义一个全局变量来接受$post的返回值 var result; //用ajax的“同步方式”调用一般处理程序 $.ajax({ url: " ...
- js中的 !!
就是这样:!!variable.哈哈,其实我也是在代码里面看见别人这样用,当时很好奇,所以就搜了一下,哈哈.还真的有很多相关的好文啊.作者是这样说的, 一般用来将后面的表达式转换为布尔型的数据 是不是 ...
- Android SDK 更新镜像服务器
搞个新的电脑,新环境下,SDK总是更新不成功.找了一下,发现有国内的镜像,记录保存一下. Android Tools Android SDK在线更新镜像服务器 中国科学院开源协会镜像站地址: ...
- XAF应用开发教程(七)外观控制模块
很多时候,我们需要按照不同的条件显示不同的效果,在传统的软件开发中,我们会直接使用 控件名称.BackColor,Enable,Visible等属性进行控制. 如果一个业务对象在多处使用,要么我们会去 ...
- jsp get方式请求参数中包含中文乱码问题解决
1. 自己接收到参数之后在后台进行转码处理 2: 修改tomcat的配置文件 server.xml <Connector port="8080" protocol=&quo ...
- JS完成改变新闻字体大中小的显示
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- views中的公共代码放在一起
在views中建立一个common.php文件,然后把views中的index.php和about.php公共代码放进去 <!doctype html> <html lang=&qu ...
- (转)Java + Excel 接口自动化
最近项目比较悠闲,想找点事干,写了个 Excel 接口测试的 "框架" 以前用 python 写过一个,这次用 java, 应该说框架都不算,反正就是写了,能帮我解决问题就行. 当 ...
- HTML5Canvas标签
- html5移动端制作知识点总结
一.测试工具:1.Chrome 2.Opera Mobile二.分辨率:一般现代手机最小320px,最大640px.三.全屏流体设计: 1.腾讯新闻:http://xw.qq.com/ 2.途牛旅游: ...