首页 > 专题系列 > Java解POJ > POJ 3763 Tour in Wonder Land [解题报告] Java
2013
11-13

POJ 3763 Tour in Wonder Land [解题报告] Java

Tour in Wonder Land

问题描述 :

You must have heard of the literature “Alice’s Adventures in Wonderland”! The 3D film “Alice in Wonderland” published by Disney Company becomes the most popular film in 2010! Today, Alice starts her new adventure in Wonderland again!

In Wonderland, there are n cities, numbered from 1 to n. The capital is city 1. Cities are connected by bi-directional roads. The road system is constructed so that from every city, there is exactly one way to reach any other.

Alice hopes that she could make a tour in which she visits each city exactly once then return to the capital. Furthermore, she doesn’t want to traverse any road more than once. Obviously, it is impossible to make such tour with current road system.

However, in Wonderland, we can use magic spells! The Cheshire Cat, with his magic power, plans to construct more roads to make Alice’s dream come true. Sadly, if he used too many magic spells, he would be weaken. “What is the minimum number of roads I have to construct?” – The poor cat wonders. Could you help him answer this question?

输入:

The first line of input contains the number n — the number of cities in Wonderland (3 ≤ n ≤ 100 000). The following n-1 lines describe the roads. Each road is represented by two integers — the indexes of two cities it connects.

输出:

The minimum number of roads to construct.

样例输入:

5
1 2
2 3
2 4
1 5

样例输出:

2

温馨提示:

In this example, we can construct 2 roads: 3-4 and 4-5, then Alice will be able to take a tour 1-2-3-4-5-1

解题代码:

//* @author 
import java.util.*;
import java.io.*;

public class Main {
 static Scanner in = new Scanner(System.in);
 static class Node extends LinkedList< Node> {
	final int[] dp = new int[]{-1, -1};
	int query(int b) {
		if(dp[b]< 0) init(null);
		return dp[b];
	}
	void init(Node father) {
         dp[0] = dp[1] = 0;
	 if(this.size()==1) return;
	 int f = 0;
	 int[] deta = new int[]{Integer.MAX_VALUE, Integer.MAX_VALUE};
	 for(Node v: this) {
		if(v == father) continue;
		v.init(this);
		dp[0] += v.dp[1]+1;
		deta[f] = Math.min(deta[f], v.dp[0]-v.dp[1]);
		if(deta[f]< deta[f^1]) f^=1;
	}
	dp[0] += deta[f^1] - 1;
	dp[1] = dp[0]+Math.min(0, deta[f]-1);
       }
   }

  public static void main(String[] args) {
   int n = in.nextInt();
   Node[] nodes = new Node[n];
   for(int i=0; i!=n; ++i)
	nodes[i] = new Node();
   for(int i=1; i!=n; ++i) {
	int x = in.nextInt()-1;
	int y = in.nextInt()-1;
	nodes[x].add(nodes[y]);
	nodes[y].add(nodes[x]);
   }
   System.out.println(nodes[0].query(1)+1);
 }
}

  1. bottes vernies blanches

    I appreciate the efforts you men and women place in to share blogs on such sort of matters, it was certainly useful. Keep Posting!

  2. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks