Codeforces Round #363 Fix a Tree(树 拓扑排序)
先做拓扑排序,再bfs处理
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = , INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
#define PB(A) push_back(A)
#define FOR(i, n) for(int i = 0; i < n; i++)
bool vis[N];
struct Node{
int to,next;
}edge[N];
int head[N], tot, n,m,indeg[N];
int fa[N];
void init(){
memset(head, -, sizeof(head));
memset(indeg, , sizeof(indeg));
tot = ;
}
void add(int u, int to){
indeg[to]++;
edge[tot].to=to;
edge[tot].next=head[u];
head[u]=tot++;
}
void Topsort(){
stack <int> st;
for(int i = ;i<=n; i++) {
if(indeg[i]==) {
st.push(i);
}
}
while(!st.empty()){
int cur = st.top();
st.pop();
vis[cur] = ;
for(int i = head[cur]; i != -; i = edge[i].next){
int to= edge[i].to;
indeg[to]--;
if(!indeg[to]){
st.push(to);
}
}
}
} void bfs(int u){
queue<int> q;
q.push(u);
vis[u] =;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i= head[u]; i != -; i = edge[i].next){
int to = edge[i].to;
if(!vis[to]){
vis[to] = ;
q.push(to);
}
}
}
}
int main(){
cin>>n;
MS(vis, );
init();
for(int i = ;i <= n ;i++){
int u;
scanf("%d", &u);
fa[i] = u;
add(i, u);
}
Topsort();
int v = ;
for(int i = ;i <= n; i++){
if(fa[i] == i){
vis[i] = ;
v = i;
break;
}
}
if(v == ){
for(int i = ;i <= n; i++){
if(!vis[i]){
fa[i] = i;
v = i;
break;
}
}
} int cnt = ;
for(int i =; i <= n; i++){
if(!vis[i]){
fa[i] = v;
cnt++;
bfs(i);
}
}
cout<<cnt<<'\n';
cout<<fa[];
for(int i = ;i <= n;i++){
printf(" %d", fa[i]);
}
return ;
}
Codeforces Round #363 Fix a Tree(树 拓扑排序)的更多相关文章
- Codeforces Round 363 Div. 1 (A,B,C,D,E,F)
Codeforces Round 363 Div. 1 题目链接:## 点击打开链接 A. Vacations (1s, 256MB) 题目大意:给定连续 \(n\) 天,每天为如下四种状态之一: 不 ...
- BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序
BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序 题意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最 ...
- Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...
- Codeforces Round #363 (Div. 2) 698B Fix a Tree
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes A tree is an und ...
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...
- Codeforces Round #363
http://codeforces.com/contest/699 ALaunch of Collider 题意:n个球,每个球向左或右,速度都为1米每秒,问第一次碰撞的时间,否则输出-1 贪心最短时 ...
- Codeforces Round #363 Div.2[111110]
好久没做手生了,不然前四道都是能A的,当然,正常发挥也是菜. A:Launch of Collider 题意:20万个点排在一条直线上,其坐标均为偶数.从某一时刻开始向左或向右运动,速度为每秒1个单位 ...
- Codeforces gym101755F Tree Restoration(拓扑排序)
题意: 一棵树,给出每个点的后代们,问你这棵树是否存在,存在就给出这棵树 n<=1000 思路: 对祖先->后代建立有向图,跑拓扑排序.跑的时候不断更新父亲并判断答案的存在性,同时注意一种 ...
随机推荐
- mysql查看字段注释(帮助信息)指令
select column_name,column_comment from INFORMATION_SCHEMA.columns where table_name='my_table'; 或者 sh ...
- django 添加动态表格的方法
传统方法(基于方法的视图):http://stellarchariot.com/blog/2011/02/dynamically-add-form-to-formset-using-javascrip ...
- apache2错误日志在哪,可以看到php错误
在以下路径中 /var/log/apache2/error.log
- docker -v挂载数据卷网络异常的问题
docker 删除容器并重新运行容器时报如下异常: docker: Error response from daemon: failed to create endpoint tomcat001 on ...
- Google Code Jam 2015 R1C B
题意:给出一个键盘,按键都是大写字母.给出一个目标单词和一个长度L.最大值或者最大长度都是100.现在随机按键盘,每个按键的概率相同. 敲击出一个长度为L的序列.求该序列中目标单词最多可能出现几次,期 ...
- 解读Unity中的CG编写Shader系列八(镜面反射)
转自http://www.itnose.net/detail/6117378.html 讨论完漫反射之后,接下来肯定就是镜面反射了 在开始镜面反射shader的coding之前,要扩充一下前面提到的知 ...
- (转载)linux中设备文件配置程序udev详解
如果你使用Linux比较长时间了,那你就知道,在对待设备文件这块,Linux改变了几次策略.在Linux早期,设备文件仅仅是是一些带有适当的属性集的普通文件,它由mknod命令创建,文件存放在/dev ...
- win8内置管理员用户无法激活此应用
在运行中输入:“gpedit.msc”,就会启动组策略编辑器, 计算机配置 --> Windows设置 --> 安全设置 --> 本地策略 --> 安全选项 :::: 用 ...
- 数据结构-链表实现删除全部特定元素x
链表节点类定义: template <class T> class SingleList; template <class T> class Node { private: T ...
- Httpsqs的安装以及安装过程错误的解决方法 转
需求 :进行商品搜索的时候,要从索引中进行搜索,由于后台要更新商品和插入商品,当时考虑到了怎么来插入新的索引和更新索引的问题,通过讨论,大家决定用Httpsqs这个消息中间来通知插入新索引和删除索引最 ...