cf22E(加最少的边形成强连通图)

202 篇文章 0 订阅
123 篇文章 0 订阅
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
3
3 3 2
output
1
3 1
input
7
2 3 1 3 4 4 1
output
3
2 5
2 6
3 7


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

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


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stack>
#include<queue>
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<int>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<k; i++)
        printf("%d %d\n",tail[i],head[(i+1)%k]);///一条条处理出来的链首尾相连,最终强连通!
    return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值