TLE3个点,求助(kruscal)

P3366 【模板】最小生成树

@[tfptfp](/space/show?uid=112714) 你把fd函数改成 ``` else return f[o]=find(f[o]); ``` 因为并查集的路径压缩。
by Meatherm @ 2019-02-02 14:20:18


或者说 ``` else { f[o]=find(f[o]); return f[o]; } ```
by Meatherm @ 2019-02-02 14:20:54


@[兹磁洛谷](/space/show?uid=108949) 多谢大佬
by tfptfp @ 2019-02-02 16:37:59


其实之前试过,但是把fd函数和hb函数合起来了,样例都过不了……
by tfptfp @ 2019-02-02 16:38:53


@[兹磁洛谷](/space/show?uid=108949) 大佬,我的怎么改,和他一样,二,九十,三个点。 ```cpp #include<iostream> #include<algorithm> using namespace std; const int maxn=100005; struct edge{ int x,y,z; }a[maxn]; int n,m,p[maxn],ans=0,bj; bool cmp(const edge&x,const edge&y) { return x.z<y.z; } int g(int x) { if(p[x]==x) return x; else { p[x]=g(p[x]); return p[x]; } } void kr() { int f1,f2; int k=0;for(int i=1;i<=m;i++) p[i]=i; for(int i=1;i<=m;i++) { f1=g(a[i].x); f2=g(a[i].y); if(f1!=f2) { ans=ans+a[i].z; p[f1]=f2; k++; if(k==n-1) break; } } if(k<n-1) { cout<<"Impossible"<<endl; bj=0; return; } } int main() { cin>>n>>m; ans=0;bj=1; for(int i=1;i<=m;i++) cin>>a[i].x>>a[i].y>>a[i].z; sort(a+1,a+m+1,cmp); kr(); if(bj) cout<<ans<<endl; return 0; } ```
by Kevin__Z @ 2019-03-04 19:07:25


@[Kevin__Z](/space/show?uid=83339) ``` #include<iostream> #include<algorithm> using namespace std; const int maxn=200005; //数据是200005 您来一个100005 什么意思嘛 struct edge{ int x,y,z; }a[maxn]; int n,m,p[maxn],ans=0,bj; bool cmp(const edge&x,const edge&y) { return x.z<y.z; } int g(int x) { if(p[x]==x) return x; else { p[x]=g(p[x]); return p[x]; } } void kr() { int f1,f2; int k=0;for(int i=1;i<=n;i++) //是i<=n 不是m ! p[i]=i; for(int i=1;i<=m;i++) { f1=g(a[i].x); f2=g(a[i].y); if(f1!=f2) { ans=ans+a[i].z; p[f1]=f2; k++; if(k==n-1) break; } } if(k<n-1) { cout<<"orz"<<endl; //您得好好读题目啊?QAQ bj=0; return; } } int main() { cin>>n>>m; ans=0;bj=1; for(int i=1;i<=m;i++) cin>>a[i].x>>a[i].y>>a[i].z; sort(a+1,a+m+1,cmp); kr(); if(bj) cout<<ans<<endl; return 0; } ```
by Meatherm @ 2019-03-04 19:25:18


@[兹磁洛谷](/space/show?uid=108949) 谢谢了。。这一题用邻接矩阵会不会爆掉
by Kevin__Z @ 2019-03-04 19:33:15


@[Kevin__Z](/space/show?uid=83339) 您用邻接矩阵怎么按照边权排序...
by Meatherm @ 2019-03-04 19:43:34


@[兹磁洛谷](/space/show?uid=108949) 也就是像李煜东的书上那种写法
by Kevin__Z @ 2019-03-04 19:50:29


@[Kevin__Z](/space/show?uid=83339) 不清楚 qwq
by Meatherm @ 2019-03-04 19:55:52


| 下一页