Tech Interview Prep C++ Challenges
A. Village Bridge
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
I am a geezer standing in the dusk, mesmerized by the gulls.

In the great Unovan sea, there are $$$n$$$ ($$$2 \leq n \leq 2600$$$) villages labeled $$$1$$$ through $$$n$$$, each of which is located on an island. At the moment, there is no way to travel between any pair of villages. As a result, you have been contracted to build $$$m$$$ ($$$0 \leq m \leq \min(n(n - 1), 2600)$$$) bidirectional magic bridges, each of which connects exactly two distinct villages.

Magic bridges come in two types — day bridges and night bridges. As their name implies, day bridges can only be crossed during the daytime, and night bridges can only be crossed at night. Crossing any bridge takes the entire duration of its active period (for example, crossing a night bridge takes the entire night). After your construction, two villages are said to be connected if it is possible to continuously travel from one village to the other without stopping for a day or night to pass.

Please find $$$m$$$ bridges to build such that afterwards, every single pair of villages is connected, or report that it is impossible.

Input

The first and only line consists of two space-separated integers $$$n$$$ ($$$2 \leq n \leq 2600$$$) and $$$m$$$ ($$$0 \leq m \leq \min(n(n - 1), 2600)$$$).

Output

If it is impossible to build $$$m$$$ bridges which satisfy the condition, output a single integer $$$-1$$$.

Otherwise, print $$$m$$$ lines of output. The $$$i$$$th line should contain $$$3$$$ space-separated integers $$$u, v, w$$$ ($$$1 \leq u, v \leq n, u \neq v, 0 \leq w \leq 1$$$), indicating that there is a bridge connecting villages $$$u$$$ and $$$v$$$. If $$$w = 0$$$, the bridge is a day bridge, otherwise it is a night bridge. For any pair of villages, there should be at most one bridge of each type connecting them.

If multiple constructions are possible, then any one will be accepted.

Examples
Input
3 3
Output
1 2 1
2 3 1
1 3 1
Input
4 5
Output
1 2 0
1 3 0
2 4 1
3 4 0
3 4 1
Input
4 0
Output
-1
Note

In the first test case, all pairs of villages can be directly connected using night bridges.

In the second test case, the bridges form a square and satisfy the condition. For example, villages $$$2$$$ and $$$3$$$ are connected due to the following path: we can begin at village $$$2$$$ at night. We can cross the night bridge from village $$$2$$$ to $$$4$$$, and it is daytime now. We can then cross the day bridge from village $$$4$$$ to $$$3$$$.

In the third test case, it is impossible to make any pair of villages connected, since there will be no bridges.

B. Village Bridge 2
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Gazing fixedly into the blue sea, I bemoan with shed tears, as I yearn for the lovely gulls soaring above the blue harbor.

In the great Unovan sea, there are $$$n$$$ ($$$2 \leq n \leq 2600$$$) villages labeled $$$1$$$ through $$$n$$$, each of which is located on an island. There are currently $$$m$$$ ($$$0 \leq m \leq \min(n(n - 1), 2600)$$$) bidirectional magic bridges, each of which connects two distinct villages.

Magic bridges come in two types — day bridges and night bridges. As their name implies, day bridges can only be crossed during the daytime, and night bridges can only be crossed at night. Crossing any bridge takes the entire duration of its active period (for example, crossing a night bridge takes the entire night). Two villages are said to be connected if it is possible to continuously travel from one village to the other without stopping for a day or night to pass.

Please determine whether every single pair of villages is connected or not.

Input

The first line consists of two space-separated integers $$$n$$$ ($$$2 \leq n \leq 2600$$$) and $$$m$$$ ($$$0 \leq m \leq \min(n(n - 1), 2600)$$$).

The following $$$m$$$ lines contain the descriptions of each bridge. The $$$i$$$th line contains $$$3$$$ space-separated integers $$$u, v, w$$$ ($$$1 \leq u, v \leq n, u \neq v, 0 \leq w \leq 1$$$), indicating that there is a bridge connecting villages $$$u$$$ and $$$v$$$. If $$$w = 0$$$, the bridge is a day bridge, otherwise it is a night bridge.

