版本分支

第五题:版本分支
小明负责维护公司一个奇怪的项目。这个项目的代码一直在不断分支(branch)但是从未发生过合并(merge)。
现在这个项目的代码一共有N个版本,编号1~N,其中1号版本是最初的版本。
除了1号版本之外,其他版本的代码都恰好有一个直接的父版本;即这N个版本形成了一棵以1为根的树形结构。 如下图就是一个可能的版本树: 1
/ \
2 3
| / \
5 4 6 现在小明需要经常检查版本x是不是版本y的祖先版本。你能帮助小明吗? 输入
----
第一行包含两个整数N和Q,代表版本总数和查询总数。
以下N-1行,每行包含2个整数u和v,代表版本u是版本v的直接父版本。
再之后Q行,每行包含2个整数x和y,代表询问版本x是不是版本y的祖先版本。 对于30%的数据,1 <= N <= 1000 1 <= Q <= 1000
对于100%的数据,1 <= N <= 100000 1 <= Q <= 100000 输出
----
对于每个询问,输出YES或NO代表x是否是y的祖先。 【样例输入】
6 5
1 2
1 3
2 5
3 6
3 4
1 1
1 4
2 6
5 2
6 4 【样例输出】
YES
YES
NO
NO
NO 资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms 请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。 所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
不要使用package语句。不要使用jdk1.7及以上版本的特性。
主类的名字必须是:Main,否则按无效代码处理。
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*; public class MainB {
public static InputReader in = new InputReader(new BufferedInputStream(System.in));
public static PrintWriter out = new PrintWriter(System.out);
public static int n, q, u, v, x, y;
public static ArrayList<Integer>[] child = new ArrayList[100010];
public static ArrayList<Integer>[] next = new ArrayList[100010]; public static void main(String[] args) {
n = in.nextInt();
q = in.nextInt();
for (int i = 1; i <= n; i++) {
next[i] = new ArrayList<>();
child[i] = new ArrayList<>();
}
for (int i = 1; i < n; i++) {
u = in.nextInt();
v = in.nextInt();
next[u].add(v);
}
child[1] = getChild(1);
while (q-- > 0) {
x =in.nextInt();
y = in.nextInt();
if (child[x].contains(y)) {
out.println("YES");
out.flush();
} else {
out.println("NO");
out.flush();
}
}
out.close();
} static ArrayList<Integer> getChild(int root) {
int len = next[root].size();
for (int i = 0; i < len; i++)
child[root].addAll(getChild(next[root].get(i)));
child[root].add(root);
return child[root];
} static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer; public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
} public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public String nextLine() {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
} public int nextInt() {
return Integer.parseInt(next());
} public long nextLong() {
return Long.parseLong(next());
} public Double nextDouble() {
return Double.parseDouble(next());
} public BigInteger nextBigInteger() {
return new BigInteger(next());
} }
}

最新文章

  1. UVa1401 Remember the Word(DP+Trie树)
  2. SHOW OPEN TABLES – what is in your table cache
  3. eclipse 每次切换工作空间都要重新配置
  4. FlashBuilder 4.7 破解方法
  5. agile学习
  6. JavaScript开发中几个常用知识点总结
  7. VBA 中窗体模式切换,一次设计2种表现
  8. 20145237 实验五《Java网络编程》
  9. CentOS 7系统初始化
  10. 90道Python面试题,做对80%直击年薪40w
  11. Flink--输入数据集Data Sources
  12. Python自学:第三章 修改列表元素
  13. day11 map函数
  14. 用visual studio 2017来调试python
  15. 关于Web安全的那些事(XSS攻击)
  16. oracle获取执行计划及优缺点 详解
  17. &quot;|&quot;,“.”,&quot;include&quot;在jade中的用法(原样输出)
  18. sudo 其他用户执行命令
  19. SpringBoot配置成Liunx服务
  20. jquery on方法(事件委托)

热门文章

  1. python之导入模块的方法
  2. chmod指令用法
  3. assign 与 深浅拷贝
  4. 终于,帮开发写了一个bug
  5. appium——如何导出夜神模拟器下载“微信”app的apk
  6. 系列13 docker asp.net core部署
  7. jQuery的替换/克隆
  8. Quartz3.0定时任务学习之异步调度器
  9. mysql小白系列_09 mysql性能优化关键点
  10. 30分钟快速上手Docker,看这篇就对了!