CodeForces 22E Scheme【变成强联通图至少增加多少边并输出】

时间:2021-07-05 09:48:11

Description

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.

Sample Input

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

这个题和hdu2767Proving Equivalences【STL版SCCTarjan+缩点】(有注释) 十分类似。让我一度以为找出入度为0与出度为0的团的最大值,再输出就可以了==

然而输出是个大问题,由于要输出,缩点的话无法出路,所以要换思路。我们用深搜来找环,in0[]=0相当于是最后的“尾巴”,对于这个点深搜的结果是这个环的终点,终点链接起点~~~就可以了,我开始还觉得dfs里面应该是遍历与他相连的所有点,其实按照这个逻辑,倒是应该只找一个点比较合理==

#include <iostream>
#include<cstdio>
#include<stack>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 200005
vector<int>G[maxn],st,ed;
int in0[maxn];
int col[maxn];
int dfs(int p)
{
    col[p]=1;
    for(int i=0;i<G[p].size();i++)
    {
        int v=G[p][i];
        if(!col[v])return col[p]=dfs(v);
        return col[p]=p;
    }
}
int main()
{
   // freopen("cin.txt","r",stdin);
    int  n;
   // scanf("%d",&T);
    while(~scanf("%d",&n))
   // while(T--)
    {
        //scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++) G[i].clear();
        memset(in0,0,sizeof(in0));
        memset(col,0,sizeof(col));
        for(int i=1;i<=n;i++)
        {
            int v;
            scanf("%d",&v);
            in0[v]++;
            G[i].push_back(v);
        }
        int k=0,t=0;
        for(int i=1;i<=n;i++)
        {
            if(!in0[i])
            {
                k++;
                st.push_back(i);
                ed.push_back(dfs(i));
            }
        }
        t=k;
        for(int i=1;i<=n;i++)
        {
            if(!col[i])
            {
                k++;
                st.push_back(i);
                ed.push_back(dfs(i));
            }
        }
        if(t==0&&k==1)k=0;
        printf("%d\n",k);
        for(int i=0;i<k;i++)
        {
            printf("%d %d\n",ed[i],st[(i+1)%k]);
        }
    }
    return 0;
}