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 思路: 对祖先->后代建立有向图,跑拓扑排序.跑的时候不断更新父亲并判断答案的存在性,同时注意一种 ...
随机推荐
- 比较NHibernate和Entity Framework
葡萄牙的一位开发者 Ricardo Peres 最近发布了一篇文章,以看起来无偏见的形式对领先的两种 .NET ORM:NHibernate 和 Entity Framework 进行了比较. 我们建 ...
- 【leetcode】Partition List
Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...
- iOS 活动图 流程图
活动图 活动图和状态机图都被称为演化图,其区别和联系如下: 1.活动图:用于描述用例内部的活动或方法的流程,如果除去活动图中的并行活动描述以后,它就变成流程图. ...
- FastReport里面正确调用函数的方法
FastReport里面正确调用函数的方法 错误: [FormatDateTime('yyyy-mm-dd',[frxDBDataset1."日期"])] --------- ...
- 8. javacript高级程序设计-BOM
1. BOM 1.1 window BOM的核心对象是window,它表示浏览器的一个实例.在浏览器中,window对象有双重身份, 1.1.1 全局作用域 由于window对象同时扮演着ECMASc ...
- BestCoder8 1001.Summary(hdu 4989) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4989 题目意思:给出 n 个数,然后将这些数两两相加,得到 n*(n-1) /2 对和,把重复的和去掉 ...
- Ubuntu 配置 Tomcat
系统环境:Ubuntu 10.10(linux-kernel 2.6.35-22) 安装版本:apache-tomcat-7.0.29.tar.gz(官方网址:Apache Tomcat) 安装步骤: ...
- 【leetcode】LRU Cache(hard)★
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【linux】学习2
鸟哥那本书的第6章 文件权限: ^ ^ ^ ^ ^ ^ ^ 1 ...
- 240个jquery插件(转)
http://www.kollermedia.at/archive/2007/11/21/the-ultimate-jquery-plugin-list/File upload Ajax File U ...