Codeforces 920E-Connected Components? (set,补图,连通块)
Connected Components?
CodeForces - 920E
You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x,?y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.
You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices X such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to X violates this rule.
Input
The first line contains two integers n and m (1?≤?n?≤?200000, ).
Then m lines follow, each containing a pair of integers x and y (1?≤?x,?y?≤?n, x?≠?y) denoting that there is no edge between x and y. Each pair is listed at most once; (x,?y) and (y,?x) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices.
Output
Firstly print k — the number of connected components in this graph.
Then print k integers — the sizes of components. You should output these integers in non-descending order.
Example
Input
5 51 23 43 24 22 5Output
21 4思路:
bfs,同時用一個set記錄哪些點還未確定是哪個聯通塊,再開一個set記錄能走到哪些點。
代碼:
#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 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 #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define du2(a,b) scanf("%d %d",&(a),&(b)) #define du1(a) scanf("%d",&(a)); 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) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;} void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}inline void getInt(int *p); const int maxn = 200000 + 10; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ set<int> st; set<int> temp; std::vector<int> v[maxn]; int ans[maxn]; int tot = 0; int n, m; bool vis[maxn]; queue<int> q; int main() { //freopen("D:\\code\\text\\input.txt","r",stdin); //freopen("D:\\code\\text\\output.txt","w",stdout);du2(n, m);repd(i, 1, m) {int x, y;du2(x, y);v[x].push_back(y);v[y].push_back(x);}repd(i,1,n){st.insert(i);}repd(i, 1, n) {if (!vis[i]) {q.push(i);tot++;while (!q.empty()) {int x = q.front();q.pop();if (vis[x]) {continue;}ans[tot]++;vis[x] = 1;for (auto y : v[x]) {if (!vis[y]) {temp.insert(y);}}for (auto y : temp) {st.erase(y);}for (auto y : st) {if (!vis[y]) {q.push(y);}}swap(temp, st);temp.clear();}}}printf("%d\n", tot );sort(ans + 1, ans + 1 + tot);repd(i, 1, tot) {printf("%d%c", ans[i], i == tot ? '\n' : ' ');}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/11617120.html
總結
以上是生活随笔為你收集整理的Codeforces 920E-Connected Components? (set,补图,连通块)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何:将 TraceSource 和筛选
- 下一篇: asp.net定时执行任务-解决应用池回