E. Scheme
 

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

题意:

  给出n个节点,以及和这个节点指向的节点fi,表示从i能够到达fi,问至少需要添加多少条边能够使得原图变为强连通分量,

  输出边数及添加的边,多解输出任意一组解。

题解:

  根据题意,每个点出发都可以到达一个强连通分量

  那么起始点我们选取入度为0的就行,那么它可以到达一个环上的点,有多个独立的这样链的形式

  最少的边使它们强连通,那么就是首尾相连了,注意还有就是独立的环,我们也要作出一条链来

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e3+, mod = 1e9+,inf = 2e9; int vis[N],call[N],recall[N],called[N],asd[N],n,callin[N],callout[N];
void dfs(int u) {
vis[u] = ;
if(!vis[call[u]]) dfs(call[u]);
asd[u] = asd[call[u]];
if(asd[u] == ) asd[u] = u;
}
int main() {
scanf("%d",&n);
for(int i = ; i <= n; ++i) {
scanf("%d",&call[i]);
called[call[i]] = ;
}
for(int i = ; i <= n; ++i)
if(!vis[i]) dfs(i);
int top = ,top2 = ;
for(int i = ; i <= n; ++i) {
if(!called[i]) {
callin[++top] = i;
callout[top] = asd[i];
recall[asd[i]] = ;
}
}
top2 = top;
for(int i = ; i <= n; ++i) {
if(asd[i] == i && !recall[i]) {
++top;
callin[top] = callout[top] = i;
}
}
if(top2 == && top == ) top = ;
printf("%d\n",top);
for(int i = ; i <= top; ++i)
printf("%d %d\n",callout[i],callin[i%top+]);
return ;
}

Codeforces Beta Round #22 (Div. 2 Only) E. Scheme dfs贪心的更多相关文章

  1. Codeforces Beta Round #22 (Div. 2 Only)

    Codeforces Beta Round #22 (Div. 2 Only) http://codeforces.com/contest/22 A 水题 #include<bits/stdc+ ...

  2. 暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

    题目传送门 /* 题意:求最大矩形(全0)的面积 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 详细解释:http://www ...

  3. Codeforces Beta Round #69 (Div. 1 Only) C. Beavermuncher-0xFF 树上贪心

    题目链接: http://codeforces.com/problemset/problem/77/C C. Beavermuncher-0xFF time limit per test:3 seco ...

  4. Codeforces Beta Round #87 (Div. 2 Only)-Party(DFS找树的深度)

    A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exa ...

  5. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  6. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  7. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  8. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  9. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

随机推荐

  1. Could not find conduit initiator for address:xxxxxxxxx and transport: http://schemas.xmlsoap.org/soap/http

    <properties> <cxf.version>3.1.12</cxf.version> </properties> <dependencie ...

  2. c++值传递和引用及指针传递区别

    以下程序各有何问题? ***************************************************************************************** ...

  3. keepalived安装脚本

    keepalived_install.sh: #cat keepalived_install.sh #!/bin/bash pkg=keepalived-2.0.10.tar.gz tar xf $p ...

  4. tomcat——大致简介和执行过程

    jsp简介 JSP: JAVA Server Page 使用JAVA语言编写的一种在服务器运行的动态页面 JSP = JAVA + HTML JSP 的执行过程 1: 翻译阶段 把JSP源文件翻译成 ...

  5. 3. express 框架使用 vue框架 weiUI

    express 1. 安装 npm install express --save 2. 创建项目 vue js 安装Vuejs vue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue- ...

  6. 用python写自定义模板

    模板语法有点像php !/usr/bin/env python """ #demo.py.html <html> <?py include head.p ...

  7. (转)php_curl模拟登录有验证码实例

    三年来的第一篇博客,还记得那是一个夜深人静的夜晚, 独自一人坐在不到10平米的小屋里,指头迅速的敲打着键盘,这天真TMD热.BJ生活啊. 唉! 最近一直在参加一个论坛批量发帖的项目开发. 模拟登录,模 ...

  8. 数组和String几种方法的需要注意的地方

    array的方法总结 会更改原来的的数组 push.unshift方法,返回length.增加值得就返回length,其他返回该元素 pop,shift返回该元素 reverse返回该元素 splic ...

  9. 五、PL/SQL循环、游标、函数和过程

    --PL/SQL基础知识学习 --一.PL/SQL语句块,基础语法格式 DECLARE --变量声明列表 info varchar(25); --变量声明 stu_unm integer := 15; ...

  10. 使用 wsgiref 创建WSGI APP

    wsgify装饰器将一个普通函数转变成WSGI应用程序. class webob.dec.wsgify(func=None, RequestClass=None, args=(), kwargs=No ...