You have just discovered that the server network you supervise is the victim of a cyberattack. A group of hackers managed to infiltrate and remain hidden for a long period of time. They started from one server and spread by moving through the links between servers across the entire network. Now you must discover the initial server from which the intruders connected and stop their connection.
The network consists of servers and links between these servers represented by cables. A link allows two servers to communicate with each other. Also, there is at most one link between two distinct servers and no link between a server and itself. You have the option to check a link and see whether it was used in the attack, and if it was used, you will also learn the direction of malware propagation. You do not want to allow those hackers to stay long in the network, so you want to find the computer from which the attack started as quickly as possible.
Task
You must implement a function
int Find (int n, vector <int> a, vector <int> b)
The parameters are: , which represents the number of servers; and , which are two vectors representing the computers between which there exists a link (there exists a link between and ). This function must return the index of the server from which the attack started.
Your function will communicate with the grader function:
int Ask (int x, int y)
This allows you to find out whether the link between and was used in the attack or not. If the link does not exist or was not used in the attack, the function will return the value . Otherwise, if was infected through , then the function will return the value . If server was infected through , then the function will return .
To work, your program must include "apt.h".
To make local testing easier, you are provided in the problem sources with an example grader and solution, and the file apt.h.
Constraints
Let be the number of servers and the number of links. Then,
- Servers are indexed starting from
- Any server is connected to at most other servers
- It is guaranteed that for any two servers in the network there exists at least one communication path through the existing links
- All servers were infected exactly once
- The network was generated randomly such that if we considered only the links used to infect the servers, then the maximum distance between two servers would be . The other links were added randomly.
Scoring
Your program will run on multiple tests. If you do not return the correct answer or your program encounters a compilation error, time limit exceeded, memory limit exceeded, or any other error, then you will receive points on that test. Otherwise, your score will be given based on the number of calls to the function "Ask".
The total score will be distributed over multiple subtasks, and the score obtained on a subtask will be the minimum score obtained on any of the tests in that subtask. The final score will be the sum of the scores over all subtasks.
For each test, your number of calls, denoted , will be compared with the number of calls to the function "Ask" in the committee solution, denoted . Also, is defined as . On a test you will receive points based on the following formula, where represents the score of the subtask to which the test belongs:
The terms were taken from a deterministic solution to which a certain indulgence term was added.
| # | Points | Restrictions |
|---|---|---|
| 1 | 15 | |
| 2 | 25 | |
| 3 | 25 | |
| 4 | 35 | No additional constraints |
Example
The example grader will read the number of servers, the number of links, and then, on each line, the links in the network. On each line describing a link there will be numbers, the first two being the servers between which there is a link, and the next number will be if the first computer was infected through the second, if the link was not used in the network, and if the second server was infected through the first. At the end, it will print the answer returned by your program and the number of calls to the function “Ask”.
stdin
6 8
3 1 1
2 1 -1
1 6 1
3 5 1
4 5 -1
6 4 0
2 4 0
3 4 0
stdout
3 4
Explanation
The network in the example consists of servers and links. The hackers started their attack from server . From server they spread to servers and . From server they spread to servers and , and from server they spread to server . All servers were infected exactly once.
One way to obtain the correct answer in questions is to call the function “Ask” as follows: , , , . The answers will be, in this order: , , , . After these questions we can be sure that the server from which the attack started is server number , so we return .