【解题报告】【图论专题】

图论500题http://blog.csdn.net/luomingjun12315/article/details/47438607

【最小生成树+并查集】

1、hdu 1856   基础并查集★

/*
转化为图论模型
求一个图的最大联通分量
用并查集,每个联通分量用一个集合维护,最后求集合元素个数的最大值
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 10000010;
int fa[maxn];
int ans[maxn];
void init(){
    for(int i=0;i<maxn;++i){
        fa[i] = i;
    }
    memset(ans , 0, sizeof(ans));
}

int find(int u){
    return u==fa[u] ? u : fa[u] = find(fa[u]);
}
void un(int u, int v){
    int fu = find(u);
    int fv = find(v);
    fa[fv] = fu;
}

int main(){
    int n;
    while(scanf("%d", &n)==1){
        if(n==0){
            printf("1\n");
            continue;
        }
        init();
        int u, v;
        int maxnum = -1;
        for(int i=0;i<n;++i){
            scanf("%d%d", &u, &v);
            if(u>maxnum) maxnum = u;
            if(v>maxnum) maxnum = v;
            if(find(u)!=find(v)) un(u, v);
        }

//        ///test
//        for(int i=1;i<=maxnum;++i){
//            cout << fa[i] << " ";
//        }
//        cout << endl;

        for(int i=1;i<=maxnum;++i){
            ans[find(i)]++;
        }
        int maxans = -1;
        for(int i=1;i<=maxn;++i){
            if(ans[i] > maxans) maxans = ans[i];
        }
        printf("%d\n", maxans);
    }

    return 0;
}

AC

2、hdu 1102    基础最小生成树★  错误原因还未找到

/*
克鲁斯卡尔算法
将边从小到大排序
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 10010;

struct node{
    int u;
    int v;
    int weight;
} ;
node edges[maxn];
bool cmp(const node & e1, const node & e2){
    return e1.weight <= e2.weight;
}

int d[105];        //并查集
int find(int u){
    return u==d[u] ? u : d[u] = find(d[u]);
}
void init(){
    for(int i=0;i<105;++i){
        d[i] = i;
    }
}
void un(int u, int v){
    int fu = find(u);
    int fv = find(v);
    d[fv] = fu;
}

int main(){
    int n;
    int en = 0;
    int w;
    init();
    scanf("%d", &n);
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            scanf("%d", &w);
            if(j>i){
                edges[en].weight = w;
                edges[en].u = i;
                edges[en].v = j;
                en++;
            }
        }
    }

    int q;
    int u, v;
    scanf("%d", &q);
    for(int i=0;i<q;++i){
        scanf("%d%d", &u, &v);
        if(find(u)!=find(v)) un(u, v);
    }

    int ans = 0;
    sort(edges, edges+en, cmp);
    for(int i=0;i<en;++i){
        if(find(edges[i].u) != find(edges[i].v)){
            ans+=edges[i].weight;
            un(edges[i].u, edges[i].v);
        }
    }

    printf("%d\n", ans);
    return 0;
}

WA

原文地址:https://www.cnblogs.com/chsobin/p/8428649.html

时间: 2024-08-30 02:10:08

【解题报告】【图论专题】的相关文章

2014 UESTC暑前集训数据结构专题解题报告

A.Islands 这种联通块的问题一看就知道是并查集的思想. 做法:从高水位到低水位依序进行操作,这样每次都有新的块浮出水面,可以在前面的基础上进行合并集合的操作.给每个位置分配一个数字,方便合并集合.同时将这些数字也排一个序,降低枚举的复杂度.合并集合时向四周查询浮出水面但是没有合并到同一集合的点进行合并. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath&

UVa10048_Audiophobia(最短路/floyd)(小白书图论专题)

解题报告 题意: 求所有路中最大分贝最小的路. 思路: 类似floyd算法的思想,u->v可以有另外一点k,通过u->k->v来走,拿u->k和k->v的最大值和u->v比较,存下最小的值. #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std;

UVa10397_Connect the Campus(最小生成树)(小白书图论专题)

解题报告 题目传送门 题意: 使得学校网络互通的最小花费,一些楼的线路已经有了. 思路: 存在的线路当然全都利用那样花费肯定最小,把存在的线路当成花费0,求最小生成树 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std; int n,m,_hash[1110][1110],vis

UVa10099_The Tourist Guide(最短路/floyd)(小白书图论专题)

解题报告 题意: 有一个旅游团现在去出游玩,现在有n个城市,m条路.由于每一条路上面规定了最多能够通过的人数,现在想问这个旅游团人数已知的情况下最少需要运送几趟 思路: 求出发点到终点所有路当中最小值最大的那一条路. 求发可能有多种,最短路的松弛方式改掉是一种,最小生成树的解法也是一种(ps,prime和dijs就是这样子类似的) #include <iostream> #include <cstdio> #include <cstring> #include <

UVa10034/POJ2560_Freckles(最小生成树)(小白书图论专题)

解题报告 题意: 把所有点连起来,求使用的墨水最少. 思路: 裸最小生成树. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0x3f3f3f3f using namespace std; struct N { double x,y; } node[110]; int vis[110],n; double mmap[110][110],

poj分类解题报告索引

图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Journey poj1724 - ROADS(邻接表+DFS) BFS poj3278 - Catch That Cow(空间BFS) poj2251 - Dungeon Master(空间BFS) poj3414 - Pots poj1915 - Knight Moves poj3126 - Prim

poj 1094 Sorting It All Out 解题报告

题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具体的字母顺序不能确定但至少不矛盾.这些关系均是这样的一种形式: 字母1 < 字母2 这道题目属于图论上的拓扑排序,由于要知道读入第几行可以确定具体的顺序,所以每次读入都需要进行拓扑排序来检验,这时每个点的入度就需要存储起来,所以就有了代码中memcpy 的使用了. 拓扑排序的思路很容易理解,但写起来

解题报告 之 HDU5326 Work

解题报告 之 HDU5326 Work Description It's an interesting experience to move from ICPC to work, end my college life and start a brand new journey in company. As is known to all, every stuff in a company has a title, everyone except the boss has a direct le

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了