Mr. Kitayuta‘s Technology CodeForces - 505D(并查集+拓扑排序或dfs找环) 题解

题目 
Shuseki Kingdom is the world’s leading nation for innovation and technology. There are n cities in the kingdom, numbered from 1 to n.

Thanks to Mr. Kitayuta’s research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally, that is, a teleportation pipe from city x to city y cannot be used to travel from city y to city x. The transportation within each city is extremely developed, therefore if a pipe from city x to city y and a pipe from city y to city z are both constructed, people will be able to travel from city x to city z instantly.

Mr. Kitayuta is also involved in national politics. He considers that the transportation between the m pairs of city (ai, bi) (1 ≤ i ≤ m) is important. He is planning to construct teleportation pipes so that for each important pair (ai, bi), it will be possible to travel from city ai to city bi by using one or more teleportation pipes (but not necessarily from city bi to city ai). Find the minimum number of teleportation pipes that need to be constructed. So far, no teleportation pipe has been constructed, and there is no other effective transportation between cities.

Input 
The first line contains two space-separated integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105), denoting the number of the cities in Shuseki Kingdom and the number of the important pairs, respectively.

The following m lines describe the important pairs. The i-th of them (1 ≤ i ≤ m) contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), denoting that it must be possible to travel from city ai to city bi by using one or more teleportation pipes (but not necessarily from city bi to city ai). It is guaranteed that all pairs (ai, bi) are distinct.

Output 
Print the minimum required number of teleportation pipes to fulfill Mr. Kitayuta’s purpose.

Example 
Input 
4 5 
1 2 
1 3 
1 4 
2 3 
2 4 
Output 

Input 
4 6 
1 2 
1 4 
2 3 
2 4 
3 2 
3 4 
Output 
4


题目大意就是城市之间可以修单向的瞬时传送门,现在给出需求(需要那个城市可以到达娜个城市),问你最少需要修多少个传送通道,首先可以知道,对于需求构成的一个联通块,若连通块有环,只需要把连通块里的点修出一个单项环,任意点就可以互相达到了,若无环,只需要修n-1条路就可以满足需求了。 
所以我们只需要用并查集找出所有的连通块,然后对每个连通块判断环就行,我用了拓扑排序和dfs找环两种方法来做。 
首先是拓扑排序,拓扑排序本身是针对无环有向图的,通过不断把没有入度的点入队来实现,而若有环,则环上的点始终无法入队,最后处理完它们的入度也不会变成零,这样就很容易找出环了。 
dfs找环的方法就是对点赋予状态,0表示没访问过,1表示这个点及其相连的连通块都已经访问过了,-1则表示这个点正在对其dfs深搜访问,若一个点状态为-1又被再一次访问到,那么这肯定就是一个环了。

 

1.拓扑排序

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<set>
#include<stack>
using namespace std;
int f[100005];
int n, m;
vector<int > e[100005];
queue<int> que;
int F(int x){if (f[x] == x)return x;return f[x] = F(f[x]); }
bool H[100005];
int lt[100005];
bool vis[100005];
int deg[100005];void func(){while (!que.empty()){int cnt = que.front();que.pop();for (int i = 0; i < e[cnt].size(); i++){deg[e[cnt][i]]--;if (deg[e[cnt][i]] == 0)que.push(e[cnt][i]);}}
}int main(){int x, y;scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++)f[i] = i;for (int i = 0; i < m; i++){scanf("%d%d", &x, &y);e[x].push_back(y);deg[y]++;f[F(x)] = F(y);}for (int i = 1; i <= n; i++){lt[F(i)]++;if (deg[i] == 0)que.push(i);}func();int ans = 0;for (int i = 1; i <= n; i++){if (deg[i] != 0)H[F(i)] = 1;}for (int i = 1; i <= n; i++){if (f[i] == i){if (H[i])ans += lt[i];else{ ans += lt[i] - 1; }}}printf("%d", ans);return 0;
}

 

