Title Description
If that, given an undirected graph, the minimum spanning tree is obtained, if this figure is not connected, the output orz
Input Format
The first line contains two integers N, M, N represents the total FIG nodes and the M undirected edges. (N <= 5000, M <= 200000)
Next M lines contains three integers Xi, Yi, Zi, Zi expressed a length of edges connected to node No Xi, Yi
Output Format
Comprising a number of output, i.e., the minimum spanning tree and the length of each side; if the output is not connected orz FIG.
Sample input and output
4 5 1 2 2 1 3 2 1 4 3 2 3 4 3 4 3
7
Description / Tips
Constraints of time: 1000ms, 128M
Data Scale:
For 20% of the data: N <= 5, M <= 20
For 40% of the data: N <= 50, M <= 2500
For 70% of the data: N <= 500, M <= 10000
To 100% of the data: N <= 5000, M <= 200000
Sample explained:
Therefore, the minimum spanning tree of total edge weight of 2 + 2 + 3 = 7
- Ideas: an input side (left points a, a right end points b, the length w) all fit inside the vector, and then by the length from small to large, after traversing the vector, using disjoint-set ab determines whether the endpoint is connected, has been linked to skipped, otherwise to use (a, b, w) connect.
- Finally, connected to the N-1 when the edges, is already FIG communication, the edge is small and is selected to the election, and so is the minimum length.
- If the edge is not selected edges to N-1, then no communication is definitely FIG.
AC codes and templates
#include <bits / STDC H ++.> the using namespace STD; const int NN = 1E4 + 10 ; int N, M; int FA [NN]; struct Node { int A, B, W; Friend BOOL operator <(Node N1, Node N2) { // collation node structure, not want to define cmp, after a write function return n1.w < n2.w; } }; Vector <node> VE; // storage container side int Find ( int X ) { // Find ancestors IF (the X-! = FA [the X-]) FA [the X-] = find(fa[x]); return fa[x]; } void join(int x,int y){ //合并 int fx = find(x),fy = find(y); if(fx!=fy) fa[fx] = fy; } int main(){ cin>>N>>M; for(int i = 1;i<=N;i++) fa[i] = i; int a,b,w; while(M--){ scanf("%d%d%d",&a,&b,&w); ve.push_back ({A, B, W}); } Sort (ve.begin (), ve.end ()); // Sort Long Long SUM = 0 ; // All patterned edges total length int Edges = 0 ; // selected number of sides for (Auto V: VE) { IF {(Find (VA) = Find (VB)!) // if the point is not a point b Unicom, chose this edge merge join (va, vb ); SUM + = VW; Edges ++ ; } } IF (Edges> = N- . 1 ) SUM COUT << << endl; the else COUT <<"orz"<<endl; return 0; }