题目描述

Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of roads, making it impossible for his cows to travel around without crossing many of them.

为什么牛过马路? 其中一个简单的原因就是农民约翰的农场有很多道路,使得他的母牛不得不穿越许多道路。

FJ's farm is arranged as an N×NN \times NN×N square grid of fields ( 2≤N≤1002 \leq N \leq 1002≤N≤100 ), Certain pairs of adjacent fields (e.g., north-south or east-west) are separated by roads, and a tall fence runs around the external perimeter of the entire grid, preventing cows from leaving the farm. Cows can move freely from any field to any other adjacent field (north, east, south, or west), although they prefer not to cross roads unless absolutely necessary.

FJ的农场在 N×NN\times NN×N 的网格中( 2≤N≤1002\le N\le 1002≤N≤100 ),某些相邻的区域(例如,南北或东西)由道路分隔,高大的围栏围绕着整个格栅的外围,防止牛离开农场。 牛可以从任何场地自由移动到任何其他相邻的区域(北,东,南或西),不过除非不得已,她们并不愿意穿越道路。

There are KKK cows ( 1≤K≤100,K≤N21 \leq K \leq 100, K \leq N^21≤K≤100,K≤N2 ) on FJ's farm, each located in a different field. A pair of cows is said to be "distant" if, in order for one cow to visit the other, it is necessary to cross at least one road. Please help FJ count the number of distant pairs of cows.

在FJ的农场有 KKK 头牛( 1≤K≤100,K≤N21\le K\le 100,K\le N^{2}1≤K≤100,K≤N2 ),每个位于不同的区域。 定义一对牛是“遥远的”,是指让一头牛访问另一头牛时,必须至少穿过一条路。 请帮助FJ计算有多少对牛是“遥远的”。

输入输出格式

输入格式:

The first line of input contains NNN , KKK , and RRR . The next RRR lines describe RRR roads that exist between pairs of adjacent fields. Each line is of the form rrr ccc r′r'r′ c′c'c′ (integers in the range 1…N1 \ldots N1…N ), indicating a road between the field in (row rrr , column ccc ) and the adjacent field in (row r′r'r′ , column c′c'c′ ). The final KKK lines indicate the locations of the KKK cows, each specified in terms of a row and column.

第一行输入包含 NNN , KKK 和 RRR 。 接下来的 RRR 行描述存在于相邻区域对之间的 RRR 条路。 每行的格式为 rrr ; ccc ; r′r'r′ ; c′c'c′ (都是在 1...N1...N1...N 中的整数),表示在两个相邻的区域(第 rrr 行第 ccc 列,和第 $r​'$ ​​ 行第 $c​'$ ​​ 列)之间的路。 最终的 KKK 行表示 KKK 头牛的位置,也用行列来表示。

输出格式:

Print the number of pairs of cows that are distant.

输出遥远的牛数量对。

输入输出样例

输入样例#1:

3 3 3
2 2 2 3
3 3 3 2
3 3 2 3
3 3
2 2
2 3
输出样例#1:

2