For any pair of villages, it is guaranteed that there is at most one bridge of each type connecting them.

Output

On a single line, output the string YES if all pairs of villages are connected, and NO otherwise.

Examples
Input
3 3
1 2 1
2 3 1
1 3 1
Output
YES
Input
4 5
1 2 0
1 3 0
2 4 1
3 4 0
3 4 1
Output
YES
Input
4 0
Output
NO
Note

In the first test case, all pairs of villages are directly connected using night bridges.

In the second test case, the bridges form a square and satisfy the condition. For example, villages $$$2$$$ and $$$3$$$ are connected due to the following path: we can begin at village $$$2$$$ at night. We can cross the night bridge from village $$$2$$$ to $$$4$$$, and it is daytime now. We can then cross the day bridge from village $$$4$$$ to $$$3$$$.

In the third test case, no pair of villages is connected, since there are no bridges.

C. End Time
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

This is an interactive problem. You have to use a flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout), in Java — System.out.flush(), in Pascal — flush(output) and in Python — sys.stdout.flush().

Definitions

A permutation of length $$$n$$$ is a sequence of $$$n$$$ integers between $$$1$$$ and $$$n$$$ inclusive, such that each number appears exactly once.

When a string $$$s$$$ gets shifted, all characters except the last move to the right by one position. The last character moves to the beginning. For example, the string "End Time" can be shifted to read "eEnd Tim".

Let $$$\Sigma = \{a, b, ..., z, A, B, ..., Z\}$$$, in other words the set of lowercase and uppercase characters from the English alphabet. When a character $$$c$$$ in $$$\Sigma - \{z, Z\}$$$ gets shifted, it becomes the alphabetically next character of the same case. In particular, $$$z$$$ gets shifted to $$$A$$$, and $$$Z$$$ gets shifted to $$$a$$$.

Problem Statement

It is a sunny, but humid day as your time comes to an end. You are given a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. Your last words are a sequence of $$$26n$$$ messages $$$m_1, m_2, ..., m_{26n}$$$, each of which is a string of length $$$26n$$$ consisting only of characters from $$$\Sigma$$$.

However, your last words have been misinterpreted, and the following transformation has occurred! A permutation $$$p$$$ of length $$$26n$$$ is picked. The $$$i$$$th message becomes the $$$p_i$$$th message. Afterwards, for each message $$$m_i$$$, two integers $$$a_i$$$ and $$$b_i$$$ are chosen from the range $$$[0, 52n - 1]$$$, inclusive. $$$m_i$$$ first gets shifted $$$a_i$$$ times. Then, for all $$$j$$$, the $$$j$$$th character of $$$m_i$$$ gets shifted $$$b_i(j - 1)$$$ times.

You see what your last words have been misinterpreted as. With your consciousness slowly fading, you are determined to figure out the permutation $$$p$$$.

Interaction

Your program is given a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$).

After reading in the integer, your program should output $$$26n$$$ lines, the $$$i$$$th line consisting of the message $$$m_i$$$. It must be true that each $$$m_i$$$ contains exactly $$$26n$$$ characters, and each character is a lowercase or uppercase letter.

The judge program will then respond with the $$$26n$$$ transformed strings, each of which is on a separate line.

Finally, your program should output $$$26n$$$ space-separated integers on a single line. The $$$i$$$th integer is what your program thinks $$$p_i$$$ is.

In order to pass a test case, the guessed permutation must be exactly correct, even if multiple permutations are possible.

Example
Input
1

acegikmoqsuwyACEGIKMOQSUWY
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxENDxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxTIMExxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
Output
aaaaaaaaaaaaaaaaaaaaaaaaaa
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxENDxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxTIMExxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Note

The choices for $$$a_1$$$ and $$$b_1$$$ for the first message are $$$20$$$ and $$$2$$$, respectively. Miraculously, the chosen permutation was the identity, and for all other lines, $$$a_i$$$ and $$$b_i$$$ were chosen to both be $$$0$$$.