Codeforces 325D

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
using namespace std; const int maxn=**+;
const int dx[]={-, -, -, , , , , };
const int dy[]={-, , , -, , -, , }; int n, r, c, ti;
int ans;
int fa[maxn];
bool vis[][];
int mark[maxn]; int find(int cur)
{
if (fa[cur]<) 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< || x1>r) return false;
if (y1==) y1=c;
else if (y1>c) y1=;
if (!vis[x1][y1]) return false;
return true;
}
void merge(int x, int y)
{
int nid=(x-)*c+y;
for (int i=; i<; ++i)
{
int x1=x+dx[i];
int y1=y+dy[i];
if (check(x1, y1)) Union(nid, (x1-)*c+y1);
}
}
bool get_list(int x, int y, int id)
{
for (int i=; i<; ++i)
{
int x1=x+dx[i];
int y1=y+dy[i];
if (!check(x1, y1)) continue;
int tmp=find((x1-)*c+y1);
if (id && mark[tmp]==ti-) return false;
mark[tmp]=ti;
}
return true;
}
void solve()
{
if (c==) return;
c*=; for (int i=; i<=r; ++i)
for (int j=; j<=c; ++j)
fa[(i-)*c+j]=-; for (int i=; i<=n; ++i)
{
int x, y;
scanf("%d%d", &x, &y);
++ti;
get_list(x, y, );
++ti;
bool flag=get_list(x, y+c/, );
if (!flag) continue;
++ans;
merge(x, y);
merge(x, y+c/);
vis[x][y]=true;
vis[x][y+c/]=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 ;
}
Codeforces 325D的更多相关文章
- Codeforces Round #325D (Div. 2) (DP)
题目链接: D. Phillip and Trains 分析:dp 我们先初始化,dp[i]表示当前列第i行是否可达,r[i]表示上一个dp值,接下来从头搜到尾 如果该位置满足s[i+1]=='.'且 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- ServletConfig和ServletContext
ServletConfig和ServletContext Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为Servle ...
- C语言学习笔记frist---输入两个数比较大小
C#学习中,问道艰辛,今自C学起,第一个函数学习:输入两个数比较大小,仅作练习: #include "stdafx.h" #include<stdio.h> // 包含 ...
- python操作redis--string
#!/usr/bin/python #!coding:utf-8 """ 完成用redis模块操作string类型的数据 """ impor ...
- hightchart or hightstock 格式Y数据
hightchart or hightstock 格式Y数据,鼠标放在上面显示两位小数 方法一: tooltip: { shared: true, crosshairs: true , formatt ...
- stream~filestream
http://blog.csdn.net/feliciafay/article/details/6157356 http://blog.csdn.net/feliciafay/article/deta ...
- delete 多表删除的使用(连表删除)
delete 多表删除的使用 1.从数据表t1中把那些id值在数据表t2里有匹配的记录全删除掉 DELETE t1 FROM t1,t2 WHERE t1.id=t2.id 或 DELETE ...
- openGL 初试 绘制三角形 和添加鼠标键盘事件
code: #include <gl/glut.h> #include <stdlib.h> void render(void); void keyboard(unsigned ...
- 命名空间“Aspose”中不存在类型或命名空间名称“Slides”。
有可能引用的dll与项目的 .netFramework版本不同,需要确认两个版本是否相同.如果不同,项目右键->属性->应用程序.选择和引用的dll相同的版本.
- IOC原理分析
IOC(inversion of control)控制反转 在我们的程序中,要实现某个功能,我们都会用到两个或两个以上的类来协同完成,那么在一个类中,我们就会要有它的合作类的引用,也就是说这个类依赖于 ...
- 模块化利器:RequireJS常用知识
1. 模块化 目前常见的模块化开发方式,全局空间方式是最基本的一种,另外常见的还有遵循AMD规范的开发方式,遵循CMD规范的开发方式,和ECMAScript 6的开发方式.需要说明的是,CMD和ES6 ...