“美登杯”上海市高校大学生程序设计 C. 小花梨判连通 (并查集+map)

Problem C C 、 小 花梨 判连通
时间限制:2000ms 空间限制:512MB
Description
小花梨给出?个点,让?位同学对这?个点任意添加无向边,构成?张图。小花梨想知道对于
每个点?,存在多少个点?(包括?本身),使得?和?在这?张图中都是连通的。
Input
第一行输入两个正整数?和?,分别表示点的个数和同学数。
接下来分成?部分进行输入,每部分输入格式相同。
每部分第一行输入一个整数??,表示第?位同学连边的数目。
接下来??行,每行两个正整数?,?,表示第?位同学将点?和点?之间进行连接。
可能会存在重边或者自环。
(1 ≤ ? ≤ 100000,1 ≤ ? ≤ 10,1 ≤ ?,? ≤ ?,0 ≤ ?? ≤ 200000)
Output
输出?行,第?行输出在?张图中都和编号为?的点连通的点的数目(包括?本身)
Example
Sample Input Sample Output
4 2
3
1 2
1 3
2 3
2
1 2
3 4
2
2
1
1

思路:

· 我们如果根据图中每一条边进行并查集的merge,那么在一张图中,如果两个节点联通,那么他们的祖先一定相等。

那么我们对每一个节点创建一个vector,来依次存它在k张图中的祖先。

那么我们可以知道 如果两个节点在k张图中都联通,那么它们的vector数组是相等的。

然后我们不妨使用map对vector 出现的次数进行统计,从而可以得出答案。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int far[maxn];
int n;
void init()
{
    repd(i, 1, n) {
        far[i] = i;
    }
}
int findpar(int x)
{
    if (x == far[x]) {
        return x;
    } else {
        return far[x] = findpar(far[x]);
    }
}

void merge_(int x, int y)
{
    x = findpar(x);
    y = findpar(y);
    if (x != y) {
        far[x] = y;
    }
}

int k;
std::vector<int> v[maxn];
map<vector<int>, int> vis;


int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    cin >> n >> k;
    int num;
    while (k--) {
        init();
        cin >> num;
        repd(i, 1, num) {
            int x, y;
            cin >> x >> y;
            merge_(x, y);
        }
        repd(i, 1, n) {
            v[i].push_back(findpar(i));
        }
    }
    // repd(i, 1, n) {
    //     for (auto x : v[i]) {
    //         cout << x << " ";
    //     }
    //     cout << endl;
    // }
    repd(i, 1, n) {
        vis[v[i]]++;
    }
    repd(i, 1, n) {
        cout << vis[v[i]] << endl;
    }
    return 0;
}

inline void getInt(int *p)
{
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    } else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}




转载于:https://www.cnblogs.com/qieqiemin/p/11491137.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值