import java.io.*; import java.util.*; /** * Red/Blue Spanning Tree * @author vanb * * The trick here is to use Kruskal's algorithm twice. The first time, * we'll use red edges first. Then, we'll fill in with blue edges. * That will tell us which blue edges are *necessary*. * * Then we'll reset, and do it all again. We'll start with the necessary * blue edges, then other blue edges until we have enough, and * finish off with red edges. If we use exactly n-1 edges, and exactly * k blue edges, then we've got a solution. */ public class redblue { public Scanner sc; public PrintStream ps; public static final int MAX_NODES = 1000; public static final int MAX_EDGES = 1000000; /** * A node in our graph * * @author vanb */ public class Node { public int id = 0; public int set = 0; // This is the list of edges from/to this node // that are in the spanning tree. public LinkedList tree = new LinkedList(); public Node( int i ) { id = i; } /** * Merge the subtree rooted here with another set. * * @param newset The set to merge with * @param from The Edge we came from */ public void merge( int newset, Edge from ) { set = newset; for( Edge edge : tree ) if( edge!=from ) { edge.destination( this ).merge( newset, edge ); } } } /** * An edge in our graph * * @author vanb */ public class Edge { public Node n1, n2; /** * Find the destination of this edge, given the source. * * @param source Source node * @return Destination node */ public Node destination( Node source ) { return source==n1 ? n2 : n1; } /** * Put this Edge in the spanning tree */ public void putInTree() { // Merge the two sets if( n1.set==n2.set ) System.out.println( "PANIC!! n1==n2" ); n1.merge( n2.set, null ); // Put this edge in the list of tree nodes // for each of its endpoints n1.tree.add( this ); n2.tree.add( this ); } /** * Pretty String for debugging * * @return Pretty String */ public String toString() { return "[" + n1.id + "-" + n2.id + "]"; } } /** * Do it! * @throws Exception */ public void doit() throws Exception { sc = new Scanner( System.in ); //new File( "redblue.in" ) ); ps = System.out; //new PrintStream( new FileOutputStream( "redblue.out" ) ); // Create all of the nodes beforehand Node nodes[] = new Node[MAX_NODES]; for( int i=0; i reds = new LinkedList(); LinkedList blues = new LinkedList(); LinkedList necessary = new LinkedList(); for(;;) { int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); if( n==0 ) break; // Prepare the nodes for( int i=0; i=k ) break; // Break if we've got enough blues if( edge.n1.set != edge.n2.set ) { edge.putInTree(); ++nblues; ++nedges; } } // Finish off with reds for( Edge edge : reds ) { if( edge.n1.set != edge.n2.set ) { edge.putInTree(); ++nedges; } } // We succeeded if we have exactly n-1 edges, and exactly k of them are blue ps.println( (nedges==n-1 && nblues==k) ? 1 : 0 ); } } /** * Main * * @param args Not used * @throws Exception */ public static void main( String[] args ) throws Exception { new redblue().doit(); } }