传递闭包的计算过程一般可以用Warshell算法描述:
For 每个节点i Do
For 每个节点j Do
If j能到i Then
For 每个节点k Do
a[j, k] := a[j, k] Or ( a[j, i] And a[ i, k] )
其中a数组为布尔数组,用来描述两个节点是否相连,可以看做一个无权图的邻接矩阵。可以看到,算法过程跟Floyd很相似,三重循环,枚举每个中间节点。只不过传递闭包只需要求出两个节点是否相连,而不用求其间的最短路径长。
Description There are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The following comparison has been performed on some pairs of beads: A scale is given to compare the weights of beads. We can determine which one is heavier than the other between two beads. As the result, we now know that some beads are heavier than others. We are going to remove some beads which cannot have the medium weight. For example, the following results show which bead is heavier after M comparisons where M=4 and N=5. 1. Bead 2 is heavier than Bead 1. 2. Bead 4 is heavier than Bead 3. 3. Bead 5 is heavier than Bead 1. 4. Bead 4 is heavier than Bead 2. From the above results, though we cannot determine exactly which is the median bead, we know that Bead 1 and Bead 4 can never have the median weight: Beads 2, 4, 5 are heavier than Bead 1, and Beads 1, 2, 3 are lighter than Bead 4. Therefore, we can remove these two beads. Write a program to count the number of beads which cannot have the median weight.Input The first line of the input file contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. The input for each test case will be as follows: The first line of input data contains an integer N (1 <= N <= 99) denoting the number of beads, and M denoting the number of pairs of beads compared. In each of the next M lines, two numbers are given where the first bead is heavier than the second bead.Output There should be one line per test case. Print the number of beads which can never have the medium weight.Sample Input 1 5 4 2 1 4 3 5 1 4 2Sample Output 2
DescriptionN (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors. The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B), then cow A will always beat cow B. Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.Input * Line 1: Two space-separated integers: N and M * Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and BOutput * Line 1: A single integer representing the number of cows whose ranks can be determinedSample Input 5 5 4 3 4 2 3 2 1 2 2 5Sample Output 2
自反闭包 传递闭包Let’s take the following scenario: You have a ParentView with two child views, ChildView1 and ChildView2. On ChildView1, you have a button that should trigger an action in ChildView2...
自反闭包 传递闭包
Let’s take the following scenario: You have a ParentView with two child views, ChildView1 and ChildView2. On ChildView1, you have a button that should trigger an action in ChildView2.
Now on that button tap, we want to change the text on that text field to something more appropriate. Let’s start by defining a typealias for our closure. If you don’t know what a closure is, it’s basically a method. You can read more about closures in the documentation.
The idea here is that this onClick defined in the ParentView is our single source of truth. We don’t want another closure initialized somewhere along the call stack. We want this one passed to both our ChildViews.
In ChildView2, where our button is, we add it as an @Binding, as it’s already initialized in our ParentView and ChildView2 at this point only manipulates it. Then we add it as the action to our button:
You’ll notice that we deleted the old closure in which we would print our message and just passed ours as a parameter. This is not mandatory, but it’s shorter and cleaner.
At this point, your ParentView is notifying you that you’re missing your onClick parameter when you’re initializing ChildView2, so let’s just add that:
We’re now going to do the same thing to our ChildView1 — add a binding property — but this time we’re also going to write the function that gets called when the button is tapped, and we’re going to assign that function to our passed closure:
The magic here is calling onAppearon the Text element. This means that when that field appears (think of viewDidAppear in UIKit), we are going to run the following code block. In that code block, we assign a function to our onClick closure that modifies the value of our string.
You can see that I’ve sent it to a background thread. This is because the compiler will notify us at runtime that “Modifying state during view update, this will cause undefined behavior.”
Description There are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The following comparison has been performed on some pairs of beads: A scale is given to compare the weights of beads. We can determine which one is heavier than the other between two beads. As the result, we now know that some beads are heavier than others. We are going to remove some beads which cannot have the medium weight. For example, the following results show which bead is heavier after M comparisons where M=4 and N=5. 1. Bead 2 is heavier than Bead 1. 2. Bead 4 is heavier than Bead 3. 3. Bead 5 is heavier than Bead 1. 4. Bead 4 is heavier than Bead 2. From the above results, though we cannot determine exactly which is the median bead, we know that Bead 1 and Bead 4 can never have the median weight: Beads 2, 4, 5 are heavier than Bead 1, and Beads 1, 2, 3 are lighter than Bead 4. Therefore, we can remove these two beads. Write a program to count the number of beads which cannot have the median weight.Input The first line of the input file contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. The input for each test case will be as follows: The first line of input data contains an integer N (1 <= N <= 99) denoting the number of beads, and M denoting the number of pairs of beads compared. In each of the next M lines, two numbers are given where the first bead is heavier than the second bead.Output There should be one line per test case. Print the number of beads which can never have the medium weight.Sample Input 1 5 4 2 1 4 3 5 1 4 2Sample Output 2
DescriptionN (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors. The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B), then cow A will always beat cow B. Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.Input * Line 1: Two space-separated integers: N and M * Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and BOutput * Line 1: A single integer representing the number of cows whose ranks can be determinedSample Input 5 5 4 3 4 2 3 2 1 2 2 5Sample Output 2