Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<vector>
- #include<queue>
- using namespace std;
- struct edge{
- int u,v,w;
- bool operator<(const edge &rhs)const{
- return w > rhs.w;
- }
- };
- int set[1010];
- int find(int x){
- if(set[x] == x)return x;
- return set[x] = find(set[x]);
- }
- bool isset(int x,int y){
- return find(x) == find(y);
- }
- void unions(int x,int y){
- int rx = find(x);
- int ry = find(y);
- set[rx] = ry;
- }
- int max(int a,int b){
- return (a > b ? a : b);
- }
- int main(){
- for(int i=0;i<=1000;i++)set[i] = i;
- int n,m;
- scanf("%d %d",&n,&m);
- vector<pair<int,int> > adj[n+1];
- priority_queue<edge> pq;
- for(int i=0;i<m;i++){
- int u,v,w;
- scanf("%d %d %d",&u,&v,&w);
- adj[u].push_back({w,v});
- adj[v].push_back({w,u});
- pq.push({u,v,w});
- }
- int MST = 0;
- int maxw = 0;
- while(!pq.empty()){
- edge e = pq.top();
- pq.pop();
- if(isset(e.u,e.v))continue;
- MST += e.w;
- unions(e.u,e.v);
- maxw = max(maxw,e.w);
- }
- int MBST = 0;
- queue<int> q;
- q.push(1);
- bool visited[n+1];for(int i=0;i<=n;i++)visited[i] = false;
- while(!q.empty()){
- int u = q.front();
- q.pop();
- if(visited[u]){
- MBST = 1;
- break;
- }
- visited[u] = true;
- for(int i=0;i<adj[u].size();i++){
- int v = adj[u][i].second;
- int w = adj[u][i].first;
- if(!visited[v] && w <= maxw){
- q.push(v);
- }
- }
- }
- printf("%d %d",MST,MBST);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement