参考博客:https://blog.csdn.net/daixinliangwyx/article/details/90300400(JAVA)
超内存了。。。。。。(有空指教一哈)
我提交的题目地址: http://oj.ecustacm.cn/problem.php?id=1400
#include <iostream> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int L = 100009; vector<int> child[L]; vector<int> data[L]; int N,Q; vector<int> getChild(int node) { for (int i = 0; i < data[node].size(); i++) { vector<int> chs = getChild(data[node][i]); child[node].insert(child[node].end(), chs.begin(), chs.end()); } child[node].push_back(node); return child[node]; } int main() { int p,s; while(~scanf("%d%d", &N, &Q)) { for (int i = 1; i < N; i++) { scanf("%d%d", &p, &s); data[p].push_back(s); } child[1] = getChild(1); for (int i = 1; i <= Q; i++) { scanf("%d%d", &p, &s); vector<int>::iterator it = find(child[p].begin(), child[p].end(), s); if (it != child[p].end()) { cout << "YES" << endl; } else { cout << "NO" << endl; } } for (int i = 0; i < L; i++) { data[i].clear(); child[i].clear(); } } return 0; }
。。。。。。。。。。。。