无脑爆搜;
首先我想到的是对于每一个牛都bfs一遍;
可想而知T的很惨
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std; inline int read(){int res=;bool flag=;char ch=getchar();while(!isdigit(ch)){if(res=='-')flag=;ch=getchar();}while(isdigit(ch)){res=(res<<)+(res<<)+(ch-'');ch=getchar();}return flag?-res:res;} int dx[] = {, -, , , }, dy[] = {, , , -, }; int n, k, r; bool can[][][]; int mp[][];
int cowx[], cowy[]; int ans[][];
bool vis[][]; struct date
{
int x;
int y;
};
inline void bfs(int sx, int sy)
{
queue <date> q;
while (!q.empty()) q.pop();
q.push((date){sx, sy}); while (!q.empty())
{
int x = q.front().x, y = q.front().y;
q.pop();
vis[x][y] = ;
for (register int i = ; i <= ; i ++)
{
if (can[x][y][i]) continue;
int tx = x + dx[i], ty = y + dy[i];
if (tx <= or tx > n or ty <= or ty > n) continue;
if (vis[tx][ty]) continue;
q.push((date){tx, ty});
}
} } int main()
{
n = read(), k = read(), r = read();
for (register int i = ; i <= r ; i++)
{
int a = read(), b = read(), x = read(), y = read();
if (a == x)
{
if (b == y + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
else
{
if (a == x + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
} for (register int i = ; i <= k ; i ++)
{
int x = read(), y = read();
mp[x][y] = ;
cowx[i] = x, cowy[i] = y;
} for (register int i = ; i <= k ; i ++)
{
memset(vis, , sizeof vis);
bfs(cowx[i], cowy[i]);
for (register int j = ; j <= k ; j ++)
{
if (i == j) continue;
if (vis[cowx[j]][cowy[j]] == )
{
ans[i][j] = ;
}
}
} int res = ;
for (register int i = ; i <= k ; i ++)
{
for (register int j = ; j <= i ; j ++)
{
if (ans[i][j]) res++;
}
}
cout << res << endl;
return ;
}

zZhBr

然后又想我们可以搜出图中所有的联通块, 然后直接暴力相加;great√!

可是现实却是这样

残酷现实;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std; inline char nc()
{
static const int BS = << ;
static unsigned char buf[BS],*st,*ed;
if(st == ed) ed = buf + fread(st=buf,,BS,stdin);
return st == ed ? EOF : *st++;
} inline int read(){int res=;bool flag=;char ch=nc();while(!isdigit(ch)){if(res=='-')flag=;ch=nc();}while(isdigit(ch)){res=(res<<)+(res<<)+(ch-'');ch=nc();}return flag?-res:res;} int dx[] = {, -, , , }, dy[] = {, , , -, }; int n, k, r; bool can[][][];
int mp[][];
int color[][];
int col;
int cowx[], cowy[]; struct date
{
int x;
int y;
};
inline void bfs(int sx, int sy)
{
queue <date> q;
while (!q.empty()) q.pop();
q.push((date){sx, sy}); while (!q.empty())
{
int x = q.front().x, y = q.front().y;
q.pop();
color[x][y] = col;
for (register int i = ; i <= ; i ++)
{
if (can[x][y][i]) continue;
int tx = x + dx[i], ty = y + dy[i];
if (tx <= or tx > n or ty <= or ty > n) continue;
if (color[tx][ty]) continue;
q.push((date){tx, ty});
}
} } int main()
{
n = read(), k = read(), r = read();
for (register int i = ; i <= r ; i++)
{
int a = read(), b = read(), x = read(), y = read();
if (a == x)
{
if (b == y + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
else
{
if (a == x + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
} for (register int i = ; i <= k ; i ++)
{
int x = read(), y = read();
mp[x][y] = ;
cowx[i] = x, cowy[i] = y;
} for (register int i = ; i <= k ; i ++)
{
if (!color[cowx[i]][cowy[i]])
{
col++;
bfs(cowx[i], cowy[i]);
}
}
int res = ;
for (register int i = ; i <= k ; i ++)
{
for (register int j = i + ; j <= k ; j ++)
{
if (color[cowx[i]][cowy[i]] != color[cowx[j]][cowy[j]]) res++;
}
}
printf("%d\n", res);
return ;
}

zZhBr

咳咳!stl队列常数巨大!
所以又手写队列;
WA!
what?
查了一年...
woc
我染色的时候顺序写错了! 真正代码:
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std; #define nc getchar
inline int read(){int res=;bool flag=;char ch=nc();while(!isdigit(ch)){if(res=='-')flag=;ch=nc();}while(isdigit(ch)){res=(res<<)+(res<<)+(ch-'');ch=nc();}return flag?-res:res;} int dx[] = {, -, , , }, dy[] = {, , , -, }; int n, k, r; bool can[][][];
int color[][];
int col;
int cowx[], cowy[]; struct date
{
int x;
int y;
}q[*];
inline void bfs(int sx, int sy)
{
int l = , r = ;
q[l] = (date){sx, sy};
color[sx][sy] = col; while (l <= r)
{
int x = q[l].x, y = q[l++].y;
for (register int i = ; i <= ; i ++)
{
if (can[x][y][i]) continue;
int tx = x + dx[i], ty = y + dy[i];
if (tx <= or tx > n or ty <= or ty > n) continue;
if (color[tx][ty]) continue;
color[tx][ty] = col;
q[++r] = (date){tx, ty};
}
}
} int main()
{
n = read(), k = read(), r = read();
for (register int i = ; i <= r ; i++)
{
int a = read(), b = read(), x = read(), y = read();
if (a == x)
{
if (b == y + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
else
{
if (a == x + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
} for (register int i = ; i <= k ; i ++)
{
int x = read(), y = read();
cowx[i] = x, cowy[i] = y;
} for (register int i = ; i <= k ; i ++)
{
if (!color[cowx[i]][cowy[i]])
{
col++;
bfs(cowx[i], cowy[i]);
}
}
int res = ;
for (register int i = ; i <= k ; i ++)
{
for (register int j = i + ; j <= k ; j ++)
{
if (color[cowx[i]][cowy[i]] != color[cowx[j]][cowy[j]]) res++;
}
}
printf("%d\n", res);
return ;
}
 
颓颓颓了一晚上, 明天上课, 现在作业还没动, 凉凉;
不说了颓作业去;

[USACO17FEB]Why Did the Cow Cross the Road III S的更多相关文章

  1. 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S

    P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...

  2. [USACO17FEB]Why Did the Cow Cross the Road III P

    [USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...

  3. 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)

    题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...

  4. 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III

    题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...

  5. P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】

    题外话:维护区间交集子集的小套路 开两个树状数组,一个维护进入区间,一个维护退出区间 $Query:$ 给定询问区间$l,r$和一些其他区间,求其他区间中与$[l,r]$交集非空的区间个数 用上面维护 ...

  6. [USACO17FEB]Why Did the Cow Cross the Road III P(CDQ分治)

    题意 两列$n$的排列,相同的数连边,如果一对数有交叉且差的绝对值$>k$,则$++ans$,求$ans$ 题解 可以把每一个数字看成一个三元组$(x,y,z)$,其中$x$表示在第一列的位置, ...

  7. [USACO17FEB]Why Did the Cow Cross the Road III G

    嘟嘟嘟 首先看到这种序列的问题,我就想到了逆序对,然后就想如何把这道题转化. 首先要满足这个条件:ai <bi.那么我们把所有数按第一次出现的顺序重新赋值,那么对于新的数列,一定满足了ai &l ...

  8. [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)

    题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...

  9. P3660 [USACO17FEB]Why Did the Cow Cross the Road III G

    Link 题意: 给定长度为 \(2N\) 的序列,\(1~N\) 各处现过 \(2\) 次,i第一次出现位置记为\(ai\),第二次记为\(bi\),求满足\(ai<aj<bi<b ...

随机推荐

  1. Dart类型变量-表示信息

    Dart执行入口 Dart要求以main函数作为执行的入口 Dart的变量和类型 在Dart中可以用var或者具体的类型来声明一个变量.当使用var定义变量时,表示类型是由编译器推断决定.使用静态类型 ...

  2. Java中常用IO流之文件流的基本使用姿势

    所谓的 IO 即 Input(输入)/Output(输出) ,当软件与外部资源(例如:网络,数据库,磁盘文件)交互的时候,就会用到 IO 操作.而在IO操作中,最常用的一种方式就是流,也被称为IO流. ...

  3. Matlab 模拟退火算法模型代码

    function [best_solution,best_fit,iter] = mySa(solution,a,t0,tf,Markov) % 模拟退化算法 % ===== 输入 ======% % ...

  4. 基于RMAN搭建DataGuard,使用Broker管理DataGuard

    一.环境准备 1.数据库软件准备 (1).在主节点,安装单机数据库软件并创建数据库. (2).在备库, 安装单机数据库软件, 但是不创建数据库. 2.操作系统配置 在/etc/hosts下面配置主机名 ...

  5. [Leetcode] 第307题 区域和检索-数组可修改

    参考博客:(LeetCode 307) Range Sum Query - Mutable(Segment Tree) 一.题目描述 给定一个整数数组  nums,求出数组从索引 i 到 j  (i  ...

  6. 使用gdb调试c++程序

    上篇(使用c++开发跨平台程序)说到,我不怕造东西,我怕的是造出来的东西,如果出了问题,我却不知道原因.所以调试分析是一个重要的手段. C++调试是一个复杂的活.虽然大部分调试可以通过IDE在开发期间 ...

  7. Mysql - 关于relay_log_recovery参数的测试

    一.概述 官方文档中对relay_log_recovery参数的解释 Enables automatic relay log recovery immediately following server ...

  8. DW网页代码笔记

    DW网页代码笔记 1.样式.       class  插入类样式  标签技术(html)解决页面的内容样式技术(css)解决页面的外观脚本技术       解决页面动态交互问题<form> ...

  9. MySQL之增删改查之

    MySQL之增删改查   前言:以下是MySQL最基本的增删改查语句,很多IT工作者都必须要会的命令,也是IT行业面试最常考的知识点,由于是入门级基础命令,所有所有操作都建立在单表上,未涉及多表操作. ...

  10. Flume系列二之案例实战

    Flume案例实战 写在前面 通过前面一篇文章http://blog.csdn.net/liuge36/article/details/78589505的介绍我们已经知道flume到底是什么?flum ...