题目描述

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. Android进程的优先级说明

    引言 Android系统尽可能长时间地保持应用程序进程,但为了新建或者运行更加重要的进程,总是需要清除一些进程来回收内存.为了决定保留或终止哪个进程,根据进程内运行的组件及这些组件的状态,系统把每个进 ...

  2. IO流 - 字节输入输出流,文件的复制

    IO流 I:input - 输入(读取),eg:把硬盘的内容读取到内存 O: output - 输出(写入) eg:把内存中的东西写入硬盘保存 流:数字(字符/字节) 一般1个字符=2Byte,1By ...

  3. JS实现数组去重的方法

    1.使用ES6的Set进行去重 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  4. [Linux] linux下vim对于意外退出的文档的再次开启

    转载自博客:https://blog.csdn.net/ljp1919/article/details/48372615 1.对于同一个文件如果上次已经打开,而未关闭的情况下,又打开该文件进行编辑时, ...

  5. BUG 的生命周期

    BUG 的生命周期 Bug-->软件程序的漏洞或缺陷 Bug 的类型:代码错误.设计缺陷.界面优化.性能问题.配置相关.安装部署.安全相关.标准规划.测试脚本....其他(功能类.界面类.性能类 ...

  6. video.js 使用中抛出异常:DOMException: "'#1098942864706113536' is not a valid selector"

    原因:video.js 在获取页面元素时使用的是querySelector方法,由于querySelector是按css规范来实现的,所以它传入的字符串中第一个字符不能是数字. 解决:元素Id在赋值时 ...

  7. Maven 梳理 - 常用三种archetype说明

    archetype:原型的意思,可理解为Maven项目模板工具包 常用archetype 1.cocoon-22-archetype-webapp 2.maven-archetype-quicksta ...

  8. 详解Java多线程锁之synchronized

    synchronized是Java中解决并发问题的一种最常用的方法,也是最简单的一种方法. synchronized的四种使用方式 修饰代码块:被修饰的代码块称为同步语句块,其作用的范围是大括号{}括 ...

  9. JavaScript 类型 检测

    前言 ECMAScript中有5种数据类型,分别为Number,Boolean,Null,Undifined和String,以及一种复杂的数据类型Object(由名值对组成,是这门语言所有对象的基础类 ...

  10. iOS开发进阶(唐巧)读书笔记(一)

    如何提高iOS开发技能 1.阅读博客:https://github.com/tangqiaoboy/iOSBlogCN 40多位iOS开发博主的博客地址 2.读书:每年阅读一本高质量的iOS开发书籍 ...