题解

1 条题解

  • 0
    @ 2017-11-08 16:58:46
    #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("");
    }
    
  • 1

信息

难度
9
分类
(无)
标签
(无)
递交数
1
已通过
1
通过率
100%
上传者