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

来源:互联网 发布:淘宝网店转让有风险吗 编辑:程序博客网 时间:2024/06/08 18:56


大致题意:

1e5个6个元素的数组,问是否有两个数组是同构的

思路:

对每个数组HASH后插入,用链表遇到地址冲突的时候用最小表示法判同构近似O(n)





Snowflake Snow Snowflakes
Time Limit: 4000MS Memory Limit: 65536KTotal 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

21 2 3 4 5 64 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_pairtypedef 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);//#endifint 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 ;}


0 0
原创粉丝点击