某个小镇有 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 遥远的村庄的更多相关文章

  1. 《剑指offer》解题笔记

    <剑指offer>解题笔记 <剑指offer>共50题,这两周使用C++花时间做了一遍,谨在此把一些非常巧妙的方法.写代码遇到的难点.易犯错的细节等做一个简单的标注,但不会太过 ...

  2. 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 ...

  3. 110.Balanced Binary Tree Leetcode解题笔记

    110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  4. 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 ...

  5. LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  6. LeetCode解题笔记 - 2. Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  7. 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 ...

  8. CTF实验吧-WEB题目解题笔记(1)简单的登陆题

    1.简单的登陆题 解题链接: http://ctf5.shiyanbar.com/web/jiandan/index.php  Burp抓包解密 乱码,更换思路.尝试id intruder 似乎也没什 ...

  9. PTA 乙级解题笔记 1001 害死人不偿命的(3n+1)猜想

    卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 (3n+1) 砍掉一半.这样一直反复砍下去,最后一定在某一步得到 n=1.卡拉兹在 1950 ...

随机推荐

  1. Centos 7.5安装 Mysql5.7.24

    1. 下载 MySQL 本文采用的Linux为是腾讯云 标准型S2 (1 核 1 GB) Centos 7.5 64位  1.1 官网下载地址: https://dev.mysql.com/downl ...

  2. 【题解】 P2763 试题库问题(网络流)

    P2763 试题库问题 考虑一个试题要被加入进答案的集合有什么条件? 是某种类型 只算作一次 就这两种且的限制,所以我们用串联的方式连接"类型点"和"作用点". ...

  3. $CH$ $0x50$ & $0x51$ 做题记录

    [X]$Mr.Young's\ Picture\ Permutations$ 前面这儿写了挺多道辣,,,懒得写辣$QAQ$ (后面所有同上都是同这个$QwQ$ [X]$LCIS$ 做过了,看这儿 $u ...

  4. highlight.js代码风格引入方法

    <link href="https://cdn.bootcss.com/highlight.js/9.15.10/styles/darcula.min.css" rel=&q ...

  5. 「USACO08JAN」电话线Telephone Lines 解题报告

    题面 大意:在加权无向图上求出一条从 \(1\) 号结点到 \(N\) 号结点的路径,使路径上第 \(K + 1\) 大的边权尽量小. 思路: 由于我们只能直接求最短路,不能记录过程中的具体的边--那 ...

  6. SpringBoot-2.1.1系列一:使用https

    1.什么是https? HTTPS中文名称:超文本传输安全协议,是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要 ...

  7. linux入门系列4--vi/vim编辑器

    上一篇文章"linux入门系列3--linux远程登陆工具"讲解了如何使用常用的工具远程连接和管理linux服务器,要管理服务器必然会涉及到脚本文件的创建.编辑工作,因此在介绍命令 ...

  8. PHP实现取得HTTP请求的原文【转】

    本文实例讲述了PHP实现取得HTTP请求的原文的方法,具体步骤如下: 1. 取得请求行:Method.URI.协议 可以从超级变量$_SERVER中获得,三个变量的值如下: $_SERVER['REQ ...

  9. CTPN中anchors代码

    import numpy as np def generate_basic_anchors(sizes, base_size=16): #base_anchor([0,0,15,15]) base_a ...

  10. 《【面试突击】— Redis篇》--Redis都有哪些数据类型?分别在哪些场景下使用比较合适?

    能坚持别人不能坚持的,才能拥有别人不能拥有的.关注编程大道公众号,让我们一同坚持心中所想,一起成长!! <[面试突击]— Redis篇>--Redis都有哪些数据类型?分别在哪些场景下使用 ...