Codeforces Beta Round #22 (Div. 2 Only) E. Scheme dfs贪心
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?
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.
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.
3
3 3 2
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贪心的更多相关文章
- Codeforces Beta Round #22 (Div. 2 Only)
Codeforces Beta Round #22 (Div. 2 Only) http://codeforces.com/contest/22 A 水题 #include<bits/stdc+ ...
- 暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table
题目传送门 /* 题意:求最大矩形(全0)的面积 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 详细解释:http://www ...
- 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 ...
- 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 ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
随机推荐
- 不同子系统采用不同MySQL编码LATIN1和UTF8的兼容
程序处理 这是一个历史遗留系统, 旧的系统是C++开发的, 插入数据的时候, 没有统一MYSQL各个层次(服务器, 数据库, 表, 列)的编码, 这个情况基本上是MYSQL的默认安装导致的, 实际的数 ...
- Cisco路由器交换机基础配置
交换机配置 交换机基本状态: switch: # ROM状态, 路由器是rommon> hostname> # 用户模式 hostname# # 特权模式 hostname(config) ...
- jquery validate基本
http://www.runoob.com/jquery/jquery-plugin-validate.html jquery validate 默认 在键盘按下并释放及提交后验证提交表单 例如: $ ...
- Java集合之Queue
queue 定义 C++:queue Java:Queue 创建与其基本操作 创建: Queue<Integer> q=new LinkedList<Integer>(); 数 ...
- 【BZOJ 1222】 [HNOI2001] 产品加工(DP)
Description 某加工厂有A.B两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同的机器加工同一产品所需的时间会不同,若同时由两台机 ...
- c和c++如何把一个整数转化为string
c和c++如何把一个整数转化为string C++: 一.string转int的方式 采用最原始的string, 然后按照十进制的特点进行算术运算得到int,但是这种方式太麻烦,这里不介绍了. 采用标 ...
- luogu2468 [SDOI2010]粟粟的书架
二合一-- #include <iostream> #include <cstdio> using namespace std; int r, c, m, a[205][205 ...
- Javascript拼接HTML字符串的方法列举及思路
转载过来,去掉一些废话吧. 目标: 方便的拼接字符串,不使用让人眼晕的+=.使用过程如下: 1,先创建一个作为“模板”的字符串,如:’My name is ${name},I\’m ${age}.’ ...
- Fidder详解-抓取HTTPS清求(Web/App)抓包分析(靠谱篇)
为什么要学Fidder抓包? 学习接口,必须要学http协议,不要求您对协议的掌握有多深.只是希望你能够了解什么是协议.协议的报文.状态码等等!本文通过抓包工具Fidder带你进入接口的大门.我们通过 ...
- NYOJ-481平衡字符串
平衡字符串 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给你一定长度的字符串.字符串中只包含26个小写字母,首先我们把字母a-z分为2堆(a--m)和(n--z),判 ...