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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 解决ERROR C2011: 'FD_SET' : 'STRUCT' TYPE REDEFINITION问题
在socket编程的过程中头文件中 #include <windows.h> #include "stdafx.h" #include "WinSock2.h ...
- HDU 1312 Red and Black(bfs)
Red and Black Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descr ...
- vs2005的MFC程序在64位机上不能运行
出现上述错误大多因为该机上没有安装vs2005程序,vs2005要运行需要一些必要的文件,没有添加到你发布的可执行文件目录下,所以程序不能运行. 解决方法: 安装目录\Microsoft Visual ...
- SIEM思考
https://securosis.com/blog/comments/understanding-and-selecting-siem-log-management-introduction/ ht ...
- 【二叉树->链表】二叉树结构转双向线性链表结构(先序遍历)
二叉树存储结构属于非线性链表结构,转化成线性链表结构,能简化操作和理解.然而由非线性转线性需要对整个树遍历一次,不同的遍历方式转化结果页不一样.下面以先序为例. 方法一: 递归法.递归遍历二叉树,因为 ...
- poj 1458 Common Subsequence_最长公共子串
题意:略 求最长公共子串 #include<iostream> #include<cstdio> #include<string> using namespace ...
- systemctl 命令完全指南
http://www.linuxidc.com/Linux/2015-07/120833.htm Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器. System ...
- ROS Node/Topic/Message/Service的一些问题
1.Node http://blog.exbot.net/archives/1412 (摘自老王说ros) node干的什么活?callback queue里的活.这个callback queue里的 ...
- hdu 4521 线段树改点求点的应用
小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- jquery的clone办法bug修复
发现测试,textarea和select的jquery的clone有问题的方法,textarea和select值clone时间会输.这是发现jquery一个bug,上不了的能够看下代码.比較简单.就是 ...