File Paths
Time limit1sMemory limit256 MB
Decide for each file whether one directory shortcut of fixed name length can create a path of exactly k characters to it.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Number theory, Math
- Solved
- No attempts yet
Problem
Byteasar likes to live risky. He runs with scissors, submits contest solutions without trying them on the example input, and wants every one of his files to have a path name exactly as long as the operating system allows. On Linux that length is 4095 characters.
When he works on someone else's computer, some files do not meet that criterion. In that case he creates one symbolic link and builds new path names with it. For each file in the file system, decide whether he can create a single symbolic link, whose name length he fixes in advance, so that the file can be referred to by a path name of length exactly .
If a file named file sits inside the chain of directories dir1, dir2, ..., dirj, its absolute path is /dir1/dir2/.../dirj/file. The root directory is referred to as /, and a file contained directly in the root has an absolute path of the form /file.
A symbolic link is a named shortcut to a directory and can be placed in any directory of the file system. In this problem symbolic links to files are not allowed. A symbolic link gives alternative paths to the same file. For example, with a link named hello in / that points to /, the paths /dir/file, /hello/dir/file and /hello/hello/dir/file all refer to the same file and differ only in length. With a link named hi in /dir that points to /, you get /dir/file, /dir/hi/dir/file and /dir/hi/dir/hi/dir/file. A link may point upwards, downwards or sidewards in the hierarchy, and even back at the directory that holds it. The components ./, ../ and // are not allowed in path names.
Input
The first line contains three positive integers , and : the number of directories other than the root, the number of files, and the desired path name length. The root directory has number 0, the remaining directories are numbered 1 through , and the files are numbered 1 through .
The second line contains the length of the symbolic link name. The name itself does not matter, and it collides with nothing else in the file system.
Then follow lines describing the directories other than the root. The -th of them contains two integers and : directory has a name of length , and the directory that directly contains it has number .
Finally lines describe the files. The -th of them contains two integers and : file has a name of length and sits in directory .
Output
Print lines, one per file. On line print YES if a single symbolic link of length makes it possible to refer to file by a path name of length exactly , and NO otherwise.
Constraints
- Every file name and directory name has positive length, and every absolute path is at most characters long.
Notes
The first example describes the file system below. Call the symbolic link LL, the directory names a and bbbbb, and the file names ccccccccccccc, dddddddddd, eeee and fffffff in that order. The root holds the directory a and the file fffffff, the directory a holds the directory bbbbb and the file eeee, and bbbbb holds the files ccccccccccccc and dddddddddd.
/
|-- a
| |-- bbbbb
| | |-- ccccccccccccc
| | +-- dddddddddd
| +-- eeee
+-- fffffff
For file 1 the absolute path /a/bbbbb/ccccccccccccc already has length 22, so no link is needed. For file 2, put the link LL in /a pointing to /a and use /a/LL/bbbbb/dddddddddd. For file 3, put the link LL in /a pointing to / and use /a/LL/a/LL/a/LL/a/eeee. For file 4 no placement of the link reaches length 22.