#P13377. [GCJ 2011 #2] A.I. War

    ID: 15244 远端评测题 3000~6000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>图论2011广度优先搜索 BFSbitsetGoogle Code Jam

[GCJ 2011 #2] A.I. War

题目背景

A.I. War is a real-time strategy game developed by Arcen Games. This problem was inspired by the game, but does not assume you have played it.

Arcen Games is the creator of A.I. War. Arcen Games does not endorse and has no involvement with Google Code Jam.

题目描述

You're facing an artificial intelligence in a deadly war for the future of the galaxy. In order to defeat the A.I., you will need to threaten its home planethome\ planet. Some planets are connected to each other by wormholes; any planet may be connected to any number of other planets using the wormholes.

You begin by owning only your home planet. Each turn, you may conquer any planet you threatenthreaten. You threaten a planet if you don't own it, and it is connected by a wormhole to any of the planets you own. Once you have conquered a planet, you own it. As soon as you threaten the A.I.'s home planet, you may not conquer any more planets.

While attending the most important day in tactical school, you discovered two things about the A.I.:

  • For each planet you conquer, the A.I. will become more powerful, because it will see you as a threat and produce more ships to defend itself.
  • The A.I. will defend every planet you're currently threatening.

You have combined those two facts to create a strategy:

  1. You will conquer planets until you threaten the A.I.'s home base.
  2. If there are multiple ways of completing step 1, do it while conquering the smallestsmallest possible number of planets.
  3. If there are multiple ways of completing step 2, do it so that at the end you will threaten the largestlargest possible number of planets.

Given the planets and the wormholes, how many planets will you conquer and threaten on your way to the A.I.'s home base if you follow the strategy described above?

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case starts with a single line containing two space-separated integers: PP, the number of planets, and WW, the number of wormholes. Your home planet is planet 00, and the A.I.'s home planet is planet 11.

The second line of each test case will contain WW space-separated pairs of comma-separated integers xi,yix_{i}, y_{i}. Each of these indicates that there is a two-way wormhole connecting planets xix_{i} and yiy_{i}.

输出格式

For each test case, output one line containing "Case #xx: c tc\ t", where xx is the case number (starting from 11), cc is the number of planets you conquer if you follow the above strategy, and tt is the number of planets you threaten at the end (including the A.I.'s home planet).

4
2 1
0,1
3 3
0,1 1,2 0,2
5 5
0,4 0,2 2,4 1,2 1,4
7 9
0,6 0,2 0,4 2,4 3,4 2,3 3,5 4,5 1,5
Case #1: 0 1
Case #2: 0 2
Case #3: 1 2
Case #4: 2 4

提示

Sample Explanation

In the first case, you don't have to conquer anything, and you're already threatening the A.I.'s home planet.

In the third case, you can threaten the A.I.'s home planet after conquering only one planet. You end up threatening two planets, and there's an extra planet that isn't connected to anything.

In the fourth case, you can threaten the A.I.'s home planet by conquering planets 44 and 55. You end up threatening planets 66, 22, 33 and 11 (the A.I.'s home planet).

Limits

  • 1T501 \leq T \leq 50.
  • 0xi<yi<P0 \leq x_{i} < y_{i} < P.
  • Each wormhole is unique: If iji \neq j, then (xi,yi)(xj,yj)(x_{i}, y_{i}) \neq (x_{j}, y_{j}).
  • There will be at least one way to reach the A.I.'s home planet from your home planet using a series of wormholes.

Small dataset (10 Pts, Test set 1 - Visible)

  • 2P362 \leq P \leq 36.
  • 1W6301 \leq W \leq 630.
  • Time limit: 30 3 seconds.

Large dataset (22 Pts, Test set 2 - Hidden)

  • 2P4002 \leq P \leq 400.
  • 1W20001 \leq W \leq 2000.
  • Time limit: 60 6 seconds.