最小生成树+并查集:题目集及简解

HDU1102 Constructing Roads比较水,只要把修建的道路置为零就可以

计蒜客 灌溉:比上一题还水,直接上手就可以

2018.4.14

HDU1213 how many tables:一道并查集的题目,不难,但是出了篓子

#include<iostream>
#include<algorithm>
using namespace std;
int p[1007];
int found(int x)
{
    return p[x] == x ? x : p[x] = found(p[x]);
}

int main()
{
    int t,m,n;
    int a,b;
    cin >> t;
    while(t--)
    {
        cin >> n >> m;
        int ans = n;
        for(int i = 1; i <= n; i ++)
        {
            p[i] = i;
        }
        for(int i = 0; i < m; i ++)
        {
            cin >> a >> b;
            int x = found(a);
            int y = found(b);
            if(x != y)
            {
                p[x] = y;
                ans --;//错误更改为此处
            }
        }
//        —————————错误部分————————————
//        for(int i = 1; i <= n; i ++)
//            found(i);
//        int ans = 1;
//        sort(p+1,p+1+n);
//        for(int i = 1; i < n; i ++)
//            if(p[i]!= p[i+1])ans++;
//        —————————错误部分————————————
        cout << ans << endl;
    }
    return 0;
}

HDU1232 畅通工程 水题 最小生成树

2018.4.15

HDU1233 还是畅通工程 水题 最小生成树

HDU1863 畅通工程 不是太难,注意条件

HDU1875 畅通工程再续 注意条件
HDU1879 继续畅通工程 这个题使用cin cout来输入输出超时了,改成scanf printf就过了

2018.4.16

HDU3371 connect the cities 从这道题开始我要相信玄学了,TLE了无数次,然后在把cmp的const int 改成int之后就AC了?

贴代码

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn = 25002;
int u[maxn],v[maxn],w[maxn],r[maxn],p[maxn];

bool cmp(int i, int j)//就是这里的锅←
{
    return w[i] < w[j];
}

int found(int x)
{
    return p[x] == x ? x : p[x] = found(p[x]);
}

int main()
{
    int n,k,m;
    int cases,t;
    scanf("%d",&cases);
    while(cases --)
    {
        int counts = 1;
        int ans = 0;
        scanf("%d%d%d",&n,&m,&k);
        for(int i = 0; i < m; i ++)
        {
            scanf("%d%d%d",&u[i],&v[i],&w[i]);
        }
        for(int i = 0; i <= max(m,n); i ++)
        {
            p[i] = i;
            r[i] = i;
        }
        int temp1,temp2;
        for(int i = 0; i < k; i ++)
        {
            scanf("%d%d",&t,&temp1);
            t--;
            while(t--)
            {
                scanf("%d",&temp2);
                int x = found(temp1);
                int y = found(temp2);
                if(x != y)
                {
                    p[x] = y;
                    counts++;
                }
                temp1 = temp2;
            }
        }
        sort(r,r+m,cmp);
        for(int i = 0; i < m; i ++)
        {
            int e = r[i];
            int x = found(u[e]);
            int y = found(v[e]);
            if(x != y)
            {
                p[x] = y;
                ans += w[e];
                counts ++;
            }
        }
        counts == n ? printf("%d\n",ans) : printf("-1\n");
    }
    return 0;
}

一道atcoder的题目,学到了一点骚操作,单独写了个博客,链接

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值