算法:dfs,连通块

难度:NOIP

题解:搜索出每个连通块,找到每个连通块里有几头牛。然后每个连通块内牛数量相乘,求和即为正解

不清楚luogu题解中的代码明明MLE了,却还能AC???

代码如下:

时间复杂度:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cmath>
#include <algorithm>
#define ll long long
#define N 35005
using namespace std;
int viss[105][105];
int cow[105][105];
struct node
{int x,y;
}cc[10005];
int n,k,r,col=1;
int usles;
bool edg[105][105][105][105];
void dfs(int x,int y)
{if(x+1<=n){if(!viss[x+1][y]){if(edg[x][y][x+1][y]||edg[x+1][y][x][y]){usles=4;}else{viss[x+1][y]=col;dfs(x+1,y);}}}if(x-1>0){if(!viss[x-1][y]){if(edg[x][y][x-1][y]||edg[x-1][y][x][y]){usles=4;}else{viss[x-1][y]=col;dfs(x-1,y);}}}if(y+1<=n){if(!viss[x][y+1]){if(edg[x][y][x][y+1]||edg[x][y+1][x][y]){usles=4;}else{viss[x][y+1]=col;dfs(x,y+1);}} }if(y-1>0){if(!viss[x][y-1]){if(edg[x][y][x][y-1]||edg[x][y-1][x][y]){usles=4;}else{viss[x][y-1]=col;dfs(x,y-1);}} }
}
int kk[10005];
int main()
{//126 MB//printf("%d\n",(sizeof(kk)+sizeof(edg)+sizeof(cc)+sizeof(cow)+sizeof(viss))/1024/1024);scanf("%d%d%d",&n,&k,&r);int tt=0; for(int i = 1;i <= r;i++){int a,b,x,y;scanf("%d%d%d%d",&a,&b,&x,&y);edg[a][b][x][y]=1;edg[x][y][a][b]=1;}int tto=0;for(int i = 1;i <= k;i++){int a,b;scanf("%d%d",&a,&b);cow[a][b]=1;cc[++tto].x=a;cc[tto].y=b;}for(int i = 1;i <= k;i++){if(!viss[cc[i].x][cc[i].y]){viss[cc[i].x][cc[i].y]=col; dfs(cc[i].x,cc[i].y);col++;}}for(int i = 1;i <= n;i++){for(int j = 1;j <= n;j++){if(cow[i][j]) kk[viss[i][j]]++;}}int ans=0;for(int i = 1;i < col;i++){for(int j = i+1;j < col;j++){ans+=kk[i]*kk[j];}}printf("%d\n",ans); return 0 ;
}

bzoj 4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III(Pu1 2018.10.1)相关推荐

  1. BZOJ 4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III

    本来想做一道搜索 最后发现是一道并查集的水题 如果两个邻格之间没有路 就把他们合并就行了 #include <cstdio> #include <cstring> #inclu ...

  2. bzoj 4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III

    题意: 给你一个n*n的地图 某对相邻的点中间有墙. 然后给你n个坐标,问你有多少对坐标不可以互相到达. 题解: 至今为止,提交记录就只有1个WA的-- 随便暴力做一下就好了.

  3. [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)

    传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...

  4. bzoj4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III(bfs)

    枚举每一个起点,bfs能访问到几头牛即可.复杂度O(n3)O(n^3) #include <cstdio> #include <cstring> #include <al ...

  5. BZOJ 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road

    题意很简单,不说了. 这题思路其实挺简单,把每个格编号为i,3*i走到3*j+1,3*i+1走到3*j+2,以此类推,注意一下边权是多少就好了. 然而一开始用分层图的普遍套路i+j*n*n编号调了好久 ...

  6. 【bzoj4994】[Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组

    题目描述 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 样例输入 4 3 2 4 4 1 3 2 1 样例输 ...

  7. BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III

    欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...

  8. bzoj4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III

    题意 给你一个n*n的地图 一开始两个相邻的点是有边的的 然后告诉你,某对相邻的点没有边了 然后给你n个坐标,问你有多少对坐标不可以互相到达 题解 随便暴力做一下就好了 至今为止,提交记录就只有1个W ...

  9. 【bzoj4992: [Usaco2017 Feb]Why Did the Cow Cross the Road】动规

    4992: [Usaco2017 Feb]Why Did the Cow Cross the Road Time Limit: 10 Sec   Memory Limit: 256 MB Submit ...

最新文章

  1. Java-JSON 解析
  2. linux下安装apache tomcat,Linux CentOS 7下 Apache Tomcat 7 安装与配置
  3. Linux sed 删除行命令常见使用详解
  4. Ureport2源码修改
  5. Web安全之Cookie劫持
  6. SQL Server复制需要有实际的服务器名称才能连接到服务器 错误解决方案
  7. 演练 玩游戏支付游戏币
  8. Solaris 中的环境变量
  9. android spi读写不通,Android-SPI学习笔记
  10. kubespray容器化部署kubernetes高可用集群
  11. python 对象转dict_python model对象转为dict数据
  12. “关起门来搞开源,做不了开源世界的Leader”
  13. 「docker实战篇」python的docker爬虫技术-在linux下mitmproxy介绍和安装(四)
  14. html5 密码强度,前端开发判断输入密码强度
  15. java script 下载_JavaScript下载
  16. html中车牌号省份简称输入键盘
  17. 72+常用Axure交互原型免费下载
  18. VLSI Basic4——antenna effect天线效应
  19. 暴力裁员绝症员工,网易刚刚道歉!丁磊沉默,刘强东意外刷屏:说了这句硬气的话……
  20. win7上的截图小工具

热门文章

  1. pandoc把word转为html,借助pandoc将Word文档成网页
  2. 模拟LED屏幕文字滚动开发和悬浮
  3. 水晶报表万能下载链接
  4. understanding eigrp soo bgp cost community
  5. 用js限制网页只在微信浏览器中打开,复制粘贴即可使用
  6. 地球物理:大地测量学综述002
  7. 技嘉主板BIOS恢复方法
  8. 汤晓丹的第四版计算机操作系统--第三章总结概述
  9. 转发:python的八荣八耻
  10. ActivePerl从源码安装模块