HDU 4547 CD操作

Problem Description

在Windows下我们可以通过cmd运行DOS的部分功能,其中CD是一条很有意思的命令,通过CD操作,我们可以改变当前目录。

这里我们简化一下问题,假设只有一个根目录,CD操作也只有两种方式:

  1. CD 当前目录名\…\目标目录名 (中间可以包含若干目录,保证目标目录通过绝对路径可达)

  2. CD .. (返回当前目录的上级目录)

现在给出当前目录和一个目标目录,请问最少需要几次CD操作才能将当前目录变成目标目录?

Input

输入数据第一行包含一个整数T(T<=20),表示样例个数;

每个样例首先一行是两个整数N和M(1<=N,M<=100000),表示有N个目录和M个询问;

接下来N-1行每行两个目录名A B(目录名是只含有数字或字母,长度小于40的字符串),表示A的父目录是B。

最后M行每行两个目录名A B,表示询问将当前目录从A变成B最少要多少次CD操作。

数据保证合法,一定存在一个根目录,每个目录都能从根目录访问到。

Output

请输出每次询问的结果,每个查询的输出占一行。

Sample Input

2

3 1

B A

C A

B C

3 2

B A

C B

A C

C A

Sample Output

2

1

2

Solution

LCA水题,用map来存每个string的id就好了。(一开始我居然LCA写跪了…)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <map>
#include <string>
using namespace std;
map<string, int> hhhh;
int tot = 1;
typedef long long ll;
const int maxn = 200005;
int T, n, m;
struct edge_type {
    int to, next;
} edge[maxn << 1];
int cnte = 0, h[maxn];
void ins(int u, int v) {
    edge[++cnte].to = v;
    edge[cnte].next = h[u];
    h[u] = cnte;
}
int siz[maxn], son[maxn], top[maxn], fa[maxn], dis[maxn], du[maxn];
void dfs1(int now, int father, int deep) {
    fa[now] = father;
    dis[now] = deep;
    siz[now] = 1;
    int t = 0;
    for (int i = h[now]; i; i = edge[i].next) {
        if (edge[i].to == father) continue;
        dfs1(edge[i].to, now, deep+1);
        siz[now] += siz[edge[i].to];
        if (siz[edge[i].to] > siz[t]) t = edge[i].to;
    }
    son[now] = t;
}
void dfs2(int now, int father, int tp) {
    top[now] = tp;
    if (son[now]) {
        dfs2(son[now], now, tp);
        for (int i = h[now]; i; i = edge[i].next) {
            if (edge[i].to == father || edge[i].to == son[now]) continue;
            dfs2(edge[i].to, now, edge[i].to);
        }
    }
}
int lca(int u, int v) {
    while (top[u] != top[v]) {
        if (dis[top[u]] < dis[top[v]]) swap(u, v);
        u = fa[top[u]];
    }
    if (dis[u] > dis[v]) return v;
    return u;
}
char str[50];
int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);
    cout.tie(0);
    cout.sync_with_stdio(false);
    cin >> T;
    while (T--) {
        hhhh.clear();
        tot = 1;
        cin >> n >> m;
        for (int i = 0; i <= n; ++i) h[i] = du[i] = 0;
        int x, y;
        for (int i = 1; i < n; ++i) {
            cin >> str;
            if (hhhh.find(str) == hhhh.end()) hhhh[str] = tot++;
            x = hhhh[str];
            cin >> str;
            if (hhhh.find(str) == hhhh.end()) hhhh[str] = tot++;
            y = hhhh[str];
            ins(x, y);
            ins(y, x);
            ++du[x];
        }
        int rt;
        for (int i = 1; i < tot; ++i) if (du[i] == 0) { rt = i; break; }
        dfs1(rt, -1, 1);
        dfs2(rt, -1, rt);
        for (int i = 0; i < m; ++i) {
            cin >> str;
            x = hhhh[str];
            cin >> str;
            y = hhhh[str];
            int l = lca(x, y);
            int ans = dis[x] - dis[l];
            if (l != y) ++ans;
            cout << ans << endl;
        }
    }
    return 0;
}