图论基本题第一道!
kruskal算法!!
#include<bits/stdc++.h>
using namespace std;
struct path
{
int k,l,q;
}p[200000];
int bcj[200000];
int get(int x)
{
if (bcj[x]==x)return x;
else return bcj[x]=get(bcj[x]);
}
void merge(int x,int y)
{
bcj[get(x)]=get(y);
}
void haha(int l,int r)
{
int i,j,x,y,n;
x=p[(l+r)/2].q;i=l;j=r;
do{
while(p[i].q<x)i++;
while(p[j].q>x)j--;
if (i<=j)
{swap(p[i].q,p[j].q);swap(p[i].k,p[j].k);swap(p[i].l,p[j].l);i++;j--;}
}while(i<=j);
if (i<r)haha(i,r);
if (j>l)haha(l,j);
}
int main()
{
int i,j,x,y,n,ans=0,t;
cin>>x>>y;
for (i=1;i<=x;i++)bcj[i]=i;
for (i=1;i<=y;i++)
{
int k,l;
cin>>p[i].k>>p[i].l>>p[i].q;
}
haha(1,y);
for (i=1;i<=y;i++)
if (get(p[i].k)!=get(p[i].l)){merge(p[i].k,p[i].l);ans+=p[i].q;t=p[i].q;};//只要稍微改一下改成输出最后一条边就过了!其余部分见模板题。
cout<<t;
return 0;
}
为什么这道题难度要比模板高?