博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cf22E(加最少的边形成强连通图)
阅读量:5088 次
发布时间:2019-06-13

本文共 2668 字,大约阅读时间需要 8 分钟。

E. Scheme
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

To learn as soon as possible the latest news about their favourite fundamentally new operating system, BolgenOS community from Nizhni Tagil decided to develop a scheme. According to this scheme a community member, who is the first to learn the news, calls some other member, the latter, in his turn, calls some third member, and so on; i.e. a person with index i got a person with index fi, to whom he has to call, if he learns the news. With time BolgenOS community members understood that their scheme doesn't work sometimes — there were cases when some members didn't learn the news at all. Now they want to supplement the scheme: they add into the scheme some instructions of type (xi, yi), which mean that person xi has to call person yi as well. What is the minimum amount of instructions that they need to add so, that at the end everyone learns the news, no matter who is the first to learn it?

Input

The first input line contains number n (2 ≤ n ≤ 105) — amount of BolgenOS community members. The second line contains n space-separated integer numbers fi (1 ≤ fi ≤ n, i ≠ fi) — index of a person, to whom calls a person with index i.

Output

In the first line output one number — the minimum amount of instructions to add. Then output one of the possible variants to add these instructions into the scheme, one instruction in each line. If the solution is not unique, output any.

Examples
input
33 3 2
output
13 1
input
72 3 1 3 4 4 1
output
32 52 63 7

题意:给出一个有向图,现在让你加最少的边让原图形成强连通图,并输出要加的边。

思路:这题很容易让人联想到缩点,因为题意的原因一定会存在回路,可是试想,缩点之后,可能有很多很多环,然后怎样去处理这么多入度出度都为0的缩点?而且这些缩点是要按照顺序去连接的,否则肯定无法强连通,不仅是这些环,还有不是环的缩点,缩完点之后的处理反而变的很是复杂。所以这题的思路得换!因为这一题一题的原因,我们可以直接dfs去搜索入度为0的点最终的尾巴,还有就是环自身就相当于一个入度为0的点,也得dfs,搜索完了之后我们可以得到很多链式的起点和终点,然后我们要做的就是把这些链按照顺序首尾相连就ok!

#include 
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;int next[100010],rd[100010];int vis[100010];int dfs(int x){ vis[x]=1; if(!vis[next[x]]) return vis[x]=dfs(next[x]); else return vis[x]=x;}vector
head,tail;int main (){ memset(vis,0,sizeof(vis)); int n; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d",&next[i]); rd[next[i]]++; } int k=0; for(int i=1; i<=n; i++) if(!rd[i]) { k++; head.push_back(i); tail.push_back(dfs(i)); } int kk=k; for(int i=1; i<=n; i++) if(!vis[i]) { k++; head.push_back(i); tail.push_back(dfs(i)); } if(k==1&&!kk) k=0; printf("%d\n",k); for(int i=0; i

转载于:https://www.cnblogs.com/martinue/p/5490378.html

你可能感兴趣的文章
CSS3动画入门 CSS动画如何使用(举例说明)
查看>>
手机应用开发框架
查看>>
Spring Cloud微服务笔记(五)Feign
查看>>
Linux Bonding
查看>>
C++一些知识难点
查看>>
用户相似度衡量
查看>>
Windows平台,Apache Http Server启动失败,排错思路是什么?
查看>>
简单工厂模式与工厂方法模式的区别-(原)
查看>>
Canvas 获得键盘焦点的方法
查看>>
2018-2019-1 20165202 《信息安全系统设计基础》第六周学习总结
查看>>
C#中控件的Focus()和GotFocus()的区别?
查看>>
linux command line send email
查看>>
IOS 怎么设置UIButton UITextField 不可点击且变灰
查看>>
Leetcode Permutations
查看>>
Android用户登录的问题
查看>>
MFC中修改光标形状
查看>>
有时候我们在项目中需要录入大量的数据,Excel操作
查看>>
Visual Studio Code 写Python 代码
查看>>
django里的http协议
查看>>
判断一个文件是否是指定后缀名的文件
查看>>