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 思路: 对祖先->后代建立有向图,跑拓扑排序.跑的时候不断更新父亲并判断答案的存在性,同时注意一种 ...
随机推荐
- thinkphp save()方法没有数据,保存失败解决办法
thinkphp save()方法没有数据保存返回0,保存失败返回false 可以对返回值判断一下就好 $ret = $model->save($data); //var_dump($ret ...
- ip netns相关命令
1.增加虚拟网络命名空间 ip netns add net0 2.显示所有的虚拟网络命名空间 EULER:~ # ip netns list net0 也可通过查看/var/run/netns ...
- Openstack Neutron 允许VM流量转发
neutron port-update <port-id> --port-security-enabled=False --no-security-groups
- Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- Python之socket简介
http://goodcandle.cnblogs.com/archive/2005/12/10/294652.aspx http://yangrong.blog.51cto.com/6945369/ ...
- ios 关于使用异步网络请求时block回调的内存注意
在一个controller中,使用 NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest ...
- Angular中使用Rainbow
在使用js类库和框架的时候,大家都习惯于编写自己的使用示例,如果能将示例中的html,js和css 进行展示, 并进行高亮显示,效果会很棒,例如在html高亮显示jquery代码 上面的示例是使用ra ...
- 细看INNODB数据落盘
本文来自:沃趣科技 http://www.woqutech.com/?p=1459 1. 概述 前面很多大侠都分享过MySQL的InnoDB存储引擎将数据刷新的各种情况.我们这篇文章从InnoDB往 ...
- percona-toolkit 之 【pt-slave-delay】说明
摘要: 在主从复制的架构中,正常情况下主上操作的记录也会在从上进行操作,虽说是异步复制,但操作会“实时”的同步到从.比如在主上不小心误操作了,还没等反应过来从上也会马上执行误操作,后期只有通过二进制或 ...
- 1616 最小集合 51NOD(辗转相处求最大公约数+STL)
1616 最小集合 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 A君有一个集合. 这个集合有个神奇的性质. 若X,Y属于该集合,那么X与Y的最大 ...