codeforces上一种应对大测试数据debug方法

        接触codeforces时间不长,也就参加了10场左右。之前有次比赛就出现了wa了一道题,在比赛结束后看测试数据log时,发现出错的那组用例是个超过一万行输入的情况,codeforces上的测试数据无法完全显示,也就没办法复制后台出问题的测试数据,导致想在比赛后纠错却无从下手。昨天(2017.2.2)的比赛时同样遇到了这个情况。题目如下:

C. Timofey and a tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so that the i-th vertex gets color ci.

Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.

Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex.

A subtree of some vertex is a subgraph containing that vertex and all its descendants.

Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed.

Input

The first line contains single integer n (2 ≤ n ≤ 105) — the number of vertices in the tree.

Each of the next n - 1 lines contains two integers u and v (1 ≤ u, v ≤ nu ≠ v), denoting there is an edge between vertices u and v. It is guaranteed that the given graph is a tree.

The next line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 105), denoting the colors of the vertices.

Output

Print "NO" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him.

Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.

Examples
input
4
1 2
2 3
3 4
1 2 1 1
output
YES
2
input
3
1 2
2 3
1 2 3
output
YES
2
input
4
1 2
2 3
3 4
1 2 1 2
output
NO
        这是一个批着“树”外衣的水题,解题时没用到树的存储方法,只是挑一下所有树枝中,两侧顶点颜色不同的,并观察这些枝干是否有都包含某个相同的顶点。晚上写代码就容易逻辑混乱,在判断的时候我就用到了很多的变量和数组,并且看到数据规格采用了n*logn的算法,后来证实这种方法麻烦,费时间并且还容易出现错误。

        提交时在第10组数据时候wa了,日志如下:

Test: # 10, time:  436 ms., memory:  3208 KB, exit code:  0, checker exit code:  1, verdict:  WRONG_ANSWER
Checker Log
wrong answer participant hasn't found the solution

        开始的时候第一反应是int改成long long,后来发现根本不是这个问题,题中的数据用int完全可以。再思考思考还是没有头绪,凌晨2点左右也困了,就先睡去了。今天思考这个问题时突然想起寝室大牛说的一次acm省赛经历,他在比赛时收到赛方的道歉,承认测试数据的问题,这份道歉是re的一位参赛选手的质疑。这名选手比赛时感觉测试数据超出给定范围,并利用死循环证明了测试数据中确实有问题。(对非法测试数据进行死循环来显示Time Limit...)于是突然想到codeforces的测试数据其实可以不用得到,通过一些手段我可以在网站上直接调试的!观察了一下前9组测试用例,没有一个第二个输入与第10组相同的(87340),果断加了一个判断,若第二个输入为87340,则输出一些我需要的信息,之后的进展很顺利,我利用网站上的输出信息很快就找出了问题所在,最终ac。

         这个发现对我来说还是很有价值的,可能之前也有人用过吧,不过自己一步一步探索出来是对我以后看待问题角度的一个提升。

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值