欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1253D.Harmonious Graph
Harmonious Graph
CF1253D · Harmonious Graph
- 难度:1700
- 标签:constructive algorithms、dfs and similar、dsu、graphs、greedy、sortings
- 链接:https://codeforces.com/problemset/problem/1253/D
- 时间限制:1 second 内存限制:256 megabytes
- 出现位置:Day12-Kruskal-离线排序-路径贡献
英文原题面
Statement
You're given an undirected graph with nodes and edges. Nodes are numbered from to . The graph is considered harmonious if and only if the following property holds:
- For every triple of integers such that , if there exists a path going from node to node , then there exists a path going from node to node .
In other words, in a harmonious graph, if from a node we can reach a node through edges (), then we should able to reach nodes too. What is the minimum number of edges we need to add to make the graph harmonious?
Input
The first line contains two integers and ( and ). The -th of the next lines contains two integers and (, ), that mean that there's an edge between nodes and . It is guaranteed that the given graph is simple (there is no self-loop, and there is at most one edge between every pair of nodes).
Output
Print the minimum number of edges we have to add to the graph to make it harmonious.
样例
样例 1
输入:
14 8
1 2
2 7
3 4
6 3
5 7
3 8
6 8
11 12
输出:
1
样例 2
输入:
200000 3
7 9
9 8
4 5
输出:
0
样例解释(英文原文)
In the first example, the given graph is not harmonious (for instance, , node can reach node through the path , but node can't reach node ). However adding the edge is sufficient to make it harmonious. In the second example, the given graph is already harmonious.
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |