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. npm start问题

    问题:在执行命令npm start 是出现下列问题: npm [] WARN invalid config loglevel="notice" [] npm WARN invali ...

  2. Ducci Sequence解题报告

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... ,  ...

  3. Oracle の ty_str_split + MySQL の proc_split

    oracle实现字符串分割 功能描述:用指定分隔符切割输入的字符串,返回一维数组,每个数组元素为一个子串. ); CREATE OR REPLACE FUNCTION fn_split (p_str ...

  4. Git 二进制文件冲突解决

    Git 二进制文件冲突解决 在我们合并分支的时候,如果两个分支都进行了修改那么就会产生合并冲突.对于非二进制文件的冲突解决,git会给出冲突的位置我们可以手动修改然后再commit.但是对于非二进制文 ...

  5. Personal Recommendation Using Deep Recurrent Neural Networks in NetEase读书笔记

    一.文章综述 1.研究目的:实现网易考拉电商平台的商品高效实时个性化推荐.缩短用户与目标商品的距离,让用户点击最少的页面就可以得到想要的商品 2.研究背景:基于用户和基于物品的协同过滤(Collabo ...

  6. 【HDOJ6324】Grab The Tree(博弈)

    题意: 思路: #include <stdio.h> #include <vector> #include <algorithm> #include <str ...

  7. 新建一个基于vue.js+Mint UI的项目

    上篇文章里面讲到如何新建一个基于vue,js的项目(详细文章请戳用Vue创建一个新的项目). 该项目如果需要组件等都需要自己去写,今天就学习一下如何新建一个基于vue.js+Mint UI的项目,直接 ...

  8. 移动web页面字体大小二

    按设计稿中的字体大小设置页面字体大小(px),在手机端,因为不同设备会有不同,换算成rem单位.rem是“font size of the root element”,可以通过这篇文章了解http:/ ...

  9. python学习之-- logging模块

    logging模块功能:提供了标准的日志接口,可以通过它存储各种格式的日志.日志5个级别分:debug(),info(),warning(),error(),critical() logging.ba ...

  10. RedirectAttributes

    RedirectAttributes是Spring mvc 3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的 他有两种带参的方式: 第一种:  attr.addAttribute(&q ...