import java.io.*; import java.util.*; class Component { private ArrayList adj; private int col; public Component() { adj = new ArrayList(); col = 0; } public void linkWith(Component other) { adj.add(other); other.adj.add(this); } public void mergeWith(Component other) { adj.addAll(other.adj); for(int i = 0; i < other.adj.size(); i++) { Component component = (Component)(other.adj.get(i)); for(int j = 0; j < component.adj.size(); j++) { if(component.adj.get(j) == other) { component.adj.set(j, this); } } } } public boolean isThreeColorable() { if(col > 0) { return true; } else { List list = new ArrayList(); trace(list); return isThreeColorable(list, 0); } } private static boolean isThreeColorable(List list, int k) { if(k == list.size()) { return true; } Component c = (Component)(list.get(k)); for(int i = 1; i <= 3; i++) { if(c.color(i)) { if(isThreeColorable(list, k + 1)) return true; c.col = 0; } } return false; } private void trace(List list) { list.add(this); col = -1; for(int i = 0; i < adj.size(); i++) { Component c = (Component)(adj.get(i)); if(c.col == 0) { c.trace(list); } } } private boolean color(int c) { for(int i = 0; i < adj.size(); i++) { if(((Component)(adj.get(i))).col == c) { return false; } } col = c; return true; } } class Module { private ArrayList allComponents; private Component[] activeComponents; private int numTracks; public Module(int numTracks) { allComponents = new ArrayList(); activeComponents = new Component[numTracks]; for(int i = 0; i < numTracks; i++) { Component component = new Component(); allComponents.add(component); activeComponents[i] = component; } this.numTracks = numTracks; } public void add(int track) { Component component = new Component(); allComponents.add(component); activeComponents[track] = component; } public void link(int track1, int track2) { activeComponents[track1].linkWith(activeComponents[track2]); } public void mergeWith(Module other) { if(numTracks != other.numTracks) { throw new IllegalArgumentException("Cannot be merged"); } allComponents.addAll(other.allComponents); for(int i = 0; i < numTracks; i++) { activeComponents[i].mergeWith(other.activeComponents[i]); allComponents.remove(other.activeComponents[i]); } } public boolean isThreeColorable() { for(int i = 0; i < allComponents.size(); i++) { Component component = (Component)(allComponents.get(i)); if(!component.isThreeColorable()) { return false; } } return true; } } class Robot { private int numTracks; public Robot() { } public Module build(String blueprint) { StringTokenizer lex = new StringTokenizer(blueprint); numTracks = Integer.parseInt(lex.nextToken()) + 1; if(!lex.nextToken().equals("(")) { throw new IllegalArgumentException("Illegal blueprint"); } Module module = build(lex); if(lex.hasMoreTokens()) { throw new IllegalArgumentException("Illegal blueprint"); } return module; } private Module build(StringTokenizer lex) { Module module = new Module(numTracks); while(true) { String token = lex.nextToken(); if(token.equals("(")) { module.mergeWith(build(lex)); continue; } if(token.equals(")")) { return module; } if(token.length() == 1) { module.add(Integer.parseInt(token)); continue; } if(token.length() == 2) { int tracks = Integer.parseInt(token); int track1 = tracks / 10; int track2 = tracks % 10; module.link(track1, track2); continue; } throw new IllegalArgumentException("Operation: " + token); } } } public class colours_yuizumi { public static void main(String[] args) throws Exception { BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(System.in)); int nCase = 0; while(true) { String line = br.readLine(); if(line.startsWith("0 ")) break; ++nCase; System.out.print("Toy " + nCase + ": "); boolean flag = new Robot().build(line).isThreeColorable(); System.out.print(flag ? "Yes" : "No"); System.out.println(); } } finally { if(br != null) br.close(); } } }