Describe
C Country consists of n small islands, in order to facilitate the contact between the island, C in the island between the establishment of the M-bridge, each bridge connecting two small islands. There may be multiple bridge connections between the two islets. However, due to the erosion of seawater, some bridges are at risk of being unable to be used. If all the bridges between the two islets are not available, then the two islands cannot be reached directly. However, as long as the residents of the two islands can reach each other through other bridges or other islets, they will be safe. However, if there is a way between the two small islands the previous day and the latter cannot be reached, the residents will protest together.
Now the king of C has known the number of days each bridge can use, beyond which the number of days cannot be used. Now he wants to know how many protests the residents are going to launch.
-
-
Input
-
-
multiple sets of test data.
Enter two positive integers n and m first for each set of data.
Next m line, each line three integers a, B, t, respectively, that the bridge connects A and B two small islands, can use T-day. The number of islets is incremented from 1 onwards. (1≤n≤10000,1≤m≤100000,1<=a,b<=n,1≤t≤100000)
-
-
Output
-
-
output An integer that indicates the number of protests the residents have initiated.
-
-
Sample input
-
-
4 41 2 21 3 22 3 13 4 3
-
-
Sample output
-
-
2
-
-
Tips
-
-
For example:
The first day after the bridge between 2 and 3 can not be used, does not affect.
After the second day between 1 and 2, and the bridge between 1 and 3 is not available, residents will protest.
Third days after the bridge between 3 and 4 is not available, residents will protest.
-
Packagecom. Major;ImportJava.util.*; Public classmain{StaticHashset<integer> f=NewHashset<integer>(); StaticList<integer> x=NewArraylist<integer>(); Static intN=0; Public Static voidMain (String vgs[])throwsexception{Scanner SN=NewScanner (system.in); String Line=Sn.nextline (); N=integer.valueof (Line.split ("") [0]); intM=integer.valueof (Line.split ("") [1]); List<Brg> brg=NewArraylist<brg>(); for(inti=0;i<m;i++) {string[] str=sn.nextline (). Split (""); intA=integer.valueof (str[0]); intB=integer.valueof (str[1]); intDate=integer.valueof (str[2]); BRG BG=NewBrg (a,b,date); Brg.add (BG); } x.add (1)//starting from the first island to take while(X.size () <n) {//Judging whether all the islets have been takenPrim (brg,x); } System.out.println (F.size ()); } Public Static voidPrim (list<brg> brg,list<integer>a) { intD=0; intN=0; BRG BBB=NewBrg (); //extract the island by the largest edge from the island numbered 1, from the island and other connected islands, remove the longest bridge in the island x//put this island x in set a of the island and remove the removed island from the BRG and repeat until all the islands have been removed . for(intI=0;i<a.size (); i++){ for(Brg brg1:brg) {if(Brg1.geta () = =A.get (i)) { if(d<brg1.getdate ()) {N=Brg1.getb (); D=brg1.getdate (); BBB=BRG1; } }Else if(BRG1.GETB () = =A.get (i)) { if(d<brg1.getdate ()) {N=Brg1.geta (); D=brg1.getdate (); BBB=BRG1; } } } } //determine if a value is taken (there is a small island) if(d!=0) {brg.remove (BBB); F.add (d); X.add (n); } }}classbrg{Private intA; Private intb; Private intdate; PublicBRG (intAintBintdate) { This. a=A; This. b=b; This. date=date; } PublicBrg () {//TODO auto-generated Constructor stub } Public voidSetA (inta) { This. a=A; } Public intGeta () {return This. A; } Public voidSETB (intb) { This. b=b; } Public intGetb () {return This. b; } Public voidSetDate (intdate) { This. date=date; } Public intgetDate () {return This. Date; }}
I am using the prim algorithm, but the result of the submission is timelimitexceeded, and the optimization scheme is not expected.
-
The King's troubles