题目描述

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. centos 7.x安装 jdk1.8

    参考链接即可:本人已经操作实现,才推荐出来 https://blog.csdn.net/weixin_42266606/article/details/80863781

  2. 开源导入导出通用库Magicodes.ExporterAndImporter发布

    导入导出通用库 Magicodes.ExporterAndImporter为心莱团队封装的导入导出通用库,并且仍在跟随项目不断地打磨. GitHub地址: https://github.com/xin ...

  3. Falsk中的Request、Response

    Flask 中的Response 1.HTTPResponse('helloword') "helloword" from flask import Flask # 实例化Flas ...

  4. EditPlus 全系列 注册码

    EditPlus4注册码 注册名:host1991 序列号:14F50-CD5C8-E13DA-51100-BAFE6  注册名:360xw 注册码:93A52-85B80-A3308-BF130-4 ...

  5. html中的空格

    网上摘录: HTML提供了6种空格实体.除第一种外,其他几种空格在不同浏览器中宽度各异.               它叫不换行空格,全称No-Break Space,它是最常见和我们使用最多的空格, ...

  6. 从零开始OpenGL—— 一、 环境配置

    前言 高考完之后填志愿,当时想以后去做游戏,所以选择了计算机这个专业,之前捣鼓过U3D,这学期也开始了计算机图形学的学习,最近学习了OpenGL相关的一些内容,将在博客中记录这系列的学习.这篇开篇博客 ...

  7. Spring boot 梳理 - 显示Springboot默认自动生成的bean

    @Autowired public ApplicationContext context; @Bean public ViewResolver freeMarkerViewResolver(){ St ...

  8. quartz-scheduler定时器实现

    第一步,在pom.xml中引入quartz-scheduler. <dependency> <groupId>org.quartz-scheduler</groupId& ...

  9. HDU 1159——Common Subsequence(DP)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题解 #include<iostream> #include<cstring> ...

  10. 浅谈个人对客户端JavaScript同步、异步、执行顺序等概念的理解

    一.同步和异步的概念. 同步:即按代码的顺序执行任务. 在下列代码中,按照同步概念,则是先打印1后打印2. console.log(1); console.log(2); 异步:即执行一个任务的同时执 ...