#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N=55;
int n,m,pri[105],tot,sz[N];
ull h[N],Hash[N];
bool np[1005];
vector<int>v[N];
unordered_map<ull,int>res;
void pre()
{
for(int i=2;i<=1e3;i++)
{
if(!np[i])
{
pri[++tot]=i;
for(int j=2;j*i<=1e3;j++)
{
np[i*j]=1;
}
}
}
}
void dfs1(int p)
{
sz[p]=1;
h[p]=1;
for(int t:v[p])
{
dfs1(t);
h[p]+=h[t]*pri[sz[t]];
}
return;
}
void dfs2(int p)
{
for(int t:v[p])
{
ull tmp=h[p]-h[t]*pri[sz[t]];
h[t]+=tmp*pri[n-sz[t]];
dfs2(t);
}
}
ull calc()
{
ull res=1;
for(int i=1;i<=n;i++)res*=h[i];
return res;
}
int main()
{
clock_t c1=clock();
#ifdef LOCAL
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>m;
pre();
for(int i=1;i<=m;i++)
{
int root;
cin>>n;
for(int i=1;i<=n;i++)v[i].clear();
for(int j=1;j<=n;j++)
{
int f;
cin>>f;
if(!f)root=j;
v[f].push_back(j);
}
dfs1(root);
dfs2(root);
Hash[i]=calc();
if(!res.count(Hash[i]))res[Hash[i]]=i;
}
for(int i=1;i<=m;i++)cout<<res[Hash[i]]<<endl;
#ifdef LOCAL
cerr<<"Time used:"<<clock()-c1<<"ms";
#endif
return 0;
}
在我输入样例后,程序出现了意料之外的 RE,我是按照平时建图的方法写的。