Submission #140770


Source Code Expand

import java.util.*;

public class Main {
  public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    ArrayList<ArrayList<Integer>> g1 = readGraph(in);
    ArrayList<ArrayList<Integer>> g2 = readGraph(in);
    int[] d1 = dist(g1);
    int[] d2 = dist(g2);
    int rMax1 = d1[0];
    int rMin1 = d1[0];
    for( int d : d1 ) {
      rMax1 = Math.max(rMax1,d);
      rMin1 = Math.min(rMin1,d);
    }
    int rMax2 = d2[0];
    int rMin2 = d2[0];
    for( int d : d2 ) {
      rMax2 = Math.max(rMax2,d);
      rMin2 = Math.min(rMin2,d);
    }
    int rMin = Math.max (Math.max(rMax1,rMax2),rMin1 + rMin2 + 1);
    int rMax = rMax1 + rMax2 + 1;
    System.out.println(rMin + " " + rMax);
  }

  public static ArrayList<ArrayList<Integer>> readGraph(Scanner in) {
    int n = in.nextInt();
    int m = in.nextInt();
    ArrayList<ArrayList<Integer>> g = new ArrayList<ArrayList<Integer>>();
    for( int i = 0; i < n; i++ ) 
      g.add(new ArrayList<Integer>());
    for( int i = 0; i < m; i++ ) {
      int u = in.nextInt();
      int v = in.nextInt();
      g.get(u).add(v);
      g.get(v).add(u);
    }
    return g;
  }

  public static int[] dist(ArrayList<ArrayList<Integer>> g) {
    int[] d = new int[g.size()];
    boolean[] vis = new boolean[g.size()];
    Queue<Pair> queue = new LinkedList<Pair>();
    for( int i = 0; i < g.size(); i++ ) {
      Arrays.fill(vis,true);
      queue.add(new Pair(i,0));
      vis[i] = false;
      int max = 0;
      //System.out.println("start: "+ i);
      while (!queue.isEmpty()) {
        Pair p = queue.poll();
        for( int u : g.get(p.first) ) {
          if( vis[u] ) {
            vis[u] = false;
            queue.offer(new Pair(u,p.second+1));
            max = p.second+1;
            //System.out.println("v: " + u + " d: " +(p.second+1));
          }
        }
      }
      d[i] = max;
    }
    return d;
  }
  static class Pair {
    int first;
    int second;
    public Pair(int f,int s) {
      this.first = f;
      this.second = s;
    }
  }
}

Submission Info

Submission Time
Task C - 直径
User autotaker
Language Java (OpenJDK 1.7.0)
Score 100
Code Size 2112 Byte
Status AC
Exec Time 1379 ms
Memory 36296 KB

Judge Result

