(BFS) #include using namespace std; int nodecount, edgecount; bool vis[1005]; void bfs(int src,vector g[1005]) { queue q; q.push(src); vis [src]=true; while (!q. empty()) { int u = q. front(); cout << u << endl; q.pop(); for (auto v: g[u]) { if(!vis[v]) { vis[v]=true; q.push(v); } } } } int main() { cin>>nodecount>>edgecount; memset(vis,false,sizeof(vis)); vectorg[1005]; for (int i=1; i<=edgecount;i++) { int a,b; cin>>a>>b; g[a].push_back(b); } bfs(2,g); } (prims algo) #include using namespace std; typedef pairpill; int nodecount, edgecount; bool vis[1005]; int primsmst(int source, vector>g[1005]) { priority_queue,greater>pq; int res=0; pq.push({0,0}); while(!pq.empty()) { auto p = pq.top(); pq.pop(); int weight = p.first; int u=p.second; if(vis[u]==true) { continue; } res +=weight; vis[u]=true; for(auto v: g[u]) { pq.push({v[1],v[0]}); } } } int main() { cin>>nodecount>>edgecount; memset(vis,false,sizeof(vis)); vector>g[1005]; for (int i=1; i<=edgecount;i++) { int a,b,weight; cin>>a>>b>>weight; g[a].push_back({b,weight}); g[b].push_back({a,weight}); } int res=primsmst(0,g); cout<