清北学堂-D6-T2-tree

这里写图片描述
此题要判同构,太麻烦了,而且时间空间都承受不起。
30pts:
暴力即可。
50pts:
听说可以dp?反正我不会。
100pts:
1.STL(std做法):

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int N = 120000;
vector<int> E[N];
map<vector<int>, int> id;
int ans[N], p[N], vis[N];
int n, cnt;
int work(int x) {
    vector<int> u;
    for(int i = 0; i < E[x].size(); i++) {
        int y = E[x][i];
        u.push_back(work(y));
    }
    sort(u.begin(), u.end());
    if (!id[u]) id[u] = ++cnt;
    ans[x] = id[u];
    return ans[x];
}


int main(){
    scanf("%d", &n);
    for(int i = 2; i <= n; i++){
        scanf("%d", &p[i]);
        E[p[i]].push_back(i);
    }
    work(1);
    cnt = 0;

    for(int i = 1; i <= n; i++) {
        if (!vis[ans[i]]) {
            vis[ans[i]] = ++cnt;
        }
        ans[i] = vis[ans[i]];
    }

    for(int i = 1; i <= n; i++) printf("%d ", ans[i]);
    puts("");
}

2.toposort+hash
我们可以对于每一个点记录一个cnt值,来记录它的hash值,然后怎么hash呢?
我一开始是这么写的:

void toposort(){
    while(!q.empty()){
        int x=q.front();
        q.pop();
        du[fa[x]]--;
        cnt[fa[x]]+=cnt[x]*13331;
        cnt[fa[x]]%=p;
        if(du[fa[x]]==1)q.push(fa[x]);
    }
}

思想:在toposort的时候用子节点更新父节点的hash值。
然而此算法是错的,因为对于这样的树,我的代码会判它是同构:
这里写图片描述
从中体现的问题就是:我的代码只是判了子树hash值的和,并没有判出来每个子树的不同。
有一种方法解决这个问题:
我们把代码改一下,把 cnt[x] 改成 primecnt[x] 这里就要看RP了,一个可以过的prime是2333(滑稽)
AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<map>
#define ll long long
using namespace std;
inline int read(){
    int x=0;char ch=' ';int f=1;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')f=-1,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
inline ll ksm(ll a,ll b,ll p){
    a%=p;
    ll ans=1;
    while(b){
        if(b&1){
            ans=(ans*a)%p;
        }
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
}
int n;
int du[100001];
queue<int> q;
ll cnt[100001];
int fa[100001];
const int p=1e9+7;
void toposort(){
    while(!q.empty()){
        int x=q.front();
        q.pop();
        du[fa[x]]--;
        cnt[fa[x]]+=ksm(2333,cnt[x],p)*13331;
        cnt[fa[x]]%=p;
        if(du[fa[x]]==1)q.push(fa[x]);
    }
}
map<int,int> mp;
int color[100001];
int main(){
    freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);
    n=read();
    for(int i=2;i<=n;i++){
        fa[i]=read();
        du[i]++;du[fa[i]]++;
    }
    for(int i=2;i<=n;i++){
        if(du[i]==1){
            cnt[i]=1;
            q.push(i);
        }
    }
    toposort();
    int now=0;
    for(int i=1;i<=n;i++){
        if(mp[cnt[i]]==0){
            mp[cnt[i]]=++now;
            color[i]=now;
        }
        else{
            color[i]=mp[cnt[i]];
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d ",color[i]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值