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 ...
随机推荐
- LeetCode(237)Delete Node in a Linked List
题目 Write a function to delete a node (except the tail) in a singly linked list, given only access to ...
- 【HIHOCODER 1605】小Hi的生成树计数
描述 小Hi最近对生成树(包含所有顶点的联通无环子图.)非常的感兴趣,他想知道对于特定的简单平面无向图是不是存在求生成树个数的简单方法. 小Hi定义了这样的图:一个以{0,1,2--n}为顶点的图,顶 ...
- Root of AVL Tree
04-树5 Root of AVL Tree(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the ...
- 【02】GitHub 工具 Octotree
#推荐一个 GitHub 工具 Octotree Chrome extension 它可以让你在看任何仓库时,获得一个左边的树状图.
- mysql汉字转拼音函数
-- 创建汉字拼音对照临时表 CREATE TABLE IF NOT EXISTS `t_base_pinyin` ( `pin_yin_` ) CHARACTER SET gbk NOT NULL, ...
- Cow Exhibition (01背包)
"Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with G ...
- [BZOJ2120][BZOJ2453]数颜色
[BZOJ2120]数颜色 试题描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中 ...
- [POJ2443]Set Operation(bitset)
传送门 题意:给出n个集合(n<=1000),每个集合中最多有10000个数,每个数的范围为1~10000,给出q次询问(q<=200000),每次给出两个数u,v判断是否有一个集合中同时 ...
- 实体类与实体DTO类之间的转换
实体类与实体DTO类之间的转换 实体类与实体DTO类之间的转换 1.通过使用第三方序列化反序列化工具Newtonsoft.Json 2.通过反射实现 3.通过表达式目录树加字典缓存实现 4. 通过表达 ...
- [NOIP2003] 提高组 洛谷P1038 神经网络
题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神经网络的研究一直是当今 ...