Problem A Idol Master! (Programming)
Problem Description
The story. As we all know, students in the NU CSIE department often idolize peers whom they look up to. Due to this phenomenon, WillyPillow decides to design a game called Idol Master!. In the game, each character either has the character it admires the most as its idol or has no idols. In the source code, each character is represented by the following structure:
struct Character {
// The characters idol, null if empty. std::shared_ptr<Character> idol;
// Other data related to the character. CharacterData data;
};
Introduced in C++11, std::shared ptr is a smart pointer supporting reference counting. Specifically, an instance of the pointer tracks the number of its existing copies, and destructs the referenced object if the number falls to zero.
WillyPillow has a list of characters and their corresponding idols together with a list of characters that appears in a certain scene. (Due to the nature of the scene, it is guaranteed that no character in it is the idol of another character.) However, the memory usage of his game is too high, so he needs to remove some characters from the scene. As he wants to compare how much memory he can save by removing different sets of characters, he needs to know the number of character objects that can be destructed, i.e., its corresponding character is destroyed or all character objects that link to it are destructed, if we remove a given set of characters.
Problem formulation. Formally speaking, we say a vertex with a zero in-degree is not referenced and shall be removed. A vertex is destructed if it is removed either manually or due to being nonreferenced.
You are given a directed graph G in which the out-degree of each vertex is at most 1 [1], and multiple queries on the same graph G. Each query results in a new graph G0 that is constructed by removing a set of vertices X and their outgoing edges on G. You can assume that all vertices in X have no incoming edges. Please implement a program to compute the number of vertices that are destructed on G0 for each query.
Note that destructing one vertex may cause another vertex to become non-referenced and be destructed as well. In addition, if a vertex not in X already has a zero in-degree in G, then it is not destructed (one can imagine that they are connected to by a special hidden vertex).
Input Format
The first line contains the number of vertices, N.
The second line consists of N integers. The i-th integer, pi (0 pi N) indicates that there is an edge i pi. Notably, pi = 0 if the out-degree of i is zero.
The third line consists of the number of queries Q.
Q lines follow, the i-th of which starts with an integer xi (xi 1), denoting the number of vertices to be removed, and is followed by xi integers, denoting the set of vertices to be removed.
Test Group 0: Test Group 2 :
- Sample Input N 218
- Pxi 2N
Test Group 1 (15%):
- N 213
- Pxi 2N
Output Format
For each query, output a line indicating the number of vertices that can be deleted according to the rules above.
Sample Input 1 Sample Output 1
4 1
4 2 4 1
1
1 3
Sample Input 2 Sample Output 2
8 3
1 6 6 6 1 2 3 4 4
12 1
2 5 7 2
2 7 8 2
1 5 3
1 8 1
- 8 4
- 5 7 2
- 5 1
- 7 8 2
1 7 1
1 5
1 8
1 5
Problem B Traveling Pony Problem (Programming) (15 points)
Problem Description
A little pony is traveling in a city. He starts from his home s and goes to the destination t. However, each road in the city has two values di and li, the length of the road and the level of danger. To ensure his safety, the pony needs to be well-prepared before the trip. He has a non-negative value L indicating the amount of protection. To pass a road safely, he needs to ensure that L li. Since the protection is expensive, he wants to minimize the amount of protection. Your goal is to calculate the shortest path under the condition that a minimum amount of protection is needed.
Input
The map can be considered as an undirected graph. Each road is an edge on the graph. And the pony travels from a vertex s, which is his home, to a vertex t, which is the destination.
The first line of the input consists of 2 integers n, m, which indicate the number of vertices and the number of edges. The second line consists of 2 integers s and t, indicating ponys home and the destination. On the follow m lines, there are 4 integers xi, yi, di, and li, which means an edge connecting vertices xi and yi; the distance between xi and yi is di, and the level of danger is li.
- 2 n 200000
- 1 m 500000
- 0 s,t < n
- 0 xi,yi < n, xi 6= yi for 0 i < m
- There is no two edges connecting a same pair of vertices.
- 0 di,li 109
- The graph is connected.
Output
You need to output two non-negative integers D and L. D is the length of shortest path if the pony only goes through roads whose danger level L; L is the minimum amount of protection needed that the pony can arrive the destination safely.
Subtask 1 (30 %)
- l0 = l1 = = lm1
Subtask 2 (30 %)
- li 6= lj for 0 i,j < m and i 6= j
Subtask 3 (40 %)
- No other constraints.
Sample Input 1
5 5
0 4
- 1 1 3
- 2 2 1
- 3 1 2
- 4 1 3
0 4 1 4
Sample Output 1
5 3
Sample Input 2
5 5
0 4
0 1 1 3
- 2 2 2
- 3 1 4
- 3 2 2
- 4 2 5
Sample Output 2
- 5
Problem C Magic Song (Programming) (20 points)
Problem Description
You have a collection of songs. Each song contains some jump points, and when a song plays to a jump point, you can optionally jump to another song. Your goal is to determine how to jump between songs so as to maximize the length of played music.
Input
The first line of the input file contains an integer indicating T, the number test cases.
For each test case, the first line contains two integers, N and M, indicating the number of songs and the number of total jump points, respectively. The second line contains N integers separated by spaces, a1,a2, ai, indicating the length of i-th song (in terms of the number of notes). The following M lines, each of which contains four integers separated by spaces, s1,t1,s2,t2, indicating that the ending of the t1-th note of the s1-th song could jump to the beginning of t2-th note of the s2-th song.
- 1 T 10
- 1 N 105
- 0 M 5 105
- 1 ai 108
- 1 s1,s2 N
- 1 t1 as1
- 1 t2 as2
Test Group 1 (30 %) Test Group 2 (30 %)
- 1 N,M 104
Test Group 3 (40 %)
- 1 N,M 103
- 1 ai 103 No other constraints.
Output
Please output an integer indicating the maximum number of notes that you can play. If the music can be played forever, output LoveLive!.
Sample Input 1
1
3 3
10 10 10
- 5 2 3
- 7 1 6
2 3 3 5
Sample Output 1
15
Sample Input 2
1
1 1
10
1 6 1 3
Sample Output 2
LoveLive!
Hint
Some example of suite: https://www.youtube.com/watch?v=95tLMjeK0kw https://www.bilibili.com/video/av5986635
Problem D Basic Problems in Graphs (Hand-Written) (20 points)
- Give one example of a directed graph with four vertices, A, B, C, and D, such that both depthfirst search and breadth-first search discover the vertices in the same order when starting at A. Give one example of a directed graph where DFS and BFS discover the vertices in different orders when starting at A. (Discover means the time that the algorithm first reaches the vertex, when the vertex color becomes gray in CLRS textbook.) You can assume that both DFS and BFS iterate over outgoing neighbors in an alphabetical order. (4 points, 2 points for each example)
- Prove or disprove that the minimum spanning tree is the same as the shortest path tree. Please give one example to explain your proof. (You need to show 3 graphs, 1 for the original graph, 1 for the shortest path tree on that graph, and 1 for the minimum spanning tree on that graph. Note that the number of vertices in your graph should be no greater than 5.) (4 points)
- We are given a network of V cities and E All roads are one-way, i.e., if a road goes from the city u to the city v, then there will be no roads from v to u. In addition, the roads would not form any cycle. That is, you cannot start from a city v, go through several cities, and go back to v. Finally, we assume that there is a capital that can go to all other cities in the network.
We want to compute the minimum number of days for a tourist to go from the capital to each non-capital city. When a tourist encounters a city v, he will spend c(v) > 0 days to visit all attractions in v. When a tourist goes from the city u to the city v, he will spend r(u,v) > 0 days on the road. Now given the function c for all cities, and the function r for all roads, please compute the minimum number of days for a tourist to go from the capital to each non-capital city. We use M(v) to denote this minimum number of days from the capital to a city v. We illustrate the problem with an example of four cities A, B, C, D and four roads. Let A be the capital and c(A) = c(B) = c(C) = c(D) = 1, and r(A,B) = r(A,C) = r(B,D) = 2, and r(C,D) = 10. Then M(D) = c(A) + r(A,B) + c(B) + r(B,D) + c(D) = 7. Similarly M(B) = M(C) = 4.
- Please derive an optimal algorithm to compute the function M for all cities other than the capital. (6 points)
- Please analyze the time complexity of your algorithm and prove that it is correct. (You need to show why your algorithm gives the minimum number of days from the capital to a city v.) (6 points)
Problem E Mailing Fees (Hand-Written) (22 points)
Your computer bought on the Double Eleventh Day has broken, so you have to send it to a factory to repair. There are N factories and E roads, and you need to send your device to one of them. When you send it to any factory, you have to pay the mailing fees. As shown in the following graph, the starting position is the node 0; each vertex represents one factory, and an edge between two factories shows the mailing fee. You can ask a factory to transfer your computer to another one, and you still have to pay the mailing fees. Note that you need to pay the mailing fees for the factory sending back your device too. Your goal is to find out the minimum amount you have to spend on the mailing fee for a round trip to and from each factory.
In the example below: there are 8 factories (nodes 1 to 9) and you are at the location 0. The weights of edges indicate the mailing fees you have to pay for sending your computer.
The answer for the above example is
factory | Mailing fees from the source |
1 | 8 |
2 | 24 |
3 | 38 |
4 | 42 |
5 | 22 |
6 | 18 |
7 | 16 |
8 | 28 |
- (4pts) Please write down a pseudo code to solve the problem.
- (5pts) Due to some unexpected reasons, the paths to the factory change from bi-directional to uni-directional (as shown in the figure below). Please use the function you created above and explain how you can find the lowest mailing fee for a round trip to and from each factory. Your algorithm should run in O(ElogE) time complexity. Briefly explain the correctness and why your algorithm meets the time complexity requirement.
Note: You do not have to write a pseudo code for this question.
- (4pts) Now there are some offers provided by factories; some mailing fees change to negative, meaning that you can earn some money when using that path for sending your device. Please write a pseudo code to solve this problem. If there exists a cycle that you can actually earn money, please output I am rich!
Y our algorithm should run in O(N E) time complexity (E is the number of edges). Briefly explain the correctness and why your algorithm meets the time complexity requirement.
- (3pts) Briefly compare these two algorithms you use in subproblem 2 and 3 (time and space complexity) and discuss their advantages and disadvantages.
- (6pts) Now each factory has a reliability value, we define a trustful cycle if the ratio of total reliability to total mailing fees is strictly larger than K (K N). You can assume that there are no negative mailing fees for this subproblem. For example, in the example below, K = 10, the simple cycle route 30 40 20 30 has the total reliability 30 + 40 + 20 = 90, and total mailing fees 2 + 3 + 1 = 6. The ratio between them is 15, which is larger than K.
Please provide an algorithm to find if there exists a trustful cycle. Your algorithm should run in O(N E) time complexity. Briefly explain the correctness and why your algorithm meets the time complexity requirement.
Hint: you can try reweighting the graph.
Problem F Gaussian is Too Slow (Hand-Written) (8 points)
There are N unknown variables, namely x1xn. Your task is to solve all variables by picking N equations from the equation pool below and make sure your solution of x is non-singular. The following describes the equations characteristics inside the equation pool:
- all coefficients for the unknown variables are 1. (ex: x1 +x2 is in the equation pool but x1 +2x2 is not.)
- all variables inside an equation are consecutive. (ex: x2 + x3 + x4 and x3 are in the equation pool but x2 + x4 and x6 + x1 are not).
- each equation has their own cost. (ex: equation x1 + x2 cost is 6, x2 + x3 is 3 )
- you can assume all functions you choose will have a consistent solution for x.
Your task is to solve all variables x by picking N equations so that the total cost can be minimized. As an example N = 3 and the equation pool:
equation | cost |
x1 | 1 |
x1 + x2 | 5 |
x1 + x2 + x3 | 2 |
x2 | 6 |
x2 + x3 | 3 |
x3 | 4 |
The solution for this example is to pick the equations x1, x1 + x2 + x3 and x3. Thus, the total cost will be 1 + 2 + 4 = 7.
Please reduce this problem to any algorithm you learned before (for example: Dijkstra, BellmanFord, Warshall, Kruskal, DP,etc). Your algorithm should run in O(N2) time complexity. Briefly explain the correctness and why your algorithm meets the time complexity requirement.
Hint: you might need to add an extra node for this question.
Note: You do not need to write a pseudo code for this question. You just have to describe how you reduce this problem to any algorithm.
[1] This is known as a directed pseudoforest.
Reviews
There are no reviews yet.