poj 1861 Network MST

简介:

    期末后第一次写题,结果就是这么灵异的一题……

    样例是错的,这题实际上就是求个最小生成树,spj


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define INF 1E9
using namespace std;
struct edge
{
    int f,t,v;
};
bool cmp(edge a,edge b)
{
    return a.v<b.v;
}
edge e[15005];
int fa[1005];
int far(int nn)
{
    if(fa[nn]<0)return nn;
    return fa[nn]=far(fa[nn]);
}
int main()
{
    memset(fa,-1,sizeof(fa));
    int n,m,f,t,v;
    scanf("%d%d",&n,&m);
    int i;
    for(i=0;i<m;i++)
        scanf("%d%d%d",&e[i].f,&e[i].t,&e[i].v);
    sort(e,e+m,cmp);
    int now,k,Max;
    int ans[1000];
    for(k=Max=0,now=1;now<n;k++)
    {
        f=far(e[k].f);t=far(e[k].t);
        if(f==t)continue;
        Max=max(Max,e[k].v);
        if(fa[f]<fa[t])//加权法则,避免退化
        {
            fa[f]+=fa[t];
            fa[t]=f;
        }
        else
        {
            fa[t]+=fa[f];
            fa[f]=t;
        }
        ans[now]=k;
        now++;
    }
    printf("%d\n",Max);
    printf("%d\n",now-1);
    for(i=1;i<now;i++)
     printf("%d %d\n",e[ans[i]].f,e[ans[i]].t);

}


目录
相关文章
[POJ 1236] Network of Schools | Tarjan缩点
Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”).
113 0
[POJ 1236] Network of Schools | Tarjan缩点
|
机器学习/深度学习 人工智能 BI
codeforces-1242-B 0-1 MST
B. 0-1 MST time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out.
138 0
codeforces-1242-B 0-1 MST
|
算法 数据建模
【POJ 1236 Network of Schools】强联通分量问题 Tarjan算法,缩点
题目链接:http://poj.org/problem?id=1236 题意:给定一个表示n所学校网络连通关系的有向图。现要通过网络分发软件,规则是:若顶点u,v存在通路,发给u,则v可以通过网络从u接收到。
1149 0
【POJ 1679 The Unique MST】最小生成树
无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和。 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树。(并不一定是最小生成树) 分析贪心策略求最小生成树的过程(贪心地选最短的边来扩充已加入生成树的顶点集合U),发现只有当出现“U中两个不同的点到V-U中同一点的距离同时为当前最短边”时,才会出现“异构”的最小生成树。
1207 0