Google Kick Start 2018 Round C Planet Distance

思想:

1、用邻接表建图。

2、建完图之后,先把图中的环给找出来。

  怎么找呢?

(1)先统计每一个节点的度。

   (2)统计完了之后,通过使用队列,把度为1 的点给剔除。每剔除一个,所谓剔除其实就是用一个dis[] 数组来做标记,其相应的邻居的度减一,如果该邻居的度为1了,那么把它加到队列里,重复上述过程,直到队列为空。

  (3)最后统计一下,剩下的就是环里的元素。

3、再对环里的元素用BFS搞一下,往外扩散。记录距离。

AC 代码如下:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @program: Leetcode
 * @description:
 * @author: Wangky
 * @create: 2019-09-15 15:05
 **/
public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        int time = 1;
        while ((time )<= T){
            int n = sc.nextInt(); //  n 个顶点
            // construct a graph
            // 邻接表
            ArrayList<Integer> adj[] = new ArrayList[n+1];
            for (int i=0;i<adj.length;i++){
                adj[i] = new ArrayList<>();
            }
            for(int i=0;i<n;i++){
                int x = sc.nextInt();
                int y = sc.nextInt();
                adj[x].add(y);
                adj[y].add(x);
            }

            // compute the degree of each node
            int[] degree = new int[n+1];
            Queue<Integer> q = new LinkedList<>();
            for(int i=1;i<=n;i++){
                degree[i] = adj[i].size();
                if (degree[i] == 1){

                    // 度为1的点(即只有一条边),入队列
                    ((LinkedList<Integer>) q).push(i);
                }
            }

            int[] dis = new int[n+1];
            // Topological sort
            while (!q.isEmpty()){
                int node = q.peek();
                q.poll();

                dis[node] = -1; // 代表该node节点不在环里,可以剔除掉,用 -1 做一个标记

                // 节点node 的所有邻接点的度减1
                for(int i=0;i<adj[node].size();i++){
                    int v = adj[node].get(i);
                    degree[v]--;
                    if (degree[v] == 1){
                        q.offer(v);
                    }
                }
            }

            // Add node in the cycle
            // 复用该 queue
            //  从环里往外bfs
            for(int i=1;i<=n;i++){
                if (dis[i] == 0){
                    q.offer(i);
                }
            }

            while (!q.isEmpty()){
                int node = q.peek();
                q.poll();
                for(int i=0;i<adj[node].size();i++){
                    int v=adj[node].get(i);
                    if (dis[v] == -1){
                        // 如果该节点在环外
                        dis[v] = dis[node]+1;
                        q.offer(v);
                    }
                }
            }

            System.out.print("Case #"+time+": ");
            for(int i=1;i<=n;i++){
                System.out.print(dis[i] + " ");
            }
            System.out.println();
            time++;

        }
    }

}

原文地址:https://www.cnblogs.com/vector11248/p/11523590.html

时间: 2024-08-30 12:30:13

Google Kick Start 2018 Round C Planet Distance的相关文章

Google Kick Start 2020 Round A

Allocation 题意 N个房子出售,每个卖Ai刀,现有B刀资金,求最多买多少个. 思路 贪心,排序后从小到大买 代码 #include<bits/stdc++.h> using namespace std; const int MAX=1e5+5; int a[MAX]; int main() { int T,cas=0; scanf("%d",&T); while(T--) { int n,b,res=0; scanf("%d%d",&a

【Kickstart】2018 Round (Practice ~ H)

Practice Round Problem A GBus count (9pt/15pt) (2019年1月14日,kickstart群每日一题) 题意:有一条笔直的大路,上面有城市编号从 1 开始从左到右一次排列.上面有 N 个 GBuses, 每一个 bus[i] 连接 A[i]  到 B[i] 两个地点(包含这两个地方).我们想要求 P 个城市,每个城市经过的公交车数量. 输入输出 和 数据规模 如下: There exist some cities that are built alo

Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)

Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. Cells number i and i+1 are adjacent, and prisoners in adjacent cells are called "neighbours." A wall with a window separates adjacent cells, and

google kcikstart 2018 round c

https://code.google.com/codejam/contest/4384486/dashboard#s=p0 A 题意 给定一个无向图,其中存在着唯一的一个环,求每个点到这个环的最短距离. 数据范围 ≤ T ≤ 100. 1 ≤ xi ≤ N, for all i. 1 ≤ yi ≤ N, for all i. xi ≠ yi, for all i. (xi, yi) ≠ (xj, yj), for all i ≠ j. The graph in which planets ar

Google Code Jam 2014 Round 2回顾和分析

回顾 比赛开始网络就一直在抽风,不知道宿舍网渣还是有人攻击服务器.刷了n遍n久刷出了题目.提交A小case的时候眼睁睁看着时间过去,却提交不上,这破网.最后A题B题还是解决了,C题扫了一眼,读都没读,就奔D题去了,因为我看到了熟悉的trie这个单词,加之看到小case只有9分,判断小case应该比较容易.前面因为网络浪费了很多时间,加之从未编过trie的程序,只能临时上网翻书去学,最后D小这个可以很容易暴力解的问题也没编完. 最终的rank是13xx,考虑到在这次GCJ之前从未接触过编程竞赛,而

google kick start in March

https://codingcompetitions.withgoogle.com/kickstart/round/0000000000050e01/000000000006987d 二分思想太牛逼!!!! 原文地址:https://www.cnblogs.com/waldenlake/p/10649624.html

Kick Start 2019 Round A Parcels

题目大意 $R \times C$ 的网格,格子间的距离取曼哈顿距离.有些格子是邮局.现在可以把至多一个不是邮局的格子变成邮局,问每个格子到最近的邮局的曼哈顿距离的最大值最小是多少. 数据范围 $ 1 \le R \le 250 $ $ 1 \le C \le 250 $ 100 组测试数据 Time limit: 15 s 分析 显然可以二分答案. 几何视角 考虑平面上的整点(也称格点).到一个格点的曼哈顿距离不大于 $k$ 的所有格点的轮廓是一个旋转了 45° 的正方形( For any p

[树形DP]VK Cup 2012 Round 1 D. Distance in Tree

题意: 给出一棵树,然后问任意两点间距离为k的情况有多少种. 分析: 显然是DP,但是状态方程如何向呢?一棵树,肯定是先从根节点开始考虑情况,那么就把每个点看做是一课子树,然后dp[i][j] 表示计算到i点时距离为k的情况的种类数.然后扫描该点的子节点,递归,完了之后 ans+=dp[x][j-1]*dp[v][k-j]; 表示到i节点的距离和到子节点中的距离==k-1的所有情况,为什么是k-1 ,而不是k呢,因为x和子节点之间还有一条边的长度,然后更新x节点. 这里累加答案和更新x的顺序很重

Google Code Jam 2014 Round 1B Problem B

二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; #define MAX_LEN 50 long long A, B, K; int a[MAX_LEN], b[MAX_LEN], k[MAX_LEN]; long long memoize[MAX_LEN]