解题笔记——NIT 遥远的村庄
某个小镇有 N 个村庄,村庄编号1-N,给出 M 条单向道路,不存在环,即不存在 村庄A可以到达村庄B 且 村庄B也可以到达村庄A的情况。
如果村庄A与村庄B之间存在一条单向道路,则说村庄A和村庄B之间存在联系,联系具有无向性,即如果村庄A和村庄B有联系,则村庄B和村庄A有联系;联系具有传递性,即如果存在村庄A和村庄B有联系,村庄B和村庄C有联系,则村庄A和村庄C有联系。
现在小镇要在某些村庄里建垃圾站,对建垃圾站的村庄要满足与之有联系的村庄都可以通过单向道路到达该村庄。问小镇能建多少个垃圾站。
Input
输入包含多组数据,对于每组数据:
第一行输入N,M,数据范围1<=n<=1000,0<=m<10000。
接下来M行,每行两个整数,s,t,代表 s 可以到达 t 。
Output
对于每组数据,输出一个整数代表答案。
Sample Input
10 6
1 2
2 3
3 4
6 7
7 8
9 10
Sample Output
4
Hint
案例中有10个村庄,道路情况如下:
1->2->3->4
5
6->7->8
9->10
村庄4可以建垃圾站,因为与之有联系的村庄都能到达他;同理村庄5,村庄8,村庄10,所以答案有4个。
解题思路:
要建垃圾站的话,该村庄所在的关于村庄联系的联通块内的结点数和他的单向道路能到达的结点数相同,但是建边要反向建,本来u到v的边要建成v到u的边,用并查集算各个联通块内结点数,然后dfs算能到达多少个结点再跟该联通块内结点总数比较,相等的话说明可以建垃圾站
难点在看不懂题意
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define INF 0x3f3f3f3f
const ll MAXN = 1e3 + ;
const ll MAXM = 1e4 + ;
const ll MOD = 1e9 + ;
const double pi = acos(-);
int n, m;
int vis[MAXN];
int pre[MAXN];
vector<int> V[MAXN];
int root[MAXN]; //根结点的位置记录联通块内结点数
int cnt;
void init()
{
memset(vis, , sizeof(vis));
memset(root, , sizeof(root));
for (int i = ; i <= n; i++)
pre[i] = i, V[i].clear();
}
int find(int x) //查找根结点
{
int r = x;
while (r != pre[r]) //寻找根结点
r = pre[r];
int i = x, j;
while (pre[i] != r) //路径压缩
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;
} //并查集求联通块块数
//遍历有向图结点
void DFS(int u)
{
cnt++;
vis[u] = ;
for (int i = ; i < V[u].size(); i++)
{
int v = V[u][i];
if (!vis[v])
{ //如果该节点未被访问,则深度遍历
DFS(v);
}
}
}
int main()
{
while (~scanf("%d%d", &n, &m))
{
init();
int ans = ;
for (int i = ; i < m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
V[v].push_back(u);
int fa = find(u);
int fb = find(v);
if (fa != fb)
pre[fa] = fb;
}
for (int i = ; i <= n; i++)
root[find(i)]++;
for (int i = ; i <= n; i++)
{
memset(vis, , sizeof(vis));
cnt = ;
DFS(i);
if (cnt == root[find(i)])
ans++;
}
printf("%d\n", ans);
}
return ;
}
/* 14 7
1 8
2 8
4 5
7 8
9 7
6 7
6 8
*/
解题笔记——NIT 遥远的村庄的更多相关文章
- 《剑指offer》解题笔记
<剑指offer>解题笔记 <剑指offer>共50题,这两周使用C++花时间做了一遍,谨在此把一些非常巧妙的方法.写代码遇到的难点.易犯错的细节等做一个简单的标注,但不会太过 ...
- 122. Best Time to Buy and Sell Stock(二) leetcode解题笔记
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 2016/9/21 leetcode 解题笔记 395.Longest Substring with At Least K Repeating Characters
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode解题笔记 - 2. Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- HDU-5902-GCD is Funny解题笔记
Alex has invented a new game for fun. There are n integers at a board and he performs the following ...
- CTF实验吧-WEB题目解题笔记(1)简单的登陆题
1.简单的登陆题 解题链接: http://ctf5.shiyanbar.com/web/jiandan/index.php Burp抓包解密 乱码,更换思路.尝试id intruder 似乎也没什 ...
- PTA 乙级解题笔记 1001 害死人不偿命的(3n+1)猜想
卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 (3n+1) 砍掉一半.这样一直反复砍下去,最后一定在某一步得到 n=1.卡拉兹在 1950 ...
随机推荐
- 用ubuntu里的vim搭建一个apache2+php+mysql环境一路踩的坑
先是安装apache2,这个很顺利,一个apt install apache就搞定了. (PS:查看linux是否已经安装了apache服务,可以通过执行apachectl -v,如果安装了的话会显示 ...
- 33.python之操作系统,进程,线程
转载:https://www.cnblogs.com/yuanchenqi/articles/6248025.html 操作系统 一 为什么要有操作系统? 现代计算机系统是由一个或者多个处理器,主存, ...
- appium+android自动化测试环境部署
1 node.js安装 官网(https://nodejs.org/en/) 下载对应版本的node.js并安装 安装完成后cmd中输入node -v,输入版本号则安装成功 2 jdk安装 下载对应版 ...
- 配置一个yum私有仓库
使用一台服务器配置私有仓库做yum源,本身使用file,客户端使用http连接 安装http服务: [root@ceph1 ~]# yum -y install httpd 修改配置文件 Docume ...
- 小白进阶之路-python数据类型
1.数据类型:变量值是我们存储的数据,所以数据类型值得就是变量的不同种类 2.数据分类型的原因:变量值是用来保存现实世界的中的状态的,呢么针对不同的状态就应该用不同类型上午数据去表示 (1)整型int ...
- $Codeforces\ 522D\ Closest\ Equals$ 线段树
正解:线段树 解题报告: 传送门$QwQ$ 题目大意是说给定一个数列,然后有若干次询问,每次询问一个区间内相同数字之间距离最近是多少$QwQ$.如果不存在相同数字输出-1就成$QwQ$ 考虑先预处理出 ...
- 浅析Java hashCode()方法
散列码(hash code)是由对象导出的一个整数值. 散列码没有规律,两个不同的对象x和y,x.hashCode()与y.hashCode()基本上不会相同. public static voi ...
- 使用阿里云 ECS 快速部署 WordPress 博客系统
今天在 阿里云 ECS上 部署了一套 Lamp 系统,建了一个WordPress的网站,把操作过程记录下来,文中所列脚本可以直接应用. 废话不多说直接开动,ECS云服务购买可以点击 阿里云ECS 云主 ...
- Linux安装MySQL及基本操作(Centos)
安装: 系统:CentOS-7-x86_64-DVD-1810.iso 安装命令: wget http://repo.mysql.com/mysql-community-release-el7-5.n ...
- K8S集群搭建
K8S集群搭建 摘要 是借鉴网上的几篇文章加上自己的理解整理得到的结果,去掉了一些文章中比较冗余的组件和操作,力争做到部署简单化. K8S组件说明 Kubernetes包含两种节点角色:master节 ...