Fix a Tree

原创 2016年08月28日 18:16:42
链接:http://codeforces.com/problemset/problem/699/D

题目:

A tree is an undirected connected graph without cycles.

Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn, where pi denotes a parent of vertex i (here, for convenience a root is considered its own parent).

For this rooted tree the array p is [2, 3, 3, 2].

Given a sequence p1, p2, ..., pn, one is able to restore a tree:

  1. There must be exactly one index r that pr = r. A vertex r is a root of the tree.
  2. For all other n - 1 vertices i, there is an edge between vertex i and vertex pi.

A sequence p1, p2, ..., pn is called valid if the described procedure generates some (any) rooted tree. For example, for n = 3 sequences (1,2,2), (2,3,1) and (2,1,3) are not valid.

You are given a sequence a1, a2, ..., an, not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable in the minimum number of changes, print any of them.



题意:给一个序列,序号代表父节点,内容是子节点,序列不一定成树,问更改最少数目使之成为一颗树。

分析:基本思路用并查集判断子树,更改子树根节点到一棵树上即可,坑在并查集的判环上,一直不是很理解,这道题的判环是判断有没有树根。

题解:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
#define rt return 0
#define fr freopen("in.txt","r",stdin)
#define fw freopen("out.txt","w",stdout)
#define ll long long
#define ull unsigned long long
#define detie ios_base::sync_with_stdio(false);cin.tie(false);cout.tie(false)
#define maxi 0x3f3f3f3f
using namespace std;

int s[300000];
int pre[300000];
int n;

int find(int x)
{
	if (s[x]!=x)
		s[x] = find(s[x]);
	return s[x];
}

void uni(int x, int y)
{
	int xx = find(x);
	int yy = find(y);
	s[xx] = yy;
}

int main()
{
	//fr;
	detie;
	cin >> n;
	int root = -1;
	vector<int> v;
	for (int i = 1; i <= n; i++)
	{
		s[i] = i;
		cin >> pre[i];
	}
	for (int i = 1; i <= n; i++)
	{
		if (pre[i] == i)
		{
			root = i;
			v.push_back(i);
		}
		else
		{
			int xx = find(i);
			int yy = find(pre[i]);
			if (xx == yy)
				v.push_back(i);
			else
				uni(i, pre[i]);
		}
	}
	int num = 0;
	if (root != -1)
	{
		num = v.size() - 1;
		for (int i = 0; i <= num; i++)
			pre[v[i]] = root;
	}
	else
	{
		root = v[0];
		num = v.size();
		for (int i = 0; i < num; i++)
			pre[v[i]] = root;
	}
	cout << num << endl;
	for (int i = 1; i <= n; i++)
		cout << pre[i] << ' ';
	cout << endl;
	rt;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces-698B Fix a Tree

A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree w...
  • Snow_Me
  • Snow_Me
  • 2016年07月20日 14:59
  • 242

Codeforces Round #363 (Div. 2) D. Fix a Tree (并查集)

A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree wit...

Codeforces 698B Fix a Tree 并查集

题目链接:http://www.codeforces.com/problemset/problem/698/B 题意:     已知有向树的定义,除了根节点外每个节点都有且仅有一条边都指向它的父亲节...

CF 699D Fix a Tree 并查集

D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes inp...
  • ctrss
  • ctrss
  • 2016年07月20日 15:39
  • 254

CodeForces 699D—— Fix a Tree(并查集判断环)

D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes inp...

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 inp...

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 inp...

【并查集】【模拟】Codeforces 698B & 699D Fix a Tree

题目链接:   http://codeforces.com/problemset/problem/699/D 题目大意:   通过给定当前节点的父亲给你一棵有错的树,可能有多个根和环,输出改成...

Codeforces Round #363 (Div. 2) D. Fix a Tree(并查集)

题目链接:点击打开链接 题目大意:给出一个可能有环的图的父节点序列,要求修改最少的边使其变成一棵树,输出改变次数和改变后的父节点序列。 解题思路:定义一个root,表示变换后的树根。在输入的同时,如果...

Codeforces-Round-#363-Fix-a-Tree

Codeforces-Round-#363-Fix-a-Tree
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Fix a Tree
举报原因:
原因补充:

(最多只允许输入30个字)