Problem:
Problem G: Color the Map

You were lucky enough to get a map just before entering the legendary magical mystery world.
The map shows the whole area of your planned exploration, including several countries with
complicated borders. The map is clearly drawn, but in sepia ink only; it is hard to recognize at
a glance which region belongs to which country, and this might bring you into severe danger. You
have decided to color the map before entering the area. “A good deal depends on preparation,”
you talked to yourself.

Each country has one or more territories, each of which has a polygonal shape. Territories
belonging to one country may or may not “touch” each other, i.e. there may be disconnected
territories. All the territories belonging to the same country must be assigned the same color.
You can assign the same color to more than one country, but, to avoid confusion, two countries
“adjacent” to each other should be assigned different colors. Two countries are considered to be
“adjacent” if any of their territories share a border of non-zero length.

Write a program that finds the least number of colors required to color the map.

Input

The input consists of multiple map data. Each map data starts with a line containing the total
number of territories n, followed by the data for those territories. n is a positive integer not
more than 100. The data for a territory with m vertices has the following format:

String
x1 y1
x2 y2
...
xm ym
-1

“String” (a sequence of alphanumerical characters) gives the name of the country it belongs to.
A country name has at least one character and never has more than twenty. When a country
has multiple territories, its name appears in each of them.

Remaining lines represent the vertices of the territory. A vertex data line has a pair of nonneg-
ative integers which represent the x- and y-coordinates of a vertex. x- and y-coordinates are
separated by a single space, and y-coordinate is immediately followed by a newline. Edges of
the territory are obtained by connecting vertices given in two adjacent vertex data lines, and byconnecting vertices given in the last and the first vertex data lines. None of x- and y-coordinates
exceeds 1000. Finally, -1 in a line marks the end of vertex data lines. The number of vertices
m does not exceed 100.

You may assume that the contours of polygons are simple, i.e. they do not cross nor touch
themselves. No two polygons share a region of non-zero area. The number of countries in a map
does not exceed 10.

The last map data is followed by a line containing only a zero, marking the end of the input
data.

Output

For each map data, output one line containing the least possible number of colors required to
color the map satisfying the specified conditions.

Sample Input

6
Blizid
0 0
60 0
60 60
0 60
0 50
50 50
50 10
0 10
-1
Blizid
0 10
10 10
10 50
0 50
-1
Windom
10 10
50 10
40 20
20 20
20 40
10 50
-1
Accent
50 10
50 50
35 50
35 25
-1
Pilot
35 25
35 50
10 50
-1
Blizid
20 20
40 20
20 40
-1
4
A1234567890123456789
0 0
0 100
100 100
100 0
-1
B1234567890123456789
100 100
100 200
200 200
200 100
-1
C1234567890123456789
0 100
100 100
100 200
0 200
-1
D123456789012345678
100 0
100 100
200 100
200 0
-1
0

Output for the Sample Input

4
2

Solution in JAVA (judge verdict: Accepted)

import java.awt.geom.*;
import java.io.*;
import java.util.*;


public class Main {
	//0156 start
	double EPS = 1.0e-08;
	int ans ,n, m;
	boolean [] used;
	Poly [] data;
	boolean [][] pass;
	int [] color;
	ArrayList<ArrayList<Integer>> country;

	class Poly {
		int id;
		ArrayList<Line2D> lines;
		public Poly(int id,ArrayList<Line2D> lines) {
			this.id = id;
			this.lines = lines;
		}
		@Override
		public String toString() {
			return "Poly [id=" + id + ", lines=" + lines + "]";
		}

	}

