求助,wa on test 26
查看原帖
求助,wa on test 26
151647
sycqwq楼主2022/11/22 19:32

rt

大致思路是在 kruskalkruskal 的时候,如果判断两个点属于同一个连通块,并且该连通块中存在和当前边权值一样的边时,ans++

感觉和第一篇题解的法二代码本质相同。

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
struct node
{
    int x,y,w;
}a[maxn];
int fa[maxn],ma[maxn];
int getfa(int x)
{
    return fa[x]==x?fa[x]:fa[x]=getfa(fa[x]);
}
int cmp(node s1,node s2)
{
    return s1.w<s2.w;
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++)
        cin>>a[i].x>>a[i].y>>a[i].w;
    sort(a+1,a+m+1,cmp);
    for(int i=1;i<=n;i++)
        fa[i]=i;
    int s=0;
    for(int i=1;i<=m;i++)   
    {
        int fx=getfa(a[i].x),fy=getfa(a[i].y);
        // cout<<fx
        if(fx==fy&&ma[fx]==a[i].w)
            ++s;  
        if(fx!=fy)
        {
            fa[fx]=fy;  
            ma[fy]=ma[fx]=a[i].w;
        }
    }
    cout<<s<<endl;
    return 0;
}
2022/11/22 19:32
加载中...