#P6843. [BalticOI 2015] File Paths

    ID: 5862 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>图论2015深度优先搜索,DFSBalticOI

[BalticOI 2015] File Paths

Description

A file file\tt file must be located in a directory that contains many directories dir1,dir2,,dirj\tt dir1,dir2,\cdots,dirj. The absolute file path of this file is /dir1/dir2//dirj/file\tt/dir1/dir2/\cdots/dirj/file. The root directory is denoted by /\tt /. For a file placed directly under the root directory, its absolute file path has the form /file\tt /file.

A symbolic link points to a named directory and can be seen as a shortcut. It can be placed in any directory. Note that a symbolic link cannot point to a file. For example, if we place under /\tt / a symbolic link hello\tt hello that points to /\tt /, then /dir/file\tt /dir/file, /hello/dir/file\tt /hello/dir/file, and /hello/hello/dir/dile\tt /hello/hello/dir/dile all refer to the same file file\tt file. As another example, if we place under /dir\tt /dir a symbolic link hi\tt hi that points to /\tt /, then /dir/file\tt /dir/file, /dir/hi/dir/file\tt /dir/hi/dir/file, and /dir/hi/dir/hi/dir/file\tt /dir/hi/dir/hi/dir/file all refer to the same file file\tt file. A symbolic link may point to the parent directory, a child directory, or even a directory on the same level, but operations such as ./\tt ./, ../\tt ../, and //\tt // are not allowed.

Now the question is: is it possible, by introducing one symbolic link of length ss, to make it possible to find an absolute file path of some file whose length is exactly kk?

Input Format

The first line contains three integers n,m,kn,m,k, representing the number of directories (excluding the root directory), the number of files, and the required path length kk.
The second line contains an integer ss, representing the length of the symbolic link.
The next nn lines each contain two integers pi,lip_i,l_i, describing a directory: this directory has ID lil_i, and its parent directory has ID pip_i.
The next mm lines each contain two integers pj,ljp_j,l_j, describing a file: this file has length ljl_j, and its parent directory has ID pjp_j.

Output Format

Output mm lines. Each line is a string indicating whether, by introducing one symbolic link of length ss, it is possible to find an absolute file path of length exactly kk for file jj. If yes, output YES\tt YES; otherwise, output NO\tt NO.

2 4 22
2
0 1
1 5
2 13
2 10
1 4
0 7
YES
YES
YES
NO

Hint

Sample 1 Explanation

Assume the symbolic link name is LL\tt LL, the directory names are a\tt a and bbbbb\tt bbbbb, and the file names are ccccccccccccc\tt ccccccccccccc, dddddddddd\tt dddddddddd, eee\tt eee, and fffffff\tt fffffff. The root directory contains directory a\tt a and file fffffff\tt fffffff. Directory a\tt a contains directory bbbbb\tt bbbbb and file eee\tt eee. Directory bbbbb\tt bbbbb contains files ccccccccccccc\tt ccccccccccccc and dddddddddd\tt dddddddddd. A visual representation is:

/
|-- a
| |-- bbbbb
| | |-- ccccccccccccc
| | +-- dddddddddd
| +-- eeee
+-- fffffff
  • For file 11, a path that satisfies the condition is /a/bbbbb/ccccccccccccc\tt /a/bbbbb/ccccccccccccc.
  • For file 22, a path that satisfies the condition is /a/LL/bbbbb/dddddddddd\tt /a/LL/bbbbb/dddddddddd.
  • For file 33, a path that satisfies the condition is /a/LL/a/LL/a/LL/a/eeee\tt /a/LL/a/LL/a/LL/a/eeee.
  • For file 44, no path satisfies the condition.

Constraints and Notes

This problem uses bundled tests.

  • Subtask 1 (33 pts): n,m500n,m \le 500.
  • Subtask 2 (33 pts): n,m3×103n,m \le 3 \times 10^3, and the symbolic link is used at most once.
  • Subtask 3 (34 pts): no special restrictions.

For 100%100\% of the testdata, 1k,s1061 \le k,s \le 10^6, and 1m,n3×1031 \le m,n \le 3\times 10^3.

Notes

Translated from BalticOI 2015 Day2 A File Paths

Translated by ChatGPT 5