题目链接: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. LINQ之路 5:LINQ查询表达式

    书写LINQ查询时又两种语法可供选择:方法语法(Fluent Syntax)和查询表达式(Query Expression). LINQ方法语法的本质是通过扩展方法和Lambda表达式来创建查询.C# ...

  2. 小型网站如何防范DDoS攻击

    ddos(Distributed Denial of Service,分布式拒绝服务攻击),俗称洪水攻击.是在传统的DoS攻击基础之上产生的新的破坏力更强的攻击方式.分布式拒绝服务攻击是指借助于客户/ ...

  3. 关于JAVA中URL传递中文参数,取值是乱码的解决办法

    前几天看到有网友在问URLDecoder和URLEncoder方面的使用问题,突然想起,原来我刚遇到这两个类时,也觉得很神密,由此可以想想初学者的心情,于是便有了今天的这篇文章. 其实,这两个类的使用 ...

  4. Myeclipse 主题下载

    Myeclipse 主题下载 下载网址: 1. 编辑区背景主题:http://www.eclipsecolorthemes.org 2. 框架主题:https://marketplace.eclips ...

  5. JAVASCRIPT事件详解-------原生事件基础....

    javaScirpt事件详解-原生事件基础(一)   事件 JavaScript与HTML之间的交互是通过事件实现的.事件,就是文档或浏览器窗口中发生的一些特定的交互瞬间,通过监听特定事件的发生,你能 ...

  6. strcpy, memcpy, memset函数

    一. strcpy函数 原型声明:char *strcpy(char* dest, const char *src);   头文件:#include <string.h> 和 #inclu ...

  7. Hashtable HashMap

    Hashtable和HashMap类有三个重要的不同之处.第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现. ...

  8. PHP的UTF-8中文转拼音处理类(性能已优化至极致)

    <?php /** * PHP 汉字转拼音 * @author Jerryli(hzjerry@gmail.com) * @version V0.20140715 * @package SPFW ...

  9. centos 解压rar文件

    默认情况下centos是不支持winrar的解压与打包的,需要安装winrar的linux版本程序 首先得下载解压软件 wget http://www.rarsoft.com/rar/rarlinux ...

  10. 转!!Java JTable 根据表格内容 自动调整表格列宽

    //根据表格内容 自动调整列宽http://blog.sina.com.cn/s/blog_5e54d6140100s1d3.html