• 沒有找到結果。

InstructionsandAnnouncements ada-ta@csie.ntu.edu.tw DueTime:2019/12/12(Thur.)14:20ContactTAs: Homework#3

N/A
N/A
Protected

Academic year: 2022

Share "InstructionsandAnnouncements ada-ta@csie.ntu.edu.tw DueTime:2019/12/12(Thur.)14:20ContactTAs: Homework#3"

Copied!
11
0
0

加載中.... (立即查看全文)

全文

(1)

Homework #3

Due Time: 2019/12/12 (Thur.) 14:20 Contact TAs: ada-ta@csie.ntu.edu.tw

Instructions and Announcements

• There are three programming problems and three hand-written problems.

• Programming. The judge system is located at https://ada-judge.csie.ntu.edu.tw. Please login and submit your code for the programming problems (i.e., those containing “Programming”

in the problem title) by the deadline. NO LATE SUBMISSION IS ALLOWED.

• Hand-written. For other problems (also known as the “hand-written problems”), you MUST turn in a printed/written version of your answers to the submission box at your class- room. You can also upload your homework to the NTU COOL system as a backup; however, it will be marked only when you have turned in the printed/written answer but it is lost during the grading process.

NO LATE SUBMISSION IS ALLOWED.

• Collaboration policy. Discussions with others are strongly encouraged. However, you should write down your solutions in your own words. In addition, for each and every problem you have to specify the references (e.g., the Internet URL you consulted with or the people you discussed with) on the first page or comment in code of your solution to that problem. You may get zero point due to the lack of references.

(2)

Problem A - Idol Master! (Programming) (15 points)

Problem Description

The story. As we all know, students in the NΠU 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 character’s 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. Specif- ically, 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 non- referenced.

You are given a directed graph G in which the out-degree of each vertex is at most 11, 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.

1This is known as a directed pseudoforest.

(3)

Test Group 0 (0%):

• Sample Input Test Group 1 (15%):

• N ≤ 213

• P xi ≤ 2N

Test Group 2 (85%):

• N ≤ 218

• P xi ≤ 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 4

4 2 4 1 1

1 3

Sample Output 1 1

Sample Input 2 8

1 6 6 6 1 2 3 4 12

2 5 7 2 7 8 1 5 1 8 1 8 2 5 7 1 5 2 7 8 1 7 1 5 1 8

Sample Output 2 3

4 1 2 2 3 1 4 2 1 2 1

(4)

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 diand 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. To pass a road safely, he needs to have the amount of protection of at least 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 pony’s home s and the destination t are two vertices on the graph.

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 pony’s 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, xi6= 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= ... = lm−1

Subtask 2 (30 %)

• li6= lj for 0 ≤ i, j < m and i 6= j

Subtask 3 (40 %)

• No other constraints.

(5)

Sample Input 1 5 5

0 4 0 1 1 3 1 2 2 1 2 3 1 2 3 4 1 3 0 4 1 4

Sample Output 1 5 3

Sample Input 2 5 5

0 4 0 1 1 3 0 2 2 2 1 3 1 4 2 3 2 2 3 4 2 5

Sample Output 2 4 5

(6)

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 %)

• 1 ≤ N, M ≤ 103

• 1 ≤ ai≤ 103

Test Group 2 (30 %)

• 1 ≤ N, M ≤ 104

Test Group 3 (40 %)

• 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 1 5 2 3

(7)

2 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

(8)

Problem D - Basic Problems in Graphs (Hand-Written) (20 points)

1. Give one example of a directed graph with four vertices, A, B, C, and D, such that both depth- first 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)

2. 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)

3. We are given a network of V cities and E roads. 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.

(a) Please derive an optimal algorithm to compute the function M for all cities other than the capital. (6 points)

(b) 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)

(9)

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

(1) (4pts) Please write down a pseudo code to solve the problem.

(10)

Note: You do not have to write a pseudo code for this question.

(3) (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.

(4) (3pts) Briefly compare these two algorithms you use in subproblem 2 and 3 (time and space complexity) and discuss their advantages and disadvantages.

(5) (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.

30 40

20 10

2

4

3 1

5

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.

(11)

Problem F - Gaussian is Too Slow (Hand-Written) (8 points)

There are N unknown variables, namely x1...xn. 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, Bellman- Ford, 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.

參考文獻

相關文件

Hanhan wants to know the minimum possible cost to fix the string into balanced parentheses6. Please help Hanhan find the minimum cost or tell him it’s impossible

Based on the knowledge from “Machine Learning Foundations” and “Machine Learning Tech- niques”, Alpha has built a DEEP learning model to predict the price trend of the ADA coin

The first line contains two integers N, M indicating the number of cities in the HH kingdom and the number of roads between them... Problem 5 - Compiler (Handwriting)

Given this additional information, please provide an algorithm that utilizes those relics and finds the shortest route for Piepie to reach QMao’s home alive.. However, troops

Given N magic wands and M fitness values, find a linking method to enhance the maximum power in total. (4) (4pts) The professor who invented Magic Wands Linking realizes that the

Also, because the robbers work as a group, the overall time the group spends on robbing all the people will be the maximum time spent by individual members.. For example, if there are

[This function is named after the electrical engineer Oliver Heaviside (1850–1925) and can be used to describe an electric current that is switched on at time t = 0.] Its graph

A gerbe for the elliptic gamma function GIOVANNI FELDER, ANDRE HENRIQUES,.