BZOJ4337 树的同构 (树哈希)(未完成)

样例迷,没过
交了30pts

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long
 
#define ON_DEBUGG
 
#ifdef ON_DEBUGG
 
#define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "\ntime: %.3fms\n", clock() * 1000.0 / CLOCKS_PER_SEC)

#else
 
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
 
#endif
 
using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;
 
template<typename ATP>inline ATP Max(ATP a, ATP b){
    return a > b ? a : b;
}
template<typename ATP>inline ATP Min(ATP a, ATP b){
    return a < b ? a : b;
}
template<typename ATP>inline ATP Abs(ATP a){
    return a < 0 ? -a : a;
}
#include <climits>
const int N = 1003;
const int MOD = 9999991;

#define int long long
struct Edge{
	int nxt, pre;
}e[N];
int head[N], cntEdge;
inline void add(int u, int v) {
	e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;
}

int prime[1013], primeIndex;
bool vis[8007];
inline void EularPhi(int n) {
	R(i,2,n){
		if(!vis[i]) prime[++primeIndex] = i;
		for(register int j = 1; j <= primeIndex && i * prime[j] <= n; ++j){
			vis[i * prime[j]] = 1;
			if(i % prime[j] == 0) break;
		}
	}
}

int siz[N];

int f[N], g[N], fa[N];
inline void DFS(int u, int father){
	siz[u] = 1;
	f[u] = 1;
	fa[u] = father;
	for(register int i = head[u]; i; i = e[i].nxt){
		int v = e[i].pre;
		if(v == father) continue;
		DFS(v, u);
		siz[u] += siz[v];
		f[u] = (f[u] + f[v] * prime[siz[v]] % MOD + MOD) % MOD;
	}
}

namespace HASH{

	struct Node{
		int nxt, pre, w;
	}e[N * N];
	int head[MOD + 3], cntHash;

	struct Hash{

		inline void Insert(int x, int id){
			int u = x % MOD;
			for(register int i = head[u]; i; i = e[i].nxt){
				int v = e[i].pre;
				if(v == x){
					return;
				}
			}
			e[++cntHash] = (Node){ head[u], x, id}, head[u] = cntHash;
		}
		inline int Query(int x){
			int u = x % MOD;
			for(register int i = head[u]; i;  i= e[i].nxt){
				int v = e[i].pre;
				if(v == x){
					return e[i].w;
				}
			}
			return 0;
		}
		
	}H;
	
}

#undef int
int main(){
#define int long long
	EularPhi(8000);

	int n, m;
	io >> m;
	R(id,1,m){
		
		cntEdge = 0;
		Fill(head, 0);
		
		io >> n;
		R(i,1,n){
			int fa;
			io >> fa;
			add(i, fa);
			add(fa, i);
		}
		
		DFS(0, 0);
		
		int sum = LLONG_MAX;
		R(i,0,n){
			DFS(i, i);
			sum = Min(sum, f[i]);
		}
		
		HASH::H.Insert(sum, id);
		
		printf("%lld\n", HASH::H.Query(sum));
	}
	
	return 0;
}

{{uploading-image-151875.png(uploading...)}}

posted @ 2019-10-05 18:10  邱涵的秘密基地  阅读(101)  评论(0编辑  收藏  举报