Set Name Small Large
Score / Max Score 50 / 50 50 / 50
Status
AC × 28
AC × 58
Set Name Test Cases
Small 10-small_random-00, 10-small_random-01, 10-small_random-02, 10-small_random-03, 10-small_random-04, 10-small_random-05, 10-small_random-06, 10-small_random-07, 10-small_random-08, 10-small_random-09, 10-small_random-10, 10-small_random-11, 10-small_random-12, 10-small_random-13, 10-small_random-14, 10-small_random-15, 10-small_random-16, 10-small_random-17, 10-small_random-18, 10-small_random-19, 21-small_path-00, 21-small_path-01, 21-small_path-02, 21-small_path-03, 21-small_path-04, 00-sample-00, 00-sample-01, 00-sample-02
Large 00-sample-00, 00-sample-01, 00-sample-02, 10-small_random-00, 10-small_random-01, 10-small_random-02, 10-small_random-03, 10-small_random-04, 10-small_random-05, 10-small_random-06, 10-small_random-07, 10-small_random-08, 10-small_random-09, 10-small_random-10, 10-small_random-11, 10-small_random-12, 10-small_random-13, 10-small_random-14, 10-small_random-15, 10-small_random-16, 10-small_random-17, 10-small_random-18, 10-small_random-19, 20-small_tree-00, 20-small_tree-01, 20-small_tree-02, 20-small_tree-03, 20-small_tree-04, 21-small_path-00, 21-small_path-01, 21-small_path-02, 21-small_path-03, 21-small_path-04, 30-large_random-00, 30-large_random-01, 30-large_random-02, 30-large_random-03, 30-large_random-04, 30-large_random-05, 30-large_random-06, 30-large_random-07, 30-large_random-08, 30-large_random-09, 40-large_comp-00, 40-large_comp-01, 40-large_comp-02, 40-large_comp-03, 40-large_comp-04, 41-large_tree-00, 41-large_tree-01, 41-large_tree-02, 41-large_tree-03, 41-large_tree-04, 42-large_path-00, 42-large_path-01, 42-large_path-02, 42-large_path-03, 42-large_path-04
Case Name Status Exec Time Memory
00-sample-00 AC 383 ms 20772 KB
00-sample-01 AC 378 ms 20868 KB
00-sample-02 AC 382 ms 20780 KB
10-small_random-00 AC 400 ms 21520 KB
10-small_random-01 AC 412 ms 21712 KB
10-small_random-02 AC 387 ms 21280 KB
10-small_random-03 AC 378 ms 21392 KB
10-small_random-04 AC 406 ms 21512 KB
10-small_random-05 AC 414 ms 21800 KB
10-small_random-06 AC 380 ms 21288 KB
10-small_random-07 AC 395 ms 21028 KB
10-small_random-08 AC 385 ms 20900 KB
10-small_random-09 AC 407 ms 21412 KB
10-small_random-10 AC 398 ms 21416 KB
10-small_random-11 AC 408 ms 21288 KB
10-small_random-12 AC 402 ms 21540 KB
10-small_random-13 AC 381 ms 20864 KB
10-small_random-14 AC 424 ms 21800 KB
10-small_random-15 AC 397 ms 21500 KB
10-small_random-16 AC 414 ms 21668 KB
10-small_random-17 AC 409 ms 21712 KB
10-small_random-18 AC 421 ms 21760 KB
10-small_random-19 AC 436 ms 21632 KB
20-small_tree-00 AC 477 ms 25800 KB
20-small_tree-01 AC 384 ms 20900 KB
20-small_tree-02 AC 498 ms 26448 KB
20-small_tree-03 AC 382 ms 20860 KB
20-small_tree-04 AC 750 ms 31216 KB
21-small_path-00 AC 383 ms 20896 KB
21-small_path-01 AC 386 ms 20904 KB
21-small_path-02 AC 379 ms 20776 KB
21-small_path-03 AC 386 ms 20900 KB
21-small_path-04 AC 392 ms 21248 KB
30-large_random-00 AC 740 ms 34848 KB
30-large_random-01 AC 406 ms 21420 KB
30-large_random-02 AC 536 ms 26800 KB
30-large_random-03 AC 393 ms 21276 KB
30-large_random-04 AC 621 ms 30900 KB
30-large_random-05 AC 395 ms 21420 KB
30-large_random-06 AC 534 ms 29092 KB
30-large_random-07 AC 449 ms 22180 KB
30-large_random-08 AC 1379 ms 36296 KB
30-large_random-09 AC 1274 ms 36108 KB
40-large_comp-00 AC 383 ms 20900 KB
40-large_comp-01 AC 500 ms 23976 KB
40-large_comp-02 AC 438 ms 22316 KB
40-large_comp-03 AC 660 ms 28060 KB
40-large_comp-04 AC 731 ms 34948 KB
41-large_tree-00 AC 383 ms 20900 KB
41-large_tree-01 AC 461 ms 23660 KB
41-large_tree-02 AC 494 ms 25692 KB
41-large_tree-03 AC 545 ms 29884 KB
41-large_tree-04 AC 765 ms 31444 KB
42-large_path-00 AC 534 ms 30008 KB
42-large_path-01 AC 394 ms 21160 KB
42-large_path-02 AC 430 ms 21540 KB
42-large_path-03 AC 625 ms 30628 KB
42-large_path-04 AC 742 ms 31356 KB