11/10-提高组笔记

~ 2024-11-10 11:26:09

亲戚-并查集模板

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<sstream>
#include<set>
#include<time.h>
#include<stdlib.h>
#include<unordered_map>
#define ll long long
#define ull unsigned long long
#define eps 1e-10
#define INF 1e9
#define delta 0.996
#define T 3000
#define pi acos(-1.0)
#define ld long double
using namespace std;
const ll mod1=1e9+7;
const ll mod2=998244353;
const int maxn = 2e5 + 10;
const int maxm = 2e5 + 10;
const ll inf=1e18L;
typedef pair<int,int>Pii;
typedef pair<int,ll>Pil;
using namespace std;
int father[5010], n, m, p;
int find(int x)//找根函数,记得进行路径压缩
{
    if(father[x] == x) return x;
    else return father[x] = find(father[x]);
}
int same(int x, int y)//简化代码
{
    if(find(x) == find(y)) return 1;
    else return 0;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for(int i = 1; i <= n; i++)
    {
        father[i] = i;
    }
    while(m--)
    {
        int x, y;
        cin >> x >> y;
        int xx = find(x);
        int yy = find(y);
        if(xx != yy)
        {
            father[xx] = yy;
        }
    }
    while(p--)
    {
        int x, y;
        cin >> x >> y;
        if(same(x, y) == 1) cout << "Yes\n";
        else cout << "No\n";
    }
    return 0;
}

亲戚-高度优化介绍

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<sstream>
#include<set>
#include<time.h>
#include<stdlib.h>
#include<unordered_map>
#define ll long long
#define ull unsigned long long
#define eps 1e-10
#define INF 1e9
#define delta 0.996
#define T 3000
#define pi acos(-1.0)
#define ld long double
using namespace std;
const ll mod1=1e9+7;
const ll mod2=998244353;
const int maxn = 2e5 + 10;
const int maxm = 2e5 + 10;
const ll inf=1e18L;
typedef pair<int,int>Pii;
typedef pair<int,ll>Pil;
using namespace std;
int father[5010], n, m, p, high[5010];
int find(int x)//找根函数,记得进行路径压缩
{
    if(father[x] == x) return x;
    else return father[x] = find(father[x]);
}
void unite(int x, int y)//合并函数
{
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(high[x] < high[y])
    {
        father[x] = y;
    }
    else
    {
        father[y] = x;
        if(high[x] == high[y]) high[x]++;
    }
}
int same(int x, int y)//简化代码
{
    if(find(x) == find(y)) return 1;
    else return 0;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for(int i = 1; i <= n; i++)
    {
        father[i] = i;
        high[i] = 1;
    }
    while(m--)
    {
        int x, y;
        cin >> x >> y;
        unite(x, y);
    }
    while(p--)
    {
        int x, y;
        cin >> x >> y;
        if(same(x, y) == 1) cout << "Yes\n";
        else cout << "No\n";
    }
    return 0;
}

最小生成树-kruskal

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<sstream>
#include<set>
#include<time.h>
#include<stdlib.h>
#include<unordered_map>
#define ll long long
#define ull unsigned long long
#define eps 1e-10
#define INF 1e9
#define delta 0.996
#define T 3000
#define pi acos(-1.0)
#define ld long double
using namespace std;
const ll mod1=1e9+7;
const ll mod2=998244353;
const int maxn = 2e5 + 10;
const int maxm = 2e5 + 10;
const ll inf=1e18L;
typedef pair<int,int>Pii;
typedef pair<int,ll>Pil;
using namespace std;
int father[5010], n, m;
int find(int x)//找根函数,记得进行路径压缩
{
    if(father[x] == x) return x;
    else return father[x] = find(father[x]);
}
int same(int x, int y)//简化代码
{
    if(find(x) == find(y)) return 1;
    else return 0;
}
struct edge
{
    int u, v, cost;//边的两个端点,cost是边的长度
}e[200010];
int cmp(edge e1, edge e2)
{
    return e1.cost < e2.cost;
}
int kruskal()
{
    sort(e + 1, e + 1 + m, cmp);
    int res = 0;//边权总和
    int cnt = 0;//边的数量
    for(int i = 1; i <= m; i++)
    {
        if(same(e[i].u, e[i].v) == 0)
        {
            cnt++;
            res += e[i].cost;
            father[find(e[i].u)] = find(e[i].v);
        }
    }
    if(cnt == n - 1) return res;
    else return -1;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        father[i] = i;
    }
    for(int i = 1; i <= m; i++)
    {
        cin >> e[i].u >> e[i].v >> e[i].cost;
    }
    int ans = kruskal();
    if(ans == -1) cout << "orz\n";
    else cout << ans << '\n';
    return 0;
}


我们会审查剪贴板内容,并对发布不合适内容的同学进行相应的处理