首页 > 其他分享 >三星研究院机试(Order of task)

三星研究院机试(Order of task)

时间:2024-07-17 13:58:02浏览次数:9  
标签:task int graph vertex tasks new 机试 Order

Thereare V tasks to do. Some task(s) can begin only after a particular task ends,which we will call precedence relation. A graph indicating precedence relationof tasks is given. In this graph, each task is denoted as vertex, and the precedencerelation as directed edge. Note there is no cycle with this graph (cycle refers to a path that starts from one vertex and returns to the same vertex). The graph below is one example of such graph.

In this graph, task 1 can start after task 4 ends and task 6can when task 5 and task 7 end; there is no cycle with this graph.

Manager Kim is going to finish a set of tasks having precedencerelation by taking care of one task at a time. If he is going to do this withthe set of tasks illustrated above, the tasks can be handled with the followingorder.

8, 9, 4, 1, 5, 2, 3, 7, 6

And the order below is also possible.

4, 1, 2, 3, 8, 5, 7, 6, 9

But, the order below is not possible.

4, 1, 5, 2, 3, 7, 6, 8, 9

[Input/output example]

Input

9 9                             ← Test case 1 starts 

4 1 1 2 2 3 2 7 5 6 7 6 1 5 8 5 8 9

5 4                             ← Test case 2 starts

1 2 2 3 4 1 1 5

...

Output(consisting of 10 lines in total)

#1 8 9 4 1 5 2 3 7 6

#2 4 1 2 3 5

...

public class Solution {


    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int T;
        T = sc.nextInt();
        for (int t = 1; t <= T; t++) {
            sc.nextLine(); 
            String line1 = sc.nextLine();
            String line2 = sc.nextLine();

            String ans = orderTask(line1, line2);

            StringBuilder sb = new StringBuilder();
            sb.append("#").append(t).append(" ").append(ans);
            System.out.println(sb);
        }
        sc.close();
    }

    private static String orderTask(String line1, String line2) {

        String[] parts1 = line1.split(" ");
        int v = Integer.parseInt(parts1[0]);  // 节点数量
        int e = Integer.parseInt(parts1[1]);  // 边数量
        String[] edges = line2.split(" "); 


        boolean[] visited = new boolean[v + 1]; // 访问标记数组
        Arrays.fill(visited, false);

        LinkedList<Integer>[] arr = new LinkedList[v + 1]; // 邻接表
        // 初始化数组中的每个链表
        for (int i = 0; i <= v; i++) {
            arr[i] = new LinkedList<>();
        }

        int[] in_degree = new int[v + 1]; // 入度
        Arrays.fill(in_degree, 0);

        for (int i = 0; i < edges.length; i += 2) {
            int x = Integer.parseInt(edges[i]);  // 边的第一个端点
            int y = Integer.parseInt(edges[i + 1]);  // 边的第二个端点
            arr[y].add(x);
            // 增加目标顶点的入度
            in_degree[y]++;
        }

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < v; i++) {
            int index = 0;
            for (int j = 1; j <= v; j++) {  // 找到一个入度为0的点
                if (in_degree[j] == 0 && visited[j] == false) {
                    visited[j] = true;
                    index = j;
                    sb.append(j).append(" ");
                    break;
                }

            }
            // 遍历找到父节点为index的节点,入度 -1
            for (int k = 1; k <= v; k++) {
                if (arr[k].contains(index) && visited[k] == false) {
                    in_degree[k]--;
                }
            }
        }

        return sb.toString().trim();

    }

}

标签:task,int,graph,vertex,tasks,new,机试,Order
From: https://blog.csdn.net/Mr__________xiao/article/details/140425711

相关文章

  • 三星研究院机试(Optimal Path)
    Mr.KimhastodeliverrefrigeratorstoNcustomers.Fromtheoffice,heisgoingtovisitallthecustomersandthenreturntohishome.Eachlocationoftheoffice,hishome,andthecustomersisgivenintheformofintegercoordinates(x,y)(0≤x≤100,......
  • Datawhale AI 夏令营 task2
    Task1的baseline我们是基于经验模型(使用均值作为结果数据)来解决的问题,Task2版本教程将使用机器学习模型解决本次问题,模型使用简单,数据不需要过多预处理;使用机器学习方法一般主要需要从获取数据&增强、特征提取和模型三个方面下手。一般的使用机器学习模型解决问题的主要步骤......
  • 2024年华为OD机试真题-符号运算-(C++/Java/python)-OD统一考试(C卷D卷)
      2024华为OD机试真题目录-(B卷C卷D卷)-【C++JavaPython】    题目描述给定一个表达式,求其分数计算结果。表达式的限制如下:所有的输入数字皆为正整数(包括0)仅支持四则运算(+-*,/)和括号结果为整数或分数,分数必须化为最简格式(比如6,3/4,7/8,90/7)除数可能为0,如果遇到......
  • 【Datawhale AI夏令营】 Task1 学习笔记
    目录一、baseline二、NLP模型自然语言处理的主要任务自然语言处理的技术和方法自然语言处理的应用自然语言处理的挑战 三、赛题理解 赛题背景赛事任务术语词典干预术语词典干预的主要特点术语词典干预的实施方法四、实操 步骤体会感想   学习目标:跑......
  • Camunda流程运行中,需要更换UserTask的被订阅者
    主要应用于实际开发中,考虑到会有人员调动的情况publicvoidchangeManager(Stringoriginal,Stringnow,StringvariableName){//当前任务授予人替换List<Task>list=taskService.createTaskQuery().taskAssignee(original).list();list.strea......
  • Spring Task定时任务框架
    文章目录简单介绍说明说明:作用:一般应用场景:重点:cron表达式简介与举例:通配符:cron表达式案例:自动生成工具使用举例:准备小案例:简单介绍说明说明:SpringTask是Spring框架提供的任务调度工具,可以按照约定的时间自动执行某个代码逻辑。作用:定时自动执行某段代码一......
  • Linux 报错INFO: task blocked for more than 120 seconds
     一般情况下,Linux会把可用内存的40%的空间作为文件系统的缓存。 当缓存快满时,文件系统将缓存中的数据整体同步到磁盘中。但是系统对同步时间有最大120秒的限制。 如果文件系统不能在时间限制之内完成数据同步,则会发生上述的错误。 这通常发生在内存很大的系统上。系统......
  • Improving News Recommendation via Bottlenecked Multi-task Pre-training论文阅读笔
    ImprovingNewsRecommendationviaBottleneckedMulti-taskPre-training论文阅读笔记Abstract现存的问题:​ 现有的PLM大多是在大规模通用语料库上预先训练的,并没有专门用于捕捉新闻文章中的丰富信息。因此,它们生成的新闻嵌入信息可能不足以表示新闻内容或描述新闻之间的关......
  • 【译】The danger of TaskCompletionSource class
    来自SergeyTepliakov的另一篇https://devblogs.microsoft.com/premier-developer/the-danger-of-taskcompletionsourcet-class/#comments当使用async/await时,如果您想手动控制任务的生存期,TaskCompletionSource<T>类是一个非常有用的工具。下面是TaskCompletionSource的一个......
  • 电力需求预测挑战赛(机器学习方向)--task1 #Datawhale AI 夏令营
    一、概念电力需求的准确预测对于电网的稳定运行、能源的有效管理以及可再生能源的整合至关重要。【训练时序预测模型助力电力需求预测】二、赛题任务给定多个房屋对应电力消耗历史N天的相关序列数据等信息,预测房屋对应电力的消耗。三、赛题数据简介1、赛题数据由训练集......