2.DFS找环

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<set>
#include<stack>
using namespace std;
int f[100005];
int n, m;
vector<int > e[100005];
queue<int> que;
int F(int x){if (f[x] == x)return x;return f[x] = F(f[x]); }
bool H[100005];
int lt[100005];
bool vis[100005];
int c[100005];void dfs(int cnt){c[cnt] = -1;for (int i = 0; i < e[cnt].size(); i++){int cur = e[cnt][i];if (c[cur] == -1){ H[F(cur)] = 1; break; }else if (c[cur] == 0){ dfs(cur); }}c[cnt] = 1;
}int main(){int x, y;scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++)f[i] = i;for (int i = 0; i < m; i++){scanf("%d%d", &x, &y);e[x].push_back(y);//deg[y]++;f[F(x)] = F(y);}for (int i = 1; i <= n; i++){lt[F(i)]++;if (c[i] == 0)dfs(i);//if (deg[i] == 0)que.push(i);}
//  func();int ans = 0;for (int i = 1; i <= n; i++){if (f[i] == i){if (H[i])ans += lt[i];else{ ans += lt[i] - 1; }}}printf("%d", ans);return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439321.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

二进制枚举子集 CS Maxor 或运算,DP(SOS)

https://blog.csdn.net/noone0/article/details/78289517 目前没有题目链接。 题意:长度为n的序列a,选出两个元素,其或运算结果的最大值为多少,并求出a[i]|a[j]mx的方案数? n<1e5,0<a[i]<2^17,m<17. 假如最大值为mx,若x|ymx 则x和y肯定为mx的子集.否则或运算结果…

【转】SharePoint 中的编程模型

可以通过多种方式开发针对 SharePoint 平台的应用程序。可以基于以下各项将这些应用程序划分下列组中&#xff1a;用于创建应用程序的工具、用于开发应用程序的编程模型、打包和部署应用程序的方法、将应用程序投入市场的方式以及运行应用程序的设备。 SharePoint 框架 ShareP…

【转】SharePoint 术语表

术语表 术语定义访问控制条目 安全对象的自由访问控制列表 (DACL) 或对象的系统访问控制列表 (SACL) 中的一项。在 DACL 中&#xff0c;该项向用户或组授予权限或者拒绝向用户或组授予权限。在 SACL 中&#xff0c;该项指定审核特定用户或组的哪些安全事件或者控制对象的 Wind…

【HDU - 5890】Eighty seven(bitset优化背包)

题干&#xff1a; Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach children how to add numbers up. Before the class, he will prepare NN cards with numbers. The number on the ii-th card is aiai. In class, eac…

【HDU - 1937 】Finding Seats(二维前缀和+尺取法)

题干&#xff1a; A group of K friends is going to see a movie. However, they are too late to get good tickets, so they are looking for a good way to sit all nearby. Since they are all science students, they decided to come up with an optimization problem …

【POJ - 1459】Power Network(网络流最大流,建图)

题干&#xff1a; A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) > 0 of power, may produce an amount 0 < p(u) < p max(u) of power, may …

【转】React Vue MVC MVVM MVP

首先&#xff0c;在谈这个话题之前&#xff0c; 我们有必要了解一下库和框架的区别。 我们先来看react官网以及vue官网对他们的定位&#xff1a; react: vue: react我们不说了&#xff0c;官网上明明白白说了&#xff0c;人家是一个library&#xff0c;用于构建用户界面。 v…

**【POJ - 3122】 Pie(二分寻值)

题干&#xff1a; My birthday is coming up and traditionally Im serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should b…

【转】IsCallBack属性和IsPostBack属性有什么区别?

if (Page.IsCallback) return; 此句话在page的构造函数中使用&#xff0c;不让page反复生成。比如一个TEXTbox如果不组织页面刷新&#xff0c;其数据会丢失。 以postback方式进行客户端和服务器端的交互的&#xff0c; IsPostBack就是true。 以callback方式进行客户端和服务器…

【转】使用Feature导入WebPart

原文链接&#xff1a;http://www.cnblogs.com/glife/archive/2009/10/27/1590488.html 前些天在刚刚接触WebPart的时候&#xff0c;搜到了一篇《使用Feature导入WebPart》的文章&#xff0c;那个时候对Feature的了解还为零&#xff0c;所以看了是一知半解&#xff0c;等到今天…

【HDU - 5017】Ellipsoid(爬山算法,模拟退火,三分)

题干&#xff1a; Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distance between the original point (0,0,0) and points on the ellipsoid. The distance between two points (x 1,y 1,z 1) and (x 2,y 2,z 2) is defined as Input There a…

【转】[SharePoint 开发详解] 一个Feature中使用SPGridView的几个Tips

根据上面一篇随笔所介绍的PC购买流程的项目&#xff0c;在项目中&#xff0c;需要有一个生成订单的功能&#xff0c;能够使得Admin很方便的在获得批准的申请中选取一些来生成订单&#xff0c;要求界面操作简单明了&#xff0c;大概的效果图如下&#xff1a; 点击checkbox&#…

【LeetCode - 131】分割回文串(dp,dfs)

题目链接&#xff1a;https://leetcode-cn.com/problems/palindrome-partitioning/ 题目&#xff1a; 给定一个字符串 s&#xff0c;将 s 分割成一些子串&#xff0c;使每个子串都是回文串。 返回 s 所有可能的分割方案。 示例: 输入: "aab" 输出: [ ["a…

【转】VSTS中版本控制系统Git与TFVC的区别

VSTS&#xff08;Visual Studio Team Services&#xff09; VSTS简单说就是微软TFS(Team Foundation Services)的升级云版&#xff0c;不用像TFS需要在企业内部服务器上部署&#xff0c;并且是免费提供给用户使用的。 每个有微软账号&#xff08;也是免费注册的&#xff09;的…

【LeetCode - 1254】统计封闭岛屿的数目(dfs,连通块)

题目链接&#xff1a;https://leetcode-cn.com/problems/number-of-closed-islands/ 有一个二维矩阵 grid &#xff0c;每个位置要么是陆地&#xff08;记号为 0 &#xff09;要么是水域&#xff08;记号为 1 &#xff09;。 我们从一块陆地出发&#xff0c;每次可以往上下左…

【转】0.SharePoint服务器端对象模型 之 序言

对于刚刚开始接触SharePoint的开发人员&#xff0c;即使之前有较为丰富的ASP.NET开发经验&#xff0c;在面对SharePoint时候可能也很难找到入手的方向。对于任何一种开发平台而言&#xff0c;学习开发的过程大致会包括&#xff1a;开发工具的使用、开发手段的选择和开发语言的编…

【LeetCode - 122】买卖股票的最佳时机 II(贪心 或 dp)

题目链接&#xff1a;https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/ 给定一个数组&#xff0c;它的第 i 个元素是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易&#xff08;多次买卖一支股票…

【转】1.SharePoint服务器端对象模型 之 对象模型概述(Part 1)

在一个传统的ASP.NET开发过程中&#xff0c;我们往往会把开发分为界面展现层、逻辑业务层和数据访问层这三个层面。作为一个应用开发平台&#xff0c;SharePoint是微软在直观的开发能力和自由的扩展能力之间&#xff0c;取到的一个平衡点&#xff0c;其对象模型的设计理念也反映…

【LeetCode - 123】买卖股票的最佳时机 III

题目链接&#xff1a; 给定一个数组&#xff0c;它的第 i 个元素是一支给定的股票在第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。 注意&#xff1a;你不能同时参与多笔交易&#xff08;你必须在再次购买前出售掉之前的股票&#xf…

【转】1.2SharePoint服务器端对象模型 之 对象模型概述(Part 2)

&#xff08;三&#xff09;Url 作为一个B/S体系&#xff0c;在SharePoint的属性、方法参数和返回值中&#xff0c;大量的涉及到了Url&#xff0c;总的来说&#xff0c;涉及到的Url可以分为如下四类&#xff1a; 绝对路径&#xff1a;完整的Url&#xff0c;包含了协议头&…