Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Point;
- import java.io.*;
- import java.math.BigInteger;
- import java.util.*;
- import static java.lang.Math.*;
- public class E implements Runnable{
- BufferedReader in;
- PrintWriter out;
- StringTokenizer tok = new StringTokenizer("");
- boolean DEBUG = new File("input.txt").exists();
- void init() throws FileNotFoundException{
- if (!DEBUG){
- in = new BufferedReader(new InputStreamReader(System.in));
- out = new PrintWriter(System.out);
- }else{
- in = new BufferedReader(new FileReader("input.txt"));
- out = new PrintWriter("output.txt");
- }
- }
- String readString() throws IOException{
- while(!tok.hasMoreTokens()){
- try{
- tok = new StringTokenizer(in.readLine());
- }catch (Exception e){
- return null;
- }
- }
- return tok.nextToken();
- }
- int readInt() throws IOException{
- return Integer.parseInt(readString());
- }
- long readLong() throws IOException{
- return Long.parseLong(readString());
- }
- double readDouble() throws IOException{
- return Double.parseDouble(readString());
- }
- public static void main(String[] args){
- new Thread(null, new E(), "", 256 * (1L << 20)).start();
- }
- long timeBegin, timeEnd;
- void time(){
- timeEnd = System.currentTimeMillis();
- System.err.println("Time = " + (timeEnd - timeBegin));
- }
- long memoryTotal, memoryFree;
- void memory(){
- memoryFree = Runtime.getRuntime().freeMemory();
- System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB");
- }
- void debug(Object... objects){
- if (DEBUG){
- for (Object o: objects){
- System.err.println(o.toString());
- }
- }
- }
- public void run(){
- try{
- timeBegin = System.currentTimeMillis();
- memoryTotal = Runtime.getRuntime().freeMemory();
- init();
- solve();
- out.close();
- time();
- memory();
- }catch (Exception e){
- e.printStackTrace(System.err);
- System.exit(-1);
- }
- }
- class Domino{
- int first, second;
- public Domino(int first, int second) {
- this.first = first;
- this.second = second;
- }
- int getOther(int value){
- if (value == first){
- return second;
- }
- if (value == second){
- return first;
- }
- return -1;
- }
- boolean equals(Domino d){
- return (first == d.first && second == d.second
- ||
- first == d.second && second == d.first);
- }
- }
- int MAX_VALUE = 8;
- void solve() throws IOException{
- int n = readInt();
- Domino[] d = new Domino[n];
- for (int i = 0; i < n; ++i){
- d[i] = new Domino(readInt(), readInt());
- }
- int lim = (1 << n);
- long[][][] dp = new long[MAX_VALUE][MAX_VALUE][lim];
- a: for (int i = 0; i < n; ++i){
- for (int before = 0; before < i; ++before){
- if (d[before].equals(d[i])){
- continue a;
- }
- }
- dp[d[i].first][d[i].second][(1 << i)] = 1;
- dp[d[i].second][d[i].first][(1 << i)] = 1;
- }
- for (int first = 1; first < MAX_VALUE; ++first){
- for (int mask = 1; mask < lim; ++mask){
- for (int last = 1; last < MAX_VALUE; ++last){
- it: for (int cur = 0; cur < n; ++cur){
- if (check(mask, cur)){
- int next = d[cur].getOther(last);
- if (next == -1){
- continue;
- }
- for (int before = 0; before < cur; ++before){
- if (check(mask, before) && d[cur].equals(d[before])){
- continue it;
- }
- }
- int newMask = (mask | (1 << cur));
- dp[first][next][newMask] += dp[first][last][mask];
- }
- }
- }
- }
- }
- long ans = 0;
- for (int i = 1; i < MAX_VALUE; ++i){
- ans += dp[i][i][lim-1];
- }
- out.print(ans);
- }
- boolean check(int mask, int index){
- return (mask & (1 << index)) == 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement