本文主要是介绍More is better (并查集),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//新生训练
#include <iostream>
#include <algorithm>
using namespace std;
int pre[100002], num[100005];
int ans, mod = 300;
int find(int x)
{if (x != pre[x]){int fx = pre[x];pre[x] = find(fx);}return pre[x];
}
void join(int x, int y)
{int fx = find(x);int fy = find(y);if (fx != fy){pre[fy] = fx;num[fx] += num[fy];ans = max(ans, num[fx]);}
}
int main()
{int n, m, i, x, a, b;while (cin >> m){ans = 1;for (i = 1; i <= 100002; i++){pre[i] = i;num[i] = 1;}while (m--){cin >> a >> b;join(a, b);}cout << ans << endl;}return 0;
}
~~~//仅当笔者个人备忘录使用。
这篇关于More is better (并查集)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!