ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

kuangbin专题五:并查集

2021-02-01 12:01:44  阅读:203  来源: 互联网

标签:专题 int px 查集 maxn kuangbin py include findP


  不想写实验,不想改paper,写点代码吧(;´д`)ゞ

 

POJ2236 Wireless Network

思路:简单并查集。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
int N, D;
int p[maxn];
bool vis[maxn];

struct Pos{
    int x, y;
}pos[maxn];

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

void Union(int x, int y){
    int px = findP(x), py = findP(y);
    if(px != py)
        p[px] = py;
}

bool in_dis(int x, int y){
    int dx = pos[x].x - pos[y].x;
    int dy = pos[x].y - pos[y].y;
    return dx*dx + dy*dy <= D*D;
}

void repair(int x){
    for(int i = 1; i <= N; i++)
        if(vis[i] && in_dis(i, x))
            Union(i, x);
    vis[x] = true;
}

int main(){
    cin >> N >> D;

    for(int i = 1; i <= N; i++) p[i] = i;

    for(int i = 1; i <= N; i++)
        cin >> pos[i].x >> pos[i].y;

    char op;
    int u, v;
    while(cin >> op){
        if(op == 'O'){
            cin >> u;
            repair(u);
        }
        else{
            cin >> u >> v;
            int pu = findP(u), pv = findP(v);
            cout << (pu == pv ? "SUCCESS" : "FAIL") << endl;
        }
    }
    return 0;
}
View Code

 

POJ1611 The Suspects

思路:简单并查集。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 3e4 + 5;
const int maxm = 505;
int p[maxn];

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

inline void Union(int x, int y){
    int px = findP(x), py = findP(y);
    if(px != py)
        p[px] = py;
}

int main(){
    int n, m;
    while(cin >> n >> m && (n + m > 0)){
        for(int i = 0; i < n; i++) p[i] = i;

        int k, s1, s2;
        for(int i = 1; i <= m; i++){
            cin >> k;
            if(k) cin >> s1;
            for(int j = 1; j < k; j++){
                cin >> s2;
                Union(s1, s2);
            }
        }
        int res = 0;
        for(int i = 0; i < n; i++)
            if(findP(0) == findP(i))
                res++;
        cout << res << endl;
    }
    return 0;
}
View Code

 

HDU1213 How Many Tables

思路:简单并查集。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int maxm = 1e3 + 5;
int p[maxn];
bool vis[maxn];

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

inline void Union(int x, int y){
    int px = findP(x), py = findP(y);
    if(px != py)
        p[px] = py;
}

int main(){
    int T;
    cin >> T;
    while(T--){
        int n, m;
        cin >> n >> m;

        for(int i = 1; i <= n; i++) p[i] = i;
        memset(vis, 0, sizeof(vis));

        for(int i = 1; i <= m; i++){
            int x, y;
            cin >> x >> y;
            Union(x, y);
        }

        int res = 0;
        for(int i = 1; i <= n; i++){
            int pi = findP(i);
            if(!vis[pi]){
                vis[pi] = true;
                res++;
            }
        }
        cout << res << endl;
    }
    return 0;
}
View Code

 

HDU3038 How Many Answers Are Wrong

思路:带权并查集。这题个人觉得模型比较难想到,我第一次接触到的时候被提醒了才想起来带权并查集。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 2e5 + 5;
int p[maxn], w[maxn];

int findP(int x){
    if(x != p[x]){
        int px = p[x];
        p[x] = findP(p[x]);
        w[x] += w[px];
    }
    return p[x];
}

int main(){
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF){
        for(int i = 0; i <= n; i++) p[i] = i, w[i] = 0;

        int res = 0;
        while(m--){
            int a, b, s;
            scanf("%d%d%d", &a, &b, &s);
            a--;
            int pa = findP(a), pb = findP(b);
            if(pa == pb){
                if((w[a] - w[b]) != s) res++;
            }
            else{
                p[pa] = pb;
                w[pa] = w[b] - w[a] + s;
            }
        }
        cout << res << endl;
    }
    return 0;
}
View Code

 

POJ1182 食物链

思路:经典带权并查集问题了。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 5e4 + 5;
int p[maxn], w[maxn];

int findP(int x){
    if(x != p[x]){
        int px = p[x];
        p[x] = findP(p[x]);
        w[x] = (w[x] + w[px]) % 3;
    }
    return p[x];
}

int main(){
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) p[i] = i, w[i] = 0;

    int res = 0;
    while(m--){
        int d, x, y;
        scanf("%d%d%d", &d, &x, &y);

        if (x > n || y > n) {
            res++;
            continue;
        }
        if (d == 2 && x == y) {
            res++;
            continue;
        }
        d--;

        int px = findP(x), py = findP(y);
        if (px == py && (w[y] - w[x] + 3) % 3 != d){
            res++;
        }
        else {
            p[py] = px;
            w[py] = (w[x] + d - w[y] + 3) % 3;
        }
    }
    cout << res << endl;
    return 0;
}
View Code

 

POJ1417 True Liars

思路:

 

标签:专题,int,px,查集,maxn,kuangbin,py,include,findP
来源: https://www.cnblogs.com/arch/p/14352743.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有