Codeforces325 D【并查集维护连通性】
参考:大牛blog
思路:
因为是环,所以可以复制一下图,先判断一下和他是不是和与他相邻的8个之一的一个障碍使得构成了一个环,环就是一个连通,用并查集维护即可;
如果没有就ans++,然后并把这个点加入。
大致意思就是这样。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
using namespace std; const int maxn=3000*3000*2+100;
const int dx[8]={-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[8]={-1, 0, 1, -1, 1, -1, 0, 1}; int n, r, c, ti;
int ans;
int fa[maxn];
bool vis[3010][6010];
int mark[maxn]; int find(int cur)
{
if (fa[cur]<0) return cur;
else return (find(fa[cur]));
}
void Union(int u, int v)
{
u=find(u);
v=find(v);
if (u==v) return;
if (fa[u]>fa[v]) swap(u, v);
fa[u]+=fa[v];
fa[v]=u;
}
bool check(int &x1, int &y1)
{
if (x1<1 || x1>r) return false;
if (y1==0) y1=c;
else if (y1>c) y1=1;
if (!vis[x1][y1]) return false;
return true;
}
void merge(int x, int y)
{
int nid=(x-1)*c+y;
for (int i=0; i<8; ++i)
{
int x1=x+dx[i];
int y1=y+dy[i];
if (check(x1, y1)) Union(nid, (x1-1)*c+y1);
}
}
bool get_list(int x, int y, int id)
{
for (int i=0; i<8; ++i)
{
int x1=x+dx[i];
int y1=y+dy[i];
if (!check(x1, y1)) continue;
int tmp=find((x1-1)*c+y1);
if (id && mark[tmp]==ti-1) return false;
mark[tmp]=ti;
}
return true;
} void solve()
{
if (c==1) return;
c*=2; for (int i=1; i<=r; ++i)
for (int j=1; j<=c; ++j)
fa[(i-1)*c+j]=-1; for (int i=1; i<=n; ++i)
{
int x, y;
scanf("%d%d", &x, &y);
++ti;
get_list(x, y, 0);
++ti;
bool flag=get_list(x, y+c/2, 1);
if (!flag) continue;
++ans;
merge(x, y);
merge(x, y+c/2);
vis[x][y]=true;
vis[x][y+c/2]=true;
}
} int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d%d%d", &r, &c, &n);
solve();
printf("%d\n", ans);
return 0;
}
Codeforces325 D【并查集维护连通性】的更多相关文章
- 2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)
题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效.问有多少种通行证可以使得1和N连通. 思路:和bzoj魔法森林有点像,LCT维护最小生成树. 开始和队友 ...
- BZOJ2733:使用并查集维护连通性之后用线段树维护+线段树合并(动态开点)
可以说是线段树合并的裸题吧 题意就是给你两个操作 一个操作是合并两个集合,这两个集合都是用权值线段树维护的,便于查询第k小元素 另一个操作就是查询区间极值了 #include<cstdio> ...
- hihoCoder #1291 : Building in Sandbox 逆向处理+并查集维护
/** 题目:#1291 : Building in Sandbox 链接:https://hihocoder.com/problemset/problem/1291 题意:就是一个三维的空间里,按照 ...
- [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]
题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...
- Wannafly挑战赛14 - E 并查集维护线性基区间
给一个1-base数组{a},有N次操作,每次操作会使一个位置无效.一个区间的权值定义为这个区间里选出一些数的异或和的最大值.求在每次操作前,所有不包含无效位置的区间的权值的最大值. 线性基删除不知道 ...
- poj 1456 Supermarket(并查集维护区间)
题意:有一些货物,每一个货物有价值和卖出的截至日期,每天能够卖一个货物,问能卖出的最大价值是多少. 思路:算法不难想到,按价值降序排列.对于每一件货物,从deadline那天開始考虑.假设哪天空 ...
- The Suspects(并查集维护根节点信息)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 37090 Accepted: 17980 De ...
- 【uva12232/hdu3461】带权并查集维护异或值
题意: 对于n个数a[0]~a[n-1],但你不知道它们的值,通过逐步提供给你的信息,你的任务是根据这些信息回答问题: I P V :告诉你a[P] = V I P Q V:告诉你a[P] XOR a ...
- CodeForces-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合
题目链接:https://cn.vjudge.net/problem/CodeForces-766D 题意 写词典,有些词是同义词,有些是反义词,还有没关系的词 首先输入两个词,需要判断是同义还是是反 ...
随机推荐
- CSS常识
1.给一个div设置边框:border:1px #CCCCCC bold; 2.给DOM加小手:cursor:pointer; 取消小手:cursor:auto;
- bluebird-api简介及demo
var Promise = require("bluebird"); var fs = require("fs"); //方法Promise化 var read ...
- mac shell命令连接mongo
1. 安装rebomongo 2. mongo 192.168.1.100/databasename -u lsg -p 123456 3.db.drawspecs.find({name:'prize ...
- HLS切片机
参考: 1,linux下搭建生成HLS所需的.ts和.m3u8文件http://www.cnblogs.com/mystory/archive/2013/04/07/3006200.html2,iPh ...
- Centos查看端口占用情况
Centos查看端口占用情况命令,比如查看80端口占用情况使用如下命令: lsof -i tcp:80 列出所有端口 netstat -ntlp 结束进程: kill 进程代码
- hdu1015 Safecracker —— 回溯
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1015 代码1: #include<stdio.h>//hdu1015 #include&l ...
- Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem —— 贪心
题目链接:http://codeforces.com/contest/761/problem/D D. Dasha and Very Difficult Problem time limit per ...
- ffplay 一些好玩的filter
添加字幕:ffplay -vf drawtext="fontfile=arial.ttf: text='Test Text': x=100: y=300: \ fontsize=48: fo ...
- Android5.0 CheckBox颜色修改
Android5.0开始,CheckBox带有material design动画效果,其默认的样式如下图所示: 可以看到,在上图中,CheckBox的边框为灰色,当被选中后,填充色为绿色. 那么如果我 ...
- C# 多线程控制 通讯 和切换
一.多线程的概念 Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一 ...