Given any weighted undirected graph, there exists at least one minimum spanning tree (MST) if the graph is connected. Sometimes the MST may not be unique though. Here you are supposed to calculate the minimum total weight of the MST, and also tell if it is unique or not.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 2 numbers N (≤ 500), and M, which are the total number of vertices, and the number of edges, respectively. Then M lines follow, each describes an edge by 3 integers:
V1 V2 Weight
where V1
and V2
are the two ends of the edge (the vertices are numbered from 1 to N), and Weight
is the positive weight on that edge. It is guaranteed that the total weight of the graph will not exceed 230.
Output Specification:
For each test case, first print in a line the total weight of the minimum spanning tree if there exists one, or else print No MST
instead. Then if the MST exists, print in the next line Yes
if the tree is unique, or No
otherwise. There there is no MST, print the number of connected components instead.
Sample Input 1:
5 7
1 2 6
5 1 1
2 3 4
3 4 3
4 1 7
2 4 2
4 5 5
Sample Output 1:
11
Yes
Sample Input 2:
4 5
1 2 1
2 3 1
3 4 2
4 1 2
3 1 3
Sample Output 2:
4
No
Sample Input 3:
5 5
1 2 1
2 3 1
3 4 2
4 1 2
3 1 3
Sample Output 3:
No MST
2
1 #include2 using namespace std; 3 vector int,int> > > v; 4 vector int,int> > > vst; 5 vector<bool> vb; 6 unordered_set<int> us; 7 int mst; 8 struct node 9 { 10 int id; 11 int pid; 12 int w; 13 bool operator< (const node& en) const 14 { 15 return w>en.w; 16 } 17 } no; 18 vector vsst; 19 inline int st(int n) 20 { 21 priority_queue q; 22 no.id=n; 23 no.pid=n; 24 no.w=0; 25 q.push(no); 26 for(;!q.empty();) 27 { 28 auto qf=q.top(); 29 q.pop(); 30 if(vb[qf.id]) 31 { 32 mst=mst+qf.w; 33 us.emplace(qf.w); 34 vb[qf.id]=false; 35 if(qf.id!=qf.pid) 36 { 37 vst[qf.id].emplace_back(make_pair(qf.pid,qf.w)); 38 vst[qf.pid].emplace_back(make_pair(qf.id,qf.w)); 39 } 40 for(auto k:v[qf.id]) 41 { 42 if(vb[k.first]) 43 { 44 no.id=k.first; 45 no.pid=qf.id; 46 no.w=k.second; 47 q.push(no); 48 } 49 } 50 } 51 else 52 { 53 if(us.find(qf.w)!=us.end()) 54 vsst.emplace_back(qf); 55 } 56 } 57 for(int i=n;i i) 58 if(vb[i]) 59 return i; 60 return -1; 61 } 62 inline bool bfs(int c1,int c2,int w) 63 { 64 vector<int> vbb(v.size(),0); 65 queue int,int> > q; 66 q.push(make_pair(c1,1)); 67 q.push(make_pair(c2,-1)); 68 vbb[c1]=1; 69 vbb[c2]=-1; 70 for(;!q.empty();) 71 { 72 auto qf=q.front(); 73 q.pop(); 74 for(auto k:vst[qf.first]) 75 { 76 if(vbb[k.first]*qf.second==0) 77 { 78 if(k.second==w) 79 qf.second=2*qf.second; 80 vbb[k.first]=qf.second; 81 q.push(qf); 82 } 83 else if(vbb[k.first]*qf.second<0) 84 { 85 if(abs(vbb[k.first])>1) 86 return false; 87 else 88 return true; 89 } 90 } 91 } 92 return false; 93 } 94 int main() 95 { 96 // freopen("data.txt","r",stdin); 97 int n,m,c1,c2,w; 98 mst=0; 99 scanf("%d %d",&n,&m); 100 v.resize(n); 101 vb.resize(n,true); 102 vst.resize(n); 103 for(;m--;) 104 { 105 scanf("%d %d %d",&c1,&c2,&w); 106 c1--; 107 c2--; 108 v[c1].emplace_back(make_pair(c2,w)); 109 v[c2].emplace_back(make_pair(c1,w)); 110 } 111 w=st(0); 112 if(w<0) 113 { 114 printf("%d\n",mst); 115 bool flag=true; 116 for(int i=0;i i) 117 { 118 if(bfs(vsst[i].id,vsst[i].pid,vsst[i].w)) 119 continue; 120 else 121 flag=false; 122 } 123 if(flag) 124 printf("Yes"); 125 else 126 printf("No"); 127 } 128 else 129 { 130 printf("No MST\n"); 131 int part=1; 132 for(;w>-1;part++) 133 w=st(w); 134 printf("%d",part); 135 } 136 return 0; 137 }