VK Cup 2012 Round 3


A. Variable, or There and Back Again
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
4 3
1 0 0 2
1 2
2 3
3 4
Output
1
1
1
1
Input
3 1
1 0 2
1 3
Output
1
0
1
Input
3 1
2 0 1
1 3
Output
0
0
0
----------------------------------------------------------------------------------------------------
B. Ancient Berland Hieroglyphs
time limit per test: 1.5 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5 4
1 2 3 4 5
1 3 5 6
Output
2
Input
4 6
1 3 5 2
1 2 3 4 5 6
Output
3
Input
3 3
1 2 3
3 2 1
Output
2
----------------------------------------------------------------------------------------------------
C. Machine Programming
time limit per test: 5 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3 1
2 7 5
1 3 3
4 1 3
Output
0 1 1
Input
5 2
1 5 4
1 4 5
1 3 2
4 1 2
5 6 1
Output
1 1 0 0 1
----------------------------------------------------------------------------------------------------
D. Minimum Diameter
time limit per test: 1.5 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5 2
1 2
0 0
2 2
1 1
3 3
Output
5 2
Input
4 1
0 0
0 0
1 1
1 1
Output
3
----------------------------------------------------------------------------------------------------
E. Polycarpus and Tasks
time limit per test: 3 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
1 8 5
2 9 3
3 10 3
8 11 4
11 12 2
Output
0 0 1 0 -1 
Input
13
1 8 5
2 9 4
3 10 1
4 11 3
8 12 5
9 13 5
10 14 5
11 15 1
12 16 1
13 17 1
14 18 3
15 19 3
16 20 2
Output
0 0 0 2 -1 -1 0 0 0 0 7 0 12 
----------------------------------------------------------------------------------------------------