	private void doit(){
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			n = sc.nextInt();
			if(n == 0) break;
			data = new Poly[n];
			HashMap<String, Integer> map = new HashMap<String, Integer>();
			country = new ArrayList<ArrayList<Integer>>();

			for(int i = 0; i < n; i++){
				String na = sc.next();
				int id = -1;
				if(map.containsKey(na)){
					id = map.get(na);
				}
				else{
					map.put(na, map.size()+1);
					id = map.get(na);
				}
				ArrayList<Point2D> temp = new ArrayList<Point2D>();
				while(true){
					int x = sc.nextInt();
					if(x == -1){
						break;
					}
					int y = sc.nextInt();
					temp.add(new Point2D.Double(x, y));
				}
				ArrayList<Line2D> lines = new ArrayList<Line2D>();
				int len = temp.size();
				for(int j = 0; j < len; j++){
					lines.add( new Line2D.Double(temp.get(j), temp.get((j+1) % len)));
				}
				data[i] = new Poly(id, lines);
			}
			m = map.size();
			for(int i = 0; i < m; i++){
				country.add(new ArrayList<Integer>());
			}
			for(int i = 0; i < n; i++){
				country.get(data[i].id-1).add(i);
			}

			//create graph
			pass = new boolean[n][n];
			for(int i = 0; i < n; i++){
				for(int j = i + 1; j < n; j++){
					boolean res = intersect(data[i].lines, data[j].lines);
					if(res && data[i].id != data[j].id){
						pass[i][j] = true;
						pass[j][i] = true;
					}

				}
			}

			boolean ansf = false;
			for(int i = 1; i < m; i++){
				ans = 1 << 24;
				used = new boolean[m];
				color = new int[n];
				dfs(0, i, 0);
				if(ans != 1 << 24){
					ansf = true;
					break;
				}
			}
			if(ansf){
				System.out.println(ans);
			}
			else{
				System.out.println(m);
			}
		}
	}

	private void dfs(int deep,int max,int kind) {
		if(deep == m){
			ans = Math.min(ans, kind);
			return ;
		}

		for(int i = 1; i <= Math.min(max, deep+1); i++){
			boolean flg = true;
			for(int j = 0; j < country.get(deep).size(); j++){
				int num = country.get(deep).get(j);
				for(int k = 0; k < n; k++){
					if(! pass[num][k]) continue;
					if(num == k) continue;
					if(color[k] != 0 && color[k] == i){
						flg = false;
					}
				}
			}

			if(flg){
				for(int j = 0; j < country.get(deep).size(); j++){
					color[country.get(deep).get(j)] = i;
				}
				dfs(deep + 1, max, Math.max(kind, i));
				for(int j = 0; j < country.get(deep).size(); j++){
					color[country.get(deep).get(j)] = 0;
				}
			}
		}
	}

	private boolean intersect(ArrayList<Line2D> a, ArrayList<Line2D> b) {
		for(int i = 0; i < a.size();i++){
			for(int j = 0; j < b.size(); j++){
				if(a.get(i).intersectsLine(b.get(j))){
					Point2D p1 = sub(a.get(i).getP1(), a.get(i).getP2());
					Point2D p2 = sub(b.get(j).getP2(), b.get(j).getP2());
					if(Math.abs(cross(p1, p2)) < EPS){
						if(isborder(a.get(i), b.get(j)) || isborder(b.get(j), a.get(i))){
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	private boolean isborder(Line2D l, Line2D m) {
		double res = l.ptSegDist(m.getP1());
		if(res < EPS){
			if(l.getP1().distance(m.getP1()) < EPS || l.getP2().distance(m.getP1()) < EPS){

			}
			else{
				return true;
			}
		}
		 double res2 = l.ptSegDist(m.getP2());
		if(res2 < EPS){
			if(l.getP1().distance(m.getP2()) < EPS || l.getP2().distance(m.getP2()) < EPS){

			}
			else{
				return true;
			}
		}
		if(res < EPS && res2 < EPS){
			return true;
		}
		return false;
	}

	private Point2D sub(Point2D p2, Point2D p1) {
		double x = p2.getX() - p1.getX();
		double y = p2.getY() - p1.getY();
		return new Point2D.Double(x, y);
	}

	private double cross(Point2D p1, Point2D p2) {
		double res = p1.getX() * p2.getY() - p1.getY() * p2.getX();
		return res;
	}

	public static void main(String [] args){
		new Main().doit();
	}
}