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 思路: 对祖先->后代建立有向图,跑拓扑排序.跑的时候不断更新父亲并判断答案的存在性,同时注意一种 ...
随机推荐
- Linux下编译安装Apache Http Server
Linux下编译安装Apache Http Server [TOC] 1.下载httpd-2.4.12.tar.bz2 wget http://mirror.bit.edu.cn/apache/htt ...
- 装b指南
提溜一个糖水黄桃罐头瓶,放在桌边,坐下以后,脖子略微后仰,翘着二郎腿,低头盯着屏幕看需求. 最好点一根烟,牌子无所谓,能冒烟就行.要得就是云山雾绕的感觉,从烟雾中眯着眼睛看出去,一副胸有成竹的样. 一 ...
- mysql 同步
http://dev.mysql.com/doc/refman/5.5/en/replication-howto.html http://blog.csdn.net/mycwq/article/det ...
- Linux的IO性能监控工具iostat详解
Linux系统出现了性能问题,一般我们可以通过top.iostat.free.vmstat等命令来查看初步定位问题.其中iostat可以提供更丰富的IO性能状态数据. . 基本使用 $iostat - ...
- Java总结(二):继承——Inheritance
关于继承: 1.为了重用代码——引入继承. 2.父类的某些方法反正要被重写,在父类里实现在也无用——引入抽象类. 3.把抽象类里的抽象方法抽出来——引入接口.
- Java程序优化的一些最佳实践(转)
衡量程序的标准 衡量一个程序是否优质,可以从多个角度进行分析.其中,最常见的衡量标准是程序的时间复杂度.空间复杂度,以及代码的可读性.可扩展性.针对程序的时间复杂度和空间复杂度,想要优化程序代码,需要 ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- PowerDesigner16.5 连64位MySQL,出错:SQLSTATE = IM014。原因及解决方案
- hdu3652
基本的数位dp,需要记录前面除以13的余数. #include <cstdio> #include <cstring> using namespace std; #define ...
- ACM/ICPC 之 数论-素数筛选法 与 "打表"思路(POJ 1595)
何为"打表"呢,说得简单点就是: 有时候与其重复运行同样的算法得出答案,还不如直接用算法把这组数据所有可能的答案都枚举出来存到一个足够大的容器中去-例如数组(打表),然后再输入数据 ...