Advertisement
Guest User

SNSS 2012 Round 3 Problem E

a guest
Aug 19th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1.  
  2.  
  3. import java.awt.Point;
  4. import java.io.*;
  5. import java.math.BigInteger;
  6. import java.util.*;
  7.  
  8. import static java.lang.Math.*;
  9.  
  10. public class E implements Runnable{
  11.    
  12.     BufferedReader in;
  13.     PrintWriter out;
  14.     StringTokenizer tok = new StringTokenizer("");
  15.    
  16.     boolean DEBUG = new File("input.txt").exists();
  17.    
  18.     void init() throws FileNotFoundException{
  19.         if (!DEBUG){
  20.             in = new BufferedReader(new InputStreamReader(System.in));
  21.             out = new PrintWriter(System.out);
  22.         }else{
  23.             in = new BufferedReader(new FileReader("input.txt"));
  24.             out = new PrintWriter("output.txt");
  25.         }
  26.     }
  27.    
  28.     String readString() throws IOException{
  29.         while(!tok.hasMoreTokens()){
  30.             try{
  31.                 tok = new StringTokenizer(in.readLine());
  32.             }catch (Exception e){
  33.                 return null;
  34.             }
  35.         }
  36.         return tok.nextToken();
  37.     }
  38.    
  39.     int readInt() throws IOException{
  40.         return Integer.parseInt(readString());
  41.     }
  42.    
  43.     long readLong() throws IOException{
  44.         return Long.parseLong(readString());
  45.     }
  46.    
  47.     double readDouble() throws IOException{
  48.         return Double.parseDouble(readString());
  49.     }
  50.    
  51.     public static void main(String[] args){
  52.         new Thread(null, new E(), "", 256 * (1L << 20)).start();
  53.     }
  54.    
  55.     long timeBegin, timeEnd;
  56.  
  57.     void time(){
  58.         timeEnd = System.currentTimeMillis();
  59.         System.err.println("Time = " + (timeEnd - timeBegin));
  60.     }
  61.    
  62.     long memoryTotal, memoryFree;
  63.    
  64.  
  65.     void memory(){
  66.         memoryFree = Runtime.getRuntime().freeMemory();
  67.         System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB");
  68.     }
  69.    
  70.     void debug(Object... objects){
  71.         if (DEBUG){
  72.             for (Object o: objects){
  73.                 System.err.println(o.toString());
  74.             }
  75.         }
  76.     }
  77.    
  78.     public void run(){
  79.         try{
  80.             timeBegin = System.currentTimeMillis();
  81.             memoryTotal = Runtime.getRuntime().freeMemory();
  82.             init();
  83.             solve();
  84.             out.close();
  85.             time();
  86.             memory();
  87.         }catch (Exception e){
  88.             e.printStackTrace(System.err);
  89.             System.exit(-1);
  90.         }
  91.     }
  92.    
  93.     class Domino{
  94.        
  95.         int first, second;
  96.  
  97.         public Domino(int first, int second) {
  98.             this.first = first;
  99.             this.second = second;
  100.         }
  101.        
  102.         int getOther(int value){
  103.             if (value == first){
  104.                 return second;
  105.             }
  106.            
  107.             if (value == second){
  108.                 return first;
  109.             }
  110.            
  111.             return -1;
  112.         }
  113.        
  114.         boolean equals(Domino d){
  115.             return (first == d.first && second == d.second
  116.                     ||
  117.                     first == d.second && second == d.first);
  118.                
  119.         }
  120.     }
  121.    
  122.     int MAX_VALUE = 8;
  123.    
  124.     void solve() throws IOException{
  125.         int n = readInt();
  126.        
  127.         Domino[] d = new Domino[n];
  128.         for (int i = 0; i < n; ++i){
  129.             d[i] = new Domino(readInt(), readInt());
  130.         }
  131.        
  132.         int lim = (1 << n);
  133.         long[][][] dp = new long[MAX_VALUE][MAX_VALUE][lim];
  134.        
  135.     a:  for (int i = 0; i < n; ++i){
  136.             for (int before = 0; before < i; ++before){
  137.                 if (d[before].equals(d[i])){
  138.                     continue a;
  139.                 }
  140.             }
  141.        
  142.             dp[d[i].first][d[i].second][(1 << i)] = 1;
  143.            
  144.             dp[d[i].second][d[i].first][(1 << i)] = 1;
  145.         }
  146.        
  147.         for (int first = 1; first < MAX_VALUE; ++first){
  148.             for (int mask = 1; mask < lim; ++mask){
  149.                 for (int last = 1; last < MAX_VALUE; ++last){
  150.                 it: for (int cur = 0; cur < n; ++cur){
  151.                         if (check(mask, cur)){
  152.                             int next = d[cur].getOther(last);
  153.                             if (next == -1){
  154.                                 continue;
  155.                             }
  156.                            
  157.                             for (int before = 0; before < cur; ++before){
  158.                                 if (check(mask, before) && d[cur].equals(d[before])){
  159.                                     continue it;
  160.                                 }
  161.                             }
  162.                            
  163.                             int newMask = (mask | (1 << cur));
  164.                             dp[first][next][newMask] += dp[first][last][mask];
  165.                         }
  166.                     }
  167.                 }
  168.             }
  169.         }
  170.        
  171.         long ans = 0;
  172.         for (int i = 1; i < MAX_VALUE; ++i){
  173.             ans += dp[i][i][lim-1];
  174.         }
  175.        
  176.         out.print(ans);
  177.     }
  178.    
  179.     boolean check(int mask, int index){
  180.         return (mask & (1 << index)) == 0;
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement