某个小镇有 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. HolidayFileDisPersonViewList.js中的一些基础

    1,CSS display 属性 使段落生出行内框: p.inline { display:inline; } none 此元素不会被显示 详细介绍:  http://www.w3school.com ...

  2. codeforces -1214 E

    题目https://codeforces.com/problemset/problem/1214/E 就是得知奇数之间不产生影响,先造出一条链,再用偶数往里插就行.链要di从大到小排个序呀!! #in ...

  3. DEVOPS技术实践_12:创建持续集成的管道

    持续集成不仅包含了Jenkins或者相关其它的CI工具,也包含了包含代码如何控制,采用的什么分支策略等.不同的组织可能采用不同的类型的策略来完成CI,策略类型和项目的类型的有很大的关系. 一 分支策略 ...

  4. 安装ATS(apache traffic server)正向代理

    一 traffic server简介 Traffic Server是一种高性能Web代理缓存,可通过在网络边缘缓存频繁访问的信息来提高网络效率和性能.这使内容在物理上更接近最终用户,同时实现更快的交付 ...

  5. 20191010-4 alpha week 1/2 Scrum立会报告+燃尽图02

    此作业链接参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/8747 2019小组名称:“组长”组 组长:杨天宇 组员:魏新,罗杨美慧,王 ...

  6. 调用微软未公开ZwQueryInformationThread函数根据线程句柄获取线程ID

    这段时间公司项目中为了支持XP系统同事代码中用到了 GetThreadId 这个微软的API 但是这个API最低支持版本是 Windows version Windows Vista [desktop ...

  7. 洛谷$P3324\ [SDOI2015]$星际战争 网络流+二分

    正解:网络流+二分 解题报告: 传送门$QwQ$ 其实我第一反应是费用流来着,,,但是仔细想了下发现我不会实现各个武器之间独立同时?而且攻击是连续的答案可能是小数嘛$QwQ$. 所以显然不是递推就二分 ...

  8. Spring中常见的设计模式——策略模式

    策略模式(Strategy Pattern) 一.策略模式的应用场景 策略模式的应用场景如下: 系统中有很多类,而他们的区别仅仅在于行为不同. 一个系统需要动态的在集中算法中选择一种 二.用策略模式实 ...

  9. 前端面试基础回顾之深入JS继承

    前言 对于灵活的js而言,继承相比于java等语言,继承实现方式可谓百花齐放.方式的多样就意味着知识点繁多,当然也是面试时绕不开的点.撇开ES6 class不谈,传统的继承方式你知道几种?每种实现原理 ...

  10. Linux之nohup命令

    例:执行一个循环的脚本 可以使用sh命令就可以了 后台运行这个脚本使用& nohup能够正常在关闭xshell继续执行,而其他两种办法不行 但是注意 这&方法断掉这session后(进 ...