codeforce 698B Fix a Tree
来源:互联网 发布:淘宝发优惠怎么拿提成 编辑:程序博客网 时间:2024/06/06 05:04
题意:
给出n个点的父节点,问最少修改几个点能使整个图变成一棵树。
题解:
给出的图可能包括多个树、环,要使图变成树则要先将环解开,再将树合并。
用并查集保存树,再通过dfs判断是否存在环,解开环要注意一些细节。
#include<iostream>#include <stdio.h>#include <algorithm>#include <math.h>#include<stdlib.h>#include <string.h>#include<queue>#include<set>#include<map>#include<stack>#include<time.h>using namespace std;#define MAX_N 200005#define inf 0x7fffffff#define LL long long#define ull unsigned long long#define mod 10007LL INF=9e18;int n;int par[MAX_N];int ans[MAX_N];vector<int>G[MAX_N];int find(int x){ return x==par[x]?x:par[x]=find(par[x]);}void unite(int x,int y){ x = find(x); y = find(y); par[x] = y;}void init(){ for(int i=1;i<=n;i++) par[i] = i;}bool dfs(int u,int v,int s){ for(int i=0;i<G[u].size();i++) { if(G[u][i] == s || G[u][i] == v) { return true; } else { if(dfs(G[u][i], u, s)) return true; } } return false;}int main(){ cin >> n; init(); for(int i=1;i<=n;i++) { int fa; scanf("%d",&fa); if(i != fa) G[i].push_back(fa); ans[i] = fa; unite(i, fa); } int cnt = -1; int fa; int Fa = 0; for(int i=1;i<=n;i++) { if(par[i] == i) { cnt++; fa = i; if(!dfs(i, 0, i)) Fa = i; } } if(!Fa) { cnt++; ans[fa]=fa; } else { fa = Fa; } for(int i=1;i<=n;i++) { if(par[i] == i && i != fa) { ans[i] = fa; par[i] = fa; } } cout << cnt << endl; for(int i=1;i<=n;i++) { printf("%d%c",ans[i],i==n?'\n':' '); }}
0 0
- codeforce 698B Fix a Tree
- Codeforces 698B Fix a Tree
- Codeforces-698B Fix a Tree
- CodeForces 698B Fix a Tree
- 【CF 698B】Fix a Tree(dfs)
- [树] cf 698B D Fix a Tree
- Codeforces 698B Fix a Tree 并查集
- CodeForces 698 B. Fix a Tree(水~)
- CodeForces 698B - Fix a Tree(并查集)
- Codeforces 698B Fix a tree (模拟)
- 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree
- Codeforces Round #363 (Div. 1) B. Fix a Tree
- Fix a Tree
- Fix a Tree
- Fix a Tree
- Fix a Tree CodeForces
- Fix a Tree CodeForces
- codeforce 429A Xor-tree
- Shaders for Game Programmers and Artists(7) - Non-photorealistic rendering
- [07]项目实战-PC 端固定布局(7)
- CGLib动态代理
- Synchronized(对象锁)和Static Synchronized(类锁)的区别
- 1720 (错误)
- codeforce 698B Fix a Tree
- TCP建立连接为什么是三次握手,为什么不是两次或四次?
- SWIFT 函数参数 3.0 改动--Boolan
- C语言中realloc,malloc,calloc区别
- SystemBarTint实现沉浸模式
- Shaders for Game Programmers and Artists(8) - Fog
- [LeetCode] Longest Increasing Subsequence
- poj 2449 Remmarguts' Date(K短路)
- 解决tmux下vim背景颜色无法刷新问题