Hdu 5732 Subway

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a1s4z5/article/details/53165264

给你两个同构的树,找出他们节点间的对应方式


树哈希本质上就是一个树dp。考虑以树的唯一的一个节点(比如重心,直径中点)作为树根,然后用一个和子树顺序无关的方式去哈希就好了。

具体见代码

#include<bits/stdc++.h>
using namespace std;

const int maxn = 112345;
#define LL long long 
#define Deag
const int mod = 100000007;
const int seed = 17;

char inp[15],onp[15];

struct Tree{
    vector<int> edge[maxn];
    LL Har[maxn];
    string anw[maxn], rip[maxn];
    void done(int st,int fa){
        if(fa == 0) _cnt = 0;
        anw[_cnt++] = rip[st]; 
        for(auto x : edge[st]){
            if(x == fa) continue;
            done(x,st);
        }
    }
    int siz[maxn];
    LL dfsh(int st,int fa){
        Har[st] = seed;
        siz[st] = 1;
        for(auto x : edge[st]){
            if(x == fa) continue;
            dfsh(x,st);
            siz[st] += siz[x];
        }
        sort(edge[st].begin(),edge[st].end(),[&](int x,int y){return Har[x] < Har[y];});
        for(auto x : edge[st]){
            if(x == fa) continue;
            (Har[st] *= Har[x] )%= mod;
            (Har[st] ^= Har[x] )%= mod;
        }
        (Har[st] += siz[st]) %= mod;
        (Har[st] *= Har[st]) %= mod;
        return Har[st];
    } 
    int dep[maxn],fa[maxn];
    void getdep(int st,int Fa,int Dep){
        dep[st] = Dep,fa[st] = Fa;
        for(auto x : edge[st]){
            if(x == fa[st]) continue;
            getdep(x,st,Dep+1);
        }
    }
    int ctr[2];
    void getCtr(int n){
        getdep(1,0,0);
        ctr[0] = max_element(dep+1,dep+1+n) - dep;
        getdep(ctr[0],0,0);
        ctr[0] = max_element(dep+1,dep+1+n) - dep;
        int fdep = dep[ctr[0]];
        for(int i = 0; i < fdep/2; i++) 
            ctr[0] = fa[ctr[0]];
        ctr[1] = ctr[0];
        if(fdep & 1) ctr[1] = fa[ctr[1]];
    }
    map<string,int> id;
    int _cnt;
    void init(int n){
        for(int i = 0 ; i <= n;i++) 
            edge[i].clear();
        id.clear();
        _cnt = 1;
    }
    int toid(string v){
        if(id.find(v) != id.end()) return id[v];
        rip[_cnt] = v;
        return id[v] = _cnt++;
    }
    void input(int n){
        init(n);
        int l , r;
        for(int i = 1; i < n;i++){
            scanf("%s %s",inp,onp);
            l = toid(inp),r = toid(onp); 
            edge[l].push_back(r);
            edge[r].push_back(l);
        }
    }
}X,Y;

void solve(){
    for(int i = 0 ; i < 2; i ++){
        for(int j = 0 ; j < 2;j ++){
            if(X.dfsh(X.ctr[i],0) == Y.dfsh(Y.ctr[j],0)){
                X.done(X.ctr[i],0);
                Y.done(Y.ctr[j],0);
                return;
            }
        }
    }
}

int main(){
    int n;
    while(~scanf("%d",&n)){
        X.input(n);
        Y.input(n);
        X.getCtr(n);
        Y.getCtr(n);
        solve();
        for(int i = 0 ; i < n ; i++)
            printf("%s %s\n",X.anw[i].c_str(),Y.anw[i].c_str());
    }
    return 0;
}

Subway planning

09-13

Problem DescriptionnThe government in a foreign country is looking into the possibility of establishing a subway system in its capital. Because of practical reasons, they would like each subway line to start at the central station and then go in a straight line in some angle as far as necessary. You have been hired to investigate whether such an approach is feasible. Given the coordinates of important places in the city as well as the maximum distance these places can be from a subway station (possibly the central station, which is already built), your job is to calculate the minimum number of subway lines needed. You may assume that any number of subway stations can benbuilt along a subway line.n![](http://acm.hdu.edu.cn/data/images/1922-1.jpg)nFigure 1: The figure above corresponds to the first data set in the example input.n nnInputnThe first line in the input file contains an integer N, the number of data sets to follow. Each set starts with two integers, n and d (1 <= n <= 500, 0 <= d < 150). n is the number of important places in the city that must have a subway station nearby, and d is the maximum distance allowednbetween an important place and a subway station. Then comes n lines, each line containing two integers x and y (-100 <= x, y <= 100), the coordinates of an important place in the capital. The central station will always have coordinates 0, 0. All pairs of coordinates within a data setnwill be distinct (and none will be 0, 0).n nnOutputnFor each data set, output a single integer on a line by itself: the minimum number of subway lines needed to make sure all important places in the city is at a distance of at most d from a subway station.n nnSample Inputn2n7 1n-1 -4n-3 1n-3 -1n2 3n2 4n2 -2n6 -2n4 0n0 4n-12 18n0 27n-34 51n nnSample Outputn4n2n

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试