`

Hackerrank - The Good Node

 
阅读更多

原题来自Careercup: http://www.careercup.com/question?id=5840928073842688

We have a list of N nodes with each node pointing to one of the N nodes. 

It could even be pointing to itself. We call a node ‘good’, 
if it satisfies one of the following properties: 

* It is the tail node (marked as node 1) 
* It is pointing to the tail node (node 1) 
* It is pointing to a good node 

You can change the pointers of some nodes in order to make them all ‘good’. 
You are given the description of the nodes. 
You have to find out what is minimum number of nodes that you have to change in order 
to make all the nodes good. 

Input: 
The first line of input contains an integer number N which is the number of nodes. 
The next N lines contains N numbers, 
all between 1 and N. 
The first number, is the number of the node pointed to by Node 1; 
the second number is the number of the node pointed to by Node 2;
the third number is the number of the node pointed to by Node 3 and so on. 

N is no larger than 1000. 

Output: 
Print a single integer which is the answer to the problem 

Sample Input 1: 







Sample output 1: 


Explanation: 
We have to change pointers for four nodes (node #2 to node #5) to point to node #1. 
Thus 4 changes are required 

Sample input 2: 







Sample output 2: 


Explanation: 
We have to just change node #5 to point to node #1 (tail node) which will make node #5 good. 
Since all the other nodes point to a good node (node #5), every node becomes a good node.

另外还需考虑4 4 3 2 1为输入的情况,输出应该是1.

这题用union-find(并查集,或者叫disjoint-set)来做。

import java.util.*;

public class GoodNode {
	public static int find(int[] parent, int x) {
        if(x == 0) return 0;
        if(parent[x] == -1 || parent[x] == x) {
            return x;
        }
        return find(parent, parent[x]);
    }
    
    public static void union(int[] parent, int x, int y) {
        int xp = find(parent, x);
        int yp = find(parent, y);
        parent[xp] = yp;
    }
    
    public static int minChanges(int[] A) {
        int n = A.length;
        int[] parent = new int[n];
        Arrays.fill(parent, -1);
        for(int i=0; i<n; i++) {
            union(parent, i, A[i]);
        }
        int cnt = 0;
        for(int i=1; i<n; i++) {
            if(find(parent, i) == i)
                cnt++;
        }
        return cnt;
    }
    
    public static void main(String args[] ) throws Exception {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] A = new int[n];
        for(int i=0; i<n; i++) {
            A[i] = s.nextInt()-1;
        }
        s.close();
        System.out.println(minChanges(A));
    }
}

 

 

分享到:
评论

相关推荐

    前端开源库-node-red-node-serialport

    《前端开源库Node-Red-Node-Serialport详解》 在现代互联网开发中,前端技术不断发展壮大,而开源库的广泛使用为开发者提供了强大的工具集。本文将详细探讨一款名为"Node-Red-Node-Serialport"的前端开源库,它是...

    Shopify-api-node, 由 MONEI.net 赞助的官方 node Shopify连接器.zip

    Shopify-api-node, 由 MONEI.net 赞助的官方 node Shopify连接器 Shopify node.js ( 官方模块) Node.js.的官方 Shopify API绑定插件安装:$ npm install --save shopify-api-node API这个模块导出一

    图像处理节点编辑器:Image-Processing-Node-Editor

    图像处理,应该是不少 AI 工程师在平时进行模型训练,...图像处理节点编辑器:Image-Processing-Node-Editor。通过该工具,可以辅助并完成深度学习的各项图像处理工作,快速验证、对比各个图像在不同条件下的执行结果。

    node-sass-windows-x64-93-binding.node文件下载

    node-sass-windows-x64-93-binding.node文件下载

    HackerRank-10days-Javascript

    【标题】"HackerRank-10days-Javascript" 是一个专门为学习JavaScript编程设计的挑战系列,旨在帮助初学者在10天内掌握JavaScript的基础知识和常见应用。这个系列涵盖了从基本语法到高级特性的全面内容,是提升...

    HackerRank-Challenges

    【标题】"HackerRank-Challenges" 是一个与编程挑战相关的项目,主要集中在JavaScript语言上。HackerRank是一个在线平台,提供各种编程挑战,旨在帮助程序员提升技能、准备面试,并参与到全球的技术竞争中。这个项目...

    hackerrank-insert-node-in-sorted-doublylinkedlist:我在Java中将节点插入到已排序的双链表中的解决方案

    Node newNode = new Node(data); // 如果链表为空,新节点就是头节点 if (head == null) { head = newNode; } else { // 遍历链表,找到插入位置 Node current = head; while (current != null && current....

    前端开源库-node-red-node-aws

    Node-RED是一个流行的开源工具,主要用于可视化编程,尤其在物联网(IoT)和自动化领域广泛应用。这个工具通过拖拽和连接“节点”来构建应用程序,简化了复杂系统的集成。"node-red-node-aws"是Node-RED的一个扩展,...

    Hackerrank-ProblemSolving

    在编程世界里,Hackerrank 是一个非常知名的在线平台,它为程序员提供了各种挑战和问题,以提升他们的技能并准备面试。"Hackerrank-ProblemSolving" 主题聚焦于解决该平台上出现的问题,特别是使用 Java 语言。下面...

    hackerrank-10daysofjavascript

    【标题】"hackerrank-10daysofjavascript" 是一个在线编程挑战系列,旨在帮助开发者提升JavaScript技能。这个系列通常由HackerRank平台提供,涵盖了JavaScript的基础到进阶内容,适合初级到中级水平的程序员参与。 ...

    vue-task-node:vue-task-node 是一个基于Vue的任务节点图绘制插件(vue-task-node is a Vue based task node mapping plug-in)

    vue-task-node vue-task-node 是一个基于Vue的任务节点图绘制插件(vue-task-node is a Vue based task node mapping plug-in) 在线Demo 如有问题欢迎邮箱:envelope:: 一、安装 npm install vue-task-node -S 二、...

    Rest-API-Intermediate-Hackerrank-Test

    方法1: const fetch = require ( "node-fetch" ) ;let goals = [ ] ;for ( let i = 0 ; i &lt;= 10 ; i ++ ) goals . push ( i ) ;let ans = 0 ;async function getDrawnMatches ( year ) { await Promise . all ( ...

    hackerrank-[removed]我针对HackerRank挑战的解决方案

    在本项目中,"hackerrank-[removed]我针对HackerRank挑战的解决方案" 主要聚焦于使用JavaScript语言解决HackerRank平台上的编程挑战。HackerRank是一个知名的在线编码平台,它提供了各种算法、数据结构以及语言特性...

    hackerrank-[removed]我对Hackerrank问题的一些解决方案

    【标题】"hackerrank-[removed]我对Hackerrank问题的一些解决方案" 提供了一个线索,表明这个压缩包可能包含了一位开发者或编程爱好者对于解决Hackerrank网站上问题的JavaScript代码实现。Hackerrank是一个在线平台...

    win32-x64-51-57-59-64-67-72-79-83-binding.node多版本.zip

    `binding.node`文件是通过Node.js的`node-gyp`工具编译生成的,通常用于实现对硬件、系统库或其他底层功能的访问,以增强Node.js应用的功能。 压缩包子文件的文件名称列表只给出"binding",这可能意味着压缩包内每...

    hackerrank-js-bot:用于完成 HackerRank.com 上提出的 AI 挑战的 JS 机器人和构建过程

    【描述】提到“HackerRank JS 机器人正在设置中...”,这意味着这个项目正处于开发或配置阶段,可能包含一个自动化脚本或工具,用于自动解决 HackerRank 上的 JavaScript 语言编程问题,尤其是那些涉及人工智能的...

    HackerRank-[removed]HackerRank JavaScript解决方案

    HackerRank是一个在线平台,提供各种编程挑战,帮助开发者提升技能并准备面试。在这个主题中,我们主要关注的是如何利用JavaScript解决HackerRank上的问题。 首先,让我们了解JavaScript的基础。JavaScript是一种...

    Hackerrank-Solutions:解决方案源代码,是我用来跟踪我的Hackerrank解决旅程的

    在本项目中,"Hackerrank-Solutions" 是一个存储了在 Hackerrank 平台上完成的编程挑战的源代码仓库。Hackerrank 是一个知名的在线编程竞技平台,它提供了各种编程语言的挑战,旨在帮助程序员提升技能并展示他们的...

    hackerrank-solutions

    【标题】"HackerRank解决方案"是针对编程挑战平台HackerRank上各类问题的解答集。这个资源主要针对想要提高编程技能、准备技术面试或参与编程竞赛的人群。HackerRank提供了各种语言(如JavaScript)的题目,涵盖算法...

    hackerrank:我的Hackerrank解决方案集合

    【标题】"Hackerrank: 我的Hackerrank解决方案集合" 在编程世界中,HackerRank是一个广受欢迎的在线平台,它提供了丰富的编程挑战,旨在帮助开发者提升技能,准备面试,并参与到全球的编码竞赛中。这个“我的Hacker...

Global site tag (gtag.js) - Google Analytics