繁忙的都市——一脸茫然,这明明是模板,为何出现在省选

  题目描述说一大堆要求,看着吓人,但是好像都和最小生成树的性质直接就套上了,也就是说,一个模板加个变量统计下就没了。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<vector>
 4 #include<cstdio>
 5 using namespace std;
 6 const int N=16384,INF=0x3f3f3f3f;
 7 struct node{
 8     int x,y,v;
 9     bool operator<(const node oth) const {return v<oth.v;}
10 };
11 int n,m,u,v,res,cnt,fth[N];
12 vector<node> e;
13 int fnd(int x){return x==fth[x]?x:fth[x]=fnd(fth[x]);}
14 int main(){
15     for(int i=0;i<N;i++)fth[i]=i;
16     cin>>n>>m;
17     for(int i=0;i<m;i++){
18         int x,y,v;scanf("%d%d%d",&x,&y,&v);
19         e.push_back((node){x,y,v});
20     }
21     sort(e.begin(),e.end());
22     for(int i=0;i<e.size();i++)
23         if((u=fnd(e[i].x))!=(v=fnd(e[i].y)))
24             res=max(res,e[i].v),fth[v]=u,cnt++;
25     cout<<cnt<<" "<<res<<endl;
26     return 0;
27 }
Method_01

  洛谷 AJAX 8ms

posted @ 2017-09-10 21:40  Darkins  阅读(141)  评论(0)    收藏  举报