POJ 3349 Snowflake Snow Snowflakes ( HASH+最小表示判同構 )


大致題意:

1e5個6個元素的數組,問是否有兩個數組是同構的

思路:

對每個數組HASH後插入,用鏈表遇到地址衝突的時候用最小表示法判同構近似O(n)





Snowflake Snow Snowflakes
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 35401   Accepted: 9314

Description

You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.

Input

The first line of input will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by n lines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.

Output

If all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.

Sample Input

2
1 2 3 4 5 6
4 3 2 1 6 5

Sample Output

Twin snowflakes found.

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
#define rep(i,n) for ( int i=0; i< int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
#define PB push_back
#define MP make_pair
typedef pair<int,int> pii;

template <class T>
inline bool RD(T &ret) {
        char c; int sgn;
        if (c = getchar(), c == EOF) return 0;
        while (c != '-' && (c<'0' || c>'9')) c = getchar();
        sgn = (c == '-') ? -1 : 1 , ret = (c == '-') ? 0 : (c - '0');
        while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
        ret *= sgn;
        return 1;
}
template <class T>
inline void PT(T x) {
        if (x < 0) putchar('-') ,x = -x;
        if (x > 9) PT(x / 10);
        putchar(x % 10 + '0');
}

const int N = 100000 + 100;
const int MOD = 1e5+7;
int a[N][6];
int Hash[N], val[N], nxt[N]; 
int idx ;
int tmp[6];
bool miniexpress(int x , int y ){
	int p = 0 , q = 0;
	while( p < 6 && q < 6 ){
		int k = 0;
		while( a[x][(p+k)%6] == a[y][(q+k)%6] && k <= 5 ) k ++ ;
		if( k == 6 ) return true ;  
		if( a[x][(p+k)%6] > a[y][(q+k)%6] ) p += k+1 ;
		else q += k+1 ; 
	}
	return false ;
}
int f(int num){
	return (num % MOD + num / MOD ) % MOD ;
}
bool issame( int x, int y ){
	if( miniexpress( x , y ) ) return true ;
	rep( i , 3 ) swap( a[x][i] , a[x][ 6-i-1 ] );
	if( miniexpress( x , y ) ) return true ;
	return false ;
}
bool insert(int u, int num){
	int h = f( num );
	for(int i = Hash[h] ; ~i ; i = nxt[i] ){
		int v = val[i];
		if( issame( u , v ) ) return false; 
	}
	val[idx] = u ; 
	nxt[idx] = Hash[h];
	Hash[h] = idx ++ ;
	return true ;
}
int main(){


//#ifdef ac
//	freopen("in.txt","r",stdin);
//#endif
	
	int n;
	RD(n) ;
	idx = 0;
	bool flag = 0;
	memset( Hash , -1 , sizeof( Hash ) );
	REP( i , n ){
		int sum = 0;
		rep(j,6) scanf("%d",&a[i][j]) ;
		if( flag ) continue ;
		rep(j,6) sum += a[i][j] ;
		if( insert( i , sum ) == false ) flag = 1;
	}
	if( flag ) puts("Twin snowflakes found.");
	else puts("No two snowflakes are alike.");
	return true ;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章