求调
查看原帖
求调
1012144
Xiao_QianQian楼主2024/10/5 10:08
#include <iostream>
#include <vector>
#include <string>
const int MAXN = 100005;
std::vector<int> tree[MAXN];
std::string cowType;
int parent[MAXN];
int depth[MAXN];
void dfs(int node, int par, int dep) {
    parent[node] = par;
    depth[node] = dep;
    for (std::vector<int>::iterator it = tree[node].begin(); it!= tree[node].end(); ++it) {
        int child = *it;
        if (child!= par) {
            dfs(child, node, dep + 1);
        }
    }
}
char getLCAType(int a, int b) {
    while (depth[a] > depth[b]) {
        a = parent[a];
    }
    while (depth[b] > depth[a]) {
        b = parent[b];
    }
    while (a!= b) {
        a = parent[a];
        b = parent[b];
    }
    return cowType[a];
}
int main() {
    int n, m;
    std::cin >> n >> m;
    std::cin >> cowType;
    for (int i = 1; i < n; i++) {
        int x, y;
        std::cin >> x >> y;
        tree[x].push_back(y);
        tree[y].push_back(x);
    }
    dfs(1, -1, 1);
    std::string result;
    for (int i = 0; i < m; i++) {
        int a, b;
        char c;
        std::cin >> a >> b >> c;
        char lcaType = getLCAType(a, b);
        if (lcaType == c) {
            result += '1';
        } else {
            result += '0';
        }
    }
    std::cout << result << std::endl;
    return 0;
}

谢谢大佬

2024/10/5 10:08
加载中...