output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> int power(int a, int b) { int z = 1; while (b) { if (b & 1) { z *= a; z %= 1000000007; } a *= a; a %= 1000000007; b /= 2; } return z % 1000000007; } using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, m, a, b; vector<unordered_set<int>> st; vector<vector<int>> vec; vector<int> v; unordered_set<int> used; void dfs(int s) { queue<int> q; q.push(s); used.erase(used.find(s)); v.push_back(s); while (!q.empty()) { vector<int> vv; int t = q.front(); q.pop(); for (auto x : used) { if (st[t].find(x) == st[t].end()) { q.push(x); vv.push_back(x); } } for (auto x : vv) { v.push_back(x); used.erase(used.find(x)); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; st.resize(n + 1); for (int i = 0; i < m; ++i) { cin >> a >> b; st[a].insert(b); st[b].insert(a); } for (int i = 1; i <= n; ++i) used.insert(i); for (int i = 1; i <= n; ++i) { if (used.find(i) != used.end()) { v.clear(); dfs(i); vec.push_back(v); } } cout << vec.size() << "\n"; ; for (auto x : vec) { cout << x.size() << " "; sort(x.begin(), x.end()); for (auto y : x) cout << y << " "; cout << "\n"; ; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> int power(int a, int b) { int z = 1; while (b) { if (b & 1) { z *= a; z %= 1000000007; } a *= a; a %= 1000000007; b /= 2; } return z % 1000000007; } using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, m, a, b; vector<unordered_set<int>> st; vector<vector<int>> vec; vector<int> v; unordered_set<int> used; void dfs(int s) { queue<int> q; q.push(s); used.erase(used.find(s)); v.push_back(s); while (!q.empty()) { vector<int> vv; int t = q.front(); q.pop(); for (auto x : used) { if (st[t].find(x) == st[t].end()) { q.push(x); vv.push_back(x); } } for (auto x : vv) { v.push_back(x); used.erase(used.find(x)); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; st.resize(n + 1); for (int i = 0; i < m; ++i) { cin >> a >> b; st[a].insert(b); st[b].insert(a); } for (int i = 1; i <= n; ++i) used.insert(i); for (int i = 1; i <= n; ++i) { if (used.find(i) != used.end()) { v.clear(); dfs(i); vec.push_back(v); } } cout << vec.size() << "\n"; ; for (auto x : vec) { cout << x.size() << " "; sort(x.begin(), x.end()); for (auto y : x) cout << y << " "; cout << "\n"; ; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 5 * 100 * 1000 + 14; vector<int> comp[MAXN], adj[MAXN]; set<int> res; int ans = 0; void dfs(int v) { comp[ans].push_back(v); auto it = res.begin(); while (it != res.end()) { if (find(adj[v].begin(), adj[v].end(), *it) == adj[v].end()) { int u = *it; res.erase(u); dfs(u); it = res.upper_bound(u); } else it++; } return; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; while (m--) { int u, v; cin >> u >> v; adj[v].push_back(u); adj[u].push_back(v); } for (int i = 1; i <= n; i++) { res.insert(i); sort(adj[i].begin(), adj[i].end()); } while (!res.empty()) { int v = *res.begin(); res.erase(v); ans++; dfs(v); } cout << ans << '\n'; for (int i = 1; i <= ans; i++) { cout << comp[i].size() << ' '; for (auto u : comp[i]) cout << u << ' '; cout << '\n'; } return 0; }
### Prompt Generate a Cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 5 * 100 * 1000 + 14; vector<int> comp[MAXN], adj[MAXN]; set<int> res; int ans = 0; void dfs(int v) { comp[ans].push_back(v); auto it = res.begin(); while (it != res.end()) { if (find(adj[v].begin(), adj[v].end(), *it) == adj[v].end()) { int u = *it; res.erase(u); dfs(u); it = res.upper_bound(u); } else it++; } return; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; while (m--) { int u, v; cin >> u >> v; adj[v].push_back(u); adj[u].push_back(v); } for (int i = 1; i <= n; i++) { res.insert(i); sort(adj[i].begin(), adj[i].end()); } while (!res.empty()) { int v = *res.begin(); res.erase(v); ans++; dfs(v); } cout << ans << '\n'; for (int i = 1; i <= ans; i++) { cout << comp[i].size() << ' '; for (auto u : comp[i]) cout << u << ' '; cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; int n, m, ind[N]; vector<int> adj[N]; bool interest[N]; vector<vector<int> > cc; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq; void scout(int u) { for (int x : adj[u]) if (interest[x]) { ind[x]++; pq.push({ind[x], x}); } interest[u] = false; } int examine(int s, vector<int>& c) { fill(ind + 1, ind + n + 1, 0); while (!pq.empty()) pq.pop(); for (int i = 1; i <= n; ++i) if (interest[i]) pq.push({ind[i], i}); scout(s); int m = 1; c.push_back(s); while (!pq.empty()) { pair<int, int> to = pq.top(); pq.pop(); int indeg, u; indeg = to.first; u = to.second; if (!interest[u]) continue; if (ind[u] != indeg) continue; if (indeg >= m) break; c.push_back(u); scout(u); m++; } return m; } int main() { ios::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } fill(interest + 1, interest + n + 1, true); int ncomp = 0; for (int i = 1; i <= n; ++i) if (interest[i]) { cc.push_back(vector<int>()); examine(i, cc[ncomp]); ++ncomp; } cout << ncomp << endl; for (int i = 0; i < ncomp; ++i) { cout << cc[i].size() << " "; for (int x : cc[i]) cout << x << " "; cout << endl; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; int n, m, ind[N]; vector<int> adj[N]; bool interest[N]; vector<vector<int> > cc; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq; void scout(int u) { for (int x : adj[u]) if (interest[x]) { ind[x]++; pq.push({ind[x], x}); } interest[u] = false; } int examine(int s, vector<int>& c) { fill(ind + 1, ind + n + 1, 0); while (!pq.empty()) pq.pop(); for (int i = 1; i <= n; ++i) if (interest[i]) pq.push({ind[i], i}); scout(s); int m = 1; c.push_back(s); while (!pq.empty()) { pair<int, int> to = pq.top(); pq.pop(); int indeg, u; indeg = to.first; u = to.second; if (!interest[u]) continue; if (ind[u] != indeg) continue; if (indeg >= m) break; c.push_back(u); scout(u); m++; } return m; } int main() { ios::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } fill(interest + 1, interest + n + 1, true); int ncomp = 0; for (int i = 1; i <= n; ++i) if (interest[i]) { cc.push_back(vector<int>()); examine(i, cc[ncomp]); ++ncomp; } cout << ncomp << endl; for (int i = 0; i < ncomp; ++i) { cout << cc[i].size() << " "; for (int x : cc[i]) cout << x << " "; cout << endl; } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int mod = 1e9 + 7; const double eps = 1e-6; const int N = 5e5 + 7; bool flag; vector<int> G[N], out[N]; set<int> st; int res; void bfs(int u) { queue<int> que; que.push(u); while (que.size()) { int f = que.front(); que.pop(); out[res].push_back(f); auto it = st.begin(); for (; it != st.end();) { int v = *it; it++; if (!binary_search(G[f].begin(), G[f].end(), v)) { que.push(v); st.erase(v); } } } } int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int first, second; cin >> first >> second; G[first].push_back(second); G[second].push_back(first); } for (int i = 1; i <= n; i++) { st.insert(i); sort(G[i].begin(), G[i].end()); } res = 0; while (st.size()) { int u = *(st.begin()); st.erase(u); res++; bfs(u); } cout << res << endl; for (int i = 1; i <= res; i++) { cout << out[i].size() << " "; for (int t : out[i]) cout << t << " "; cout << endl; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int mod = 1e9 + 7; const double eps = 1e-6; const int N = 5e5 + 7; bool flag; vector<int> G[N], out[N]; set<int> st; int res; void bfs(int u) { queue<int> que; que.push(u); while (que.size()) { int f = que.front(); que.pop(); out[res].push_back(f); auto it = st.begin(); for (; it != st.end();) { int v = *it; it++; if (!binary_search(G[f].begin(), G[f].end(), v)) { que.push(v); st.erase(v); } } } } int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int first, second; cin >> first >> second; G[first].push_back(second); G[second].push_back(first); } for (int i = 1; i <= n; i++) { st.insert(i); sort(G[i].begin(), G[i].end()); } res = 0; while (st.size()) { int u = *(st.begin()); st.erase(u); res++; bfs(u); } cout << res << endl; for (int i = 1; i <= res; i++) { cout << out[i].size() << " "; for (int t : out[i]) cout << t << " "; cout << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int const inf = 1e18; long long int const maxn = 1e6 + 5; long long int const mod = 1e9 + 7; set<int> unvisited; vector<pair<int, int>> v; int cur = 0; vector<int> components[maxn]; pair<int, int> fx(int x, int y) { return make_pair(min(x, y), max(x, y)); } void dfs(int i) { components[cur].push_back(i); for (auto it = unvisited.begin(); it != unvisited.end();) { if (!binary_search(v.begin(), v.end(), fx(i, *it))) { int val = *it; unvisited.erase(it); dfs(val); it = unvisited.upper_bound(val); } else it++; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; v.push_back(fx(x, y)); } sort(v.begin(), v.end()); for (int i = 1; i <= n; i++) unvisited.insert(i); while (!unvisited.empty()) { int v = *unvisited.begin(); unvisited.erase(unvisited.begin()); dfs(v); cur++; } cout << cur << "\n"; ; for (int i = 0; i < cur; i++) { int sz = components[i].size(); cout << sz << " "; for (auto u : components[i]) cout << u << " "; cout << "\n"; ; } return 0; }
### Prompt Generate a CPP solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int const inf = 1e18; long long int const maxn = 1e6 + 5; long long int const mod = 1e9 + 7; set<int> unvisited; vector<pair<int, int>> v; int cur = 0; vector<int> components[maxn]; pair<int, int> fx(int x, int y) { return make_pair(min(x, y), max(x, y)); } void dfs(int i) { components[cur].push_back(i); for (auto it = unvisited.begin(); it != unvisited.end();) { if (!binary_search(v.begin(), v.end(), fx(i, *it))) { int val = *it; unvisited.erase(it); dfs(val); it = unvisited.upper_bound(val); } else it++; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; v.push_back(fx(x, y)); } sort(v.begin(), v.end()); for (int i = 1; i <= n; i++) unvisited.insert(i); while (!unvisited.empty()) { int v = *unvisited.begin(); unvisited.erase(unvisited.begin()); dfs(v); cur++; } cout << cur << "\n"; ; for (int i = 0; i < cur; i++) { int sz = components[i].size(); cout << sz << " "; for (auto u : components[i]) cout << u << " "; cout << "\n"; ; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 500005, M = 1e6 + 5; int g[N]; vector<int> a[N], ans[N]; int FIND(int x) { return g[x] == x ? x : g[x] = FIND(g[x]); } void UNION(int x, int y) { g[FIND(x)] = FIND(y); } int main(void) { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) g[i] = i; while (m--) { int x, y; scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x); } int mindeg = n, mdi = 0; for (int i = 1; i <= n; i++) if (((int)(a[i]).size()) < mindeg) { mindeg = ((int)(a[i]).size()); mdi = i; } sort(a[mdi].begin(), a[mdi].end()); for (int i = 1, j = 0; i <= n; i++) { if (j < ((int)(a[mdi]).size()) && a[mdi][j] == i) ++j; else UNION(mdi, i); } for (__typeof((a[mdi]).begin()) it = (a[mdi]).begin(); it != (a[mdi]).end(); it++) sort(a[*it].begin(), a[*it].end()); for (__typeof((a[mdi]).begin()) it = (a[mdi]).begin(); it != (a[mdi]).end(); it++) { for (int i = 1, j = 0; i <= n; i++) { if (j < ((int)(a[*it]).size()) && a[*it][j] == i) ++j; else UNION(*it, i); } } for (int i = 1; i <= n; i++) ans[FIND(i)].push_back(i); int sans = 0; for (int i = 1; i <= n; i++) if (g[i] == i) ++sans; printf("%d\n", sans); for (int i = 1; i <= n; i++) if (((int)(ans[i]).size())) { printf("%d", ((int)(ans[i]).size())); for (__typeof((ans[i]).begin()) it = (ans[i]).begin(); it != (ans[i]).end(); it++) printf(" %d", *it); puts(""); } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 500005, M = 1e6 + 5; int g[N]; vector<int> a[N], ans[N]; int FIND(int x) { return g[x] == x ? x : g[x] = FIND(g[x]); } void UNION(int x, int y) { g[FIND(x)] = FIND(y); } int main(void) { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) g[i] = i; while (m--) { int x, y; scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x); } int mindeg = n, mdi = 0; for (int i = 1; i <= n; i++) if (((int)(a[i]).size()) < mindeg) { mindeg = ((int)(a[i]).size()); mdi = i; } sort(a[mdi].begin(), a[mdi].end()); for (int i = 1, j = 0; i <= n; i++) { if (j < ((int)(a[mdi]).size()) && a[mdi][j] == i) ++j; else UNION(mdi, i); } for (__typeof((a[mdi]).begin()) it = (a[mdi]).begin(); it != (a[mdi]).end(); it++) sort(a[*it].begin(), a[*it].end()); for (__typeof((a[mdi]).begin()) it = (a[mdi]).begin(); it != (a[mdi]).end(); it++) { for (int i = 1, j = 0; i <= n; i++) { if (j < ((int)(a[*it]).size()) && a[*it][j] == i) ++j; else UNION(*it, i); } } for (int i = 1; i <= n; i++) ans[FIND(i)].push_back(i); int sans = 0; for (int i = 1; i <= n; i++) if (g[i] == i) ++sans; printf("%d\n", sans); for (int i = 1; i <= n; i++) if (((int)(ans[i]).size())) { printf("%d", ((int)(ans[i]).size())); for (__typeof((ans[i]).begin()) it = (ans[i]).begin(); it != (ans[i]).end(); it++) printf(" %d", *it); puts(""); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10, M = 1e6 + 10; vector<int> g[N]; vector<vector<int> > ans; bool mark[N]; set<int> st; int dfs(int v) { mark[v] = 1; int ans = 1; for (auto u : g[v]) { if (!mark[u]) ans += dfs(u); } return ans; } vector<int> bfs(int s) { queue<int> q; vector<int> comp; q.push(s); while (!q.empty()) { int v = q.front(); comp.push_back(v); q.pop(); set<int> others = st; for (auto u : g[v]) { others.erase(u); } for (auto i : others) { q.push(i); st.erase(i); } } return comp; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; i++) { st.insert(i); } if (dfs(1) < n) { cout << 1 << "\n" << n << ' '; for (int i = 1; i <= n; i++) cout << i << ' '; return 0; } while (!st.empty()) { int v = *(st.begin()); st.erase(v); ans.push_back(bfs(v)); } cout << ans.size() << "\n"; for (auto v : ans) { cout << v.size() << ' '; for (auto i : v) cout << i << ' '; cout << "\n"; } }
### Prompt Please create a solution in Cpp to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10, M = 1e6 + 10; vector<int> g[N]; vector<vector<int> > ans; bool mark[N]; set<int> st; int dfs(int v) { mark[v] = 1; int ans = 1; for (auto u : g[v]) { if (!mark[u]) ans += dfs(u); } return ans; } vector<int> bfs(int s) { queue<int> q; vector<int> comp; q.push(s); while (!q.empty()) { int v = q.front(); comp.push_back(v); q.pop(); set<int> others = st; for (auto u : g[v]) { others.erase(u); } for (auto i : others) { q.push(i); st.erase(i); } } return comp; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; i++) { st.insert(i); } if (dfs(1) < n) { cout << 1 << "\n" << n << ' '; for (int i = 1; i <= n; i++) cout << i << ' '; return 0; } while (!st.empty()) { int v = *(st.begin()); st.erase(v); ans.push_back(bfs(v)); } cout << ans.size() << "\n"; for (auto v : ans) { cout << v.size() << ' '; for (auto i : v) cout << i << ' '; cout << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const long long mx = 5000 * 100 + 5; set<long long> ms, msp; vector<long long> ad[mx], cm[mx]; queue<long long> q; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m, u, v, c = 0; cin >> n >> m; for (long long i = 0; i < m; i++) { cin >> u >> v; u--, v--; ad[u].push_back(v); ad[v].push_back(u); } for (long long i = 0; i < n; i++) ms.insert(i); while ((long long)ms.size()) { v = *(ms.begin()); ms.erase(ms.begin()); q.push(v); while ((long long)q.size()) { v = q.front(); cm[c].push_back(v); q.pop(); for (auto u : ad[v]) { if (ms.find(u) == ms.end()) continue; msp.insert(u); ms.erase(u); } while ((long long)ms.size()) { q.push(*ms.begin()); ms.erase(ms.begin()); } ms.swap(msp); } c++; } cout << c << endl; for (long long i = 0; i < c; i++) { cout << (long long)cm[i].size() << " "; for (auto u : cm[i]) cout << u + 1 << " "; cout << endl; } return 0; }
### Prompt Create a solution in CPP for the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mx = 5000 * 100 + 5; set<long long> ms, msp; vector<long long> ad[mx], cm[mx]; queue<long long> q; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m, u, v, c = 0; cin >> n >> m; for (long long i = 0; i < m; i++) { cin >> u >> v; u--, v--; ad[u].push_back(v); ad[v].push_back(u); } for (long long i = 0; i < n; i++) ms.insert(i); while ((long long)ms.size()) { v = *(ms.begin()); ms.erase(ms.begin()); q.push(v); while ((long long)q.size()) { v = q.front(); cm[c].push_back(v); q.pop(); for (auto u : ad[v]) { if (ms.find(u) == ms.end()) continue; msp.insert(u); ms.erase(u); } while ((long long)ms.size()) { q.push(*ms.begin()); ms.erase(ms.begin()); } ms.swap(msp); } c++; } cout << c << endl; for (long long i = 0; i < c; i++) { cout << (long long)cm[i].size() << " "; for (auto u : cm[i]) cout << u + 1 << " "; cout << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 500000 + 100; vector<int> mp[MAXN], ans[MAXN]; int n, m, cnt = 0, f[MAXN]; int F(int x) { return f[x] != x ? f[x] = F(f[x]) : f[x]; } void dfs(int x) { ans[cnt].push_back(x); f[x] = x + 1; for (int j = 0, i = F(1); i <= n; i = F(i + 1)) { j = lower_bound(mp[x].begin(), mp[x].end(), i) - mp[x].begin(); if (j == mp[x].size() || mp[x][j] > i) dfs(i); } } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i <= n + 1; i++) { mp[i].clear(); ans[i].clear(); f[i] = i; } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; mp[x].push_back(y); mp[y].push_back(x); } for (int i = 1; i <= n; i++) sort(mp[i].begin(), mp[i].end()); for (int i = 1; i <= n; i++) if (f[i] == i) { dfs(i); cnt++; } cout << cnt << endl; for (int i = 0; i < cnt; i++) { cout << ans[i].size(); for (int j = 0; j < ans[i].size(); j++) cout << " " << ans[i][j]; cout << endl; } return 0; }
### Prompt Please formulate a cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 500000 + 100; vector<int> mp[MAXN], ans[MAXN]; int n, m, cnt = 0, f[MAXN]; int F(int x) { return f[x] != x ? f[x] = F(f[x]) : f[x]; } void dfs(int x) { ans[cnt].push_back(x); f[x] = x + 1; for (int j = 0, i = F(1); i <= n; i = F(i + 1)) { j = lower_bound(mp[x].begin(), mp[x].end(), i) - mp[x].begin(); if (j == mp[x].size() || mp[x][j] > i) dfs(i); } } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i <= n + 1; i++) { mp[i].clear(); ans[i].clear(); f[i] = i; } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; mp[x].push_back(y); mp[y].push_back(x); } for (int i = 1; i <= n; i++) sort(mp[i].begin(), mp[i].end()); for (int i = 1; i <= n; i++) if (f[i] == i) { dfs(i); cnt++; } cout << cnt << endl; for (int i = 0; i < cnt; i++) { cout << ans[i].size(); for (int j = 0; j < ans[i].size(); j++) cout << " " << ans[i][j]; cout << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 500005, INF = 2000000000000000000; long long power(long long a, long long b, long long p) { if (a == 0) return 0; long long res = 1; a %= p; while (b > 0) { if (b & 1) res = (res * a) % p; b >>= 1; a = (a * a) % p; } return res; } vector<long long> prime; bool isprime[N]; void pre() { for (long long i = 2; i < N; i++) { if (isprime[i]) { for (long long j = i * i; j < N; j += i) isprime[j] = false; prime.push_back(i); } } return; } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long spf[N + 1]; void spfFun() { for (long long i = 0; i <= N; i++) spf[i] = i; for (long long i = 2; i * i <= N; i++) { if (spf[i] == i) { for (long long j = i * i; j <= N; j += i) { spf[j] = min(spf[j], i); } } } } bool isPerfectSquare(long double x) { long double sr = sqrt(x); return ((sr - floor(sr)) == 0); } void print(bool n) { if (n) { cout << "YES"; } else { cout << "NO"; } } vector<long long> par(N), ran(N, 0); vector<long long> g[N]; void init() { for (long long i = 0; i < N; i++) par[i] = i; } long long find(long long v) { if (par[v] == v) { return v; } return par[v] = find(par[v]); } void merge(long long a, long long b) { a = find(a); b = find(b); if (a == b) { return; } if (ran[a] < ran[b]) swap(a, b); par[b] = a; if (ran[a] == ran[b]) ran[a]++; } map<long long, vector<long long>> comp; int32_t main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n, m; cin >> n >> m; for (long long i = 0; i < m; i++) { long long u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } init(); long long mn = INF, el = -1; for (long long i = 1; i <= n; i++) { g[i].push_back(i); if (g[i].size() < mn) { mn = g[i].size(); el = i; } } vector<long long> s = g[el]; s.push_back(el); for (long long i = 1; i <= n; i++) sort((g[i]).begin(), (g[i]).end()); for (auto j : s) { long long sz = g[j].size() - 1; for (long long i = 1; i <= n; i++) { if (g[j][0] > i || g[j][sz] < i) { merge(i, j); continue; } long long l = 0; long long r = sz + 1; while (r > l + 1) { long long mid = (l + r) / 2; if (g[j][mid] <= i) { l = mid; } else { r = mid; } } if (g[j][l] != i) merge(i, j); } } for (long long i = 1; i <= n; i++) { comp[find(i)].push_back(i); } cout << comp.size() << "\n"; for (auto i : comp) { cout << i.second.size() << " "; for (auto j : i.second) cout << j << " "; cout << "\n"; } }
### Prompt Please provide a cpp coded solution to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 500005, INF = 2000000000000000000; long long power(long long a, long long b, long long p) { if (a == 0) return 0; long long res = 1; a %= p; while (b > 0) { if (b & 1) res = (res * a) % p; b >>= 1; a = (a * a) % p; } return res; } vector<long long> prime; bool isprime[N]; void pre() { for (long long i = 2; i < N; i++) { if (isprime[i]) { for (long long j = i * i; j < N; j += i) isprime[j] = false; prime.push_back(i); } } return; } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long spf[N + 1]; void spfFun() { for (long long i = 0; i <= N; i++) spf[i] = i; for (long long i = 2; i * i <= N; i++) { if (spf[i] == i) { for (long long j = i * i; j <= N; j += i) { spf[j] = min(spf[j], i); } } } } bool isPerfectSquare(long double x) { long double sr = sqrt(x); return ((sr - floor(sr)) == 0); } void print(bool n) { if (n) { cout << "YES"; } else { cout << "NO"; } } vector<long long> par(N), ran(N, 0); vector<long long> g[N]; void init() { for (long long i = 0; i < N; i++) par[i] = i; } long long find(long long v) { if (par[v] == v) { return v; } return par[v] = find(par[v]); } void merge(long long a, long long b) { a = find(a); b = find(b); if (a == b) { return; } if (ran[a] < ran[b]) swap(a, b); par[b] = a; if (ran[a] == ran[b]) ran[a]++; } map<long long, vector<long long>> comp; int32_t main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n, m; cin >> n >> m; for (long long i = 0; i < m; i++) { long long u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } init(); long long mn = INF, el = -1; for (long long i = 1; i <= n; i++) { g[i].push_back(i); if (g[i].size() < mn) { mn = g[i].size(); el = i; } } vector<long long> s = g[el]; s.push_back(el); for (long long i = 1; i <= n; i++) sort((g[i]).begin(), (g[i]).end()); for (auto j : s) { long long sz = g[j].size() - 1; for (long long i = 1; i <= n; i++) { if (g[j][0] > i || g[j][sz] < i) { merge(i, j); continue; } long long l = 0; long long r = sz + 1; while (r > l + 1) { long long mid = (l + r) / 2; if (g[j][mid] <= i) { l = mid; } else { r = mid; } } if (g[j][l] != i) merge(i, j); } } for (long long i = 1; i <= n; i++) { comp[find(i)].push_back(i); } cout << comp.size() << "\n"; for (auto i : comp) { cout << i.second.size() << " "; for (auto j : i.second) cout << j << " "; cout << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 1000; vector<int> adj[MAXN], com[MAXN]; int nxt[MAXN]; bool mark[MAXN]; int n, m; int fnxt(int x) { return nxt[x] == x ? x : nxt[x] = fnxt(nxt[x]); } void dfs(int v, int cnt) { mark[v] = true; nxt[v] = v + 1; com[cnt].push_back(v); for (int i = fnxt(1), j = 0; i <= n; i = fnxt(i + 1)) { while (j < adj[v].size() and adj[v][j] < i) j++; if (j == adj[v].size() or i != adj[v][j]) dfs(i, cnt); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n + 1; i++) nxt[i] = i; for (int i = 0, a, b; i < m; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 0; i <= n; i++) sort(adj[i].begin(), adj[i].end()); int cnt = 0; for (int i = fnxt(1); i <= n; i = fnxt(i)) if (!mark[i]) dfs(i, cnt++); cout << cnt << '\n'; for (int i = 0; i < cnt; i++) { cout << com[i].size() << " "; for (auto j : com[i]) cout << j << " "; cout << '\n'; } return 0; }
### Prompt Please create a solution in cpp to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 1000; vector<int> adj[MAXN], com[MAXN]; int nxt[MAXN]; bool mark[MAXN]; int n, m; int fnxt(int x) { return nxt[x] == x ? x : nxt[x] = fnxt(nxt[x]); } void dfs(int v, int cnt) { mark[v] = true; nxt[v] = v + 1; com[cnt].push_back(v); for (int i = fnxt(1), j = 0; i <= n; i = fnxt(i + 1)) { while (j < adj[v].size() and adj[v][j] < i) j++; if (j == adj[v].size() or i != adj[v][j]) dfs(i, cnt); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n + 1; i++) nxt[i] = i; for (int i = 0, a, b; i < m; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 0; i <= n; i++) sort(adj[i].begin(), adj[i].end()); int cnt = 0; for (int i = fnxt(1); i <= n; i = fnxt(i)) if (!mark[i]) dfs(i, cnt++); cout << cnt << '\n'; for (int i = 0; i < cnt; i++) { cout << com[i].size() << " "; for (auto j : com[i]) cout << j << " "; cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; set<int> s; set<pair<int, int> > m; vector<int> ans[500005]; void dfs(int i, int c) { s.erase(i); ans[c].push_back(i); auto it = s.begin(); while (it != s.end()) { int y = *it; if (m.find(make_pair(min(i, y), max(i, y))) == m.end()) dfs(y, c); it = s.upper_bound(y); } } int main() { int n, e, a, b; scanf("%d%d", &n, &e); for (int i = 1; i <= n; i++) s.insert(i); for (int i = 0; i < e; i++) { scanf("%d%d", &a, &b); m.insert(make_pair(min(a, b), max(a, b))); } int c = 0; for (int i = 1; i <= n; i++) { if (s.find(i) != s.end()) dfs(i, c++); } printf("%d\n", c); for (int i = 0; i < c; i++) { printf("%d ", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j]); printf("\n"); } }
### Prompt Please formulate a cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; set<int> s; set<pair<int, int> > m; vector<int> ans[500005]; void dfs(int i, int c) { s.erase(i); ans[c].push_back(i); auto it = s.begin(); while (it != s.end()) { int y = *it; if (m.find(make_pair(min(i, y), max(i, y))) == m.end()) dfs(y, c); it = s.upper_bound(y); } } int main() { int n, e, a, b; scanf("%d%d", &n, &e); for (int i = 1; i <= n; i++) s.insert(i); for (int i = 0; i < e; i++) { scanf("%d%d", &a, &b); m.insert(make_pair(min(a, b), max(a, b))); } int c = 0; for (int i = 1; i <= n; i++) { if (s.find(i) != s.end()) dfs(i, c++); } printf("%d\n", c); for (int i = 0; i < c; i++) { printf("%d ", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j]); printf("\n"); } } ```
#include <bits/stdc++.h> int n, m, c[500001]; std::set<std::pair<int, int>> e; std::set<int> unvis; std::vector<int> ans[500001]; void Bfs(int s) { std::queue<int> q; q.emplace(s); c[s] = s; while (!q.empty()) { int u = q.front(); q.pop(); for (auto it = unvis.begin(); it != unvis.end(); ++it) { int v = *it; if (!c[v] && !e.count({std::min(u, v), std::max(u, v)})) { c[v] = s; q.emplace(v); it = --unvis.erase(it); } } } } int main(int argc, char const *argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); std::cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; std::cin >> u >> v; if (u > v) { std::swap(u, v); } e.emplace(u, v); } for (int i = 1; i <= n; i++) { unvis.emplace(i); } for (int i = 1; i <= n; i++) { if (!c[i]) { Bfs(i); } } for (int i = 1; i <= n; i++) { ans[c[i]].emplace_back(i); } std::cout << std::count_if(ans + 1, ans + n + 1, [](const std::vector<int> &x) -> bool { return x.size() > 0; }) << '\n'; for (int i = 1; i <= n; i++) { if (ans[i].size()) { std::cout << ans[i].size() << ' '; for (auto &&j : ans[i]) { std::cout << j << ' '; } std::cout << '\n'; } } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> int n, m, c[500001]; std::set<std::pair<int, int>> e; std::set<int> unvis; std::vector<int> ans[500001]; void Bfs(int s) { std::queue<int> q; q.emplace(s); c[s] = s; while (!q.empty()) { int u = q.front(); q.pop(); for (auto it = unvis.begin(); it != unvis.end(); ++it) { int v = *it; if (!c[v] && !e.count({std::min(u, v), std::max(u, v)})) { c[v] = s; q.emplace(v); it = --unvis.erase(it); } } } } int main(int argc, char const *argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); std::cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; std::cin >> u >> v; if (u > v) { std::swap(u, v); } e.emplace(u, v); } for (int i = 1; i <= n; i++) { unvis.emplace(i); } for (int i = 1; i <= n; i++) { if (!c[i]) { Bfs(i); } } for (int i = 1; i <= n; i++) { ans[c[i]].emplace_back(i); } std::cout << std::count_if(ans + 1, ans + n + 1, [](const std::vector<int> &x) -> bool { return x.size() > 0; }) << '\n'; for (int i = 1; i <= n; i++) { if (ans[i].size()) { std::cout << ans[i].size() << ' '; for (auto &&j : ans[i]) { std::cout << j << ' '; } std::cout << '\n'; } } return 0; } ```
#include <bits/stdc++.h> const int N = 500010; const int inf = 0x3f3f3f3f; using namespace std; int n, m; int vis[N]; bool q[N]; set<int> st; int l[N]; vector<int> vt[N], ret[N]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); vt[a].push_back(b); vt[b].push_back(a); } int tot = 0; for (int i = 1; i <= n; i++) l[i - 1] = i; l[n] = -1; while (l[0] + 1) { tot++; int u = l[0]; queue<int> que; que.push(u); ret[tot].push_back(u); l[0] = l[u]; while (!que.empty()) { int v = que.front(); que.pop(); int sz = vt[v].size(); for (int i = 0; i < sz; i++) vis[vt[v][i]] = v; for (int pre = 0, i = l[0]; i + 1; pre = i, i = l[i]) { if (vis[i] == v) continue; que.push(i); ret[tot].push_back(i); i = pre; l[i] = l[l[i]]; } } } printf("%d\n", tot); for (int i = 1; i <= tot; i++) { sort(ret[i].begin(), ret[i].end()); int sz = ret[i].size(); printf("%d", sz); for (int j = 0; j < sz; j++) printf(" %d", ret[i][j]); puts(""); } return 0; }
### Prompt Create a solution in Cpp for the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> const int N = 500010; const int inf = 0x3f3f3f3f; using namespace std; int n, m; int vis[N]; bool q[N]; set<int> st; int l[N]; vector<int> vt[N], ret[N]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); vt[a].push_back(b); vt[b].push_back(a); } int tot = 0; for (int i = 1; i <= n; i++) l[i - 1] = i; l[n] = -1; while (l[0] + 1) { tot++; int u = l[0]; queue<int> que; que.push(u); ret[tot].push_back(u); l[0] = l[u]; while (!que.empty()) { int v = que.front(); que.pop(); int sz = vt[v].size(); for (int i = 0; i < sz; i++) vis[vt[v][i]] = v; for (int pre = 0, i = l[0]; i + 1; pre = i, i = l[i]) { if (vis[i] == v) continue; que.push(i); ret[tot].push_back(i); i = pre; l[i] = l[l[i]]; } } } printf("%d\n", tot); for (int i = 1; i <= tot; i++) { sort(ret[i].begin(), ret[i].end()); int sz = ret[i].size(); printf("%d", sz); for (int j = 0; j < sz; j++) printf(" %d", ret[i][j]); puts(""); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main(int argc, char *argv[]) { int n, m, a, b; vector<vector<int> > results; set<pair<int, int> > edges; set<int> nodes; ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> a >> b; a--; b--; edges.insert(make_pair(min(a, b), max(a, b))); } for (int i = 0; i < n; ++i) nodes.insert(i); while (!nodes.empty()) { set<int>::iterator c_it = nodes.begin(); int c = *c_it; nodes.erase(c_it); vector<int> cc; queue<int> q; q.push(c); while (!q.empty()) { c = q.front(); q.pop(); cc.push_back(c); vector<set<int>::iterator> other; for (set<int>::iterator it = nodes.begin(); it != nodes.end(); ++it) { a = min(c, *it); b = max(c, *it); if (edges.count(make_pair(a, b)) == 0) { other.push_back(it); q.push(*it); } } vector<set<int>::iterator>::iterator it; for (it = other.begin(); it != other.end(); ++it) nodes.erase(*it); } results.push_back(cc); } cout << results.size() << endl; for (unsigned int i = 0; i < results.size(); ++i) { cout << results[i].size(); for (unsigned int j = 0; j < results[i].size(); ++j) cout << ' ' << results[i][j] + 1; cout << endl; } return EXIT_SUCCESS; }
### Prompt Please provide a cpp coded solution to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(int argc, char *argv[]) { int n, m, a, b; vector<vector<int> > results; set<pair<int, int> > edges; set<int> nodes; ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> a >> b; a--; b--; edges.insert(make_pair(min(a, b), max(a, b))); } for (int i = 0; i < n; ++i) nodes.insert(i); while (!nodes.empty()) { set<int>::iterator c_it = nodes.begin(); int c = *c_it; nodes.erase(c_it); vector<int> cc; queue<int> q; q.push(c); while (!q.empty()) { c = q.front(); q.pop(); cc.push_back(c); vector<set<int>::iterator> other; for (set<int>::iterator it = nodes.begin(); it != nodes.end(); ++it) { a = min(c, *it); b = max(c, *it); if (edges.count(make_pair(a, b)) == 0) { other.push_back(it); q.push(*it); } } vector<set<int>::iterator>::iterator it; for (it = other.begin(); it != other.end(); ++it) nodes.erase(*it); } results.push_back(cc); } cout << results.size() << endl; for (unsigned int i = 0; i < results.size(); ++i) { cout << results[i].size(); for (unsigned int j = 0; j < results[i].size(); ++j) cout << ' ' << results[i][j] + 1; cout << endl; } return EXIT_SUCCESS; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 5; vector<int> adj[MAXN]; int n, m; int p[MAXN]; vector<int> c[MAXN]; bool is[MAXN]; int root(int v) { if (p[v] == -1) { return v; } return p[v] = root(p[v]); } void merg(int v, int u) { v = root(v); u = root(u); if (c[v].size() < c[u].size()) { swap(u, v); } if (u == v) { return; } for (int i = 0; i < c[u].size(); i++) { c[v].push_back(c[u][i]); } c[u].clear(); p[u] = v; } void mc(int v) { memset(is, 0, sizeof is); for (int i = 0; i < adj[v].size(); i++) { is[adj[v][i]] = 1; } for (int i = 1; i <= n; i++) { if (!is[i]) { merg(v, i); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { p[i] = -1; c[i].push_back(i); } int u, v; for (int i = 0; i < m; i++) { cin >> u >> v; adj[v].push_back(u); adj[u].push_back(v); } int d; int mmin = 5e5 + 6; for (int i = 1; i <= n; i++) { if (adj[i].size() < mmin) { mmin = adj[i].size(); d = i; } } mc(d); for (int i = 0; i < adj[d].size(); i++) { mc(adj[d][i]); } int cnt = 0; for (int i = 1; i <= n; i++) { cnt += (p[i] == -1); } cout << cnt << '\n'; for (int i = 1; i <= n; i++) { if (p[i] == -1) { cout << c[i].size() << ' '; for (int j = 0; j < c[i].size(); j++) { cout << c[i][j] << ' '; } cout << '\n'; } } }
### Prompt Please formulate a cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 5; vector<int> adj[MAXN]; int n, m; int p[MAXN]; vector<int> c[MAXN]; bool is[MAXN]; int root(int v) { if (p[v] == -1) { return v; } return p[v] = root(p[v]); } void merg(int v, int u) { v = root(v); u = root(u); if (c[v].size() < c[u].size()) { swap(u, v); } if (u == v) { return; } for (int i = 0; i < c[u].size(); i++) { c[v].push_back(c[u][i]); } c[u].clear(); p[u] = v; } void mc(int v) { memset(is, 0, sizeof is); for (int i = 0; i < adj[v].size(); i++) { is[adj[v][i]] = 1; } for (int i = 1; i <= n; i++) { if (!is[i]) { merg(v, i); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { p[i] = -1; c[i].push_back(i); } int u, v; for (int i = 0; i < m; i++) { cin >> u >> v; adj[v].push_back(u); adj[u].push_back(v); } int d; int mmin = 5e5 + 6; for (int i = 1; i <= n; i++) { if (adj[i].size() < mmin) { mmin = adj[i].size(); d = i; } } mc(d); for (int i = 0; i < adj[d].size(); i++) { mc(adj[d][i]); } int cnt = 0; for (int i = 1; i <= n; i++) { cnt += (p[i] == -1); } cout << cnt << '\n'; for (int i = 1; i <= n; i++) { if (p[i] == -1) { cout << c[i].size() << ' '; for (int j = 0; j < c[i].size(); j++) { cout << c[i][j] << ' '; } cout << '\n'; } } } ```
#include <bits/stdc++.h> using namespace std; const int N = 5000 * 100 + 5; int n, m, cnt, c; set<int> st, bkst; vector<int> adj[N], ans[N]; bool flag; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; i++) st.insert(i); for (int i = 1, v, u; i <= m; i++) { cin >> v >> u; adj[v].push_back(u); adj[u].push_back(v); } int v; while (!st.empty()) { flag = true; v = *st.begin(); st.erase(v); ans[cnt].push_back(v); c = 0; while (c < ans[cnt].size()) { bkst.clear(); v = ans[cnt][c++]; for (auto u : adj[v]) if (st.find(u) != st.end()) bkst.insert(u), st.erase(u); for (auto u : st) ans[cnt].push_back(u); swap(st, bkst); } cnt++; } cout << cnt << '\n'; for (int i = 0; i < cnt; i++) { cout << ans[i].size() << ' '; for (auto j : ans[i]) cout << j << ' '; cout << '\n'; } }
### Prompt Please formulate a cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 5000 * 100 + 5; int n, m, cnt, c; set<int> st, bkst; vector<int> adj[N], ans[N]; bool flag; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; i++) st.insert(i); for (int i = 1, v, u; i <= m; i++) { cin >> v >> u; adj[v].push_back(u); adj[u].push_back(v); } int v; while (!st.empty()) { flag = true; v = *st.begin(); st.erase(v); ans[cnt].push_back(v); c = 0; while (c < ans[cnt].size()) { bkst.clear(); v = ans[cnt][c++]; for (auto u : adj[v]) if (st.find(u) != st.end()) bkst.insert(u), st.erase(u); for (auto u : st) ans[cnt].push_back(u); swap(st, bkst); } cnt++; } cout << cnt << '\n'; for (int i = 0; i < cnt; i++) { cout << ans[i].size() << ' '; for (auto j : ans[i]) cout << j << ' '; cout << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; const int Distance[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; const int Nmax = 500100; int n, m; vector<vector<int> > g; queue<int> que; set<int> unUsed; int d[Nmax]; inline void add(int vertex, int color) { d[vertex] = color; unUsed.erase(vertex); que.push(vertex); } inline int componentCount() { for (int i = 1; i <= n; i++) { d[i] = 0; unUsed.insert(i); } int comps = 0; for (int vv = 1; vv <= n; vv++) { if (d[vv] > 0) continue; add(vv, ++comps); while (!que.empty()) { int vertex = que.front(); que.pop(); int i = 0; vector<int> good; for (set<int>::iterator it = unUsed.begin(); it != unUsed.end(); it++) { int u = *it; assert(d[u] == 0); while (i < g[vertex].size() && g[vertex][i] < u) i++; if (i == g[vertex].size() || g[vertex][i] != u) good.push_back(u); } for (int i = 0; i < good.size(); i++) add(good[i], d[vertex]); } } return comps; } int main() { ios_base::sync_with_stdio(0); scanf("%d %d", &n, &m); g.resize(n + 1); for (int i = 1; i <= m; i++) { int x, y; scanf("%d %d", &x, &y); g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); int comps = componentCount(); printf("%d\n", comps); g.resize(0); g.resize(n + 1); for (int i = 1; i <= n; i++) g[d[i]].push_back(i); for (int i = 1; i <= comps; i++) { printf("%d", (int)g[i].size()); for (int j = 0; j < g[i].size(); j++) printf(" %d", g[i][j]); printf("\n"); } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int Distance[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; const int Nmax = 500100; int n, m; vector<vector<int> > g; queue<int> que; set<int> unUsed; int d[Nmax]; inline void add(int vertex, int color) { d[vertex] = color; unUsed.erase(vertex); que.push(vertex); } inline int componentCount() { for (int i = 1; i <= n; i++) { d[i] = 0; unUsed.insert(i); } int comps = 0; for (int vv = 1; vv <= n; vv++) { if (d[vv] > 0) continue; add(vv, ++comps); while (!que.empty()) { int vertex = que.front(); que.pop(); int i = 0; vector<int> good; for (set<int>::iterator it = unUsed.begin(); it != unUsed.end(); it++) { int u = *it; assert(d[u] == 0); while (i < g[vertex].size() && g[vertex][i] < u) i++; if (i == g[vertex].size() || g[vertex][i] != u) good.push_back(u); } for (int i = 0; i < good.size(); i++) add(good[i], d[vertex]); } } return comps; } int main() { ios_base::sync_with_stdio(0); scanf("%d %d", &n, &m); g.resize(n + 1); for (int i = 1; i <= m; i++) { int x, y; scanf("%d %d", &x, &y); g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); int comps = componentCount(); printf("%d\n", comps); g.resize(0); g.resize(n + 1); for (int i = 1; i <= n; i++) g[d[i]].push_back(i); for (int i = 1; i <= comps; i++) { printf("%d", (int)g[i].size()); for (int j = 0; j < g[i].size(); j++) printf(" %d", g[i][j]); printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int M = 5 * 100000; const int N = 1e6; vector<int> adj[N]; vector<int> ans[M]; set<int> s; int cnt, n, m; queue<int> q; bool mark[M]; void bfs(int x) { q.push(x); s.erase(x); mark[x] = true; while (!q.empty()) { ans[cnt].push_back(q.front()); int u = q.front(); q.pop(); for (int i = 0; i < adj[u].size(); i++) { s.erase(adj[u][i]); } for (auto i = s.begin(); i != s.end(); i++) { q.push(*i); mark[*i] = true; } s.clear(); for (auto v : adj[u]) { if (!mark[v]) { s.insert(v); } } } cnt++; if (!s.empty()) bfs(*s.begin()); } int main() { ios::sync_with_stdio(false); scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { s.insert(i); } for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--; v--; adj[u].push_back(v); adj[v].push_back(u); } bfs(0); printf("%d\n", cnt); for (int i = 0; i < cnt; i++) { printf("%d ", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) { printf("%d ", ans[i][j] + 1); } printf("\n"); } }
### Prompt Please provide a cpp coded solution to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int M = 5 * 100000; const int N = 1e6; vector<int> adj[N]; vector<int> ans[M]; set<int> s; int cnt, n, m; queue<int> q; bool mark[M]; void bfs(int x) { q.push(x); s.erase(x); mark[x] = true; while (!q.empty()) { ans[cnt].push_back(q.front()); int u = q.front(); q.pop(); for (int i = 0; i < adj[u].size(); i++) { s.erase(adj[u][i]); } for (auto i = s.begin(); i != s.end(); i++) { q.push(*i); mark[*i] = true; } s.clear(); for (auto v : adj[u]) { if (!mark[v]) { s.insert(v); } } } cnt++; if (!s.empty()) bfs(*s.begin()); } int main() { ios::sync_with_stdio(false); scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { s.insert(i); } for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--; v--; adj[u].push_back(v); adj[v].push_back(u); } bfs(0); printf("%d\n", cnt); for (int i = 0; i < cnt; i++) { printf("%d ", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) { printf("%d ", ans[i][j] + 1); } printf("\n"); } } ```
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 7; const int INF = 1e9 + 7; const int MOD = 1e6 + 3; const double Pi = acos(-1.0); const double EPS = 1e-8; int n, m, f[N]; vector<int> e[N]; inline int get(int x) { int r, t = x; while (t != f[t]) t = f[t]; r = t, t = x; while (t != f[t]) x = f[t], f[t] = r, t = x; return r; } inline void merge(int x, int y) { f[get(x)] = get(y); } int col[N << 2]; inline void down(int t) { if (col[t] != 0) { if (!col[((t) << 1)]) col[((t) << 1)] = col[t]; else merge(col[((t) << 1)], col[t]); if (!col[((t) << 1 | 1)]) col[((t) << 1 | 1)] = col[t]; else merge(col[((t) << 1 | 1)], col[t]); } } void build(int t, int l, int r) { if (l == r) { col[t] = l; return; } int m = (l + r) >> 1; col[t] = 0; build(((t) << 1), l, m), build(((t) << 1 | 1), m + 1, r); } inline void upd(int t, int l, int r, int L, int R, int rt) { if (col[t]) { merge(col[t], rt); return; } if (L <= l && r <= R) { if (!col[t]) col[t] = rt; else merge(rt, col[t]); return; } down(t); int m = (l + r) >> 1; if (R <= m) upd(((t) << 1), l, m, L, R, rt); else if (L > m) upd(((t) << 1 | 1), m + 1, r, L, R, rt); else upd(((t) << 1), l, m, L, R, rt), upd(((t) << 1 | 1), m + 1, r, L, R, rt); } void dfs(int t, int l, int r) { if (l == r) return; down(t); int m = (l + r) >> 1; dfs(((t) << 1), l, m), dfs(((t) << 1 | 1), m + 1, r); } int main() { scanf("%d%d", &n, &m); for (int i = (0); i < (m); ++i) { int u, v; scanf("%d%d", &u, &v); e[u].push_back(v), e[v].push_back(u); } for (int i = (1); i < (n + 1); ++i) f[i] = i, sort(e[i].begin(), e[i].end()); build(1, 1, n); for (int u = (1); u < (n + 1); ++u) { int last = 0; for (int j = (0); j < (((int)(e[u]).size())); ++j) { if (last + 1 <= e[u][j] - 1) upd(1, 1, n, last + 1, e[u][j] - 1, u); last = e[u][j]; } if (last + 1 <= n) upd(1, 1, n, last + 1, n, u); } dfs(1, 1, n); int k = 0; for (int i = (1); i < (n + 1); ++i) k += i == get(i), e[i].clear(); for (int i = (1); i < (n + 1); ++i) e[get(i)].push_back(i); printf("%d\n", k); for (int i = (1); i < (n + 1); ++i) if (((int)(e[i]).size())) { printf("%d", ((int)(e[i]).size())); for (int j = (0); j < (((int)(e[i]).size())); ++j) printf(" %d", e[i][j]); puts(""); } return 0; }
### Prompt Develop a solution in CPP to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 7; const int INF = 1e9 + 7; const int MOD = 1e6 + 3; const double Pi = acos(-1.0); const double EPS = 1e-8; int n, m, f[N]; vector<int> e[N]; inline int get(int x) { int r, t = x; while (t != f[t]) t = f[t]; r = t, t = x; while (t != f[t]) x = f[t], f[t] = r, t = x; return r; } inline void merge(int x, int y) { f[get(x)] = get(y); } int col[N << 2]; inline void down(int t) { if (col[t] != 0) { if (!col[((t) << 1)]) col[((t) << 1)] = col[t]; else merge(col[((t) << 1)], col[t]); if (!col[((t) << 1 | 1)]) col[((t) << 1 | 1)] = col[t]; else merge(col[((t) << 1 | 1)], col[t]); } } void build(int t, int l, int r) { if (l == r) { col[t] = l; return; } int m = (l + r) >> 1; col[t] = 0; build(((t) << 1), l, m), build(((t) << 1 | 1), m + 1, r); } inline void upd(int t, int l, int r, int L, int R, int rt) { if (col[t]) { merge(col[t], rt); return; } if (L <= l && r <= R) { if (!col[t]) col[t] = rt; else merge(rt, col[t]); return; } down(t); int m = (l + r) >> 1; if (R <= m) upd(((t) << 1), l, m, L, R, rt); else if (L > m) upd(((t) << 1 | 1), m + 1, r, L, R, rt); else upd(((t) << 1), l, m, L, R, rt), upd(((t) << 1 | 1), m + 1, r, L, R, rt); } void dfs(int t, int l, int r) { if (l == r) return; down(t); int m = (l + r) >> 1; dfs(((t) << 1), l, m), dfs(((t) << 1 | 1), m + 1, r); } int main() { scanf("%d%d", &n, &m); for (int i = (0); i < (m); ++i) { int u, v; scanf("%d%d", &u, &v); e[u].push_back(v), e[v].push_back(u); } for (int i = (1); i < (n + 1); ++i) f[i] = i, sort(e[i].begin(), e[i].end()); build(1, 1, n); for (int u = (1); u < (n + 1); ++u) { int last = 0; for (int j = (0); j < (((int)(e[u]).size())); ++j) { if (last + 1 <= e[u][j] - 1) upd(1, 1, n, last + 1, e[u][j] - 1, u); last = e[u][j]; } if (last + 1 <= n) upd(1, 1, n, last + 1, n, u); } dfs(1, 1, n); int k = 0; for (int i = (1); i < (n + 1); ++i) k += i == get(i), e[i].clear(); for (int i = (1); i < (n + 1); ++i) e[get(i)].push_back(i); printf("%d\n", k); for (int i = (1); i < (n + 1); ++i) if (((int)(e[i]).size())) { printf("%d", ((int)(e[i]).size())); for (int j = (0); j < (((int)(e[i]).size())); ++j) printf(" %d", e[i][j]); puts(""); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mxn = 1e6 + 5; set<pair<int, int> > st; set<int> a; vector<int> ans[mxn]; int n, m, x, y, t, de[mxn]; void bfs(int v) { int s = *a.begin(); queue<int> q; ans[t].push_back(v); a.erase(v); q.push(v); while (q.size()) { int u = q.front(); q.pop(); int j = 0; for (int i : a) { if (!st.count({min(i, u), max(i, u)})) { de[j++] = i; ans[t].push_back(i); q.push(i); } } for (int i = 0; i < j; i++) a.erase(de[i]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> x >> y; st.insert({min(x, y), max(x, y)}); } for (int i = 1; i <= n; i++) { a.insert(i); } while (a.size()) { bfs(*a.begin()); t++; } cout << t << "\n"; for (int i = 0; i < t; i++) { cout << ans[i].size() << " "; for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << " "; cout << "\n"; } return 0; }
### Prompt Create a solution in CPP for the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mxn = 1e6 + 5; set<pair<int, int> > st; set<int> a; vector<int> ans[mxn]; int n, m, x, y, t, de[mxn]; void bfs(int v) { int s = *a.begin(); queue<int> q; ans[t].push_back(v); a.erase(v); q.push(v); while (q.size()) { int u = q.front(); q.pop(); int j = 0; for (int i : a) { if (!st.count({min(i, u), max(i, u)})) { de[j++] = i; ans[t].push_back(i); q.push(i); } } for (int i = 0; i < j; i++) a.erase(de[i]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> x >> y; st.insert({min(x, y), max(x, y)}); } for (int i = 1; i <= n; i++) { a.insert(i); } while (a.size()) { bfs(*a.begin()); t++; } cout << t << "\n"; for (int i = 0; i < t; i++) { cout << ans[i].size() << " "; for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << " "; cout << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 100; vector<int> vv[N], tmp; int f[N]; void fun(int x, int y) { int ex = vv[x].size(); int ey = vv[y].size(); tmp.clear(); for (int i = 0, j = 0; i < ex; i++) { while (j < ey && vv[y][j] < vv[x][i]) j++; if (j < ey && vv[y][j] == vv[x][i]) { tmp.push_back(vv[x][i]); } if (j == ey) break; } int ed = tmp.size(); vv[y].clear(); for (int i = 0; i < ed; i++) { vv[y].push_back(tmp[i]); } f[x] = y; } int main() { int n, m; while (scanf("%d%d", &n, &m) != EOF) { for (int i = 1; i <= n; i++) { vv[i].clear(); f[i] = i; } for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); vv[a].push_back(b); vv[b].push_back(a); } for (int i = 1; i <= n; i++) sort(vv[i].begin(), vv[i].end()); for (int i = 1; i <= n; i++) { int k = 0; int ed = vv[i].size(); for (int j = i + 1; j <= n; j++) { while (k < ed && vv[i][k] < j) k++; if (k == ed || vv[i][k] > j) { fun(i, j); break; } } } int cnt = 0; for (int i = 1; i <= n; i++) { if (f[i] == i) cnt++; } printf("%d\n", cnt); for (int i = 1; i <= n; i++) if (f[i] == i) { int ed = vv[i].size(); printf("%d", n - ed); int k = 0; for (int j = 1; j <= n; j++) { while (k < ed && vv[i][k] < j) k++; if (k == ed || vv[i][k] > j) printf(" %d", j); } printf("\n"); } } return 0; }
### Prompt Please create a solution in CPP to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 100; vector<int> vv[N], tmp; int f[N]; void fun(int x, int y) { int ex = vv[x].size(); int ey = vv[y].size(); tmp.clear(); for (int i = 0, j = 0; i < ex; i++) { while (j < ey && vv[y][j] < vv[x][i]) j++; if (j < ey && vv[y][j] == vv[x][i]) { tmp.push_back(vv[x][i]); } if (j == ey) break; } int ed = tmp.size(); vv[y].clear(); for (int i = 0; i < ed; i++) { vv[y].push_back(tmp[i]); } f[x] = y; } int main() { int n, m; while (scanf("%d%d", &n, &m) != EOF) { for (int i = 1; i <= n; i++) { vv[i].clear(); f[i] = i; } for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); vv[a].push_back(b); vv[b].push_back(a); } for (int i = 1; i <= n; i++) sort(vv[i].begin(), vv[i].end()); for (int i = 1; i <= n; i++) { int k = 0; int ed = vv[i].size(); for (int j = i + 1; j <= n; j++) { while (k < ed && vv[i][k] < j) k++; if (k == ed || vv[i][k] > j) { fun(i, j); break; } } } int cnt = 0; for (int i = 1; i <= n; i++) { if (f[i] == i) cnt++; } printf("%d\n", cnt); for (int i = 1; i <= n; i++) if (f[i] == i) { int ed = vv[i].size(); printf("%d", n - ed); int k = 0; for (int j = 1; j <= n; j++) { while (k < ed && vv[i][k] < j) k++; if (k == ed || vv[i][k] > j) printf(" %d", j); } printf("\n"); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int M = 500000 + 10; vector<int> adj[M]; set<int> S; vector<int> compo[M]; queue<int> Q; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) { if (!adj[i].empty()) { sort(adj[i].begin(), adj[i].end()); } S.insert(i); } int cnt = 0; while (!S.empty()) { set<int>::iterator it = S.begin(); int cur = (*it); S.erase(cur); Q.push(cur); compo[cnt].push_back(cur); while (!Q.empty()) { int head = Q.front(); Q.pop(); vector<int> added; for (it = S.begin(); it != S.end(); it++) { int nxt = (*it); vector<int>::iterator tid = lower_bound(adj[head].begin(), adj[head].end(), nxt); if (tid == adj[head].end() || (*tid) != nxt) { Q.push(nxt); compo[cnt].push_back(nxt); added.push_back(nxt); } } for (vector<int>::iterator it = added.begin(); it != added.end(); it++) { S.erase(*it); } } cnt++; } cout << cnt << endl; for (int i = 0; i < cnt; i++) { int isize = compo[i].size(); cout << isize << " "; for (vector<int>::iterator it = compo[i].begin(); it != compo[i].end(); it++) { cout << (*it) << " "; } cout << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int M = 500000 + 10; vector<int> adj[M]; set<int> S; vector<int> compo[M]; queue<int> Q; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) { if (!adj[i].empty()) { sort(adj[i].begin(), adj[i].end()); } S.insert(i); } int cnt = 0; while (!S.empty()) { set<int>::iterator it = S.begin(); int cur = (*it); S.erase(cur); Q.push(cur); compo[cnt].push_back(cur); while (!Q.empty()) { int head = Q.front(); Q.pop(); vector<int> added; for (it = S.begin(); it != S.end(); it++) { int nxt = (*it); vector<int>::iterator tid = lower_bound(adj[head].begin(), adj[head].end(), nxt); if (tid == adj[head].end() || (*tid) != nxt) { Q.push(nxt); compo[cnt].push_back(nxt); added.push_back(nxt); } } for (vector<int>::iterator it = added.begin(); it != added.end(); it++) { S.erase(*it); } } cnt++; } cout << cnt << endl; for (int i = 0; i < cnt; i++) { int isize = compo[i].size(); cout << isize << " "; for (vector<int>::iterator it = compo[i].begin(); it != compo[i].end(); it++) { cout << (*it) << " "; } cout << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 5e5 + 5; vector<int> v[N], tmp; vector<vector<int>> ans; set<int> st; bool ok(int node, int x) { return !binary_search(v[node].begin(), v[node].end(), x); } void dfs(int cur) { tmp.push_back(cur); vector<int> let; for (int x : st) { if (ok(cur, x)) { let.push_back(x); } } for (int x : let) { st.erase(x); } for (int x : let) { dfs(x); } } inline void solve() { int n, m, l, r; cin >> n >> m; for (int i = 1; i <= m; ++i) { cin >> l >> r; v[l].push_back(r); v[r].push_back(l); } for (int i = 1; i <= n; ++i) { sort(v[i].begin(), v[i].end()); st.insert(i); } while (!st.empty()) { int before = ((int)st.size()); int store = *st.begin(); st.erase(st.begin()); dfs(store); int after = ((int)st.size()); if (before - after) { ans.push_back(tmp); } tmp.clear(); } cout << ((int)ans.size()) << '\n'; for (vector<int> v : ans) { cout << ((int)v.size()) << ' '; for (int x : v) { cout << x << ' '; } cout << '\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; solve(); return 0; }
### Prompt In cpp, your task is to solve the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 5e5 + 5; vector<int> v[N], tmp; vector<vector<int>> ans; set<int> st; bool ok(int node, int x) { return !binary_search(v[node].begin(), v[node].end(), x); } void dfs(int cur) { tmp.push_back(cur); vector<int> let; for (int x : st) { if (ok(cur, x)) { let.push_back(x); } } for (int x : let) { st.erase(x); } for (int x : let) { dfs(x); } } inline void solve() { int n, m, l, r; cin >> n >> m; for (int i = 1; i <= m; ++i) { cin >> l >> r; v[l].push_back(r); v[r].push_back(l); } for (int i = 1; i <= n; ++i) { sort(v[i].begin(), v[i].end()); st.insert(i); } while (!st.empty()) { int before = ((int)st.size()); int store = *st.begin(); st.erase(st.begin()); dfs(store); int after = ((int)st.size()); if (before - after) { ans.push_back(tmp); } tmp.clear(); } cout << ((int)ans.size()) << '\n'; for (vector<int> v : ans) { cout << ((int)v.size()) << ' '; for (int x : v) { cout << x << ' '; } cout << '\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 500 * 1000 + 10, maxm = (1 << 17), mod = (int)1e9 + 7, hash = 7000001, inf = (1 << 29); const double pi = 3.14159265359, ee = 2.71828; struct node { int par, sz; bool mark; vector<int> g; } v[maxn]; vector<int> comp[maxn]; set<int> ch; int father(int u) { if (v[u].par == -1) return u; return v[u].par = father(v[u].par); } void merge(int a, int b) { a = father(a), b = father(b); if (a == b) return; if (v[a].sz < v[b].sz) swap(a, b); v[a].sz += v[b].sz, v[b].par = a; } int main() { ios::sync_with_stdio(0); int n, m, u = 0, a, b, c; cin >> n >> m; for (int i = 0; i < n; i++) v[i].par = -1, v[i].sz = 1; for (int i = 0; i < m; i++) cin >> a >> b, a--, b--, v[a].g.push_back(b), v[b].g.push_back(a); for (int i = 0; i < n; i++) { if (v[i].g.size() < v[u].g.size()) u = i; sort(v[i].g.begin(), v[i].g.end()); } for (int i = 0; i < v[u].g.size(); i++) v[v[u].g[i]].mark = 1; for (int i = 0; i < n; i++) if (!v[i].mark && u != i) merge(i, u); for (int i = 0; i < v[u].g.size(); i++) { a = v[u].g[i], c = v[a].g.size(); for (int j = 0; j < v[a].g.size(); j++) if (v[v[a].g[j]].mark) c--; if (c < n - v[u].g.size()) merge(u, a); for (int j = i + 1; j < v[u].g.size(); j++) { b = v[u].g[j]; c = lower_bound(v[a].g.begin(), v[a].g.end(), b) - v[a].g.begin(); if (c == v[a].g.size()) merge(a, b); else if (v[a].g[c] != b) merge(a, b); } } for (int i = 0; i < n; i++) a = father(i), comp[a].push_back(i + 1), ch.insert(a); cout << ch.size() << endl; while (ch.size()) { u = *ch.begin(), ch.erase(ch.begin()); cout << comp[u].size() << " "; for (int i = 0; i < comp[u].size(); i++) cout << comp[u][i] << " "; cout << endl; } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 500 * 1000 + 10, maxm = (1 << 17), mod = (int)1e9 + 7, hash = 7000001, inf = (1 << 29); const double pi = 3.14159265359, ee = 2.71828; struct node { int par, sz; bool mark; vector<int> g; } v[maxn]; vector<int> comp[maxn]; set<int> ch; int father(int u) { if (v[u].par == -1) return u; return v[u].par = father(v[u].par); } void merge(int a, int b) { a = father(a), b = father(b); if (a == b) return; if (v[a].sz < v[b].sz) swap(a, b); v[a].sz += v[b].sz, v[b].par = a; } int main() { ios::sync_with_stdio(0); int n, m, u = 0, a, b, c; cin >> n >> m; for (int i = 0; i < n; i++) v[i].par = -1, v[i].sz = 1; for (int i = 0; i < m; i++) cin >> a >> b, a--, b--, v[a].g.push_back(b), v[b].g.push_back(a); for (int i = 0; i < n; i++) { if (v[i].g.size() < v[u].g.size()) u = i; sort(v[i].g.begin(), v[i].g.end()); } for (int i = 0; i < v[u].g.size(); i++) v[v[u].g[i]].mark = 1; for (int i = 0; i < n; i++) if (!v[i].mark && u != i) merge(i, u); for (int i = 0; i < v[u].g.size(); i++) { a = v[u].g[i], c = v[a].g.size(); for (int j = 0; j < v[a].g.size(); j++) if (v[v[a].g[j]].mark) c--; if (c < n - v[u].g.size()) merge(u, a); for (int j = i + 1; j < v[u].g.size(); j++) { b = v[u].g[j]; c = lower_bound(v[a].g.begin(), v[a].g.end(), b) - v[a].g.begin(); if (c == v[a].g.size()) merge(a, b); else if (v[a].g[c] != b) merge(a, b); } } for (int i = 0; i < n; i++) a = father(i), comp[a].push_back(i + 1), ch.insert(a); cout << ch.size() << endl; while (ch.size()) { u = *ch.begin(), ch.erase(ch.begin()); cout << comp[u].size() << " "; for (int i = 0; i < comp[u].size(); i++) cout << comp[u][i] << " "; cout << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxl = 5 * 1000 * 100 + 5; vector<int> v[maxl]; int n, m; bool flg; set<int> deg[2]; bool marked[maxl]; int res; vector<int> ans[maxl]; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; v[a].push_back(b); v[b].push_back(a); } for (int i = 0; i < n; i++) deg[0].insert(i); for (int i = 0; i < n; i++) if (!marked[i]) { queue<int> q; q.push(i); deg[flg].erase(i); marked[i] = true; while (!q.empty()) { int node = q.front(); ans[res].push_back(node); for (int i = 0; i < v[node].size(); i++) { int index = v[node][i]; if (!marked[index]) { deg[flg].erase(index); deg[(!flg)].insert(index); } } for (set<int>::iterator j = deg[flg].begin(); j != deg[flg].end(); j++) { marked[*j] = true; q.push(*j); } deg[flg].clear(); flg = !flg; q.pop(); } res++; } printf("%d\n", res); for (int i = 0; i < res; i++) { printf("%d", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) printf(" %d", ans[i][j] + 1); printf("\n"); } }
### Prompt Create a solution in CPP for the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxl = 5 * 1000 * 100 + 5; vector<int> v[maxl]; int n, m; bool flg; set<int> deg[2]; bool marked[maxl]; int res; vector<int> ans[maxl]; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; v[a].push_back(b); v[b].push_back(a); } for (int i = 0; i < n; i++) deg[0].insert(i); for (int i = 0; i < n; i++) if (!marked[i]) { queue<int> q; q.push(i); deg[flg].erase(i); marked[i] = true; while (!q.empty()) { int node = q.front(); ans[res].push_back(node); for (int i = 0; i < v[node].size(); i++) { int index = v[node][i]; if (!marked[index]) { deg[flg].erase(index); deg[(!flg)].insert(index); } } for (set<int>::iterator j = deg[flg].begin(); j != deg[flg].end(); j++) { marked[*j] = true; q.push(*j); } deg[flg].clear(); flg = !flg; q.pop(); } res++; } printf("%d\n", res); for (int i = 0; i < res; i++) { printf("%d", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) printf(" %d", ans[i][j] + 1); printf("\n"); } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; set<int> unused; for (int i = 0; i < n; i++) { unused.insert(i); } vector<vector<int>> adj(n); vector<int> check(n, 0); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--, v--; adj[u].emplace_back(v); adj[v].emplace_back(u); } auto BFS = [&](int s) { vector<int> cur; cur.emplace_back(s); for (int i = 0; i < (int)cur.size(); i++) { unused.erase(cur[i]); for (auto v : adj[cur[i]]) check[v]++; if (i == cur.size() - 1) { int m = (int)cur.size(); for (auto &v : unused) { if (check[v] != m) { cur.emplace_back(v); } } } } for (auto &u : cur) { for (auto &v : adj[u]) { check[v] = 0; } } return cur; }; vector<vector<int>> res; for (int i = 0; i < n; i++) { if (unused.count(i)) { auto cur = BFS(i); res.emplace_back(cur); } } cout << res.size() << '\n'; for (int i = 0; i < res.size(); i++) { cout << res[i].size(); for (int j = 0; j < res[i].size(); j++) { cout << ' ' << res[i][j] + 1; } cout << '\n'; } }
### Prompt Please create a solution in Cpp to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; set<int> unused; for (int i = 0; i < n; i++) { unused.insert(i); } vector<vector<int>> adj(n); vector<int> check(n, 0); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--, v--; adj[u].emplace_back(v); adj[v].emplace_back(u); } auto BFS = [&](int s) { vector<int> cur; cur.emplace_back(s); for (int i = 0; i < (int)cur.size(); i++) { unused.erase(cur[i]); for (auto v : adj[cur[i]]) check[v]++; if (i == cur.size() - 1) { int m = (int)cur.size(); for (auto &v : unused) { if (check[v] != m) { cur.emplace_back(v); } } } } for (auto &u : cur) { for (auto &v : adj[u]) { check[v] = 0; } } return cur; }; vector<vector<int>> res; for (int i = 0; i < n; i++) { if (unused.count(i)) { auto cur = BFS(i); res.emplace_back(cur); } } cout << res.size() << '\n'; for (int i = 0; i < res.size(); i++) { cout << res[i].size(); for (int j = 0; j < res[i].size(); j++) { cout << ' ' << res[i][j] + 1; } cout << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } set<int> s; set<int>::iterator it; vector<vector<int> > com; int q[500500], qf, qb; int head[999983], nxt[500500 * 2], E; long long to[500500 * 2]; void add(int u, int v) { if (u > v) swap(u, v); long long val = (long long)u * 500500 + v; int id = val % 999983; E++; nxt[E] = head[id]; to[E] = val; head[id] = E; } bool is_con(int u, int v) { if (u > v) swap(u, v); long long val = (long long)u * 500500 + v; int id = val % 999983; for (int e = head[id]; e; e = nxt[e]) { if (to[e] == val) return true; } return false; } void bfs(int u) { qf = qb = 0; q[qb++] = u; s.erase(u); com.back().push_back(u); while (qf < qb) { u = q[qf++]; for (it = s.begin(); it != s.end();) { int v = *it; if (!is_con(u, v)) { s.erase(v); com.back().push_back(v); q[qb++] = v; it = s.lower_bound(v); } else it++; } } } void inline getint(int &a) { a = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') { a = (a << 3) + (a << 1) + c - '0'; c = getchar(); } } void inline outint(int a) { if (a >= 10) outint(a / 10); putchar(a % 10 + '0'); } int main() { int n, m, u, v; cin >> n >> m; for (int i = 0; i < m; i++) { getint(u), getint(v); add(u, v); } for (int i = 1; i <= n; i++) s.insert(i); while (!s.empty()) { int u = *s.begin(); com.push_back(vector<int>(0)); bfs(u); } printf("%d\n", com.size()); for (int i = com.size(); i--;) { outint(com[i].size()); for (int j = com[i].size(); j--;) putchar(' '), outint(com[i][j]); puts(""); } return 0; }
### Prompt Generate a cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } set<int> s; set<int>::iterator it; vector<vector<int> > com; int q[500500], qf, qb; int head[999983], nxt[500500 * 2], E; long long to[500500 * 2]; void add(int u, int v) { if (u > v) swap(u, v); long long val = (long long)u * 500500 + v; int id = val % 999983; E++; nxt[E] = head[id]; to[E] = val; head[id] = E; } bool is_con(int u, int v) { if (u > v) swap(u, v); long long val = (long long)u * 500500 + v; int id = val % 999983; for (int e = head[id]; e; e = nxt[e]) { if (to[e] == val) return true; } return false; } void bfs(int u) { qf = qb = 0; q[qb++] = u; s.erase(u); com.back().push_back(u); while (qf < qb) { u = q[qf++]; for (it = s.begin(); it != s.end();) { int v = *it; if (!is_con(u, v)) { s.erase(v); com.back().push_back(v); q[qb++] = v; it = s.lower_bound(v); } else it++; } } } void inline getint(int &a) { a = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') { a = (a << 3) + (a << 1) + c - '0'; c = getchar(); } } void inline outint(int a) { if (a >= 10) outint(a / 10); putchar(a % 10 + '0'); } int main() { int n, m, u, v; cin >> n >> m; for (int i = 0; i < m; i++) { getint(u), getint(v); add(u, v); } for (int i = 1; i <= n; i++) s.insert(i); while (!s.empty()) { int u = *s.begin(); com.push_back(vector<int>(0)); bfs(u); } printf("%d\n", com.size()); for (int i = com.size(); i--;) { outint(com[i].size()); for (int j = com[i].size(); j--;) putchar(' '), outint(com[i][j]); puts(""); } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int mod = 1e9 + 7; const double eps = 1e-6; const int N = 5e5 + 7; vector<int> G[N], res[N]; set<int> st; int sum; void bfs(int u) { queue<int> q; q.push(u); while (q.size()) { int f = q.front(); q.pop(); res[sum].push_back(f); auto it = st.begin(); for (; it != st.end();) { int t = *it; it++; if (!binary_search(G[f].begin(), G[f].end(), t)) { st.erase(t); q.push(t); } } } } signed main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int first, second; cin >> first >> second; G[first].push_back(second); G[second].push_back(first); } for (int i = 1; i <= n; i++) { st.insert(i); sort(G[i].begin(), G[i].end()); } while (st.size()) { int t = *(st.begin()); st.erase(t); sum++; bfs(t); } cout << sum << endl; for (int i = 1; i <= sum; i++) { cout << res[i].size() << " "; for (int t : res[i]) cout << t << " "; cout << endl; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int mod = 1e9 + 7; const double eps = 1e-6; const int N = 5e5 + 7; vector<int> G[N], res[N]; set<int> st; int sum; void bfs(int u) { queue<int> q; q.push(u); while (q.size()) { int f = q.front(); q.pop(); res[sum].push_back(f); auto it = st.begin(); for (; it != st.end();) { int t = *it; it++; if (!binary_search(G[f].begin(), G[f].end(), t)) { st.erase(t); q.push(t); } } } } signed main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int first, second; cin >> first >> second; G[first].push_back(second); G[second].push_back(first); } for (int i = 1; i <= n; i++) { st.insert(i); sort(G[i].begin(), G[i].end()); } while (st.size()) { int t = *(st.begin()); st.erase(t); sum++; bfs(t); } cout << sum << endl; for (int i = 1; i <= sum; i++) { cout << res[i].size() << " "; for (int t : res[i]) cout << t << " "; cout << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 5 * 100 * 1000 + 14; vector<int> comp[MAXN], adj[MAXN]; set<int> res; int ans = 0; void dfs(int v) { comp[ans].push_back(v); auto it = res.begin(); while (it != res.end()) { if (find(adj[v].begin(), adj[v].end(), *it) == adj[v].end()) { int u = *it; res.erase(u); dfs(u); it = res.upper_bound(u); } else it++; } return; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; while (m--) { int u, v; cin >> u >> v; adj[v].push_back(u); adj[u].push_back(v); } for (int i = 1; i <= n; i++) { res.insert(i); sort(adj[i].begin(), adj[i].end()); } while (!res.empty()) { int v = *res.begin(); res.erase(v); ans++; dfs(v); } cout << ans << '\n'; for (int i = 1; i <= ans; i++) { cout << comp[i].size() << ' '; for (auto u : comp[i]) cout << u << ' '; cout << '\n'; } return 0; }
### Prompt Please create a solution in CPP to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 5 * 100 * 1000 + 14; vector<int> comp[MAXN], adj[MAXN]; set<int> res; int ans = 0; void dfs(int v) { comp[ans].push_back(v); auto it = res.begin(); while (it != res.end()) { if (find(adj[v].begin(), adj[v].end(), *it) == adj[v].end()) { int u = *it; res.erase(u); dfs(u); it = res.upper_bound(u); } else it++; } return; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; while (m--) { int u, v; cin >> u >> v; adj[v].push_back(u); adj[u].push_back(v); } for (int i = 1; i <= n; i++) { res.insert(i); sort(adj[i].begin(), adj[i].end()); } while (!res.empty()) { int v = *res.begin(); res.erase(v); ans++; dfs(v); } cout << ans << '\n'; for (int i = 1; i <= ans; i++) { cout << comp[i].size() << ' '; for (auto u : comp[i]) cout << u << ' '; cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; int n, m, comps; set<pair<int, int> > roads; set<int> nods, components[N]; set<int>::iterator it; bool not_exist(int v, int u) { if (v > u) swap(v, u); if (roads.find({v, u}) == roads.end()) return true; else return false; } void dfs(int v) { vector<int> tmp; for (auto i : nods) if (not_exist(v, i)) tmp.push_back(i); for (auto i : tmp) nods.erase(i); for (auto i : tmp) dfs(i); components[comps].insert(v); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) nods.insert(i); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); if (u > v) swap(u, v); roads.insert({u, v}); } for (int i = 1; i <= n; i++) if (nods.find(i) != nods.end()) { dfs(i); comps++; } printf("%d\n", comps); for (int i = 0; i < comps; i++) { printf("%d", components[i].size()); for (auto j : components[i]) printf(" %d", j); printf("\n"); } return 0; }
### Prompt In Cpp, your task is to solve the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; int n, m, comps; set<pair<int, int> > roads; set<int> nods, components[N]; set<int>::iterator it; bool not_exist(int v, int u) { if (v > u) swap(v, u); if (roads.find({v, u}) == roads.end()) return true; else return false; } void dfs(int v) { vector<int> tmp; for (auto i : nods) if (not_exist(v, i)) tmp.push_back(i); for (auto i : tmp) nods.erase(i); for (auto i : tmp) dfs(i); components[comps].insert(v); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) nods.insert(i); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); if (u > v) swap(u, v); roads.insert({u, v}); } for (int i = 1; i <= n; i++) if (nods.find(i) != nods.end()) { dfs(i); comps++; } printf("%d\n", comps); for (int i = 0; i < comps; i++) { printf("%d", components[i].size()); for (auto j : components[i]) printf(" %d", j); printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 3; unordered_set<int> adj[N], rem; vector<int> comp[N]; vector<int> del; queue<int> q; int conn, n, m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> m; for (int i = 1; i <= n; i++) rem.insert(i); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].insert(v); adj[v].insert(u); } while (!rem.empty()) { int i = *(rem.begin()); conn++; q.push(i); rem.erase(i); comp[conn].push_back(i); while (!q.empty()) { int x = q.front(); q.pop(); for (auto it : rem) { if (adj[x].find(it) == adj[x].end()) { q.push(it); comp[conn].push_back(it); del.push_back(it); } } for (auto it : del) rem.erase(it); del.clear(); } } cout << conn << "\n"; for (int i = 1; i <= conn; i++) { cout << comp[i].size() << " "; for (auto it : comp[i]) cout << it << " "; cout << "\n"; } }
### Prompt Construct a Cpp code solution to the problem outlined: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 3; unordered_set<int> adj[N], rem; vector<int> comp[N]; vector<int> del; queue<int> q; int conn, n, m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> m; for (int i = 1; i <= n; i++) rem.insert(i); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].insert(v); adj[v].insert(u); } while (!rem.empty()) { int i = *(rem.begin()); conn++; q.push(i); rem.erase(i); comp[conn].push_back(i); while (!q.empty()) { int x = q.front(); q.pop(); for (auto it : rem) { if (adj[x].find(it) == adj[x].end()) { q.push(it); comp[conn].push_back(it); del.push_back(it); } } for (auto it : del) rem.erase(it); del.clear(); } } cout << conn << "\n"; for (int i = 1; i <= conn; i++) { cout << comp[i].size() << " "; for (auto it : comp[i]) cout << it << " "; cout << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; const int maxn5 = 5e5 + 10; const int maxnt = 1.2e6 + 10; const int maxn3 = 1e3 + 10; const long long mod = 1e9 + 7; const long long inf = 2e18; int st, cmp[maxn5], nxt[maxn5], pre[maxn5]; vector<int> adj[maxn5], ver[maxn5]; bool mark[maxn5]; void join(int a, int b) { if (ver[a].size() > ver[b].size()) swap(a, b); for (auto u : ver[a]) { cmp[u] = b; ver[b].push_back(u); } if (a == st) { st = nxt[a]; pre[nxt[a]] = -1; } else { if (nxt[a] != -1) pre[nxt[a]] = pre[a]; nxt[pre[a]] = nxt[a]; } ver[a].clear(); return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= n; i++) { cmp[i] = i; ver[i].push_back(i); pre[i] = i == 1 ? -1 : i - 1; nxt[i] = i == n ? -1 : i + 1; } st = 1; for (int i = 1; i <= n; i++) { int ind = st; for (auto u : adj[i]) mark[u] = true; while (ind != -1) { if (ind == cmp[i]) { ind = nxt[ind]; continue; } bool done = false; for (auto u : ver[ind]) if (!mark[u]) { int c = ind; ind = nxt[ind]; done = true; join(c, cmp[i]); break; } if (!done) ind = nxt[ind]; } for (auto u : adj[i]) mark[u] = false; } int cnt = 0, ind = st; while (ind != -1) { cnt++; ind = nxt[ind]; } cout << cnt << '\n'; while (st != -1) { cout << ver[st].size() << ' '; for (auto u : ver[st]) cout << u << ' '; cout << '\n'; st = nxt[st]; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; const int maxn5 = 5e5 + 10; const int maxnt = 1.2e6 + 10; const int maxn3 = 1e3 + 10; const long long mod = 1e9 + 7; const long long inf = 2e18; int st, cmp[maxn5], nxt[maxn5], pre[maxn5]; vector<int> adj[maxn5], ver[maxn5]; bool mark[maxn5]; void join(int a, int b) { if (ver[a].size() > ver[b].size()) swap(a, b); for (auto u : ver[a]) { cmp[u] = b; ver[b].push_back(u); } if (a == st) { st = nxt[a]; pre[nxt[a]] = -1; } else { if (nxt[a] != -1) pre[nxt[a]] = pre[a]; nxt[pre[a]] = nxt[a]; } ver[a].clear(); return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= n; i++) { cmp[i] = i; ver[i].push_back(i); pre[i] = i == 1 ? -1 : i - 1; nxt[i] = i == n ? -1 : i + 1; } st = 1; for (int i = 1; i <= n; i++) { int ind = st; for (auto u : adj[i]) mark[u] = true; while (ind != -1) { if (ind == cmp[i]) { ind = nxt[ind]; continue; } bool done = false; for (auto u : ver[ind]) if (!mark[u]) { int c = ind; ind = nxt[ind]; done = true; join(c, cmp[i]); break; } if (!done) ind = nxt[ind]; } for (auto u : adj[i]) mark[u] = false; } int cnt = 0, ind = st; while (ind != -1) { cnt++; ind = nxt[ind]; } cout << cnt << '\n'; while (st != -1) { cout << ver[st].size() << ' '; for (auto u : ver[st]) cout << u << ' '; cout << '\n'; st = nxt[st]; } return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> ans[(int)5e5 + 2]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> adj[n]; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; adj[x].push_back(y); adj[y].push_back(x); } set<int> S, X, Y; int k = -1; for (int i = 0; i < n; i++) Y.insert(i); while (!Y.empty() || !X.empty()) { if (X.empty()) { k++; X.insert(*Y.begin()); Y.erase(*Y.begin()); } int p = *X.begin(); ans[k].push_back(p + 1); set<int> YP; for (int i = 0; i < adj[p].size(); i++) { if (Y.find(adj[p][i]) != Y.end()) { YP.insert(adj[p][i]); Y.erase(adj[p][i]); } } for (set<int>::iterator it = Y.begin(); it != Y.end(); it++) { X.insert(*it); } Y = YP; X.erase(p); } cout << k + 1 << '\n'; for (int i = 0; i <= k; i++) { cout << ans[i].size() << ' '; for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << ' '; cout << '\n'; } return 0; }
### Prompt Create a solution in Cpp for the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> ans[(int)5e5 + 2]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> adj[n]; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; adj[x].push_back(y); adj[y].push_back(x); } set<int> S, X, Y; int k = -1; for (int i = 0; i < n; i++) Y.insert(i); while (!Y.empty() || !X.empty()) { if (X.empty()) { k++; X.insert(*Y.begin()); Y.erase(*Y.begin()); } int p = *X.begin(); ans[k].push_back(p + 1); set<int> YP; for (int i = 0; i < adj[p].size(); i++) { if (Y.find(adj[p][i]) != Y.end()) { YP.insert(adj[p][i]); Y.erase(adj[p][i]); } } for (set<int>::iterator it = Y.begin(); it != Y.end(); it++) { X.insert(*it); } Y = YP; X.erase(p); } cout << k + 1 << '\n'; for (int i = 0; i <= k; i++) { cout << ans[i].size() << ' '; for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << ' '; cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int lim = 1e4, stop = 128, nmax = 5e5 + 42; int n, m; bitset<lim> edge[lim]; vector<vector<int> > outp; bitset<nmax> been, emp; void dfs(int node) { if (been[node]) return; been[node] = 1; outp[outp.size() - 1].push_back(node); for (int i = 1; i <= n; i++) if (edge[node][i] && been[i] == 0) dfs(i); } void print() { printf("%i\n", outp.size()); for (auto k : outp) { printf("%i", k.size()); for (auto p : k) printf(" %i", p); printf("\n"); } exit(0); } void slow() { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) edge[i][j] = 1; int x, y; for (int i = 1; i <= m; i++) { scanf("%i%i", &x, &y); edge[x][y] = 0; edge[y][x] = 0; } for (int i = 1; i <= n; i++) if (been[i] == 0) { outp.push_back({}); dfs(i); } print(); } bool out[nmax]; vector<int> adj[nmax]; vector<int> now; int NOW_SZ = 0; void second_dfs(int node) { if (been[node] || out[node]) return; if (NOW_SZ >= stop) return; been[node] = 1; now.push_back(node); NOW_SZ++; int pos = 0, SZ = adj[node].size(); for (int i = 1; i <= n; i++) { if (NOW_SZ >= stop) break; if (pos < SZ && adj[node][pos] == i) { pos++; continue; } second_dfs(i); } } int main() { scanf("%i%i", &n, &m); if (n < lim) slow(); int x, y; for (int i = 1; i <= m; i++) { scanf("%i%i", &x, &y); adj[x].push_back(y); adj[y].push_back(x); } for (int i = 1; i <= n; i++) sort(adj[i].begin(), adj[i].end()); for (int i = 1; i <= n; i++) if (out[i] == 0 && n - adj[i].size() <= stop) { second_dfs(i); if (NOW_SZ < stop && NOW_SZ) { outp.push_back(now); for (auto k : now) out[k] = 1; } NOW_SZ = 0; now = {}; been = emp; } for (int i = 1; i <= n; i++) if (out[i] == 0) now.push_back(i); if (now.size()) outp.push_back(now); print(); return 0; }
### Prompt Develop a solution in CPP to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int lim = 1e4, stop = 128, nmax = 5e5 + 42; int n, m; bitset<lim> edge[lim]; vector<vector<int> > outp; bitset<nmax> been, emp; void dfs(int node) { if (been[node]) return; been[node] = 1; outp[outp.size() - 1].push_back(node); for (int i = 1; i <= n; i++) if (edge[node][i] && been[i] == 0) dfs(i); } void print() { printf("%i\n", outp.size()); for (auto k : outp) { printf("%i", k.size()); for (auto p : k) printf(" %i", p); printf("\n"); } exit(0); } void slow() { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) edge[i][j] = 1; int x, y; for (int i = 1; i <= m; i++) { scanf("%i%i", &x, &y); edge[x][y] = 0; edge[y][x] = 0; } for (int i = 1; i <= n; i++) if (been[i] == 0) { outp.push_back({}); dfs(i); } print(); } bool out[nmax]; vector<int> adj[nmax]; vector<int> now; int NOW_SZ = 0; void second_dfs(int node) { if (been[node] || out[node]) return; if (NOW_SZ >= stop) return; been[node] = 1; now.push_back(node); NOW_SZ++; int pos = 0, SZ = adj[node].size(); for (int i = 1; i <= n; i++) { if (NOW_SZ >= stop) break; if (pos < SZ && adj[node][pos] == i) { pos++; continue; } second_dfs(i); } } int main() { scanf("%i%i", &n, &m); if (n < lim) slow(); int x, y; for (int i = 1; i <= m; i++) { scanf("%i%i", &x, &y); adj[x].push_back(y); adj[y].push_back(x); } for (int i = 1; i <= n; i++) sort(adj[i].begin(), adj[i].end()); for (int i = 1; i <= n; i++) if (out[i] == 0 && n - adj[i].size() <= stop) { second_dfs(i); if (NOW_SZ < stop && NOW_SZ) { outp.push_back(now); for (auto k : now) out[k] = 1; } NOW_SZ = 0; now = {}; been = emp; } for (int i = 1; i <= n; i++) if (out[i] == 0) now.push_back(i); if (now.size()) outp.push_back(now); print(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 5 * 1e5 + 7; int n, m; vector<int> g[maxn]; bool vis[maxn]; vector<int> ans[maxn]; int co; struct node { int u; node *nxt; } * head; int sum; void del(node *cmp) { cmp->nxt = cmp->nxt->nxt; } void bfs() { co++; queue<int> q; node *cmp; cmp = head; q.push(cmp->nxt->u); del(head); int u; while (!q.empty()) { u = q.front(); q.pop(); ans[co].push_back(u); for (node *sb = head; sb->nxt != NULL;) { if (!binary_search(g[u].begin(), g[u].end(), sb->nxt->u)) { q.push(sb->nxt->u); del(sb); } else sb = sb->nxt; } } sum += ans[co].size(); } void add_node(int u) { node *cmp = new node; cmp->u = u; cmp->nxt = head->nxt; head->nxt = cmp; } int main() { scanf("%d %d", &n, &m); int u, v; head = new node; head->nxt = NULL; for (int i = 1; i <= n; i++) add_node(i); for (int i = 0; i < m; i++) { scanf("%d %d", &u, &v); g[u].push_back(v), g[v].push_back(u); } for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); co = 0; while (sum < n) bfs(); printf("%d\n", co); for (int i = 1; i <= co; i++) { printf("%d ", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) { printf("%d ", ans[i][j]); } printf("\n"); } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 5 * 1e5 + 7; int n, m; vector<int> g[maxn]; bool vis[maxn]; vector<int> ans[maxn]; int co; struct node { int u; node *nxt; } * head; int sum; void del(node *cmp) { cmp->nxt = cmp->nxt->nxt; } void bfs() { co++; queue<int> q; node *cmp; cmp = head; q.push(cmp->nxt->u); del(head); int u; while (!q.empty()) { u = q.front(); q.pop(); ans[co].push_back(u); for (node *sb = head; sb->nxt != NULL;) { if (!binary_search(g[u].begin(), g[u].end(), sb->nxt->u)) { q.push(sb->nxt->u); del(sb); } else sb = sb->nxt; } } sum += ans[co].size(); } void add_node(int u) { node *cmp = new node; cmp->u = u; cmp->nxt = head->nxt; head->nxt = cmp; } int main() { scanf("%d %d", &n, &m); int u, v; head = new node; head->nxt = NULL; for (int i = 1; i <= n; i++) add_node(i); for (int i = 0; i < m; i++) { scanf("%d %d", &u, &v); g[u].push_back(v), g[v].push_back(u); } for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); co = 0; while (sum < n) bfs(); printf("%d\n", co); for (int i = 1; i <= co; i++) { printf("%d ", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) { printf("%d ", ans[i][j]); } printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const double eps = 1e-6; const int mod = (int)1e9 + 7; const int N = 4 * (int)1e4 + 5; const int sz = (int)1 << 19; int cnt = 0, x, y, n, m, hm; vector<int> comp[500500], g[500500]; bitset<500500> b; int t[sz + sz + 100]; inline void upd(int x, int delta) { for (t[x += sz] = delta; x >>= 1;) t[x] = min(t[x + x], t[x + x + 1]); } inline int get(int l, int r) { int res = int(1e9); for (l += sz, r += sz; l <= r; l >>= 1, r >>= 1) { if (l & 1) res = min(res, t[l++]); if (!(r & 1)) res = min(res, t[r--]); if (l > r) break; } return res; } void dfs(int v) { upd(v, int(1e9)); comp[cnt].push_back(v); for (int i = get(1, n); i != int(1e9); i = get(i + 1, n)) { if (binary_search(g[v].begin(), g[v].end(), i)) continue; dfs(i); } } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; ++i) upd(i, i); for (int i = 1; i <= n; ++i) { sort(g[i].begin(), g[i].end()); } while (get(1, n) != int(1e9)) { dfs(get(1, n)); ++cnt; } cout << cnt << "\n"; for (int i = 0; i < cnt; ++i) { cout << comp[i].size() << " "; for (int j = 0; j < comp[i].size(); ++j) { cout << comp[i][j] << " "; } cout << "\n"; } }
### Prompt Please create a solution in CPP to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const double eps = 1e-6; const int mod = (int)1e9 + 7; const int N = 4 * (int)1e4 + 5; const int sz = (int)1 << 19; int cnt = 0, x, y, n, m, hm; vector<int> comp[500500], g[500500]; bitset<500500> b; int t[sz + sz + 100]; inline void upd(int x, int delta) { for (t[x += sz] = delta; x >>= 1;) t[x] = min(t[x + x], t[x + x + 1]); } inline int get(int l, int r) { int res = int(1e9); for (l += sz, r += sz; l <= r; l >>= 1, r >>= 1) { if (l & 1) res = min(res, t[l++]); if (!(r & 1)) res = min(res, t[r--]); if (l > r) break; } return res; } void dfs(int v) { upd(v, int(1e9)); comp[cnt].push_back(v); for (int i = get(1, n); i != int(1e9); i = get(i + 1, n)) { if (binary_search(g[v].begin(), g[v].end(), i)) continue; dfs(i); } } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; ++i) upd(i, i); for (int i = 1; i <= n; ++i) { sort(g[i].begin(), g[i].end()); } while (get(1, n) != int(1e9)) { dfs(get(1, n)); ++cnt; } cout << cnt << "\n"; for (int i = 0; i < cnt; ++i) { cout << comp[i].size() << " "; for (int j = 0; j < comp[i].size(); ++j) { cout << comp[i][j] << " "; } cout << "\n"; } } ```
#include <bits/stdc++.h> const int NMAX = 100666; using namespace std; const int INF = 1000000007; vector<int> G[500555]; set<int> S; bool viz[500555]; vector<int> comps[500555]; int q[500555]; int a, b, n, m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { scanf("%d%d", &a, &b); G[a].push_back(b); G[b].push_back(a); } int c = 0; for (int i = 1; i <= n; ++i) S.insert(i), sort(G[i].begin(), G[i].end()); queue<int> q; while (!S.empty()) { int i = *(S.begin()); S.erase(S.begin()); q.push(i); c++; while (!q.empty()) { int curr = q.front(); q.pop(); comps[c].push_back(curr); vector<int> ver; for (set<int>::iterator it = S.begin(); it != S.end(); ++it) { int u = *it; if (!binary_search(G[curr].begin(), G[curr].end(), u)) { q.push(u); ver.push_back(u); } } for (auto el : ver) S.erase(el); } } printf("%d\n", c); for (int i = 1; i <= c; ++i) { printf("%d ", comps[i].size()); for (auto el : comps[i]) printf("%d ", el); printf("\n"); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> const int NMAX = 100666; using namespace std; const int INF = 1000000007; vector<int> G[500555]; set<int> S; bool viz[500555]; vector<int> comps[500555]; int q[500555]; int a, b, n, m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { scanf("%d%d", &a, &b); G[a].push_back(b); G[b].push_back(a); } int c = 0; for (int i = 1; i <= n; ++i) S.insert(i), sort(G[i].begin(), G[i].end()); queue<int> q; while (!S.empty()) { int i = *(S.begin()); S.erase(S.begin()); q.push(i); c++; while (!q.empty()) { int curr = q.front(); q.pop(); comps[c].push_back(curr); vector<int> ver; for (set<int>::iterator it = S.begin(); it != S.end(); ++it) { int u = *it; if (!binary_search(G[curr].begin(), G[curr].end(), u)) { q.push(u); ver.push_back(u); } } for (auto el : ver) S.erase(el); } } printf("%d\n", c); for (int i = 1; i <= c; ++i) { printf("%d ", comps[i].size()); for (auto el : comps[i]) printf("%d ", el); printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; long long int t, n, m; set<long long int> vis; queue<long long int> q; vector<long long int> temp, v[500005], g[500005]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int i, j, k, x, y; cin >> n >> m; for (i = 1; i <= m; i++) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (i = 1; i <= n; i++) { sort(g[i].begin(), g[i].end()); vis.insert(i); } long long int comp = 0; for (i = 1; i <= n; i++) { if (vis.find(i) == vis.end()) continue; else x = i; vis.erase(x); q.push(x); comp++; while (!q.empty()) { x = q.front(); q.pop(); v[comp].push_back(x); temp.clear(); for (auto it : vis) { if (!binary_search(g[x].begin(), g[x].end(), it)) { temp.push_back(it); q.push(it); } } for (auto it : temp) vis.erase(it); } } cout << comp << '\n'; for (i = 1; i <= comp; i++) { cout << v[i].size() << " "; for (auto it : v[i]) cout << it << " "; cout << '\n'; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; long long int t, n, m; set<long long int> vis; queue<long long int> q; vector<long long int> temp, v[500005], g[500005]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int i, j, k, x, y; cin >> n >> m; for (i = 1; i <= m; i++) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (i = 1; i <= n; i++) { sort(g[i].begin(), g[i].end()); vis.insert(i); } long long int comp = 0; for (i = 1; i <= n; i++) { if (vis.find(i) == vis.end()) continue; else x = i; vis.erase(x); q.push(x); comp++; while (!q.empty()) { x = q.front(); q.pop(); v[comp].push_back(x); temp.clear(); for (auto it : vis) { if (!binary_search(g[x].begin(), g[x].end(), it)) { temp.push_back(it); q.push(it); } } for (auto it : temp) vis.erase(it); } } cout << comp << '\n'; for (i = 1; i <= comp; i++) { cout << v[i].size() << " "; for (auto it : v[i]) cout << it << " "; cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 500001; int n, m, k, l, pv; set<int> s; queue<int> q; vector<int> g[MAXN], jj[MAXN], co; void bfs(int v) { q.push(v); s.erase(v); jj[pv].push_back(v); int x; while (!q.empty()) { x = q.front(); q.pop(); for (int itt : s) if (lower_bound(g[x].begin(), g[x].end(), itt) == g[x].end()) { q.push(itt); co.push_back(itt); jj[pv].push_back(itt); } else if (*lower_bound(g[x].begin(), g[x].end(), itt) != itt) { q.push(itt); co.push_back(itt); jj[pv].push_back(itt); } for (int i = 0; i < co.size(); i++) s.erase(co[i]); co.clear(); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) s.insert(i); for (int i = 0; i < m; i++) { scanf("%d%d", &k, &l); g[k].push_back(l), g[l].push_back(k); } for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); while (s.begin() != s.end()) { bfs(*s.begin()); pv++; } printf("%d\n", pv); for (int i = 0; i < pv; i++) { printf("%d ", jj[i].size()); for (int j = 0; j < jj[i].size(); j++) printf("%d ", jj[i][j]); printf("\n"); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 500001; int n, m, k, l, pv; set<int> s; queue<int> q; vector<int> g[MAXN], jj[MAXN], co; void bfs(int v) { q.push(v); s.erase(v); jj[pv].push_back(v); int x; while (!q.empty()) { x = q.front(); q.pop(); for (int itt : s) if (lower_bound(g[x].begin(), g[x].end(), itt) == g[x].end()) { q.push(itt); co.push_back(itt); jj[pv].push_back(itt); } else if (*lower_bound(g[x].begin(), g[x].end(), itt) != itt) { q.push(itt); co.push_back(itt); jj[pv].push_back(itt); } for (int i = 0; i < co.size(); i++) s.erase(co[i]); co.clear(); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) s.insert(i); for (int i = 0; i < m; i++) { scanf("%d%d", &k, &l); g[k].push_back(l), g[l].push_back(k); } for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); while (s.begin() != s.end()) { bfs(*s.begin()); pv++; } printf("%d\n", pv); for (int i = 0; i < pv; i++) { printf("%d ", jj[i].size()); for (int j = 0; j < jj[i].size(); j++) printf("%d ", jj[i][j]); printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 1e9; const long long LNF = 1e18; void setIO(string s) { ios_base::sync_with_stdio(0); cin.tie(0); } const int N = 5e5 + 7; set<int> unvis; vector<int> adj[N]; vector<vector<int> > ans; void dfs(int u) { ans.back().emplace_back(u); for (auto it = unvis.begin(); it != unvis.end();) { if (!binary_search(begin(adj[u]), end(adj[u]), *it)) { int x = *it; unvis.erase(it); dfs(x); it = unvis.upper_bound(x); } else ++it; } } int main() { setIO("input"); int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u, --v; adj[u].emplace_back(v), adj[v].emplace_back(u); } for (int i = 0; i < n; ++i) unvis.emplace(i), sort(begin(adj[i]), end(adj[i])); while (!unvis.empty()) { ans.emplace_back(); int u = *unvis.begin(); unvis.erase(unvis.begin()); dfs(u); } cout << (int)ans.size() << '\n'; for (auto& i : ans) { cout << (int)i.size() << '\n'; for (auto& j : i) cout << j + 1 << ' '; cout << '\n'; } }
### Prompt Your task is to create a cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 1e9; const long long LNF = 1e18; void setIO(string s) { ios_base::sync_with_stdio(0); cin.tie(0); } const int N = 5e5 + 7; set<int> unvis; vector<int> adj[N]; vector<vector<int> > ans; void dfs(int u) { ans.back().emplace_back(u); for (auto it = unvis.begin(); it != unvis.end();) { if (!binary_search(begin(adj[u]), end(adj[u]), *it)) { int x = *it; unvis.erase(it); dfs(x); it = unvis.upper_bound(x); } else ++it; } } int main() { setIO("input"); int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u, --v; adj[u].emplace_back(v), adj[v].emplace_back(u); } for (int i = 0; i < n; ++i) unvis.emplace(i), sort(begin(adj[i]), end(adj[i])); while (!unvis.empty()) { ans.emplace_back(); int u = *unvis.begin(); unvis.erase(unvis.begin()); dfs(u); } cout << (int)ans.size() << '\n'; for (auto& i : ans) { cout << (int)i.size() << '\n'; for (auto& j : i) cout << j + 1 << ' '; cout << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 1000 + 100; set<int> head; int par[MAXN], n, m, mn = MAXN, maxD, sz[MAXN]; vector<int> n_def, nb[MAXN]; void init() { for (int i = 0; i < n; i++) { par[i] = i; head.insert(i); sz[i] = 1; } } int find_par(int x) { if (x == par[x]) return x; return par[x] = find_par(par[x]); } void join(int x, int y) { x = find_par(x); y = find_par(y); if (x == y) return; if (sz[x] <= sz[y]) { par[x] = y; head.erase(x); sz[y] += sz[x]; } else { par[y] = x; head.erase(y); sz[x] += sz[y]; } return; } void find_max_comp() { for (int i = 0; i < n; i++) if (nb[i].size() < mn) { mn = nb[i].size(); maxD = i; } for (int i = 0; i < n; i++) if (!(binary_search(nb[maxD].begin(), nb[maxD].end(), i))) join(i, maxD); else n_def.push_back(i); } void input() { cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> v >> u; v--; u--; nb[v].push_back(u); nb[u].push_back(v); } for (int i = 0; i < n; i++) sort(nb[i].begin(), nb[i].end()); } int main() { ios::sync_with_stdio(0); input(); init(); find_max_comp(); for (int i = 0; i < n_def.size(); i++) { int cur = n_def[i]; for (int j = 0; j < n; j++) if (!(binary_search(nb[cur].begin(), nb[cur].end(), j))) join(n_def[i], j); } cout << head.size() << endl; for (set<int>::iterator it = head.begin(); it != head.end(); it++) { cout << sz[(*it)] << ' '; for (int i = 0; i < n; i++) if (find_par(i) == (*it)) cout << i + 1 << ' '; cout << endl; } }
### Prompt Please formulate a Cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 1000 + 100; set<int> head; int par[MAXN], n, m, mn = MAXN, maxD, sz[MAXN]; vector<int> n_def, nb[MAXN]; void init() { for (int i = 0; i < n; i++) { par[i] = i; head.insert(i); sz[i] = 1; } } int find_par(int x) { if (x == par[x]) return x; return par[x] = find_par(par[x]); } void join(int x, int y) { x = find_par(x); y = find_par(y); if (x == y) return; if (sz[x] <= sz[y]) { par[x] = y; head.erase(x); sz[y] += sz[x]; } else { par[y] = x; head.erase(y); sz[x] += sz[y]; } return; } void find_max_comp() { for (int i = 0; i < n; i++) if (nb[i].size() < mn) { mn = nb[i].size(); maxD = i; } for (int i = 0; i < n; i++) if (!(binary_search(nb[maxD].begin(), nb[maxD].end(), i))) join(i, maxD); else n_def.push_back(i); } void input() { cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> v >> u; v--; u--; nb[v].push_back(u); nb[u].push_back(v); } for (int i = 0; i < n; i++) sort(nb[i].begin(), nb[i].end()); } int main() { ios::sync_with_stdio(0); input(); init(); find_max_comp(); for (int i = 0; i < n_def.size(); i++) { int cur = n_def[i]; for (int j = 0; j < n; j++) if (!(binary_search(nb[cur].begin(), nb[cur].end(), j))) join(n_def[i], j); } cout << head.size() << endl; for (set<int>::iterator it = head.begin(); it != head.end(); it++) { cout << sz[(*it)] << ' '; for (int i = 0; i < n; i++) if (find_par(i) == (*it)) cout << i + 1 << ' '; cout << endl; } } ```
#include <bits/stdc++.h> using namespace std; struct edge { int u, v; edge(){}; edge(int u, int v) : u(u), v(v){}; } e[1000100 * 2]; int qu[5 * 1000010]; int nex[5 * 1000010], pre[5 * 1000010], connect[5 * 1000010], start[5 * 1000010], stop[5 * 1000010]; bool fr[5 * 1000010]; vector<vector<int> > ans; int N, M; bool cmp(edge x, edge y) { return x.u < y.u || (x.u == y.u && x.v < y.v); } void list_remove(int u) { pre[nex[u]] = pre[u]; nex[pre[u]] = nex[u]; } bool _find(int l, int r, int value) { while (l < r) { int mid = (l + r) >> 1; if (e[mid].v == value) return true; if (e[mid].v >= value) r = mid; else l = mid + 1; } return e[l].v == value; } void bfs(int u, vector<int> &ans) { list_remove(u); queue<int> qu; qu.push(u); fr[u] = false; ans.push_back(u); while (!qu.empty()) { u = qu.front(); qu.pop(); if (connect[u] == 0) { int v = nex[0]; while (v != N + 1) { fr[v] = false; list_remove(v); ans.push_back(v); qu.push(v); v = nex[v]; } } else { int v = nex[0]; while (v != N + 1) { bool found = _find(start[u], stop[u], v); if (!found) { fr[v] = false; list_remove(v); ans.push_back(v); qu.push(v); } v = nex[v]; } } } } int main() { scanf("%d %d", &N, &M); for (int i = 1; i <= M; ++i) { int u, v; scanf("%d %d", &u, &v); e[i] = edge(u, v); e[i + M] = edge(v, u); } sort(e + 1, e + M * 2 + 1, cmp); int ii = 1; int jj = 2, dem = 1; e[M * 2 + 1].v = 0x7f7f7f7f; while (ii <= M * 2 && jj <= M * 2 + 1) { if (e[ii].u == e[jj].u) ++jj, ++dem; else { connect[e[ii].u] = dem; start[e[ii].u] = ii; stop[e[ii].u] = jj - 1; ii = jj; dem = 1; } } for (int i = 0; i <= N; ++i) nex[i] = i + 1, pre[i + 1] = i; memset(fr, true, sizeof(fr)); for (int i = 1; i <= N; ++i) if (fr[i]) { ans.push_back(vector<int>()); bfs(i, ans.back()); } printf("%d", ans.size()); for (int i = 0; i <= int(ans.size()) - 1; ++i) { printf("\n%d ", ans[i].size()); for (int j = 0; j <= int(ans[i].size()) - 1; ++j) printf("%d ", ans[i][j]); } return 0; }
### Prompt Create a solution in CPP for the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct edge { int u, v; edge(){}; edge(int u, int v) : u(u), v(v){}; } e[1000100 * 2]; int qu[5 * 1000010]; int nex[5 * 1000010], pre[5 * 1000010], connect[5 * 1000010], start[5 * 1000010], stop[5 * 1000010]; bool fr[5 * 1000010]; vector<vector<int> > ans; int N, M; bool cmp(edge x, edge y) { return x.u < y.u || (x.u == y.u && x.v < y.v); } void list_remove(int u) { pre[nex[u]] = pre[u]; nex[pre[u]] = nex[u]; } bool _find(int l, int r, int value) { while (l < r) { int mid = (l + r) >> 1; if (e[mid].v == value) return true; if (e[mid].v >= value) r = mid; else l = mid + 1; } return e[l].v == value; } void bfs(int u, vector<int> &ans) { list_remove(u); queue<int> qu; qu.push(u); fr[u] = false; ans.push_back(u); while (!qu.empty()) { u = qu.front(); qu.pop(); if (connect[u] == 0) { int v = nex[0]; while (v != N + 1) { fr[v] = false; list_remove(v); ans.push_back(v); qu.push(v); v = nex[v]; } } else { int v = nex[0]; while (v != N + 1) { bool found = _find(start[u], stop[u], v); if (!found) { fr[v] = false; list_remove(v); ans.push_back(v); qu.push(v); } v = nex[v]; } } } } int main() { scanf("%d %d", &N, &M); for (int i = 1; i <= M; ++i) { int u, v; scanf("%d %d", &u, &v); e[i] = edge(u, v); e[i + M] = edge(v, u); } sort(e + 1, e + M * 2 + 1, cmp); int ii = 1; int jj = 2, dem = 1; e[M * 2 + 1].v = 0x7f7f7f7f; while (ii <= M * 2 && jj <= M * 2 + 1) { if (e[ii].u == e[jj].u) ++jj, ++dem; else { connect[e[ii].u] = dem; start[e[ii].u] = ii; stop[e[ii].u] = jj - 1; ii = jj; dem = 1; } } for (int i = 0; i <= N; ++i) nex[i] = i + 1, pre[i + 1] = i; memset(fr, true, sizeof(fr)); for (int i = 1; i <= N; ++i) if (fr[i]) { ans.push_back(vector<int>()); bfs(i, ans.back()); } printf("%d", ans.size()); for (int i = 0; i <= int(ans.size()) - 1; ++i) { printf("\n%d ", ans[i].size()); for (int j = 0; j <= int(ans[i].size()) - 1; ++j) printf("%d ", ans[i][j]); } return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> ans[500010]; set<pair<int, int> > edges; set<int> unused; queue<int> q; int id = 1; void BFS() { if (q.empty()) return; int u = q.front(); q.pop(); for (set<int>::iterator it = unused.begin(); it != unused.end();) { set<int>::iterator next = it; next++; if (!edges.count({min(u, *it), max(u, *it)})) { q.push(*it); ans[id].push_back(*it); unused.erase(it); } it = next; } BFS(); } int main() { ios_base::sync_with_stdio(0); int n, m, u, v; scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d%d", &u, &v); if (u > v) swap(u, v); edges.insert({u, v}); } for (int i = 1; i <= n; i++) unused.insert(i); while (unused.size()) { set<int>::iterator it = unused.begin(); q.push(*it); ans[id].push_back(*it); unused.erase(it); BFS(); id++; } printf("%d\n", id - 1); for (int i = 1; i < id; i++) { printf("%d ", int(ans[i].size())); for (int j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j]); printf("\n"); } }
### Prompt Generate a CPP solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> ans[500010]; set<pair<int, int> > edges; set<int> unused; queue<int> q; int id = 1; void BFS() { if (q.empty()) return; int u = q.front(); q.pop(); for (set<int>::iterator it = unused.begin(); it != unused.end();) { set<int>::iterator next = it; next++; if (!edges.count({min(u, *it), max(u, *it)})) { q.push(*it); ans[id].push_back(*it); unused.erase(it); } it = next; } BFS(); } int main() { ios_base::sync_with_stdio(0); int n, m, u, v; scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d%d", &u, &v); if (u > v) swap(u, v); edges.insert({u, v}); } for (int i = 1; i <= n; i++) unused.insert(i); while (unused.size()) { set<int>::iterator it = unused.begin(); q.push(*it); ans[id].push_back(*it); unused.erase(it); BFS(); id++; } printf("%d\n", id - 1); for (int i = 1; i < id; i++) { printf("%d ", int(ans[i].size())); for (int j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j]); printf("\n"); } } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 10; int c[MAXN]; vector<int> g[MAXN], com[MAXN], ans, r; inline void mer(int v, int u) { if (c[v] == c[u]) return; v = c[v]; u = c[u]; if (com[v].size() > com[u].size()) swap(v, u); while (!com[v].empty()) { int t = com[v].back(); com[v].pop_back(); com[u].push_back(t); c[t] = u; } } inline void add(int v) { unordered_map<int, int> m; for (int i = 0; i < g[v].size(); i++) { int u = g[v][i]; if (c[u] == c[v]) continue; u = c[u]; m[u]++; } for (int i = 0; i < r.size(); i++) { int x = r[i]; if (m[x] < com[x].size()) mer(v, x); } if (c[v] == v) r.push_back(v); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { c[i] = i; com[i].push_back(i); } for (int i = 1; i <= m; i++) { int v, u; scanf("%d%d", &v, &u); g[v].push_back(u); g[u].push_back(v); } for (int i = 1; i <= n; i++) add(i); for (int i = 1; i <= n; i++) if (!com[i].empty()) ans.push_back(i); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { int p = ans[i]; printf("%d ", (int)com[p].size()); for (int j = 0; j < com[p].size(); j++) printf("%d ", com[p][j]); printf("\n"); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 10; int c[MAXN]; vector<int> g[MAXN], com[MAXN], ans, r; inline void mer(int v, int u) { if (c[v] == c[u]) return; v = c[v]; u = c[u]; if (com[v].size() > com[u].size()) swap(v, u); while (!com[v].empty()) { int t = com[v].back(); com[v].pop_back(); com[u].push_back(t); c[t] = u; } } inline void add(int v) { unordered_map<int, int> m; for (int i = 0; i < g[v].size(); i++) { int u = g[v][i]; if (c[u] == c[v]) continue; u = c[u]; m[u]++; } for (int i = 0; i < r.size(); i++) { int x = r[i]; if (m[x] < com[x].size()) mer(v, x); } if (c[v] == v) r.push_back(v); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { c[i] = i; com[i].push_back(i); } for (int i = 1; i <= m; i++) { int v, u; scanf("%d%d", &v, &u); g[v].push_back(u); g[u].push_back(v); } for (int i = 1; i <= n; i++) add(i); for (int i = 1; i <= n; i++) if (!com[i].empty()) ans.push_back(i); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { int p = ans[i]; printf("%d ", (int)com[p].size()); for (int j = 0; j < com[p].size(); j++) printf("%d ", com[p][j]); printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAX_N = 500000 + 20; vector<int> adj[MAX_N]; int st[MAX_N]; int par[MAX_N]; set<int> D; vector<int> seq[MAX_N], ver; pair<int, int> minDeg; int n, m; int edge(int v, int u) { vector<int>::iterator it = lower_bound(adj[v].begin(), adj[v].end(), u); return (it == adj[v].end()) || ((*it) != u); } int find(int v) { return par[v] = par[v] == v ? v : find(par[v]); } void merge(int v, int u) { v = find(v), u = find(u); if (v == u) return; par[u] = v; } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < m; ++i) { int v, u; cin >> v >> u; --v, u--; adj[v].push_back(u); adj[u].push_back(v); } minDeg.second = MAX_N; for (int i = 0; i < n; ++i) { if (minDeg.second > adj[i].size()) minDeg = make_pair(i, adj[i].size()); sort(adj[i].begin(), adj[i].end()); } st[minDeg.first] = -1; for (int i = 0; i < adj[minDeg.first].size(); ++i) { int u = adj[minDeg.first][i]; st[u] = 1; ver.push_back(u); } par[minDeg.first] = minDeg.first; for (int i = 0; i < ver.size(); ++i) par[ver[i]] = ver[i]; for (int i = 0; i < ver.size(); ++i) for (int j = i + 1; j < ver.size(); ++j) { int v = ver[i], u = ver[j]; if (edge(v, u)) merge(v, u); } for (int i = 0; i < ver.size(); ++i) { int v = ver[i], cnt = 0; for (int j = 0; j < adj[v].size(); ++j) { int u = adj[v][j]; cnt += st[u] == 0; } if (cnt != (n - 1) - adj[minDeg.first].size()) merge(minDeg.first, v); } for (int i = 0; i < n; ++i) if (st[i] == 0) par[i] = minDeg.first; for (int i = 0; i < n; ++i) { D.insert(find(i)); seq[find(i)].push_back(i); } cout << D.size() << endl; for (int i = 0; i < n; ++i) if (!seq[i].empty()) { cout << seq[i].size() << ' '; for (int j = 0; j < seq[i].size(); ++j) cout << seq[i][j] + 1 << ' '; cout << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX_N = 500000 + 20; vector<int> adj[MAX_N]; int st[MAX_N]; int par[MAX_N]; set<int> D; vector<int> seq[MAX_N], ver; pair<int, int> minDeg; int n, m; int edge(int v, int u) { vector<int>::iterator it = lower_bound(adj[v].begin(), adj[v].end(), u); return (it == adj[v].end()) || ((*it) != u); } int find(int v) { return par[v] = par[v] == v ? v : find(par[v]); } void merge(int v, int u) { v = find(v), u = find(u); if (v == u) return; par[u] = v; } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < m; ++i) { int v, u; cin >> v >> u; --v, u--; adj[v].push_back(u); adj[u].push_back(v); } minDeg.second = MAX_N; for (int i = 0; i < n; ++i) { if (minDeg.second > adj[i].size()) minDeg = make_pair(i, adj[i].size()); sort(adj[i].begin(), adj[i].end()); } st[minDeg.first] = -1; for (int i = 0; i < adj[minDeg.first].size(); ++i) { int u = adj[minDeg.first][i]; st[u] = 1; ver.push_back(u); } par[minDeg.first] = minDeg.first; for (int i = 0; i < ver.size(); ++i) par[ver[i]] = ver[i]; for (int i = 0; i < ver.size(); ++i) for (int j = i + 1; j < ver.size(); ++j) { int v = ver[i], u = ver[j]; if (edge(v, u)) merge(v, u); } for (int i = 0; i < ver.size(); ++i) { int v = ver[i], cnt = 0; for (int j = 0; j < adj[v].size(); ++j) { int u = adj[v][j]; cnt += st[u] == 0; } if (cnt != (n - 1) - adj[minDeg.first].size()) merge(minDeg.first, v); } for (int i = 0; i < n; ++i) if (st[i] == 0) par[i] = minDeg.first; for (int i = 0; i < n; ++i) { D.insert(find(i)); seq[find(i)].push_back(i); } cout << D.size() << endl; for (int i = 0; i < n; ++i) if (!seq[i].empty()) { cout << seq[i].size() << ' '; for (int j = 0; j < seq[i].size(); ++j) cout << seq[i][j] + 1 << ' '; cout << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5; int n, cnt_edge[maxn]; vector<int> g[maxn]; namespace dsu { int pa[maxn], size[maxn], rk[maxn]; vector<int> root; void Init(void) { root.clear(); for (int i = 0; i < n; ++i) { pa[i] = i; size[i] = 1; rk[i] = 0; root.push_back(i); } } int Find(int i) { return i == pa[i] ? i : pa[i] = Find(pa[i]); } void Unite(int i, int j) { i = Find(i); j = Find(j); if (i == j) return; if (rk[i] > rk[j]) swap(i, j); pa[i] = j; size[j] += size[i]; if (rk[i] == rk[j]) ++rk[j]; } void Update(void) { vector<int> new_root; for (int i : root) { if (i == pa[i]) { new_root.push_back(i); } } root = new_root; } } // namespace dsu vector<int> cc[maxn]; int main(void) { int m; scanf("%d%d", &n, &m); while (m--) { int u, v; scanf("%d%d", &u, &v); --u; --v; g[u].push_back(v); g[v].push_back(u); } dsu::Init(); for (int i = 0; i < n; ++i) { for (int j : dsu::root) { cnt_edge[j] = 0; } for (int j : g[i]) { ++cnt_edge[dsu::Find(j)]; } for (int j : dsu::root) { if (dsu::Find(i) != j && cnt_edge[j] < dsu::size[j]) { dsu::Unite(i, j); } } dsu::Update(); } int cnt_cc = 0; for (int i = 0; i < n; ++i) { cc[dsu::Find(i)].push_back(i); cnt_cc += dsu::Find(i) == i; } printf("%d\n", cnt_cc); for (int i = 0; i < n; ++i) { if (dsu::Find(i) == i) { printf("%d", (int)cc[i].size()); for (int j : cc[i]) { printf(" %d", j + 1); } putchar('\n'); } } return 0; }
### Prompt Generate a cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 5e5; int n, cnt_edge[maxn]; vector<int> g[maxn]; namespace dsu { int pa[maxn], size[maxn], rk[maxn]; vector<int> root; void Init(void) { root.clear(); for (int i = 0; i < n; ++i) { pa[i] = i; size[i] = 1; rk[i] = 0; root.push_back(i); } } int Find(int i) { return i == pa[i] ? i : pa[i] = Find(pa[i]); } void Unite(int i, int j) { i = Find(i); j = Find(j); if (i == j) return; if (rk[i] > rk[j]) swap(i, j); pa[i] = j; size[j] += size[i]; if (rk[i] == rk[j]) ++rk[j]; } void Update(void) { vector<int> new_root; for (int i : root) { if (i == pa[i]) { new_root.push_back(i); } } root = new_root; } } // namespace dsu vector<int> cc[maxn]; int main(void) { int m; scanf("%d%d", &n, &m); while (m--) { int u, v; scanf("%d%d", &u, &v); --u; --v; g[u].push_back(v); g[v].push_back(u); } dsu::Init(); for (int i = 0; i < n; ++i) { for (int j : dsu::root) { cnt_edge[j] = 0; } for (int j : g[i]) { ++cnt_edge[dsu::Find(j)]; } for (int j : dsu::root) { if (dsu::Find(i) != j && cnt_edge[j] < dsu::size[j]) { dsu::Unite(i, j); } } dsu::Update(); } int cnt_cc = 0; for (int i = 0; i < n; ++i) { cc[dsu::Find(i)].push_back(i); cnt_cc += dsu::Find(i) == i; } printf("%d\n", cnt_cc); for (int i = 0; i < n; ++i) { if (dsu::Find(i) == i) { printf("%d", (int)cc[i].size()); for (int j : cc[i]) { printf(" %d", j + 1); } putchar('\n'); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int nextInt() { int x; scanf("%d", &x); return x; } long long nextLong() { long long x; scanf("%I64d", &x); return x; } double nextDouble() { double x; scanf("%lf", &x); return x; } const int BUFSIZE = 1000000; char buf[BUFSIZE + 1]; string nextString() { scanf("%s", buf); return buf; } string nextLine() { gets(buf); return buf; } int stringToInt(string s) { stringstream in(s); int x; in >> x; return x; } struct Point { double x, y; Point() : x(0), y(0) {} Point(double x, double y) : x(x), y(y) {} Point operator-(Point op) const { return Point(x - op.x, y - op.y); } Point operator+(Point op) const { return Point(x + op.x, y + op.y); } Point operator*(double op) const { return Point(x * op, y * op); } double operator*(Point op) const { return x * op.x + y * op.y; } double operator%(Point op) const { return x * op.y - y * op.x; } double length2() { return x * x + y * y; } double length() { return sqrt(length2()); } }; Point nextPoint() { double x = nextDouble(); double y = nextDouble(); return Point(x, y); } class EdgeSet { vector<set<int> > m_set; public: EdgeSet() : m_set(501000) {} void add(int x, int y) { if (x > y) { swap(x, y); } m_set[x].insert(y); } bool check(int x, int y) { if (x > y) { swap(x, y); } return m_set[x].find(y) != m_set[x].end(); } }; int main() { int n = nextInt(); int m = nextInt(); EdgeSet missedEdges; for (int i = 0; i < m; ++i) { int x = nextInt() - 1; int y = nextInt() - 1; missedEdges.add(x, y); } vector<bool> used(n, false); vector<int> unused; for (int i = 0; i < n; ++i) { unused.push_back(i); } int resSize = 0; vector<int> res(n, -1); for (int i = 0; i < n; ++i) { if (!used[i]) { ++resSize; queue<int> q; q.push(i); used[i] = true; res[i] = resSize; while (!q.empty()) { int at = q.front(); q.pop(); for (int j = 0; j < unused.size(); ++j) { int to = unused[j]; int x = at; int y = to; if (x > y) { swap(x, y); } if (!missedEdges.check(x, y)) { used[to] = true; res[to] = resSize; q.push(to); } } int newSize = 0; for (int j = 0; j < unused.size(); ++j) { int x = unused[j]; if (!used[x]) { unused[newSize++] = x; } } unused.resize(newSize); } } } vector<vector<int> > ans(resSize); for (int i = 0; i < n; ++i) { ans[res[i] - 1].push_back(i); } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) { printf("%d", ans[i].size()); for (int j = 0; j < ans[i].size(); ++j) { printf(" %d", ans[i][j] + 1); } printf("\n"); } return 0; }
### Prompt Create a solution in cpp for the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int nextInt() { int x; scanf("%d", &x); return x; } long long nextLong() { long long x; scanf("%I64d", &x); return x; } double nextDouble() { double x; scanf("%lf", &x); return x; } const int BUFSIZE = 1000000; char buf[BUFSIZE + 1]; string nextString() { scanf("%s", buf); return buf; } string nextLine() { gets(buf); return buf; } int stringToInt(string s) { stringstream in(s); int x; in >> x; return x; } struct Point { double x, y; Point() : x(0), y(0) {} Point(double x, double y) : x(x), y(y) {} Point operator-(Point op) const { return Point(x - op.x, y - op.y); } Point operator+(Point op) const { return Point(x + op.x, y + op.y); } Point operator*(double op) const { return Point(x * op, y * op); } double operator*(Point op) const { return x * op.x + y * op.y; } double operator%(Point op) const { return x * op.y - y * op.x; } double length2() { return x * x + y * y; } double length() { return sqrt(length2()); } }; Point nextPoint() { double x = nextDouble(); double y = nextDouble(); return Point(x, y); } class EdgeSet { vector<set<int> > m_set; public: EdgeSet() : m_set(501000) {} void add(int x, int y) { if (x > y) { swap(x, y); } m_set[x].insert(y); } bool check(int x, int y) { if (x > y) { swap(x, y); } return m_set[x].find(y) != m_set[x].end(); } }; int main() { int n = nextInt(); int m = nextInt(); EdgeSet missedEdges; for (int i = 0; i < m; ++i) { int x = nextInt() - 1; int y = nextInt() - 1; missedEdges.add(x, y); } vector<bool> used(n, false); vector<int> unused; for (int i = 0; i < n; ++i) { unused.push_back(i); } int resSize = 0; vector<int> res(n, -1); for (int i = 0; i < n; ++i) { if (!used[i]) { ++resSize; queue<int> q; q.push(i); used[i] = true; res[i] = resSize; while (!q.empty()) { int at = q.front(); q.pop(); for (int j = 0; j < unused.size(); ++j) { int to = unused[j]; int x = at; int y = to; if (x > y) { swap(x, y); } if (!missedEdges.check(x, y)) { used[to] = true; res[to] = resSize; q.push(to); } } int newSize = 0; for (int j = 0; j < unused.size(); ++j) { int x = unused[j]; if (!used[x]) { unused[newSize++] = x; } } unused.resize(newSize); } } } vector<vector<int> > ans(resSize); for (int i = 0; i < n; ++i) { ans[res[i] - 1].push_back(i); } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) { printf("%d", ans[i].size()); for (int j = 0; j < ans[i].size(); ++j) { printf(" %d", ans[i][j] + 1); } printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <typename T, typename U> pair<T, U> operator+(const pair<T, U>& l, const std::pair<T, U>& r) { return {l.first + r.first, l.second + r.second}; } const long long int mod = 1e9 + 7; const int maxn = 3005; set<long long int> unvisited; vector<long long int> g[500005]; vector<vector<long long int>> cmps; void dfs(long long int u) { vector<long long int> temp; (cmps.back()).push_back(u); for (auto v : unvisited) { if (!binary_search((g[u]).begin(), (g[u]).end(), v)) { temp.push_back(v); } } for (auto v : temp) { unvisited.erase(v); } for (auto v : temp) { dfs(v); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m; cin >> n >> m; for (long long int i = 1; i <= m; i++) { long long int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for (long long int i = 1; i <= n; i++) sort((g[i]).begin(), (g[i]).end()); for (long long int i = 1; i <= n; i++) { unvisited.insert(i); } long long int cnt = 0; for (long long int i = 1; i <= n; i++) { if (unvisited.count(i)) { unvisited.erase(i); cmps.emplace_back(); dfs(i); cnt++; } } cout << cnt << " "; for (long long int i = 0; i < cnt; i++) { cout << cmps[i].size() << " "; for (auto v : cmps[i]) cout << v << " "; cout << " "; } }
### Prompt Please provide a cpp coded solution to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <typename T, typename U> pair<T, U> operator+(const pair<T, U>& l, const std::pair<T, U>& r) { return {l.first + r.first, l.second + r.second}; } const long long int mod = 1e9 + 7; const int maxn = 3005; set<long long int> unvisited; vector<long long int> g[500005]; vector<vector<long long int>> cmps; void dfs(long long int u) { vector<long long int> temp; (cmps.back()).push_back(u); for (auto v : unvisited) { if (!binary_search((g[u]).begin(), (g[u]).end(), v)) { temp.push_back(v); } } for (auto v : temp) { unvisited.erase(v); } for (auto v : temp) { dfs(v); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m; cin >> n >> m; for (long long int i = 1; i <= m; i++) { long long int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for (long long int i = 1; i <= n; i++) sort((g[i]).begin(), (g[i]).end()); for (long long int i = 1; i <= n; i++) { unvisited.insert(i); } long long int cnt = 0; for (long long int i = 1; i <= n; i++) { if (unvisited.count(i)) { unvisited.erase(i); cmps.emplace_back(); dfs(i); cnt++; } } cout << cnt << " "; for (long long int i = 0; i < cnt; i++) { cout << cmps[i].size() << " "; for (auto v : cmps[i]) cout << v << " "; cout << " "; } } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; set<pair<int, int>> edge; set<pair<int, int>> v; int cnt[MAXN]; void dfs(int &V, vector<int> &a) { a.push_back(V); vector<int> q; int now; for (auto i : v) { now = i.second; if (!edge.count({min(V, now), max(V, now)})) { q.push_back(now); } } int qs = q.size(); for (int i = qs - 1; i >= 0; --i) v.erase({-cnt[q[i]], q[i]}); for (int i = 0; i < qs; ++i) dfs(q[i], a); } int n, m, a1, a2; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> a1 >> a2; edge.insert({min(a1, a2), max(a1, a2)}); ++cnt[a1]; ++cnt[a2]; } for (int i = 1; i <= n; ++i) { v.insert({-cnt[i], i}); } vector<vector<int>> ans; vector<int> k; int p; while (!v.empty()) { ans.push_back(k); p = (*v.begin()).second; v.erase({-cnt[p], p}); dfs(p, ans[ans.size() - 1]); } cout << ans.size() << '\n'; for (auto i : ans) { cout << i.size() << " "; for (auto j : i) cout << j << " "; cout << '\n'; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; set<pair<int, int>> edge; set<pair<int, int>> v; int cnt[MAXN]; void dfs(int &V, vector<int> &a) { a.push_back(V); vector<int> q; int now; for (auto i : v) { now = i.second; if (!edge.count({min(V, now), max(V, now)})) { q.push_back(now); } } int qs = q.size(); for (int i = qs - 1; i >= 0; --i) v.erase({-cnt[q[i]], q[i]}); for (int i = 0; i < qs; ++i) dfs(q[i], a); } int n, m, a1, a2; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> a1 >> a2; edge.insert({min(a1, a2), max(a1, a2)}); ++cnt[a1]; ++cnt[a2]; } for (int i = 1; i <= n; ++i) { v.insert({-cnt[i], i}); } vector<vector<int>> ans; vector<int> k; int p; while (!v.empty()) { ans.push_back(k); p = (*v.begin()).second; v.erase({-cnt[p], p}); dfs(p, ans[ans.size() - 1]); } cout << ans.size() << '\n'; for (auto i : ans) { cout << i.size() << " "; for (auto j : i) cout << j << " "; cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m; const int maxn = 5e5 + 10; vector<int> adj[maxn]; int ind; bool haveB[maxn]; int tmp[maxn], par[maxn], sz[maxn]; int find(int v) { return (par[v] < 0 ? v : par[v] = find(par[v])); } void Union(int v, int u) { v = find(v); u = find(u); if (v == u) return; if (sz[v] > sz[u]) swap(v, u); par[v] = u; sz[u] += sz[v]; } void input() { cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &v, &u); u--; v--; adj[v].push_back(u); adj[u].push_back(v); } int mn = maxn; for (int i = 0; i < n; i++) { par[i] = -1; sz[i] = 1; sort(adj[i].begin(), adj[i].end()); if (adj[i].size() < mn) { mn = adj[i].size(); ind = i; } } } bool inA[maxn]; void fill_have() { for (auto v : adj[ind]) inA[v] = 1; for (int i = 0; i < n; i++) if (i != ind && !inA[i]) par[i] = ind; for (auto v : adj[ind]) { for (auto u : adj[v]) if (inA[u]) tmp[v]++; if (adj[v].size() - tmp[v] < n - adj[ind].size()) haveB[v] = 1; } } void CG() { for (auto i : adj[ind]) for (auto v : adj[ind]) if (*lower_bound(adj[i].begin(), adj[i].end(), v) != v) Union(i, v); } void solve() { fill_have(); CG(); sz[ind] = n - 1 - adj[ind].size(); for (auto v : adj[ind]) if (haveB[v]) Union(v, ind); } vector<int> ans[maxn]; void output() { for (int i = 0; i < n; i++) { int p = find(i); if (p == -1) ans[i].push_back(i + 1); else ans[p].push_back(i + 1); } int cnt = 0; for (int i = 0; i < n; i++) if (ans[i].size() > 0) cnt++; cout << cnt << endl; for (int i = 0; i < n; i++) if (ans[i].size() > 0) { printf("%d ", ans[i].size()); for (auto v : ans[i]) printf("%d ", v); cout << endl; } } int main() { input(); solve(); output(); }
### Prompt Create a solution in Cpp for the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m; const int maxn = 5e5 + 10; vector<int> adj[maxn]; int ind; bool haveB[maxn]; int tmp[maxn], par[maxn], sz[maxn]; int find(int v) { return (par[v] < 0 ? v : par[v] = find(par[v])); } void Union(int v, int u) { v = find(v); u = find(u); if (v == u) return; if (sz[v] > sz[u]) swap(v, u); par[v] = u; sz[u] += sz[v]; } void input() { cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &v, &u); u--; v--; adj[v].push_back(u); adj[u].push_back(v); } int mn = maxn; for (int i = 0; i < n; i++) { par[i] = -1; sz[i] = 1; sort(adj[i].begin(), adj[i].end()); if (adj[i].size() < mn) { mn = adj[i].size(); ind = i; } } } bool inA[maxn]; void fill_have() { for (auto v : adj[ind]) inA[v] = 1; for (int i = 0; i < n; i++) if (i != ind && !inA[i]) par[i] = ind; for (auto v : adj[ind]) { for (auto u : adj[v]) if (inA[u]) tmp[v]++; if (adj[v].size() - tmp[v] < n - adj[ind].size()) haveB[v] = 1; } } void CG() { for (auto i : adj[ind]) for (auto v : adj[ind]) if (*lower_bound(adj[i].begin(), adj[i].end(), v) != v) Union(i, v); } void solve() { fill_have(); CG(); sz[ind] = n - 1 - adj[ind].size(); for (auto v : adj[ind]) if (haveB[v]) Union(v, ind); } vector<int> ans[maxn]; void output() { for (int i = 0; i < n; i++) { int p = find(i); if (p == -1) ans[i].push_back(i + 1); else ans[p].push_back(i + 1); } int cnt = 0; for (int i = 0; i < n; i++) if (ans[i].size() > 0) cnt++; cout << cnt << endl; for (int i = 0; i < n; i++) if (ans[i].size() > 0) { printf("%d ", ans[i].size()); for (auto v : ans[i]) printf("%d ", v); cout << endl; } } int main() { input(); solve(); output(); } ```
#include <bits/stdc++.h> using namespace std; long long n, m; unordered_set<long long> e; set<long long> s; vector<vector<long long>> ans; queue<long long> q; void bfs(long long node) { s.erase(node); q.push(node); vector<long long> visited; while (!q.empty()) { node = q.front(); visited.push_back(node); q.pop(); vector<long long> to_erase; for (auto it = s.begin(); it != s.end(); it++) { if (e.count(n * node + *it)) continue; to_erase.push_back(*it); } for (auto x : to_erase) s.erase(x), q.push(x); } ans.push_back(visited); } signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin >> n >> m; for (long long i = 1; i <= n; i++) s.insert(i); for (long long i = 1; i <= m; i++) { long long a, b; cin >> a >> b; e.insert(n * a + b); e.insert(n * b + a); } for (long long i = 1; i <= n; i++) { if (!s.count(i)) continue; bfs(i); } cout << ans.size() << '\n'; for (auto x : ans) { cout << x.size() << ' '; for (auto y : x) cout << y << " "; cout << '\n'; } }
### Prompt Please provide a cpp coded solution to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, m; unordered_set<long long> e; set<long long> s; vector<vector<long long>> ans; queue<long long> q; void bfs(long long node) { s.erase(node); q.push(node); vector<long long> visited; while (!q.empty()) { node = q.front(); visited.push_back(node); q.pop(); vector<long long> to_erase; for (auto it = s.begin(); it != s.end(); it++) { if (e.count(n * node + *it)) continue; to_erase.push_back(*it); } for (auto x : to_erase) s.erase(x), q.push(x); } ans.push_back(visited); } signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin >> n >> m; for (long long i = 1; i <= n; i++) s.insert(i); for (long long i = 1; i <= m; i++) { long long a, b; cin >> a >> b; e.insert(n * a + b); e.insert(n * b + a); } for (long long i = 1; i <= n; i++) { if (!s.count(i)) continue; bfs(i); } cout << ans.size() << '\n'; for (auto x : ans) { cout << x.size() << ' '; for (auto y : x) cout << y << " "; cout << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int n; vector<vector<int> > g; set<int> st; vector<vector<int> > ans; vector<int> cur; bool yes(int v, int u) { if (g[v].size() == 0) { return false; } int l = 0, r = int(g[v].size()) - 1; while (l != r) { int mid = (l + r) / 2; if (g[v][mid] >= u) { r = mid; } else { l = mid + 1; } } return g[v][l] == u; } void dfs(int v) { st.erase(st.find(v)); cur.push_back(v); int u = 1; while (st.upper_bound(u) != st.end()) { int to = *st.upper_bound(u); if (!yes(v, to)) { dfs(to); } u = to; } } int main(int argc, char *argv[]) { int m; scanf("%d%d", &n, &m); g.resize(n + 1); while (m--) { int from, to; scanf("%d%d", &from, &to); g[from].push_back(to); g[to].push_back(from); } for (int i = 1; i <= n; ++i) { sort(g[i].begin(), g[i].end()); st.insert(i); } for (int i = 1; i <= n; ++i) { if (st.find(i) != st.end()) { cur.clear(); dfs(i); ans.push_back(cur); } } printf("%d\n", int(ans.size())); for (int i = 0; i < ans.size(); ++i) { printf("%d ", int(ans[i].size())); for (int j = 0; j < ans[i].size(); ++j) { printf("%d ", ans[i][j]); } printf("\n"); } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; vector<vector<int> > g; set<int> st; vector<vector<int> > ans; vector<int> cur; bool yes(int v, int u) { if (g[v].size() == 0) { return false; } int l = 0, r = int(g[v].size()) - 1; while (l != r) { int mid = (l + r) / 2; if (g[v][mid] >= u) { r = mid; } else { l = mid + 1; } } return g[v][l] == u; } void dfs(int v) { st.erase(st.find(v)); cur.push_back(v); int u = 1; while (st.upper_bound(u) != st.end()) { int to = *st.upper_bound(u); if (!yes(v, to)) { dfs(to); } u = to; } } int main(int argc, char *argv[]) { int m; scanf("%d%d", &n, &m); g.resize(n + 1); while (m--) { int from, to; scanf("%d%d", &from, &to); g[from].push_back(to); g[to].push_back(from); } for (int i = 1; i <= n; ++i) { sort(g[i].begin(), g[i].end()); st.insert(i); } for (int i = 1; i <= n; ++i) { if (st.find(i) != st.end()) { cur.clear(); dfs(i); ans.push_back(cur); } } printf("%d\n", int(ans.size())); for (int i = 0; i < ans.size(); ++i) { printf("%d ", int(ans[i].size())); for (int j = 0; j < ans[i].size(); ++j) { printf("%d ", ans[i][j]); } printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m; set<int> s; const int nax = 5e5 + 4; int link[nax]; set<pair<int, int>> edge; vector<vector<int>> group; inline void make_set(int i) { link[i] = i; } int Find(int a) { if (link[a] == a) return a; return (link[a] = Find(link[a])); } void Unite(int a, int b) { a = Find(a); b = Find(b); if (a == b) return; link[a] = b; } int component() { set<int> had; for (int i = 1; i <= n; ++i) { had.insert(Find(i)); group[Find(i)].push_back(i); } return ((int)had.size()); } void bfs(int i) { queue<int> q; q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); s.erase(u); vector<int> temp; for (auto &c : s) { if (edge.find({min(u, c), max(u, c)}) == edge.end()) { Unite(u, c); q.push(c); temp.push_back(c); } } for (auto &c : temp) s.erase(c); } } void solve() { cin >> n >> m; group.resize(n + 1); for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; edge.insert({min(u, v), max(u, v)}); } for (int i = 1; i <= n; ++i) make_set(i); for (int i = 1; i <= n; ++i) s.insert(i); for (int i = 1; i <= n; ++i) { if (s.find(i) != s.end()) bfs(i); } cout << component() << '\n'; for (int i = 1; i <= n; ++i) { if (!group[i].empty()) { cout << group[i].size() << ' '; for (auto &c : group[i]) cout << c << ' '; cout << '\n'; } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) { solve(); } }
### Prompt Please create a solution in cpp to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m; set<int> s; const int nax = 5e5 + 4; int link[nax]; set<pair<int, int>> edge; vector<vector<int>> group; inline void make_set(int i) { link[i] = i; } int Find(int a) { if (link[a] == a) return a; return (link[a] = Find(link[a])); } void Unite(int a, int b) { a = Find(a); b = Find(b); if (a == b) return; link[a] = b; } int component() { set<int> had; for (int i = 1; i <= n; ++i) { had.insert(Find(i)); group[Find(i)].push_back(i); } return ((int)had.size()); } void bfs(int i) { queue<int> q; q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); s.erase(u); vector<int> temp; for (auto &c : s) { if (edge.find({min(u, c), max(u, c)}) == edge.end()) { Unite(u, c); q.push(c); temp.push_back(c); } } for (auto &c : temp) s.erase(c); } } void solve() { cin >> n >> m; group.resize(n + 1); for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; edge.insert({min(u, v), max(u, v)}); } for (int i = 1; i <= n; ++i) make_set(i); for (int i = 1; i <= n; ++i) s.insert(i); for (int i = 1; i <= n; ++i) { if (s.find(i) != s.end()) bfs(i); } cout << component() << '\n'; for (int i = 1; i <= n; ++i) { if (!group[i].empty()) { cout << group[i].size() << ' '; for (auto &c : group[i]) cout << c << ' '; cout << '\n'; } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; int ata[(int)(5e5 + 10)], S[(int)(5e5 + 10)], S2[(int)(5e5 + 10)], i, j, k, n, m, x, y, z; int s, H[(int)(5e5 + 10)]; vector<int> arr[(int)(5e5 + 10)], v[(int)(5e5 + 10)]; set<int> first; int atabul(int x) { if (ata[x] == x) return x; return ata[x] = atabul(ata[x]); } int main() { cin >> n >> m; for (i = 1; i <= m; i++) { scanf("%d %d", &x, &y); arr[max(x, y)].push_back(min(x, y)); } set<int>::iterator it, it2; for (i = 1; i <= n; i++) { S[i] = 1; ata[i] = i; for (j = 0; j < arr[i].size(); j++) S2[atabul(arr[i][j])]--; for (it = first.begin(); it != first.end();) { int t = *it; if (S2[t]) { S[i] += S[t]; ata[t] = i; it2 = it++; first.erase(it2); } else it++; } for (it = first.begin(); it != first.end(); it++) S2[*it] = S[*it]; first.insert(i); S2[i] = S[i]; } for (i = 1; i <= n; i++) { int t = atabul(i); if (!H[t]) { H[t] = 1; s++; } v[t].push_back(i); } cout << s << '\n'; for (i = 1; i <= n; i++) { if (v[i].size()) { cout << v[i].size() << ' '; for (j = 0; j < v[i].size(); j++) cout << v[i][j] << ' '; puts(""); } } }
### Prompt Please formulate a Cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int ata[(int)(5e5 + 10)], S[(int)(5e5 + 10)], S2[(int)(5e5 + 10)], i, j, k, n, m, x, y, z; int s, H[(int)(5e5 + 10)]; vector<int> arr[(int)(5e5 + 10)], v[(int)(5e5 + 10)]; set<int> first; int atabul(int x) { if (ata[x] == x) return x; return ata[x] = atabul(ata[x]); } int main() { cin >> n >> m; for (i = 1; i <= m; i++) { scanf("%d %d", &x, &y); arr[max(x, y)].push_back(min(x, y)); } set<int>::iterator it, it2; for (i = 1; i <= n; i++) { S[i] = 1; ata[i] = i; for (j = 0; j < arr[i].size(); j++) S2[atabul(arr[i][j])]--; for (it = first.begin(); it != first.end();) { int t = *it; if (S2[t]) { S[i] += S[t]; ata[t] = i; it2 = it++; first.erase(it2); } else it++; } for (it = first.begin(); it != first.end(); it++) S2[*it] = S[*it]; first.insert(i); S2[i] = S[i]; } for (i = 1; i <= n; i++) { int t = atabul(i); if (!H[t]) { H[t] = 1; s++; } v[t].push_back(i); } cout << s << '\n'; for (i = 1; i <= n; i++) { if (v[i].size()) { cout << v[i].size() << ' '; for (j = 0; j < v[i].size(); j++) cout << v[i][j] << ' '; puts(""); } } } ```
#include <bits/stdc++.h> using namespace std; int nextInt() { int x; scanf("%d", &x); return x; } long long nextLong() { long long x; scanf("%I64d", &x); return x; } double nextDouble() { double x; scanf("%lf", &x); return x; } const int BUFSIZE = 1000000; char buf[BUFSIZE + 1]; string nextString() { scanf("%s", buf); return buf; } string nextLine() { gets(buf); return buf; } int stringToInt(string s) { stringstream in(s); int x; in >> x; return x; } struct Point { double x, y; Point() : x(0), y(0) {} Point(double x, double y) : x(x), y(y) {} Point operator-(Point op) const { return Point(x - op.x, y - op.y); } Point operator+(Point op) const { return Point(x + op.x, y + op.y); } Point operator*(double op) const { return Point(x * op, y * op); } double operator*(Point op) const { return x * op.x + y * op.y; } double operator%(Point op) const { return x * op.y - y * op.x; } double length2() { return x * x + y * y; } double length() { return sqrt(length2()); } }; Point nextPoint() { double x = nextDouble(); double y = nextDouble(); return Point(x, y); } class EdgeSet { set<pair<int, int> > m_set; public: void add(int x, int y) { if (x > y) { swap(x, y); } m_set.insert(make_pair(x, y)); } bool check(int x, int y) { if (x > y) { swap(x, y); } return m_set.find(make_pair(x, y)) != m_set.end(); } }; int main() { int n = nextInt(); int m = nextInt(); EdgeSet missedEdges; for (int i = 0; i < m; ++i) { int x = nextInt() - 1; int y = nextInt() - 1; missedEdges.add(x, y); } vector<bool> used(n, false); vector<int> unused; for (int i = 0; i < n; ++i) { unused.push_back(i); } int resSize = 0; vector<int> res(n, -1); for (int i = 0; i < n; ++i) { if (!used[i]) { ++resSize; queue<int> q; q.push(i); used[i] = true; res[i] = resSize; while (!q.empty()) { int at = q.front(); q.pop(); for (int j = 0; j < unused.size(); ++j) { int to = unused[j]; int x = at; int y = to; if (x > y) { swap(x, y); } if (!missedEdges.check(x, y)) { used[to] = true; res[to] = resSize; q.push(to); } } int newSize = 0; for (int j = 0; j < unused.size(); ++j) { int x = unused[j]; if (!used[x]) { unused[newSize++] = x; } } unused.resize(newSize); } } } vector<vector<int> > ans(resSize); for (int i = 0; i < n; ++i) { ans[res[i] - 1].push_back(i); } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) { printf("%d", ans[i].size()); for (int j = 0; j < ans[i].size(); ++j) { printf(" %d", ans[i][j] + 1); } printf("\n"); } return 0; }
### Prompt Create a solution in CPP for the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int nextInt() { int x; scanf("%d", &x); return x; } long long nextLong() { long long x; scanf("%I64d", &x); return x; } double nextDouble() { double x; scanf("%lf", &x); return x; } const int BUFSIZE = 1000000; char buf[BUFSIZE + 1]; string nextString() { scanf("%s", buf); return buf; } string nextLine() { gets(buf); return buf; } int stringToInt(string s) { stringstream in(s); int x; in >> x; return x; } struct Point { double x, y; Point() : x(0), y(0) {} Point(double x, double y) : x(x), y(y) {} Point operator-(Point op) const { return Point(x - op.x, y - op.y); } Point operator+(Point op) const { return Point(x + op.x, y + op.y); } Point operator*(double op) const { return Point(x * op, y * op); } double operator*(Point op) const { return x * op.x + y * op.y; } double operator%(Point op) const { return x * op.y - y * op.x; } double length2() { return x * x + y * y; } double length() { return sqrt(length2()); } }; Point nextPoint() { double x = nextDouble(); double y = nextDouble(); return Point(x, y); } class EdgeSet { set<pair<int, int> > m_set; public: void add(int x, int y) { if (x > y) { swap(x, y); } m_set.insert(make_pair(x, y)); } bool check(int x, int y) { if (x > y) { swap(x, y); } return m_set.find(make_pair(x, y)) != m_set.end(); } }; int main() { int n = nextInt(); int m = nextInt(); EdgeSet missedEdges; for (int i = 0; i < m; ++i) { int x = nextInt() - 1; int y = nextInt() - 1; missedEdges.add(x, y); } vector<bool> used(n, false); vector<int> unused; for (int i = 0; i < n; ++i) { unused.push_back(i); } int resSize = 0; vector<int> res(n, -1); for (int i = 0; i < n; ++i) { if (!used[i]) { ++resSize; queue<int> q; q.push(i); used[i] = true; res[i] = resSize; while (!q.empty()) { int at = q.front(); q.pop(); for (int j = 0; j < unused.size(); ++j) { int to = unused[j]; int x = at; int y = to; if (x > y) { swap(x, y); } if (!missedEdges.check(x, y)) { used[to] = true; res[to] = resSize; q.push(to); } } int newSize = 0; for (int j = 0; j < unused.size(); ++j) { int x = unused[j]; if (!used[x]) { unused[newSize++] = x; } } unused.resize(newSize); } } } vector<vector<int> > ans(resSize); for (int i = 0; i < n; ++i) { ans[res[i] - 1].push_back(i); } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) { printf("%d", ans[i].size()); for (int j = 0; j < ans[i].size(); ++j) { printf(" %d", ans[i][j] + 1); } printf("\n"); } return 0; } ```
#include <bits/stdc++.h> std::set<int> not_used; std::vector<int> comp; int n; std::vector<std::pair<int, int>> edges; int fastscan() { int number = 0; register int c = getchar(); for (; c > 47 && c < 58; c = getchar()) number = number * 10 + c - 48; return number; } int have_edge(int a, int b) { if (a < b) std::swap(a, b); std::pair<int, int> p{a, b}; return !std::binary_search(edges.begin(), edges.end(), p); } void dfs(int x) { comp.push_back(x); std::vector<int> go; for (auto it = not_used.begin(); it != not_used.end(); ++it) if (have_edge(x, *it)) go.push_back(*it); for (size_t i = 0; i < go.size(); i++) not_used.erase(go[i]); for (size_t i = 0; i < go.size(); i++) dfs(go[i]); } int main() { n = fastscan(); int m = fastscan(); for (int i = 0; i < m; i++) { int a = fastscan(); int b = fastscan(); a--; b--; if (a < b) std::swap(a, b); edges.push_back({a, b}); } std::sort(edges.begin(), edges.end()); for (int i = 0; i < n; i++) not_used.insert(i); std::vector<std::vector<int>> ans; for (int i = 0; i < n; i++) { if (not_used.count(i)) { not_used.erase(i); dfs(i); ans.push_back(comp); comp.clear(); } } printf("%d\n", ans.size()); for (size_t i = 0; i < ans.size(); i++) { printf("%d ", ans[i].size()); for (size_t j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j] + 1); printf("\n"); } return 0; }
### Prompt Please create a solution in CPP to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> std::set<int> not_used; std::vector<int> comp; int n; std::vector<std::pair<int, int>> edges; int fastscan() { int number = 0; register int c = getchar(); for (; c > 47 && c < 58; c = getchar()) number = number * 10 + c - 48; return number; } int have_edge(int a, int b) { if (a < b) std::swap(a, b); std::pair<int, int> p{a, b}; return !std::binary_search(edges.begin(), edges.end(), p); } void dfs(int x) { comp.push_back(x); std::vector<int> go; for (auto it = not_used.begin(); it != not_used.end(); ++it) if (have_edge(x, *it)) go.push_back(*it); for (size_t i = 0; i < go.size(); i++) not_used.erase(go[i]); for (size_t i = 0; i < go.size(); i++) dfs(go[i]); } int main() { n = fastscan(); int m = fastscan(); for (int i = 0; i < m; i++) { int a = fastscan(); int b = fastscan(); a--; b--; if (a < b) std::swap(a, b); edges.push_back({a, b}); } std::sort(edges.begin(), edges.end()); for (int i = 0; i < n; i++) not_used.insert(i); std::vector<std::vector<int>> ans; for (int i = 0; i < n; i++) { if (not_used.count(i)) { not_used.erase(i); dfs(i); ans.push_back(comp); comp.clear(); } } printf("%d\n", ans.size()); for (size_t i = 0; i < ans.size(); i++) { printf("%d ", ans[i].size()); for (size_t j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j] + 1); printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 45; map<pair<int, int>, bool> mapa; int n, m; vector<int> comp[N]; int main() { scanf("%i%i", &n, &m); for (int i = 1; i <= m; i++) { int a, b; scanf("%i%i", &a, &b); if (a > b) { swap(a, b); } mapa[pair<int, int>(a, b)] = 1; } set<int> s; for (int i = 1; i <= n; i++) { s.insert(i); } int br = 0; for (int i = 1; i <= n; i++) { if (s.find(i) == s.end()) { continue; } queue<int> q; q.push(i); s.erase(i); while (!q.empty()) { int u = q.front(); comp[br].push_back(u); q.pop(); vector<int> brisi; for (auto f : s) { int t = u, r = f; if (t > r) { swap(t, r); } if (!mapa.count(pair<int, int>(t, r))) { q.push(f); brisi.push_back(f); } } for (auto f : brisi) { s.erase(f); } } br++; } printf("%i\n", br); for (int i = 0; i < br; i++) { printf("%i ", comp[i].size()); for (int j = 0; j < comp[i].size(); j++) { printf("%i ", comp[i][j]); } printf("\n"); } }
### Prompt In Cpp, your task is to solve the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 45; map<pair<int, int>, bool> mapa; int n, m; vector<int> comp[N]; int main() { scanf("%i%i", &n, &m); for (int i = 1; i <= m; i++) { int a, b; scanf("%i%i", &a, &b); if (a > b) { swap(a, b); } mapa[pair<int, int>(a, b)] = 1; } set<int> s; for (int i = 1; i <= n; i++) { s.insert(i); } int br = 0; for (int i = 1; i <= n; i++) { if (s.find(i) == s.end()) { continue; } queue<int> q; q.push(i); s.erase(i); while (!q.empty()) { int u = q.front(); comp[br].push_back(u); q.pop(); vector<int> brisi; for (auto f : s) { int t = u, r = f; if (t > r) { swap(t, r); } if (!mapa.count(pair<int, int>(t, r))) { q.push(f); brisi.push_back(f); } } for (auto f : brisi) { s.erase(f); } } br++; } printf("%i\n", br); for (int i = 0; i < br; i++) { printf("%i ", comp[i].size()); for (int j = 0; j < comp[i].size(); j++) { printf("%i ", comp[i][j]); } printf("\n"); } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 5 * 100 * 1000 + 1000; vector<int> v[maxn]; vector<int> group[maxn]; int _next[maxn]; bool mark[maxn]; int n; int find_next(int x) { if (_next[x] == x) return x; return _next[x] = find_next(_next[x]); } void dfs(int x, int cnt) { group[cnt].push_back(x); mark[x] = true; _next[x] = x + 1; for (int i = find_next(1), j = 0; i <= n; i = find_next(i + 1)) { while (j < v[x].size() && v[x][j] < i) j++; if (j == v[x].size() || i != v[x][j]) dfs(i, cnt); } } int main() { ios_base::sync_with_stdio(false); int m; cin >> n >> m; while (m--) { int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } for (int i = 0; i <= n; i++) sort(v[i].begin(), v[i].end()); for (int i = 1; i <= n + 1; i++) _next[i] = i; int cnt = 0; for (int i = find_next(1); i <= n; i = find_next(i)) if (mark[i] == false) dfs(i, cnt), cnt++; cout << cnt << endl; for (int i = 0; i < cnt; i++) { cout << group[i].size() << " "; for (int j = 0; j < group[i].size(); j++) cout << group[i][j] << " "; cout << endl; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 5 * 100 * 1000 + 1000; vector<int> v[maxn]; vector<int> group[maxn]; int _next[maxn]; bool mark[maxn]; int n; int find_next(int x) { if (_next[x] == x) return x; return _next[x] = find_next(_next[x]); } void dfs(int x, int cnt) { group[cnt].push_back(x); mark[x] = true; _next[x] = x + 1; for (int i = find_next(1), j = 0; i <= n; i = find_next(i + 1)) { while (j < v[x].size() && v[x][j] < i) j++; if (j == v[x].size() || i != v[x][j]) dfs(i, cnt); } } int main() { ios_base::sync_with_stdio(false); int m; cin >> n >> m; while (m--) { int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } for (int i = 0; i <= n; i++) sort(v[i].begin(), v[i].end()); for (int i = 1; i <= n + 1; i++) _next[i] = i; int cnt = 0; for (int i = find_next(1); i <= n; i = find_next(i)) if (mark[i] == false) dfs(i, cnt), cnt++; cout << cnt << endl; for (int i = 0; i < cnt; i++) { cout << group[i].size() << " "; for (int j = 0; j < group[i].size(); j++) cout << group[i][j] << " "; cout << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; unordered_set<int> adj[maxn]; unordered_set<int> unvis; vector<int> c[maxn]; int ctr = 0; void dfs(int u) { c[ctr].push_back(u); vector<int> tovisit; for (int x : unvis) { if (adj[u].count(x) == 0) { tovisit.push_back(x); } } for (int x : tovisit) { unvis.erase(x); } for (int x : tovisit) { dfs(x); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].insert(v); adj[v].insert(u); } for (int i = 1; i <= n; i++) { unvis.insert(i); } while (unvis.size()) { int cur = *unvis.begin(); unvis.erase(unvis.begin()); dfs(cur); ctr++; } cout << ctr << '\n'; for (int i = 0; i < ctr; i++) { cout << c[i].size() << " "; for (int x : c[i]) cout << x << " "; cout << '\n'; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; unordered_set<int> adj[maxn]; unordered_set<int> unvis; vector<int> c[maxn]; int ctr = 0; void dfs(int u) { c[ctr].push_back(u); vector<int> tovisit; for (int x : unvis) { if (adj[u].count(x) == 0) { tovisit.push_back(x); } } for (int x : tovisit) { unvis.erase(x); } for (int x : tovisit) { dfs(x); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].insert(v); adj[v].insert(u); } for (int i = 1; i <= n; i++) { unvis.insert(i); } while (unvis.size()) { int cur = *unvis.begin(); unvis.erase(unvis.begin()); dfs(cur); ctr++; } cout << ctr << '\n'; for (int i = 0; i < ctr; i++) { cout << c[i].size() << " "; for (int x : c[i]) cout << x << " "; cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10, M = 1e6 + 10; int n, m, i, j, cnt; bool mark[N]; vector<int> adj[N], comp[N]; queue<int> q; unordered_set<int> unmarked; void bfs() { while (!q.empty()) { int u = q.front(); comp[cnt].push_back(u); q.pop(); for (auto v : adj[u]) { if (!mark[v]) unmarked.erase(v); } while (!unmarked.empty()) { mark[*unmarked.begin()] = 1; q.push(*unmarked.begin()); unmarked.erase(unmarked.begin()); } for (auto v : adj[u]) { if (!mark[v]) unmarked.insert(v); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); scanf("%d %d", &n, &m); for (i = 0; i < n; i++) unmarked.insert(i); for (i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; adj[u].push_back(v); adj[v].push_back(u); } for (i = 0; i < n; i++) { if (!mark[i]) { unmarked.erase(i); mark[i] = 1; q.push(i); bfs(); cnt++; } } printf("%d\n", cnt); for (i = 0; i < cnt; i++) { printf("%d ", comp[i].size()); for (auto v : comp[i]) printf("%d ", v + 1); printf("\n"); } }
### Prompt Develop a solution in cpp to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10, M = 1e6 + 10; int n, m, i, j, cnt; bool mark[N]; vector<int> adj[N], comp[N]; queue<int> q; unordered_set<int> unmarked; void bfs() { while (!q.empty()) { int u = q.front(); comp[cnt].push_back(u); q.pop(); for (auto v : adj[u]) { if (!mark[v]) unmarked.erase(v); } while (!unmarked.empty()) { mark[*unmarked.begin()] = 1; q.push(*unmarked.begin()); unmarked.erase(unmarked.begin()); } for (auto v : adj[u]) { if (!mark[v]) unmarked.insert(v); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); scanf("%d %d", &n, &m); for (i = 0; i < n; i++) unmarked.insert(i); for (i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; adj[u].push_back(v); adj[v].push_back(u); } for (i = 0; i < n; i++) { if (!mark[i]) { unmarked.erase(i); mark[i] = 1; q.push(i); bfs(); cnt++; } } printf("%d\n", cnt); for (i = 0; i < cnt; i++) { printf("%d ", comp[i].size()); for (auto v : comp[i]) printf("%d ", v + 1); printf("\n"); } } ```
#include <bits/stdc++.h> using namespace std; bool debug; const int inf = 1e9 + 5; const int nax = 1e6 + 5; vector<int> w[nax]; set<int> pozostale; vector<vector<int> > res; int first[nax]; void zacznij(int pierwszy) { vector<int> kol; kol.push_back(pierwszy); for (int i = 0; i <= ((int)kol.size()) - 1; ++i) { pozostale.erase(kol[i]); for (auto b : w[kol[i]]) ++first[b]; if (i == (int)kol.size() - 1) { int memo_size = (int)kol.size(); for (auto b : pozostale) if (first[b] != memo_size) kol.push_back(b); } } res.push_back(kol); for (auto a : kol) for (auto b : w[a]) first[b] = 0; } int main(int argc, char *argv[]) { debug = argc > 1; int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) pozostale.insert(i); while (m--) { int a, b; scanf("%d%d", &a, &b); w[a].push_back(b); w[b].push_back(a); } for (int a = 1; a <= n; ++a) if (pozostale.find(a) != pozostale.end()) zacznij(a); printf("%d\n", (int)res.size()); for (auto &w : res) { printf("%d ", (int)w.size()); for (auto a : w) printf("%d ", a); puts(""); } return 0; }
### Prompt Please create a solution in cpp to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool debug; const int inf = 1e9 + 5; const int nax = 1e6 + 5; vector<int> w[nax]; set<int> pozostale; vector<vector<int> > res; int first[nax]; void zacznij(int pierwszy) { vector<int> kol; kol.push_back(pierwszy); for (int i = 0; i <= ((int)kol.size()) - 1; ++i) { pozostale.erase(kol[i]); for (auto b : w[kol[i]]) ++first[b]; if (i == (int)kol.size() - 1) { int memo_size = (int)kol.size(); for (auto b : pozostale) if (first[b] != memo_size) kol.push_back(b); } } res.push_back(kol); for (auto a : kol) for (auto b : w[a]) first[b] = 0; } int main(int argc, char *argv[]) { debug = argc > 1; int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) pozostale.insert(i); while (m--) { int a, b; scanf("%d%d", &a, &b); w[a].push_back(b); w[b].push_back(a); } for (int a = 1; a <= n; ++a) if (pozostale.find(a) != pozostale.end()) zacznij(a); printf("%d\n", (int)res.size()); for (auto &w : res) { printf("%d ", (int)w.size()); for (auto a : w) printf("%d ", a); puts(""); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 5 * 100 * 1000 + 14; vector<int> comp[MAXN], adj[MAXN]; set<int> res; int ans = 0; void dfs(int v) { comp[ans].push_back(v); auto it = res.begin(); while (it != res.end()) { if (find(adj[v].begin(), adj[v].end(), *it) == adj[v].end()) { int u = *it; res.erase(u); dfs(u); it = res.upper_bound(u); } else it++; } return; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; while (m--) { int u, v; cin >> u >> v; adj[v].push_back(u); adj[u].push_back(v); } for (int i = 1; i <= n; i++) res.insert(i); while (!res.empty()) { int v = *res.begin(); res.erase(v); ans++; dfs(v); } cout << ans << '\n'; for (int i = 1; i <= ans; i++) { cout << comp[i].size() << ' '; for (auto u : comp[i]) cout << u << ' '; cout << '\n'; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 5 * 100 * 1000 + 14; vector<int> comp[MAXN], adj[MAXN]; set<int> res; int ans = 0; void dfs(int v) { comp[ans].push_back(v); auto it = res.begin(); while (it != res.end()) { if (find(adj[v].begin(), adj[v].end(), *it) == adj[v].end()) { int u = *it; res.erase(u); dfs(u); it = res.upper_bound(u); } else it++; } return; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; while (m--) { int u, v; cin >> u >> v; adj[v].push_back(u); adj[u].push_back(v); } for (int i = 1; i <= n; i++) res.insert(i); while (!res.empty()) { int v = *res.begin(); res.erase(v); ans++; dfs(v); } cout << ans << '\n'; for (int i = 1; i <= ans; i++) { cout << comp[i].size() << ' '; for (auto u : comp[i]) cout << u << ' '; cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; unordered_set<long long> second[500001]; unordered_set<long long> unvisited; vector<vector<long long>> ans; vector<long long> temp; void dfs(long long node) { temp.push_back(node); unvisited.erase(node); vector<long long> t; for (auto c : unvisited) { if (!second[node].count(c)) t.push_back(c); } for (auto c : t) unvisited.erase(c); for (auto &c : t) dfs(c); } void solve() { long long n, m; cin >> n >> m; for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; --a; --b; second[a].insert(b); second[b].insert(a); } for (long long i = 0; i < n; i++) unvisited.insert(i); for (long long i = 0; i < n; i++) { if (unvisited.count(i)) { temp.clear(); dfs(i); ans.push_back(temp); } } cout << ans.size() << "\n"; for (auto &c : ans) { cout << c.size() << " "; for (auto d : c) cout << d + 1 << " "; cout << "\n"; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); }
### Prompt Please formulate a CPP solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; unordered_set<long long> second[500001]; unordered_set<long long> unvisited; vector<vector<long long>> ans; vector<long long> temp; void dfs(long long node) { temp.push_back(node); unvisited.erase(node); vector<long long> t; for (auto c : unvisited) { if (!second[node].count(c)) t.push_back(c); } for (auto c : t) unvisited.erase(c); for (auto &c : t) dfs(c); } void solve() { long long n, m; cin >> n >> m; for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; --a; --b; second[a].insert(b); second[b].insert(a); } for (long long i = 0; i < n; i++) unvisited.insert(i); for (long long i = 0; i < n; i++) { if (unvisited.count(i)) { temp.clear(); dfs(i); ans.push_back(temp); } } cout << ans.size() << "\n"; for (auto &c : ans) { cout << c.size() << " "; for (auto d : c) cout << d + 1 << " "; cout << "\n"; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); } ```
#include <bits/stdc++.h> #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; const long long int N = 5e5 + 30, mod = 1e9 + 7, inf = 1e12; const long double eps = 0.0001; int n, m; bool visited[N]; set<pair<int, int> > st; set<int> ve; vector<int> g[N]; void bfs(int v, int t) { g[t].push_back(v); queue<int> q; q.push(v); visited[v] = 1; ve.erase(v); while (!q.empty()) { int u = q.front(); q.pop(); bool f = 0; if (ve.empty()) break; for (auto it = ve.begin(); it != ve.end(); it) { if (ve.empty()) return; if (f) it = ve.begin(); f = 0; int i = *it; if (!visited[i] and st.find({min(i, u), max(i, u)}) == st.end()) { q.push(i); visited[i] = 1; g[t].push_back(i); it = ve.erase(it); } else it++; if (ve.empty()) return; } } } int main() { ios ::sync_with_stdio(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; if (x > y) swap(x, y); st.insert({x, y}); } if (m < n - 1) { cout << 1 << '\n' << n << ' '; for (int i = 1; i < n + 1; i++) cout << i << ' '; return 0; } for (int i = 1; i < n + 1; i++) ve.insert(i); int t = 0; for (int i = 1; i < n + 1; i++) { if (!visited[i]) { t++; bfs(i, t); } } cout << t << '\n'; for (int i = 1; i < t + 1; i++) { cout << g[i].size() << ' '; for (int u : g[i]) cout << u << ' '; cout << '\n'; } return 0; }
### Prompt Create a solution in Cpp for the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; const long long int N = 5e5 + 30, mod = 1e9 + 7, inf = 1e12; const long double eps = 0.0001; int n, m; bool visited[N]; set<pair<int, int> > st; set<int> ve; vector<int> g[N]; void bfs(int v, int t) { g[t].push_back(v); queue<int> q; q.push(v); visited[v] = 1; ve.erase(v); while (!q.empty()) { int u = q.front(); q.pop(); bool f = 0; if (ve.empty()) break; for (auto it = ve.begin(); it != ve.end(); it) { if (ve.empty()) return; if (f) it = ve.begin(); f = 0; int i = *it; if (!visited[i] and st.find({min(i, u), max(i, u)}) == st.end()) { q.push(i); visited[i] = 1; g[t].push_back(i); it = ve.erase(it); } else it++; if (ve.empty()) return; } } } int main() { ios ::sync_with_stdio(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; if (x > y) swap(x, y); st.insert({x, y}); } if (m < n - 1) { cout << 1 << '\n' << n << ' '; for (int i = 1; i < n + 1; i++) cout << i << ' '; return 0; } for (int i = 1; i < n + 1; i++) ve.insert(i); int t = 0; for (int i = 1; i < n + 1; i++) { if (!visited[i]) { t++; bfs(i, t); } } cout << t << '\n'; for (int i = 1; i < t + 1; i++) { cout << g[i].size() << ' '; for (int u : g[i]) cout << u << ' '; cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 77, Mod = 1e9 + 7; int n, m, p[N], t[N], A; vector<int> a[N], sz[N]; vector<int> s; int find(int v) { return p[v] < 0 ? v : (p[v] = find(p[v])); } void merge(int v, int u) { v = find(v); u = find(u); if (u == v) { return; } p[v] += p[u]; p[u] = v; } int main() { fill(p, p + N, -1); scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) { int v, u; scanf("%d %d", &v, &u); a[v].push_back(u); a[u].push_back(v); } for (int v = 1; v <= n; v++) { for (auto u : a[v]) { t[find(u)]++; } vector<int> r; for (auto x : s) { if (t[x] == -p[x]) { r.push_back(x); continue; } merge(v, x); } r.push_back(v); s.clear(); for (auto x : r) s.push_back(x); for (auto u : a[v]) { t[find(u)] = 0; } } for (int i = 1; i <= n; i++) { sz[find(i)].push_back(i); A += p[i] < 0; } printf("%d\n", A); for (int i = 1; i <= n; i++) { if (sz[i].size()) { printf("%d ", sz[i].size()); for (auto x : sz[i]) printf("%d ", x); printf("\n"); } } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 77, Mod = 1e9 + 7; int n, m, p[N], t[N], A; vector<int> a[N], sz[N]; vector<int> s; int find(int v) { return p[v] < 0 ? v : (p[v] = find(p[v])); } void merge(int v, int u) { v = find(v); u = find(u); if (u == v) { return; } p[v] += p[u]; p[u] = v; } int main() { fill(p, p + N, -1); scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) { int v, u; scanf("%d %d", &v, &u); a[v].push_back(u); a[u].push_back(v); } for (int v = 1; v <= n; v++) { for (auto u : a[v]) { t[find(u)]++; } vector<int> r; for (auto x : s) { if (t[x] == -p[x]) { r.push_back(x); continue; } merge(v, x); } r.push_back(v); s.clear(); for (auto x : r) s.push_back(x); for (auto u : a[v]) { t[find(u)] = 0; } } for (int i = 1; i <= n; i++) { sz[find(i)].push_back(i); A += p[i] < 0; } printf("%d\n", A); for (int i = 1; i <= n; i++) { if (sz[i].size()) { printf("%d ", sz[i].size()); for (auto x : sz[i]) printf("%d ", x); printf("\n"); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int nextInt() { int x; scanf("%d", &x); return x; } long long nextLong() { long long x; scanf("%I64d", &x); return x; } double nextDouble() { double x; scanf("%lf", &x); return x; } const int BUFSIZE = 1000000; char buf[BUFSIZE + 1]; string nextString() { scanf("%s", buf); return buf; } string nextLine() { gets(buf); return buf; } int stringToInt(string s) { stringstream in(s); int x; in >> x; return x; } struct Point { double x, y; Point() : x(0), y(0) {} Point(double x, double y) : x(x), y(y) {} Point operator-(Point op) const { return Point(x - op.x, y - op.y); } Point operator+(Point op) const { return Point(x + op.x, y + op.y); } Point operator*(double op) const { return Point(x * op, y * op); } double operator*(Point op) const { return x * op.x + y * op.y; } double operator%(Point op) const { return x * op.y - y * op.x; } double length2() { return x * x + y * y; } double length() { return sqrt(length2()); } }; Point nextPoint() { double x = nextDouble(); double y = nextDouble(); return Point(x, y); } int main() { int n = nextInt(); int m = nextInt(); set<pair<int, int> > missedEdges; for (int i = 0; i < m; ++i) { int x = nextInt() - 1; int y = nextInt() - 1; if (x > y) { swap(x, y); } missedEdges.insert(make_pair(x, y)); } vector<bool> used(n, false); vector<int> unused; for (int i = 0; i < n; ++i) { unused.push_back(i); } int resSize = 0; vector<int> res(n, -1); for (int i = 0; i < n; ++i) { if (!used[i]) { ++resSize; queue<int> q; q.push(i); used[i] = true; res[i] = resSize; while (!q.empty()) { int at = q.front(); q.pop(); for (int j = 0; j < unused.size(); ++j) { int to = unused[j]; int x = at; int y = to; if (x > y) { swap(x, y); } if (missedEdges.find(make_pair(x, y)) == missedEdges.end()) { used[to] = true; res[to] = resSize; q.push(to); } } int newSize = 0; for (int j = 0; j < unused.size(); ++j) { int x = unused[j]; if (!used[x]) { unused[newSize++] = x; } } unused.resize(newSize); } } } vector<vector<int> > ans(resSize); for (int i = 0; i < n; ++i) { ans[res[i] - 1].push_back(i); } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) { printf("%d", ans[i].size()); for (int j = 0; j < ans[i].size(); ++j) { printf(" %d", ans[i][j] + 1); } printf("\n"); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int nextInt() { int x; scanf("%d", &x); return x; } long long nextLong() { long long x; scanf("%I64d", &x); return x; } double nextDouble() { double x; scanf("%lf", &x); return x; } const int BUFSIZE = 1000000; char buf[BUFSIZE + 1]; string nextString() { scanf("%s", buf); return buf; } string nextLine() { gets(buf); return buf; } int stringToInt(string s) { stringstream in(s); int x; in >> x; return x; } struct Point { double x, y; Point() : x(0), y(0) {} Point(double x, double y) : x(x), y(y) {} Point operator-(Point op) const { return Point(x - op.x, y - op.y); } Point operator+(Point op) const { return Point(x + op.x, y + op.y); } Point operator*(double op) const { return Point(x * op, y * op); } double operator*(Point op) const { return x * op.x + y * op.y; } double operator%(Point op) const { return x * op.y - y * op.x; } double length2() { return x * x + y * y; } double length() { return sqrt(length2()); } }; Point nextPoint() { double x = nextDouble(); double y = nextDouble(); return Point(x, y); } int main() { int n = nextInt(); int m = nextInt(); set<pair<int, int> > missedEdges; for (int i = 0; i < m; ++i) { int x = nextInt() - 1; int y = nextInt() - 1; if (x > y) { swap(x, y); } missedEdges.insert(make_pair(x, y)); } vector<bool> used(n, false); vector<int> unused; for (int i = 0; i < n; ++i) { unused.push_back(i); } int resSize = 0; vector<int> res(n, -1); for (int i = 0; i < n; ++i) { if (!used[i]) { ++resSize; queue<int> q; q.push(i); used[i] = true; res[i] = resSize; while (!q.empty()) { int at = q.front(); q.pop(); for (int j = 0; j < unused.size(); ++j) { int to = unused[j]; int x = at; int y = to; if (x > y) { swap(x, y); } if (missedEdges.find(make_pair(x, y)) == missedEdges.end()) { used[to] = true; res[to] = resSize; q.push(to); } } int newSize = 0; for (int j = 0; j < unused.size(); ++j) { int x = unused[j]; if (!used[x]) { unused[newSize++] = x; } } unused.resize(newSize); } } } vector<vector<int> > ans(resSize); for (int i = 0; i < n; ++i) { ans[res[i] - 1].push_back(i); } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) { printf("%d", ans[i].size()); for (int j = 0; j < ans[i].size(); ++j) { printf(" %d", ans[i][j] + 1); } printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 500000 + 1000; int n, m, ans, par[MAXN]; vector<int> adj[MAXN], out[MAXN]; bool mark[MAXN]; int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void dfs(int x) { mark[x] = 1; par[x] = x + 1; for (register int i = 0; i < adj[x].size(); i++) { int u = adj[x][i] + 1; while (find(u) < (i == adj[x].size() - 1 ? n : adj[x][i + 1])) { out[ans].push_back(find(u)); dfs(find(u)); } } } inline void read(register int *k) { register char c; *k = 0; do { c = getchar(); } while (c < '0' || c > '9'); do { *k = *k * 10 + c - '0'; c = getchar(); } while (c >= '0' && c <= '9'); } int main() { read(&n); read(&m); for (register int i = 0; i < m; i++) { int x, y; read(&x); read(&y); x--; y--; adj[x].push_back(y); adj[y].push_back(x); } for (register int i = 0; i < n; i++) adj[i].push_back(-1); for (register int i = 0; i < n; i++) sort(adj[i].begin(), adj[i].end()); for (register int i = 0; i < n + 100; i++) par[i] = i; for (register int i = 0; i < n; i++) if (mark[i] == 0) { out[++ans].push_back(i); dfs(i); } cout << ans << endl; for (register int i = 1; i < ans + 1; i++) { cout << out[i].size(); for (register int j = 0; j < out[i].size(); j++) cout << ' ' << out[i][j] + 1; cout << endl; } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 500000 + 1000; int n, m, ans, par[MAXN]; vector<int> adj[MAXN], out[MAXN]; bool mark[MAXN]; int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void dfs(int x) { mark[x] = 1; par[x] = x + 1; for (register int i = 0; i < adj[x].size(); i++) { int u = adj[x][i] + 1; while (find(u) < (i == adj[x].size() - 1 ? n : adj[x][i + 1])) { out[ans].push_back(find(u)); dfs(find(u)); } } } inline void read(register int *k) { register char c; *k = 0; do { c = getchar(); } while (c < '0' || c > '9'); do { *k = *k * 10 + c - '0'; c = getchar(); } while (c >= '0' && c <= '9'); } int main() { read(&n); read(&m); for (register int i = 0; i < m; i++) { int x, y; read(&x); read(&y); x--; y--; adj[x].push_back(y); adj[y].push_back(x); } for (register int i = 0; i < n; i++) adj[i].push_back(-1); for (register int i = 0; i < n; i++) sort(adj[i].begin(), adj[i].end()); for (register int i = 0; i < n + 100; i++) par[i] = i; for (register int i = 0; i < n; i++) if (mark[i] == 0) { out[++ans].push_back(i); dfs(i); } cout << ans << endl; for (register int i = 1; i < ans + 1; i++) { cout << out[i].size(); for (register int j = 0; j < out[i].size(); j++) cout << ' ' << out[i][j] + 1; cout << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int div_floor(const long long int &a, const long long int &b) { return a / b - (((a ^ b) < 0) and a % b); } long long int div_ceil(const long long int &a, const long long int &b) { return a / b + (((a ^ b) >= 0) and a % b); } vector<int> parent; vector<int> cnt; void make_set(int v) { parent[v] = v; cnt[v] = 1; } int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (cnt[a] < cnt[b]) swap(a, b); parent[b] = a; cnt[a] += cnt[b]; } } void solve() { int n, m; cin >> n >> m; vector<vector<int> > adj(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } for (auto &e : adj) sort(e.begin(), e.end()); int vertex = 0; for (int i = 0; i < n; i++) if ((long long int)adj[i].size() < (long long int)adj[vertex].size()) vertex = i; vector<int> nodes = adj[vertex]; nodes.push_back(vertex); sort(nodes.begin(), nodes.end()); int n1 = (long long int)nodes.size(); map<int, int> translate; for (int i = 0; i < n1; i++) translate[nodes[i]] = i; parent.resize(n1); cnt.resize(n1); for (int i = 0; i < n1; i++) make_set(i); for (int i = 0; i < n1; i++) { int cntr = 0; for (auto &v : adj[nodes[i]]) if (!binary_search(nodes.begin(), nodes.end(), v)) cntr++; for (auto &v : nodes) if (!binary_search(adj[nodes[i]].begin(), adj[nodes[i]].end(), v)) union_sets(i, translate[v]); if (cntr + n1 < n) union_sets(i, translate[vertex]); } int ans = 0; for (int i = 0; i < n1; i++) ans += parent[i] == i; cout << ans << '\n'; vector<vector<int> > comps(n); for (int i = 0; i < n1; i++) comps[nodes[find_set(i)]].push_back(nodes[i]); for (int i = 0; i < n; i++) if (!binary_search(nodes.begin(), nodes.end(), i)) { comps[nodes[find_set(translate[vertex])]].push_back(i); } for (auto &v : comps) { if ((long long int)v.size() > 0) { cout << (long long int)v.size() << " "; for (auto &e : v) cout << e + 1 << " "; cout << '\n'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) solve(); }
### Prompt Generate a cpp solution to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int div_floor(const long long int &a, const long long int &b) { return a / b - (((a ^ b) < 0) and a % b); } long long int div_ceil(const long long int &a, const long long int &b) { return a / b + (((a ^ b) >= 0) and a % b); } vector<int> parent; vector<int> cnt; void make_set(int v) { parent[v] = v; cnt[v] = 1; } int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (cnt[a] < cnt[b]) swap(a, b); parent[b] = a; cnt[a] += cnt[b]; } } void solve() { int n, m; cin >> n >> m; vector<vector<int> > adj(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } for (auto &e : adj) sort(e.begin(), e.end()); int vertex = 0; for (int i = 0; i < n; i++) if ((long long int)adj[i].size() < (long long int)adj[vertex].size()) vertex = i; vector<int> nodes = adj[vertex]; nodes.push_back(vertex); sort(nodes.begin(), nodes.end()); int n1 = (long long int)nodes.size(); map<int, int> translate; for (int i = 0; i < n1; i++) translate[nodes[i]] = i; parent.resize(n1); cnt.resize(n1); for (int i = 0; i < n1; i++) make_set(i); for (int i = 0; i < n1; i++) { int cntr = 0; for (auto &v : adj[nodes[i]]) if (!binary_search(nodes.begin(), nodes.end(), v)) cntr++; for (auto &v : nodes) if (!binary_search(adj[nodes[i]].begin(), adj[nodes[i]].end(), v)) union_sets(i, translate[v]); if (cntr + n1 < n) union_sets(i, translate[vertex]); } int ans = 0; for (int i = 0; i < n1; i++) ans += parent[i] == i; cout << ans << '\n'; vector<vector<int> > comps(n); for (int i = 0; i < n1; i++) comps[nodes[find_set(i)]].push_back(nodes[i]); for (int i = 0; i < n; i++) if (!binary_search(nodes.begin(), nodes.end(), i)) { comps[nodes[find_set(translate[vertex])]].push_back(i); } for (auto &v : comps) { if ((long long int)v.size() > 0) { cout << (long long int)v.size() << " "; for (auto &e : v) cout << e + 1 << " "; cout << '\n'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) solve(); } ```
#include <bits/stdc++.h> using namespace std; const long long int MOD = 1e9 + 7; const int N = 1e6 + 5; set<int> se; vector<int> w[N]; int ind[N]; vector<vector<int> > ans; void go(int n) { vector<int> v; v.emplace_back(n); for (int i = 0; i < v.size(); i++) { int num = v[i]; se.erase(num); for (auto j : w[num]) ind[j]++; if (i == v.size() - 1) { int mem = v.size(); for (auto k : se) if (ind[k] != mem) v.emplace_back(k); } } ans.emplace_back(v); for (auto i : v) for (auto j : w[i]) ind[j] = 0; } void _main_main() { long long int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) se.insert(i); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; w[x].emplace_back(y); w[y].emplace_back(x); } for (int i = 1; i <= n; i++) if (se.find(i) != se.end()) go(i); cout << ans.size() << "\n"; for (auto i : ans) { cout << i.size() << " "; for (auto j : i) cout << j << " "; cout << "\n"; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int testCase = 1; for (int i = 0; i < testCase; i++) { _main_main(); } }
### Prompt Please create a solution in cpp to the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int MOD = 1e9 + 7; const int N = 1e6 + 5; set<int> se; vector<int> w[N]; int ind[N]; vector<vector<int> > ans; void go(int n) { vector<int> v; v.emplace_back(n); for (int i = 0; i < v.size(); i++) { int num = v[i]; se.erase(num); for (auto j : w[num]) ind[j]++; if (i == v.size() - 1) { int mem = v.size(); for (auto k : se) if (ind[k] != mem) v.emplace_back(k); } } ans.emplace_back(v); for (auto i : v) for (auto j : w[i]) ind[j] = 0; } void _main_main() { long long int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) se.insert(i); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; w[x].emplace_back(y); w[y].emplace_back(x); } for (int i = 1; i <= n; i++) if (se.find(i) != se.end()) go(i); cout << ans.size() << "\n"; for (auto i : ans) { cout << i.size() << " "; for (auto j : i) cout << j << " "; cout << "\n"; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int testCase = 1; for (int i = 0; i < testCase; i++) { _main_main(); } } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 5 * 100 * 1000 + 14; vector<int> comp[MAXN], adj[MAXN]; set<int> res; int ans = 0; void dfs(int v) { comp[ans].push_back(v); auto it = res.begin(); while (it != res.end()) { if (find(adj[v].begin(), adj[v].end(), *it) == adj[v].end()) { int u = *it; res.erase(u); dfs(u); it = res.upper_bound(u); } else it++; } return; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; while (m--) { int u, v; cin >> u >> v; adj[v].push_back(u); adj[u].push_back(v); } for (int i = 1; i <= n; i++) res.insert(i); while (!res.empty()) { int v = *res.begin(); res.erase(v); ans++; dfs(v); } cout << ans << '\n'; for (int i = 1; i <= ans; i++) { cout << comp[i].size() << ' '; for (auto u : comp[i]) cout << u << ' '; cout << '\n'; } return 0; }
### Prompt In cpp, your task is to solve the following problem: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 5 * 100 * 1000 + 14; vector<int> comp[MAXN], adj[MAXN]; set<int> res; int ans = 0; void dfs(int v) { comp[ans].push_back(v); auto it = res.begin(); while (it != res.end()) { if (find(adj[v].begin(), adj[v].end(), *it) == adj[v].end()) { int u = *it; res.erase(u); dfs(u); it = res.upper_bound(u); } else it++; } return; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; while (m--) { int u, v; cin >> u >> v; adj[v].push_back(u); adj[u].push_back(v); } for (int i = 1; i <= n; i++) res.insert(i); while (!res.empty()) { int v = *res.begin(); res.erase(v); ans++; dfs(v); } cout << ans << '\n'; for (int i = 1; i <= ans; i++) { cout << comp[i].size() << ' '; for (auto u : comp[i]) cout << u << ' '; cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int num[500050], bel[500050], wz[500050]; int a[500050], dl[500050]; int n, m, top, now; int fir[500050], en[2000400], nex[2000400], tot; int get() { char t = getchar(); while (t < '0' || t > '9') t = getchar(); int x = 0; while (t >= '0' && t <= '9') { x *= 10; x += t - '0'; t = getchar(); } return x; } void tjb(int x, int y) { en[++tot] = y; nex[tot] = fir[x]; fir[x] = tot; } bool cmp(int a, int b) { return ((bel[a] < bel[b]) || ((bel[a] == bel[b]) && (a < b))); } int main() { n = get(); m = get(); for (int i = 1; i <= m; i++) { int x = get(); int y = get(); tjb(x, y); tjb(y, x); } for (int i = 1; i <= n; i++) dl[i] = wz[i] = i; for (int i = 1; i <= n; i++) { if (now < i) { now = i; ++top; } int x = dl[i]; bel[x] = top; ++num[top]; int td = n; for (int k = fir[x]; k; k = nex[k]) { int j = en[k]; if (wz[j] <= now) continue; if (wz[j] > td) continue; int y = dl[td]; swap(wz[y], wz[j]); swap(dl[wz[y]], dl[wz[j]]); --td; } now = td; } for (int i = 1; i <= n; i++) a[i] = i; sort(a + 1, a + 1 + n, cmp); printf("%d\n", top); now = 0; for (int i = 1; i <= top; i++) { printf("%d ", num[i]); for (int j = 1; j <= num[i] - 1; j++) { ++now; printf("%d ", a[now]); } ++now; printf("%d\n", a[now]); } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int num[500050], bel[500050], wz[500050]; int a[500050], dl[500050]; int n, m, top, now; int fir[500050], en[2000400], nex[2000400], tot; int get() { char t = getchar(); while (t < '0' || t > '9') t = getchar(); int x = 0; while (t >= '0' && t <= '9') { x *= 10; x += t - '0'; t = getchar(); } return x; } void tjb(int x, int y) { en[++tot] = y; nex[tot] = fir[x]; fir[x] = tot; } bool cmp(int a, int b) { return ((bel[a] < bel[b]) || ((bel[a] == bel[b]) && (a < b))); } int main() { n = get(); m = get(); for (int i = 1; i <= m; i++) { int x = get(); int y = get(); tjb(x, y); tjb(y, x); } for (int i = 1; i <= n; i++) dl[i] = wz[i] = i; for (int i = 1; i <= n; i++) { if (now < i) { now = i; ++top; } int x = dl[i]; bel[x] = top; ++num[top]; int td = n; for (int k = fir[x]; k; k = nex[k]) { int j = en[k]; if (wz[j] <= now) continue; if (wz[j] > td) continue; int y = dl[td]; swap(wz[y], wz[j]); swap(dl[wz[y]], dl[wz[j]]); --td; } now = td; } for (int i = 1; i <= n; i++) a[i] = i; sort(a + 1, a + 1 + n, cmp); printf("%d\n", top); now = 0; for (int i = 1; i <= top; i++) { printf("%d ", num[i]); for (int j = 1; j <= num[i] - 1; j++) { ++now; printf("%d ", a[now]); } ++now; printf("%d\n", a[now]); } return 0; } ```
#include <bits/stdc++.h> const int N = 500005; int H[N], id; struct Edge { int v, nt; } e[N * 4]; int n, m; int next[N]; std::vector<int> block[N]; int num, head; int p[N]; void AddEdge(int u, int v) { e[id].v = v; e[id].nt = H[u]; H[u] = id++; } int main() { scanf("%d%d", &n, &m); id = 1; num = 0; int a, b; for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); AddEdge(a, b); AddEdge(b, a); } for (int i = 1; i <= n; i++) next[i] = i + 1; for (head = 1; head <= n; num++) { block[num].push_back(head); head = next[head]; int pre; for (int i = 0; i < block[num].size(); i++) { int u = block[num][i]; for (int j = H[u]; j; j = e[j].nt) p[e[j].v] = u; for (int j = head; j <= n; j = next[j]) if (p[j] != u) { block[num].push_back(j); if (j == head) head = next[head]; else next[pre] = next[j]; } else pre = j; } } printf("%d\n", num); for (int i = 0; i < num; i++) { printf("%d", block[i].size()); for (int j = 0; j < block[i].size(); j++) printf(" %d", block[i][j]); puts(""); } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 5Β·105, 0 ≀ m ≀ 106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the numbers of cities that are connected by the i-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number k β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following k lines first print ti (1 ≀ ti ≀ n) β€” the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n. Examples Input 4 4 1 2 1 3 4 2 4 3 Output 2 2 1 4 2 2 3 Input 3 1 1 2 Output 1 3 1 2 3 Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. ### Response ```cpp #include <bits/stdc++.h> const int N = 500005; int H[N], id; struct Edge { int v, nt; } e[N * 4]; int n, m; int next[N]; std::vector<int> block[N]; int num, head; int p[N]; void AddEdge(int u, int v) { e[id].v = v; e[id].nt = H[u]; H[u] = id++; } int main() { scanf("%d%d", &n, &m); id = 1; num = 0; int a, b; for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); AddEdge(a, b); AddEdge(b, a); } for (int i = 1; i <= n; i++) next[i] = i + 1; for (head = 1; head <= n; num++) { block[num].push_back(head); head = next[head]; int pre; for (int i = 0; i < block[num].size(); i++) { int u = block[num][i]; for (int j = H[u]; j; j = e[j].nt) p[e[j].v] = u; for (int j = head; j <= n; j = next[j]) if (p[j] != u) { block[num].push_back(j); if (j == head) head = next[head]; else next[pre] = next[j]; } else pre = j; } } printf("%d\n", num); for (int i = 0; i < num; i++) { printf("%d", block[i].size()); for (int j = 0; j < block[i].size(); j++) printf(" %d", block[i][j]); puts(""); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e2 + 10; const int INF = 1e8 + 100; int dp[MAXN][MAXN][MAXN]; bool vis[MAXN][MAXN][MAXN]; int a[MAXN][MAXN]; int memo(int r, int d, int R) { if (vis[r][d][R]) return dp[r][d][R]; vis[r][d][R] = true; int D = r + d - R; int &res = dp[r][d][R]; res = -INF; if (r + d == 0) { res = a[0][0]; return res; } int temp = a[r][d] + a[R][D]; if (R == r) temp = a[r][d]; if (r && R) res = max(res, temp + memo(r - 1, d, R - 1)); if (r && D) res = max(res, temp + memo(r - 1, d, R)); if (d && R) res = max(res, temp + memo(r, d - 1, R - 1)); if (d && D) res = max(res, temp + memo(r, d - 1, R)); return res; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; cout << memo(n - 1, n - 1, n - 1); }
### Prompt In Cpp, your task is to solve the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 3e2 + 10; const int INF = 1e8 + 100; int dp[MAXN][MAXN][MAXN]; bool vis[MAXN][MAXN][MAXN]; int a[MAXN][MAXN]; int memo(int r, int d, int R) { if (vis[r][d][R]) return dp[r][d][R]; vis[r][d][R] = true; int D = r + d - R; int &res = dp[r][d][R]; res = -INF; if (r + d == 0) { res = a[0][0]; return res; } int temp = a[r][d] + a[R][D]; if (R == r) temp = a[r][d]; if (r && R) res = max(res, temp + memo(r - 1, d, R - 1)); if (r && D) res = max(res, temp + memo(r - 1, d, R)); if (d && R) res = max(res, temp + memo(r, d - 1, R - 1)); if (d && D) res = max(res, temp + memo(r, d - 1, R)); return res; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; cout << memo(n - 1, n - 1, n - 1); } ```
#include <bits/stdc++.h> using namespace std; const int mx[2] = {1, 0}; const int MAXN = 310; const int INF = 1e9; int N; int A[MAXN][MAXN]; int dp[MAXN][MAXN][MAXN]; void setmax(int& a, int b) { if (a < b) { a = b; } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> A[i][j]; } } for (int a = 0; a < N; a++) { for (int b = 0; b < N; b++) { for (int c = 0; c < N; c++) { dp[a][b][c] = -INF; } } } dp[0][0][0] = A[0][0]; for (int a = 0; a < N; a++) { for (int b = 0; b < N; b++) { for (int c = 0; c < N; c++) { if (dp[a][b][c] == -INF) continue; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { int na = a + mx[i], nb = a + b + 1 - na; int nc = c + mx[j], nd = a + b + 1 - nc; auto in = [&](int x) -> bool { return 0 <= x && x < N; }; if (in(na) && in(nb) && in(nc) && in(nd)) { int get = (na == nc ? A[na][nb] : A[na][nb] + A[nc][nd]); setmax(dp[na][nb][nc], dp[a][b][c] + get); } } } } } } cout << dp[N - 1][N - 1][N - 1] << endl; return 0; }
### Prompt Develop a solution in CPP to the problem described below: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mx[2] = {1, 0}; const int MAXN = 310; const int INF = 1e9; int N; int A[MAXN][MAXN]; int dp[MAXN][MAXN][MAXN]; void setmax(int& a, int b) { if (a < b) { a = b; } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> A[i][j]; } } for (int a = 0; a < N; a++) { for (int b = 0; b < N; b++) { for (int c = 0; c < N; c++) { dp[a][b][c] = -INF; } } } dp[0][0][0] = A[0][0]; for (int a = 0; a < N; a++) { for (int b = 0; b < N; b++) { for (int c = 0; c < N; c++) { if (dp[a][b][c] == -INF) continue; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { int na = a + mx[i], nb = a + b + 1 - na; int nc = c + mx[j], nd = a + b + 1 - nc; auto in = [&](int x) -> bool { return 0 <= x && x < N; }; if (in(na) && in(nb) && in(nc) && in(nd)) { int get = (na == nc ? A[na][nb] : A[na][nb] + A[nc][nd]); setmax(dp[na][nb][nc], dp[a][b][c] + get); } } } } } } cout << dp[N - 1][N - 1][N - 1] << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int dp[305 << 1][305][305]; int a[305][305], n; int way[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}}; int main() { scanf("%d", &n); int i, j, k; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { scanf("%d", &a[i][j]); } } memset(dp, 0x81, sizeof(dp)); dp[0][1][1] = a[1][1]; int r; for (i = 1; i <= 2 * n - 2; i++) { for (j = 1; j <= i + 1 && j <= n; j++) { for (k = 1; k <= i + 1 && k <= n; k++) { for (r = 0; r < 4; r++) { dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - way[r][0]][k - way[r][1]]); } dp[i][j][k] += a[j][i - j + 2] + a[k][i - k + 2] - (j == k ? a[k][i - k + 2] : 0); } } } printf("%d\n", dp[2 * n - 2][n][n]); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dp[305 << 1][305][305]; int a[305][305], n; int way[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}}; int main() { scanf("%d", &n); int i, j, k; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { scanf("%d", &a[i][j]); } } memset(dp, 0x81, sizeof(dp)); dp[0][1][1] = a[1][1]; int r; for (i = 1; i <= 2 * n - 2; i++) { for (j = 1; j <= i + 1 && j <= n; j++) { for (k = 1; k <= i + 1 && k <= n; k++) { for (r = 0; r < 4; r++) { dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - way[r][0]][k - way[r][1]]); } dp[i][j][k] += a[j][i - j + 2] + a[k][i - k + 2] - (j == k ? a[k][i - k + 2] : 0); } } } printf("%d\n", dp[2 * n - 2][n][n]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int pts[301][301]; int dp[2 * 301][301][301]; inline int _dp(int i, int j, int k) { if (i < 0 or j < 0 or k < 0) return -100000000; if (dp[i][j][k] != 1e8) return dp[i][j][k]; long long ans = 0; ans = max(max(_dp(i - 1, j - 1, k), _dp(i - 1, j - 1, k - 1)), max(_dp(i - 1, j, k - 1), _dp(i - 1, j, k))); ans += pts[j][i - j]; if (j != k) ans += pts[k][i - k]; dp[i][j][k] = ans; return ans; } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> pts[i][j]; for (int i = 0; i < 2 * n; ++i) for (int j = 0; j < n; ++j) for (int k = 0; k < n; ++k) dp[i][j][k] = 1e8; dp[0][0][0] = pts[0][0]; cout << _dp(2 * n - 2, n - 1, n - 1); return 0; }
### Prompt In CPP, your task is to solve the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int pts[301][301]; int dp[2 * 301][301][301]; inline int _dp(int i, int j, int k) { if (i < 0 or j < 0 or k < 0) return -100000000; if (dp[i][j][k] != 1e8) return dp[i][j][k]; long long ans = 0; ans = max(max(_dp(i - 1, j - 1, k), _dp(i - 1, j - 1, k - 1)), max(_dp(i - 1, j, k - 1), _dp(i - 1, j, k))); ans += pts[j][i - j]; if (j != k) ans += pts[k][i - k]; dp[i][j][k] = ans; return ans; } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> pts[i][j]; for (int i = 0; i < 2 * n; ++i) for (int j = 0; j < n; ++j) for (int k = 0; k < n; ++k) dp[i][j][k] = 1e8; dp[0][0][0] = pts[0][0]; cout << _dp(2 * n - 2, n - 1, n - 1); return 0; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") #pragma warning(disable : 4786) using namespace std; int MIN(int a, int b) { return a < b ? a : b; } int MAX(int a, int b) { return a > b ? a : b; } int GCD(int a, int b) { while (b) b ^= a ^= b ^= a %= b; return a; } int LCM(int a, int b) { return a * (b / GCD(a, b)); } void SWAP(int &a, int &b) { a = a ^ b; b = a ^ b; a = a ^ b; } const double PI = acos(-1); const double EPS = 1e-11; int arr[310][310], memo[310][310][310], N, M; int dp(int i, int j, int k) { int l = i + j - k; if (i == N - 1 && j == M - 1) { return arr[i][j]; } if (i == N || j == M || k >= N || l >= N) { return -(1 << 29); ; } int &ret = memo[i][j][k]; if (ret != -1) { return ret; } int res, r1, val; res = -(1 << 29); ; if (i == k && j == l) { val = arr[i][j]; } else { val = arr[i][j] + arr[k][l]; } r1 = dp(i + 1, j, k + 1) + val; res = MAX(res, r1); r1 = dp(i + 1, j, k) + val; res = MAX(res, r1); r1 = dp(i, j + 1, k + 1) + val; res = MAX(res, r1); r1 = dp(i, j + 1, k) + val; res = MAX(res, r1); return ret = res; } int main() { int T, res, n, i, ind, m, j; while (scanf("%d", &n) != EOF) { int a = -(1 << 29); ; memset(memo, -1, sizeof(memo)); memset(arr, 0, sizeof(arr)); m = n; for (i = 0; i < (n); i++) { for (j = 0; j < (m); j++) { scanf("%d", &arr[i][j]); } } N = n; M = m; res = dp(0, 0, 0); printf("%d\n", res); } return 0; }
### Prompt Please create a solution in cpp to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") #pragma warning(disable : 4786) using namespace std; int MIN(int a, int b) { return a < b ? a : b; } int MAX(int a, int b) { return a > b ? a : b; } int GCD(int a, int b) { while (b) b ^= a ^= b ^= a %= b; return a; } int LCM(int a, int b) { return a * (b / GCD(a, b)); } void SWAP(int &a, int &b) { a = a ^ b; b = a ^ b; a = a ^ b; } const double PI = acos(-1); const double EPS = 1e-11; int arr[310][310], memo[310][310][310], N, M; int dp(int i, int j, int k) { int l = i + j - k; if (i == N - 1 && j == M - 1) { return arr[i][j]; } if (i == N || j == M || k >= N || l >= N) { return -(1 << 29); ; } int &ret = memo[i][j][k]; if (ret != -1) { return ret; } int res, r1, val; res = -(1 << 29); ; if (i == k && j == l) { val = arr[i][j]; } else { val = arr[i][j] + arr[k][l]; } r1 = dp(i + 1, j, k + 1) + val; res = MAX(res, r1); r1 = dp(i + 1, j, k) + val; res = MAX(res, r1); r1 = dp(i, j + 1, k + 1) + val; res = MAX(res, r1); r1 = dp(i, j + 1, k) + val; res = MAX(res, r1); return ret = res; } int main() { int T, res, n, i, ind, m, j; while (scanf("%d", &n) != EOF) { int a = -(1 << 29); ; memset(memo, -1, sizeof(memo)); memset(arr, 0, sizeof(arr)); m = n; for (i = 0; i < (n); i++) { for (j = 0; j < (m); j++) { scanf("%d", &arr[i][j]); } } N = n; M = m; res = dp(0, 0, 0); printf("%d\n", res); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a[305][305]; bool done[305][305][305]; int memo[305][305][305]; int dr[] = {1, 0}, dc[] = {0, 1}; int solve(int r1, int c1, int r2, int c2) { if (r1 == n - 1 && c1 == n - 1) return 0; int &ret = memo[r1][c1][r2]; if (!done[r1][c1][r2]) { done[r1][c1][r2] = true; ret = -90000000; for (int i = 0; i < 2; ++i) { int x1 = r1 + dr[i], y1 = c1 + dc[i]; if (x1 < n && y1 < n) { for (int j = 0; j < 2; ++j) { int x2 = r2 + dr[j], y2 = c2 + dc[j]; if (x2 < n && y2 < n) { int aux = solve(x1, y1, x2, y2); if (x1 == x2 && y1 == y2) aux += a[x1][y1]; else aux += a[x1][y1] + a[x2][y2]; ret = max(ret, aux); } } } } } return ret; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) scanf("%d", &a[i][j]); printf("%d\n", solve(0, 0, 0, 0) + a[0][0]); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a[305][305]; bool done[305][305][305]; int memo[305][305][305]; int dr[] = {1, 0}, dc[] = {0, 1}; int solve(int r1, int c1, int r2, int c2) { if (r1 == n - 1 && c1 == n - 1) return 0; int &ret = memo[r1][c1][r2]; if (!done[r1][c1][r2]) { done[r1][c1][r2] = true; ret = -90000000; for (int i = 0; i < 2; ++i) { int x1 = r1 + dr[i], y1 = c1 + dc[i]; if (x1 < n && y1 < n) { for (int j = 0; j < 2; ++j) { int x2 = r2 + dr[j], y2 = c2 + dc[j]; if (x2 < n && y2 < n) { int aux = solve(x1, y1, x2, y2); if (x1 == x2 && y1 == y2) aux += a[x1][y1]; else aux += a[x1][y1] + a[x2][y2]; ret = max(ret, aux); } } } } } return ret; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) scanf("%d", &a[i][j]); printf("%d\n", solve(0, 0, 0, 0) + a[0][0]); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e6; const long long mod = 1e9 + 7; const long long inf = 1e9; long long a[610][610], f[2][610][610]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); int n; cin >> n; for (int i = 0; i <= 600; i++) for (int j = 0; j <= 600; j++) { a[i][j] = -inf; f[0][i][j] = -inf; f[1][i][j] = -inf; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; f[0][1][1] = a[1][1]; for (int k = 3; k <= n + n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int x = k % 2, y = 1 - x; if (i >= k || j >= k) continue; if (k - i > n || k - j > n) { f[x][i][j] = -inf; continue; } long long w = -inf; for (int dx = -1; dx <= 0; dx++) for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]); f[x][i][j] = w; if (i != j) f[x][i][j] += a[i][k - i] + a[j][k - j]; else f[x][i][j] += a[i][k - i]; } } } cout << f[0][n][n]; }
### Prompt Create a solution in Cpp for the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e6; const long long mod = 1e9 + 7; const long long inf = 1e9; long long a[610][610], f[2][610][610]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); int n; cin >> n; for (int i = 0; i <= 600; i++) for (int j = 0; j <= 600; j++) { a[i][j] = -inf; f[0][i][j] = -inf; f[1][i][j] = -inf; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; f[0][1][1] = a[1][1]; for (int k = 3; k <= n + n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int x = k % 2, y = 1 - x; if (i >= k || j >= k) continue; if (k - i > n || k - j > n) { f[x][i][j] = -inf; continue; } long long w = -inf; for (int dx = -1; dx <= 0; dx++) for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]); f[x][i][j] = w; if (i != j) f[x][i][j] += a[i][k - i] + a[j][k - j]; else f[x][i][j] += a[i][k - i]; } } } cout << f[0][n][n]; } ```
#include <bits/stdc++.h> using namespace std; int dp[606][303][303]; int solve(int n, vector<vector<int>> &vec, int d, int x1, int x2) { if (d > n + n - 2) { return 0; } if (x1 >= n || x2 >= n) { return -0x3f3f3f3f; } int y1 = d - x1, y2 = d - x2; if (y1 >= n || y2 >= n) { return -0x3f3f3f3f; } if (dp[d][x1][x2] != -1) { return dp[d][x1][x2]; } int temp = 0; temp = max(max(solve(n, vec, d + 1, x1 + 1, x2 + 1), solve(n, vec, d + 1, x1 + 1, x2)), max(solve(n, vec, d + 1, x1, x2 + 1), solve(n, vec, d + 1, x1, x2))); if (x1 == x2 && y1 == y2) { temp += vec[x1][y1]; } else { temp += vec[x1][y1] + vec[x2][y2]; } dp[d][x1][x2] = temp; return temp; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<vector<int>> vec(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> vec[i][j]; } } memset(dp, -1, sizeof(dp)); cout << solve(n, vec, 0, 0, 0); return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dp[606][303][303]; int solve(int n, vector<vector<int>> &vec, int d, int x1, int x2) { if (d > n + n - 2) { return 0; } if (x1 >= n || x2 >= n) { return -0x3f3f3f3f; } int y1 = d - x1, y2 = d - x2; if (y1 >= n || y2 >= n) { return -0x3f3f3f3f; } if (dp[d][x1][x2] != -1) { return dp[d][x1][x2]; } int temp = 0; temp = max(max(solve(n, vec, d + 1, x1 + 1, x2 + 1), solve(n, vec, d + 1, x1 + 1, x2)), max(solve(n, vec, d + 1, x1, x2 + 1), solve(n, vec, d + 1, x1, x2))); if (x1 == x2 && y1 == y2) { temp += vec[x1][y1]; } else { temp += vec[x1][y1] + vec[x2][y2]; } dp[d][x1][x2] = temp; return temp; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<vector<int>> vec(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> vec[i][j]; } } memset(dp, -1, sizeof(dp)); cout << solve(n, vec, 0, 0, 0); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int mod = 1e9 + 7; int dp[602][302][302], n, a[303][303]; int solve(int dg, int i1, int i2) { if (i1 > n || i2 > n) return -inf; int j1 = dg - i1; int j2 = dg - i2; if (j1 > n || j2 > n) return -inf; if (dg == (2 * n)) return a[i1][j1]; int &ret = dp[dg][i1][i2]; if (ret != (-inf)) return ret; if (i1 == i2 && j1 == j2) { ret = a[i1][j1]; } else ret = a[i1][j1] + a[i2][j2]; int tmp = -inf; for (int x = 0; x <= 1; x++) { for (int y = 0; y <= 1; y++) { tmp = max(tmp, solve(dg + 1, i1 + x, i2 + y)); } } ret += tmp; return ret; } void init() { for (int i = 1; i <= 2 * n; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) dp[i][j][k] = -inf; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; init(); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; int ses = solve(2, 1, 1); cout << ses << endl; return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int mod = 1e9 + 7; int dp[602][302][302], n, a[303][303]; int solve(int dg, int i1, int i2) { if (i1 > n || i2 > n) return -inf; int j1 = dg - i1; int j2 = dg - i2; if (j1 > n || j2 > n) return -inf; if (dg == (2 * n)) return a[i1][j1]; int &ret = dp[dg][i1][i2]; if (ret != (-inf)) return ret; if (i1 == i2 && j1 == j2) { ret = a[i1][j1]; } else ret = a[i1][j1] + a[i2][j2]; int tmp = -inf; for (int x = 0; x <= 1; x++) { for (int y = 0; y <= 1; y++) { tmp = max(tmp, solve(dg + 1, i1 + x, i2 + y)); } } ret += tmp; return ret; } void init() { for (int i = 1; i <= 2 * n; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) dp[i][j][k] = -inf; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; init(); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; int ses = solve(2, 1, 1); cout << ses << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 310; const int mod = 1e9 + 7; const int inf = -1e9; int a[N][N]; int cache[2 * N][N][N]; int n; int dp(int diag, int i1, int i2) { if (i1 > n || i2 > n) return -1e9; int j1 = diag - i1; int j2 = diag - i2; if (j1 > n || j2 > n) return -1e9; if (diag == 2 * n) return a[i1][j1]; int &ans = cache[diag][i1][i2]; if (ans != inf) return ans; if (i1 == i2) ans = a[i1][j1]; else ans = a[i1][j1] + a[i2][j2]; int mx = -1e9; mx = max(mx, dp(diag + 1, i1, i2 + 1)); mx = max(mx, dp(diag + 1, i1 + 1, i2)); mx = max(mx, dp(diag + 1, i1 + 1, i2 + 1)); mx = max(mx, dp(diag + 1, i1, i2)); ans += mx; return ans; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cin >> a[i][j]; } } for (int i = 1; i <= 2 * n; ++i) for (int j = 1; j <= n; ++j) for (int k = 1; k <= n; ++k) cache[i][j][k] = inf; cout << dp(2, 1, 1) << '\n'; return 0; }
### Prompt Please formulate a CPP solution to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 310; const int mod = 1e9 + 7; const int inf = -1e9; int a[N][N]; int cache[2 * N][N][N]; int n; int dp(int diag, int i1, int i2) { if (i1 > n || i2 > n) return -1e9; int j1 = diag - i1; int j2 = diag - i2; if (j1 > n || j2 > n) return -1e9; if (diag == 2 * n) return a[i1][j1]; int &ans = cache[diag][i1][i2]; if (ans != inf) return ans; if (i1 == i2) ans = a[i1][j1]; else ans = a[i1][j1] + a[i2][j2]; int mx = -1e9; mx = max(mx, dp(diag + 1, i1, i2 + 1)); mx = max(mx, dp(diag + 1, i1 + 1, i2)); mx = max(mx, dp(diag + 1, i1 + 1, i2 + 1)); mx = max(mx, dp(diag + 1, i1, i2)); ans += mx; return ans; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cin >> a[i][j]; } } for (int i = 1; i <= 2 * n; ++i) for (int j = 1; j <= n; ++j) for (int k = 1; k <= n; ++k) cache[i][j][k] = inf; cout << dp(2, 1, 1) << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 300 + 10; const int INF = 1e9 + 2; int n; int cc[MAXN][MAXN]; int d[2 * MAXN][MAXN][MAXN]; bool fit(int a, int b) { return a >= 0 && a < n && b >= 0 && b < n; } int get(int a, int b, int c) { if (d[a][b][c] != INF) return d[a][b][c]; if (a == 2 * n - 2) return d[a][b][c] = 0; int r1, r2, col1, col2; d[a][b][c] = -INF; if (a < n - 1) { r1 = b, r2 = c; col1 = a - b, col2 = a - c; if (fit(r1 + 1, col1) && fit(r2 + 1, col2)) d[a][b][c] = max(d[a][b][c], get(a + 1, b + 1, c + 1) + cc[r1 + 1][col1] + cc[r2 + 1][col2] - int(r2 == r1 && col1 == col2) * cc[r1 + 1][col1]); if (fit(r1 + 1, col1) && fit(r2, col2 + 1)) d[a][b][c] = max(d[a][b][c], get(a + 1, b + 1, c) + cc[r1 + 1][col1] + cc[r2][col2 + 1] - int(r2 == r1 + 1 && col2 + 1 == col1) * cc[r2][col2 + 1]); if (fit(r1, col1 + 1) && fit(r2 + 1, col2)) d[a][b][c] = max(d[a][b][c], get(a + 1, b, c + 1) + cc[r1][col1 + 1] + cc[r2 + 1][col2] - int(r2 + 1 == r1 && col1 + 1 == col2) * cc[r2 + 1][col2]); if (fit(r1, col1 + 1) && fit(r2, col2 + 1)) d[a][b][c] = max(d[a][b][c], get(a + 1, b, c) + cc[r1][col1 + 1] + cc[r2][col2 + 1] - int(r1 == r2 && col1 == col2) * cc[r2][col2 + 1]); } else { int t = a - n; r1 = t + b + 1, r2 = t + 1 + c; col1 = a - r1, col2 = a - r2; if (fit(r1 + 1, col1) && fit(r2 + 1, col2)) d[a][b][c] = max(d[a][b][c], get(a + 1, b, c) + cc[r1 + 1][col1] + cc[r2 + 1][col2] - int(r2 == r1 && col1 == col2) * cc[r1 + 1][col1]); if (fit(r1 + 1, col1) && fit(r2, col2 + 1)) d[a][b][c] = max(d[a][b][c], get(a + 1, b, c - 1) + cc[r1 + 1][col1] + cc[r2][col2 + 1] - int(r2 == r1 + 1 && col2 + 1 == col1) * cc[r2][col2 + 1]); if (fit(r1, col1 + 1) && fit(r2 + 1, col2)) d[a][b][c] = max(d[a][b][c], get(a + 1, b - 1, c) + cc[r1][col1 + 1] + cc[r2 + 1][col2] - int(r2 + 1 == r1 && col1 + 1 == col2) * cc[r2 + 1][col2]); if (fit(r1, col1 + 1) && fit(r2, col2 + 1)) d[a][b][c] = max(d[a][b][c], get(a + 1, b - 1, c - 1) + cc[r1][col1 + 1] + cc[r2][col2 + 1] - int(r1 == r2 && col1 == col2) * cc[r2][col2 + 1]); } return d[a][b][c]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> cc[i][j]; for (int i = 0; i < 2 * MAXN; i++) for (int j = 0; j < MAXN; j++) for (int w = 0; w < MAXN; w++) d[i][j][w] = INF; cout << get(0, 0, 0) + cc[0][0] << endl; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 300 + 10; const int INF = 1e9 + 2; int n; int cc[MAXN][MAXN]; int d[2 * MAXN][MAXN][MAXN]; bool fit(int a, int b) { return a >= 0 && a < n && b >= 0 && b < n; } int get(int a, int b, int c) { if (d[a][b][c] != INF) return d[a][b][c]; if (a == 2 * n - 2) return d[a][b][c] = 0; int r1, r2, col1, col2; d[a][b][c] = -INF; if (a < n - 1) { r1 = b, r2 = c; col1 = a - b, col2 = a - c; if (fit(r1 + 1, col1) && fit(r2 + 1, col2)) d[a][b][c] = max(d[a][b][c], get(a + 1, b + 1, c + 1) + cc[r1 + 1][col1] + cc[r2 + 1][col2] - int(r2 == r1 && col1 == col2) * cc[r1 + 1][col1]); if (fit(r1 + 1, col1) && fit(r2, col2 + 1)) d[a][b][c] = max(d[a][b][c], get(a + 1, b + 1, c) + cc[r1 + 1][col1] + cc[r2][col2 + 1] - int(r2 == r1 + 1 && col2 + 1 == col1) * cc[r2][col2 + 1]); if (fit(r1, col1 + 1) && fit(r2 + 1, col2)) d[a][b][c] = max(d[a][b][c], get(a + 1, b, c + 1) + cc[r1][col1 + 1] + cc[r2 + 1][col2] - int(r2 + 1 == r1 && col1 + 1 == col2) * cc[r2 + 1][col2]); if (fit(r1, col1 + 1) && fit(r2, col2 + 1)) d[a][b][c] = max(d[a][b][c], get(a + 1, b, c) + cc[r1][col1 + 1] + cc[r2][col2 + 1] - int(r1 == r2 && col1 == col2) * cc[r2][col2 + 1]); } else { int t = a - n; r1 = t + b + 1, r2 = t + 1 + c; col1 = a - r1, col2 = a - r2; if (fit(r1 + 1, col1) && fit(r2 + 1, col2)) d[a][b][c] = max(d[a][b][c], get(a + 1, b, c) + cc[r1 + 1][col1] + cc[r2 + 1][col2] - int(r2 == r1 && col1 == col2) * cc[r1 + 1][col1]); if (fit(r1 + 1, col1) && fit(r2, col2 + 1)) d[a][b][c] = max(d[a][b][c], get(a + 1, b, c - 1) + cc[r1 + 1][col1] + cc[r2][col2 + 1] - int(r2 == r1 + 1 && col2 + 1 == col1) * cc[r2][col2 + 1]); if (fit(r1, col1 + 1) && fit(r2 + 1, col2)) d[a][b][c] = max(d[a][b][c], get(a + 1, b - 1, c) + cc[r1][col1 + 1] + cc[r2 + 1][col2] - int(r2 + 1 == r1 && col1 + 1 == col2) * cc[r2 + 1][col2]); if (fit(r1, col1 + 1) && fit(r2, col2 + 1)) d[a][b][c] = max(d[a][b][c], get(a + 1, b - 1, c - 1) + cc[r1][col1 + 1] + cc[r2][col2 + 1] - int(r1 == r2 && col1 == col2) * cc[r2][col2 + 1]); } return d[a][b][c]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> cc[i][j]; for (int i = 0; i < 2 * MAXN; i++) for (int j = 0; j < MAXN; j++) for (int w = 0; w < MAXN; w++) d[i][j][w] = INF; cout << get(0, 0, 0) + cc[0][0] << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e14; const double eps = 1e-10; const double pi = acos(-1.0); int a[305][610]; int dp[305][305][3]; int n; int main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf("%d", &a[i][j + i]); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int k = 0; k <= 1; k++) { dp[i][j][k] = -1e9; } } } dp[1][1][2 & 1] = a[1][2]; for (int d = 2; d <= n + n - 1; d++) { for (int i = 1; i <= min(d + 1, n); i++) { for (int j = 1; j <= min(d + 1, n); j++) { int tx = i + 1; int ty = j; int& res = dp[tx][ty][(d + 1) & 1]; if (tx == ty) res = max(res, dp[i][j][(d & 1)] + a[tx][d + 1]); else res = max(res, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]); tx = i + 1; ty = j + 1; int& res1 = dp[tx][ty][(d + 1) & 1]; if (tx == ty) res1 = max(res1, dp[i][j][d & 1] + a[tx][d + 1]); else res1 = max(res1, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]); tx = i; ty = j; int& res2 = dp[tx][ty][(d + 1) & 1]; if (tx == ty) res2 = max(res2, dp[i][j][d & 1] + a[tx][d + 1]); else res2 = max(res2, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]); tx = i; ty = j + 1; int& res3 = dp[tx][ty][(d + 1) & 1]; if (tx == ty) res3 = max(res3, dp[i][j][d & 1] + a[tx][d + 1]); else res3 = max(res3, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]); dp[i][j][d & 1] = -1e9; } } } cout << dp[n][n][(n + n) & 1] << endl; return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e14; const double eps = 1e-10; const double pi = acos(-1.0); int a[305][610]; int dp[305][305][3]; int n; int main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf("%d", &a[i][j + i]); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int k = 0; k <= 1; k++) { dp[i][j][k] = -1e9; } } } dp[1][1][2 & 1] = a[1][2]; for (int d = 2; d <= n + n - 1; d++) { for (int i = 1; i <= min(d + 1, n); i++) { for (int j = 1; j <= min(d + 1, n); j++) { int tx = i + 1; int ty = j; int& res = dp[tx][ty][(d + 1) & 1]; if (tx == ty) res = max(res, dp[i][j][(d & 1)] + a[tx][d + 1]); else res = max(res, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]); tx = i + 1; ty = j + 1; int& res1 = dp[tx][ty][(d + 1) & 1]; if (tx == ty) res1 = max(res1, dp[i][j][d & 1] + a[tx][d + 1]); else res1 = max(res1, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]); tx = i; ty = j; int& res2 = dp[tx][ty][(d + 1) & 1]; if (tx == ty) res2 = max(res2, dp[i][j][d & 1] + a[tx][d + 1]); else res2 = max(res2, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]); tx = i; ty = j + 1; int& res3 = dp[tx][ty][(d + 1) & 1]; if (tx == ty) res3 = max(res3, dp[i][j][d & 1] + a[tx][d + 1]); else res3 = max(res3, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]); dp[i][j][d & 1] = -1e9; } } } cout << dp[n][n][(n + n) & 1] << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 305; const int maxint = 999999999; int dp[2][maxn][maxn]; int a[maxn][maxn]; int n; bool judge(int x, int y) { return (x >= 1 && y <= n); } int main() { while (scanf("%d", &n) != EOF) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]); dp[1][1][1] = a[1][1]; for (int i = 2; i <= 2 * n - 1; i++) { for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) dp[i % 2][j][k] = -maxint; for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { int x1 = i - j + 1, y1 = j, x2 = i - k + 1, y2 = k; if (judge(x1, y1) == false || judge(x2, y2) == false) continue; int value; if (j == k) value = a[x1][y1]; else value = a[x1][y1] + a[x2][y2]; if (j - 1 >= 1 && k - 1 >= 1) { dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k - 1] + value); } if (j - 1 >= 1 && x2 - 1 >= 1) { dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k] + value); } if (x1 - 1 >= 1 && k - 1 >= 1) { dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k - 1] + value); } if (x1 - 1 >= 1 && x2 - 1 >= 1) { dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k] + value); } } } } printf("%d\n", dp[(2 * n - 1) % 2][n][n]); } return 0; }
### Prompt Develop a solution in CPP to the problem described below: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 305; const int maxint = 999999999; int dp[2][maxn][maxn]; int a[maxn][maxn]; int n; bool judge(int x, int y) { return (x >= 1 && y <= n); } int main() { while (scanf("%d", &n) != EOF) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]); dp[1][1][1] = a[1][1]; for (int i = 2; i <= 2 * n - 1; i++) { for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) dp[i % 2][j][k] = -maxint; for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { int x1 = i - j + 1, y1 = j, x2 = i - k + 1, y2 = k; if (judge(x1, y1) == false || judge(x2, y2) == false) continue; int value; if (j == k) value = a[x1][y1]; else value = a[x1][y1] + a[x2][y2]; if (j - 1 >= 1 && k - 1 >= 1) { dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k - 1] + value); } if (j - 1 >= 1 && x2 - 1 >= 1) { dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k] + value); } if (x1 - 1 >= 1 && k - 1 >= 1) { dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k - 1] + value); } if (x1 - 1 >= 1 && x2 - 1 >= 1) { dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k] + value); } } } } printf("%d\n", dp[(2 * n - 1) % 2][n][n]); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 301; int a[N][N]; int f[N * 2 + 2][N][N]; int i, j, n, x, y, t; int max(int b, int c, int d, int e) { return max(max(b, c), max(d, e)); } void Main() { memset(f, -10, sizeof(f)); memset(a, 0, sizeof(a)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; f[1][1][1] = a[1][1]; for (int i = 2; i <= n + n - 1; i++) for (int x = 1; x <= n; x++) for (int y = 1; y <= n; y++) if (i + 1 - x > 0 && i + 1 - y > 0) { f[i][x][y] = max(f[i - 1][x - 1][y], f[i - 1][x - 1][y - 1], f[i - 1][x][y - 1], f[i - 1][x][y]); if (x != y) f[i][x][y] = f[i][x][y] + a[x][i + 1 - x] + a[y][i + 1 - y]; if (x == y) f[i][x][y] = f[i][x][y] + a[x][i + 1 - x]; } cout << f[n + n - 1][n][n] << endl; } int main() { while (cin >> n && n) { Main(); } return 0; }
### Prompt Please formulate a CPP solution to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 301; int a[N][N]; int f[N * 2 + 2][N][N]; int i, j, n, x, y, t; int max(int b, int c, int d, int e) { return max(max(b, c), max(d, e)); } void Main() { memset(f, -10, sizeof(f)); memset(a, 0, sizeof(a)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; f[1][1][1] = a[1][1]; for (int i = 2; i <= n + n - 1; i++) for (int x = 1; x <= n; x++) for (int y = 1; y <= n; y++) if (i + 1 - x > 0 && i + 1 - y > 0) { f[i][x][y] = max(f[i - 1][x - 1][y], f[i - 1][x - 1][y - 1], f[i - 1][x][y - 1], f[i - 1][x][y]); if (x != y) f[i][x][y] = f[i][x][y] + a[x][i + 1 - x] + a[y][i + 1 - y]; if (x == y) f[i][x][y] = f[i][x][y] + a[x][i + 1 - x]; } cout << f[n + n - 1][n][n] << endl; } int main() { while (cin >> n && n) { Main(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 301, inf = -(1e9 + 1); int n; int a[N][N]; int cache[2 * N][N][N]; int dp(int idx, int i1, int i2) { if (i1 > n || i2 > n) return -1e9; int j1 = idx - i1; int j2 = idx - i2; if (j1 > n || j2 > n) return -1e9; if (idx == 2 * n) return a[i1][j1]; int &ans = cache[idx][i1][i2]; if (ans != inf) return ans; if (i1 == i2) ans = a[i1][j1]; else ans = a[i1][j1] + a[i2][j2]; int best = -1e9; for (int i = 0; i <= 1; i++) for (int j = 0; j <= 1; j++) best = max(best, dp(idx + 1, i1 + i, i2 + j)); ans += best; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 1; i <= 2 * n; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) cache[i][j][k] = inf; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; int ans = dp(2, 1, 1); cout << ans << '\n'; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 301, inf = -(1e9 + 1); int n; int a[N][N]; int cache[2 * N][N][N]; int dp(int idx, int i1, int i2) { if (i1 > n || i2 > n) return -1e9; int j1 = idx - i1; int j2 = idx - i2; if (j1 > n || j2 > n) return -1e9; if (idx == 2 * n) return a[i1][j1]; int &ans = cache[idx][i1][i2]; if (ans != inf) return ans; if (i1 == i2) ans = a[i1][j1]; else ans = a[i1][j1] + a[i2][j2]; int best = -1e9; for (int i = 0; i <= 1; i++) for (int j = 0; j <= 1; j++) best = max(best, dp(idx + 1, i1 + i, i2 + j)); ans += best; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 1; i <= 2 * n; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) cache[i][j][k] = inf; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; int ans = dp(2, 1, 1); cout << ans << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a[305][305]; int p[2] = {0, 1}; int memo[305][305][305]; int dfss(int x1, int x2, int tot) { if (memo[x1][x2][tot] != -1) return memo[x1][x2][tot]; if (x1 == n && x2 == n && tot == 2 * n) return 0; int hsl = -900000; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { int lol1 = x1 + p[i]; int lol2 = x2 + p[j]; if (x1 > n || x2 > n || (tot - x1) > n || (tot - x2) > n) continue; if (lol1 == lol2) hsl = max(hsl, dfss(lol1, lol2, tot + 1) + a[lol1][tot - lol1 + 1]); else hsl = max(hsl, dfss(lol1, lol2, tot + 1) + a[lol1][tot - lol1 + 1] + a[lol2][tot - lol2 + 1]); } } return memo[x1][x2][tot] = hsl; } int main() { scanf("%d", &n); memset(memo, -1, sizeof(memo)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf("%d", &a[i][j]); } } int ans = dfss(1, 1, 2); printf("%d\n", ans + a[1][1]); return 0; }
### Prompt Generate a Cpp solution to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a[305][305]; int p[2] = {0, 1}; int memo[305][305][305]; int dfss(int x1, int x2, int tot) { if (memo[x1][x2][tot] != -1) return memo[x1][x2][tot]; if (x1 == n && x2 == n && tot == 2 * n) return 0; int hsl = -900000; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { int lol1 = x1 + p[i]; int lol2 = x2 + p[j]; if (x1 > n || x2 > n || (tot - x1) > n || (tot - x2) > n) continue; if (lol1 == lol2) hsl = max(hsl, dfss(lol1, lol2, tot + 1) + a[lol1][tot - lol1 + 1]); else hsl = max(hsl, dfss(lol1, lol2, tot + 1) + a[lol1][tot - lol1 + 1] + a[lol2][tot - lol2 + 1]); } } return memo[x1][x2][tot] = hsl; } int main() { scanf("%d", &n); memset(memo, -1, sizeof(memo)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf("%d", &a[i][j]); } } int ans = dfss(1, 1, 2); printf("%d\n", ans + a[1][1]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int F[605][305][305], a[305][305], n; int main() { if (0) freopen("input.txt", "r", stdin); ; scanf("%d", &n); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]); for (int i = 0; i <= 2 * n; ++i) for (int j = 0; j <= n; ++j) for (int k = 0; k <= n; ++k) F[i][j][k] = -1000000000; F[0][1][1] = a[1][1]; for (int i = 1; i <= 2 * n - 2; ++i) for (int j = max(1, i - (n - 2)); j <= min(n, i + 1); ++j) for (int k = max(1, i - (n - 2)); k <= min(n, i + 1); ++k) { F[i][j][k] = max(F[i - 1][j][k], max(F[i - 1][j - 1][k], max(F[i - 1][j][k - 1], F[i - 1][j - 1][k - 1]))); if (F[i][j][k] == -1000000000) continue; F[i][j][k] += a[j][i - j + 2] + a[k][i - k + 2]; if (j == k) F[i][j][k] -= a[j][i - j + 2]; } printf("%d\n", F[2 * n - 2][n][n]); return 0; }
### Prompt In cpp, your task is to solve the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int F[605][305][305], a[305][305], n; int main() { if (0) freopen("input.txt", "r", stdin); ; scanf("%d", &n); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]); for (int i = 0; i <= 2 * n; ++i) for (int j = 0; j <= n; ++j) for (int k = 0; k <= n; ++k) F[i][j][k] = -1000000000; F[0][1][1] = a[1][1]; for (int i = 1; i <= 2 * n - 2; ++i) for (int j = max(1, i - (n - 2)); j <= min(n, i + 1); ++j) for (int k = max(1, i - (n - 2)); k <= min(n, i + 1); ++k) { F[i][j][k] = max(F[i - 1][j][k], max(F[i - 1][j - 1][k], max(F[i - 1][j][k - 1], F[i - 1][j - 1][k - 1]))); if (F[i][j][k] == -1000000000) continue; F[i][j][k] += a[j][i - j + 2] + a[k][i - k + 2]; if (j == k) F[i][j][k] -= a[j][i - j + 2]; } printf("%d\n", F[2 * n - 2][n][n]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int mat[300 + 10][300 + 10]; int sol[300 + 10][300 + 10][300 + 10]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) sol[i][j][k] = -100000000; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &mat[j][i]); for (int w = 0; w < n; w++) { if (w > 0) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) sol[w][i][j] = sol[w - 1][i][j] + mat[w][i] + mat[w][j]; else sol[w][i][j] = sol[w - 1][i][j] + mat[w][i]; } else sol[0][0][0] = mat[0][0]; for (int j = 0; j < n; j++) for (int i = 1; i < j; i++) sol[w][i][j] = max(sol[w][i][j], sol[w][i - 1][j] + mat[w][i]); for (int i = 1; i < n; i++) sol[w][i][i] = max(sol[w][i][i], sol[w][i - 1][i]); for (int i = 1; i < n; i++) sol[w][i][i] = max(sol[w][i][i], sol[w][i - 1][i - 1] + mat[w][i]); for (int i = 0; i <= n; i++) for (int j = i + 1; j < n; j++) sol[w][i][j] = max(sol[w][i][j], sol[w][i][j - 1] + mat[w][j]); } printf("%d", sol[n - 1][n - 1][n - 1]); return 0; }
### Prompt Develop a solution in cpp to the problem described below: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int mat[300 + 10][300 + 10]; int sol[300 + 10][300 + 10][300 + 10]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) sol[i][j][k] = -100000000; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &mat[j][i]); for (int w = 0; w < n; w++) { if (w > 0) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) sol[w][i][j] = sol[w - 1][i][j] + mat[w][i] + mat[w][j]; else sol[w][i][j] = sol[w - 1][i][j] + mat[w][i]; } else sol[0][0][0] = mat[0][0]; for (int j = 0; j < n; j++) for (int i = 1; i < j; i++) sol[w][i][j] = max(sol[w][i][j], sol[w][i - 1][j] + mat[w][i]); for (int i = 1; i < n; i++) sol[w][i][i] = max(sol[w][i][i], sol[w][i - 1][i]); for (int i = 1; i < n; i++) sol[w][i][i] = max(sol[w][i][i], sol[w][i - 1][i - 1] + mat[w][i]); for (int i = 0; i <= n; i++) for (int j = i + 1; j < n; j++) sol[w][i][j] = max(sol[w][i][j], sol[w][i][j - 1] + mat[w][j]); } printf("%d", sol[n - 1][n - 1][n - 1]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int dp[0x12d * 2][0x12d][0x12d]; int v[0x12d][0x12d]; inline int max(int a, int b, int c, int d) { if (a < b) a = b; if (a < c) a = c; if (a < d) a = d; return a; } int main() { int N; cin >> N; for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) scanf("%d", v[i] + j); for (int i = 0; i < 2 * N; ++i) for (int j = 0; j <= N; ++j) for (int k = 0; k <= N; ++k) dp[i][j][k] = -0x80000000; dp[1][1][1] = v[1][1]; for (int i = 2; i <= N; ++i) for (int j = 1; j <= i; ++j) { dp[i][j][j] = v[i + 1 - j][j] + max(dp[i - 1][j - 1][j - 1], dp[i - 1][j - 1][j], dp[i - 1][j][j - 1], dp[i - 1][j][j]); for (int k = j + 1; k <= i; ++k) dp[i][j][k] = v[i + 1 - j][j] + v[i + 1 - k][k] + max(dp[i - 1][j][k], dp[i - 1][j - 1][k], dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1]); } for (int i = N + 1; i < 2 * N; ++i) for (int j = i + 1 - N; j <= N; ++j) { dp[i][j][j] = v[i + 1 - j][j] + max(dp[i - 1][j - 1][j - 1], dp[i - 1][j - 1][j], dp[i - 1][j][j - 1], dp[i - 1][j][j]); for (int k = j + 1; k <= N; ++k) dp[i][j][k] = v[i + 1 - j][j] + v[i + 1 - k][k] + max(dp[i - 1][j][k], dp[i - 1][j - 1][k], dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1]); } cout << dp[2 * N - 1][N][N] << '\n'; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dp[0x12d * 2][0x12d][0x12d]; int v[0x12d][0x12d]; inline int max(int a, int b, int c, int d) { if (a < b) a = b; if (a < c) a = c; if (a < d) a = d; return a; } int main() { int N; cin >> N; for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) scanf("%d", v[i] + j); for (int i = 0; i < 2 * N; ++i) for (int j = 0; j <= N; ++j) for (int k = 0; k <= N; ++k) dp[i][j][k] = -0x80000000; dp[1][1][1] = v[1][1]; for (int i = 2; i <= N; ++i) for (int j = 1; j <= i; ++j) { dp[i][j][j] = v[i + 1 - j][j] + max(dp[i - 1][j - 1][j - 1], dp[i - 1][j - 1][j], dp[i - 1][j][j - 1], dp[i - 1][j][j]); for (int k = j + 1; k <= i; ++k) dp[i][j][k] = v[i + 1 - j][j] + v[i + 1 - k][k] + max(dp[i - 1][j][k], dp[i - 1][j - 1][k], dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1]); } for (int i = N + 1; i < 2 * N; ++i) for (int j = i + 1 - N; j <= N; ++j) { dp[i][j][j] = v[i + 1 - j][j] + max(dp[i - 1][j - 1][j - 1], dp[i - 1][j - 1][j], dp[i - 1][j][j - 1], dp[i - 1][j][j]); for (int k = j + 1; k <= N; ++k) dp[i][j][k] = v[i + 1 - j][j] + v[i + 1 - k][k] + max(dp[i - 1][j][k], dp[i - 1][j - 1][k], dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1]); } cout << dp[2 * N - 1][N][N] << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 300 + 2; const int MAXA = 1e3 + 3; const int INF = 0x3f3f3f3f; int n; int a[MAXN][MAXN]; int dp[MAXN * 2][MAXN][MAXN]; bool outOfRange(int x, int y) { return x < 0 || x >= n || y < 0 || y >= n; } void upmax(int &x, int v) { if (x < v) x = v; } void solve() { for (int i = 0; i < (int)(2 * n); ++i) for (int j = 0; j < (int)(n); ++j) for (int k = 0; k < (int)(n); ++k) dp[i][j][k] = -INF; dp[0][0][0] = a[0][0]; for (int i = 0; i < (int)(2 * (n - 1)); ++i) for (int j = 0; j < (int)(n); ++j) for (int k = 0; k < (int)(n); ++k) { int dx[2] = {0, 1}; int dy[2] = {1, 0}; for (int d1 = 0; d1 < 2; ++d1) { int nx1 = j + dx[d1]; int ny1 = (i - j) + dy[d1]; if (outOfRange(nx1, ny1)) continue; for (int d2 = 0; d2 < 2; ++d2) { int nx2 = k + dx[d2]; int ny2 = (i - k) + dy[d2]; if (outOfRange(nx2, ny2)) continue; if (nx1 == nx2 && ny1 == ny2) upmax(dp[i + 1][nx1][nx2], dp[i][j][k] + a[nx1][ny1]); else upmax(dp[i + 1][nx1][nx2], dp[i][j][k] + a[nx1][ny1] + a[nx2][ny2]); } } } printf("%d\n", dp[2 * (n - 1)][n - 1][n - 1]); } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) scanf("%d", &a[i][j]); solve(); return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 300 + 2; const int MAXA = 1e3 + 3; const int INF = 0x3f3f3f3f; int n; int a[MAXN][MAXN]; int dp[MAXN * 2][MAXN][MAXN]; bool outOfRange(int x, int y) { return x < 0 || x >= n || y < 0 || y >= n; } void upmax(int &x, int v) { if (x < v) x = v; } void solve() { for (int i = 0; i < (int)(2 * n); ++i) for (int j = 0; j < (int)(n); ++j) for (int k = 0; k < (int)(n); ++k) dp[i][j][k] = -INF; dp[0][0][0] = a[0][0]; for (int i = 0; i < (int)(2 * (n - 1)); ++i) for (int j = 0; j < (int)(n); ++j) for (int k = 0; k < (int)(n); ++k) { int dx[2] = {0, 1}; int dy[2] = {1, 0}; for (int d1 = 0; d1 < 2; ++d1) { int nx1 = j + dx[d1]; int ny1 = (i - j) + dy[d1]; if (outOfRange(nx1, ny1)) continue; for (int d2 = 0; d2 < 2; ++d2) { int nx2 = k + dx[d2]; int ny2 = (i - k) + dy[d2]; if (outOfRange(nx2, ny2)) continue; if (nx1 == nx2 && ny1 == ny2) upmax(dp[i + 1][nx1][nx2], dp[i][j][k] + a[nx1][ny1]); else upmax(dp[i + 1][nx1][nx2], dp[i][j][k] + a[nx1][ny1] + a[nx2][ny2]); } } } printf("%d\n", dp[2 * (n - 1)][n - 1][n - 1]); } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) scanf("%d", &a[i][j]); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long dp[5][305][305]; long long a[305][305]; long long max(long long a, long long b, long long c, long long d) { return max(a, max(b, max(c, d))); } int main(int argc, char** argv) { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf("%lld", &a[i][j]); } } dp[0][1][1] = a[1][1] + 100000000000000; for (int i = 1; i <= (2 * (n - 1)); i++) { memset(dp[i % 2], 0, sizeof(dp[i % 2])); for (int j = 1; j <= i + 1; j++) { for (int k = 1; k <= j; k++) { long long aa = ((j == k) ? a[j][i - j + 2] : a[j][i - j + 2] + a[k][i - k + 2]); dp[(i % 2)][j][k] = max(dp[(i + 1) % 2][j - 1][k], dp[(i + 1) % 2][j][k], dp[(i + 1) % 2][j][k - 1], dp[(i + 1) % 2][j - 1][k - 1]) + aa; } } } cout << dp[0][n][n] - 100000000000000; return 0; }
### Prompt In Cpp, your task is to solve the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long dp[5][305][305]; long long a[305][305]; long long max(long long a, long long b, long long c, long long d) { return max(a, max(b, max(c, d))); } int main(int argc, char** argv) { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf("%lld", &a[i][j]); } } dp[0][1][1] = a[1][1] + 100000000000000; for (int i = 1; i <= (2 * (n - 1)); i++) { memset(dp[i % 2], 0, sizeof(dp[i % 2])); for (int j = 1; j <= i + 1; j++) { for (int k = 1; k <= j; k++) { long long aa = ((j == k) ? a[j][i - j + 2] : a[j][i - j + 2] + a[k][i - k + 2]); dp[(i % 2)][j][k] = max(dp[(i + 1) % 2][j - 1][k], dp[(i + 1) % 2][j][k], dp[(i + 1) % 2][j][k - 1], dp[(i + 1) % 2][j - 1][k - 1]) + aa; } } } cout << dp[0][n][n] - 100000000000000; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const long long MAX = 2005; int k; int dp[605][305][305]; int arr[305][305]; bool ok(int dist, int a, int c) { int b = dist - a; int d = dist - c; return ((0 <= a) && (0 <= b) && (0 <= c) && (0 <= d) && (a < k) && (b < k) && (c < k) && (d < k)); } int add(int dist, int a, int c) { int ans = 0; int b = dist - a; int d = dist - c; if (a == c && b == d) { ans += arr[a][b]; } else { ans += arr[a][b] + arr[c][d]; } return ans; } int main() { cin >> k; for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { scanf("%d", &arr[i][j]); } } memset(dp, -10000, sizeof dp); dp[0][0][0] = arr[0][0]; for (int d = 0; d < 2 * k - 1; d++) { for (int a = 0; a < d + 1; a++) { for (int b = 0; b < d + 1; b++) { int x = d - a; int y = d - b; if (ok(d + 1, a, b + 1)) { dp[d + 1][a][b + 1] = max(dp[d + 1][a][b + 1], dp[d][a][b] + add(d + 1, a, b + 1)); } if (ok(d + 1, a + 1, b)) { dp[d + 1][a + 1][b] = max(dp[d + 1][a + 1][b], dp[d][a][b] + add(d + 1, a + 1, b)); } if (ok(d + 1, a, b)) { dp[d + 1][a][b] = max(dp[d + 1][a][b], dp[d][a][b] + add(d + 1, a, b)); } if (ok(d + 1, a + 1, b + 1)) { dp[d + 1][a + 1][b + 1] = max(dp[d + 1][a + 1][b + 1], dp[d][a][b] + add(d + 1, a + 1, b + 1)); } } } } int d = 2 * k - 2; cout << dp[d][d / 2][d / 2] << endl; }
### Prompt Please provide a CPP coded solution to the problem described below: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const long long MAX = 2005; int k; int dp[605][305][305]; int arr[305][305]; bool ok(int dist, int a, int c) { int b = dist - a; int d = dist - c; return ((0 <= a) && (0 <= b) && (0 <= c) && (0 <= d) && (a < k) && (b < k) && (c < k) && (d < k)); } int add(int dist, int a, int c) { int ans = 0; int b = dist - a; int d = dist - c; if (a == c && b == d) { ans += arr[a][b]; } else { ans += arr[a][b] + arr[c][d]; } return ans; } int main() { cin >> k; for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { scanf("%d", &arr[i][j]); } } memset(dp, -10000, sizeof dp); dp[0][0][0] = arr[0][0]; for (int d = 0; d < 2 * k - 1; d++) { for (int a = 0; a < d + 1; a++) { for (int b = 0; b < d + 1; b++) { int x = d - a; int y = d - b; if (ok(d + 1, a, b + 1)) { dp[d + 1][a][b + 1] = max(dp[d + 1][a][b + 1], dp[d][a][b] + add(d + 1, a, b + 1)); } if (ok(d + 1, a + 1, b)) { dp[d + 1][a + 1][b] = max(dp[d + 1][a + 1][b], dp[d][a][b] + add(d + 1, a + 1, b)); } if (ok(d + 1, a, b)) { dp[d + 1][a][b] = max(dp[d + 1][a][b], dp[d][a][b] + add(d + 1, a, b)); } if (ok(d + 1, a + 1, b + 1)) { dp[d + 1][a + 1][b + 1] = max(dp[d + 1][a + 1][b + 1], dp[d][a][b] + add(d + 1, a + 1, b + 1)); } } } } int d = 2 * k - 2; cout << dp[d][d / 2][d / 2] << endl; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e6; const long long mod = 1e9 + 7; const long long inf = 1e9; long long a[610][610], f[2][610][610]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); int n; cin >> n; for (int i = 0; i <= 600; i++) for (int j = 0; j <= 600; j++) { a[i][j] = -inf; f[0][i][j] = -inf; f[1][i][j] = -inf; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; f[0][1][1] = a[1][1]; for (int k = 3; k <= n + n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int x = k % 2, y = 1 - x; if (k - i > n || k - j > n || k - i < 1 || k - j < 1) { f[x][i][j] = -inf; continue; } long long w = -inf; for (int dx = -1; dx <= 0; dx++) for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]); f[x][i][j] = w; if (i != j) f[x][i][j] += a[i][k - i] + a[j][k - j]; else f[x][i][j] += a[i][k - i]; } } } cout << f[0][n][n]; }
### Prompt Create a solution in cpp for the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e6; const long long mod = 1e9 + 7; const long long inf = 1e9; long long a[610][610], f[2][610][610]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); int n; cin >> n; for (int i = 0; i <= 600; i++) for (int j = 0; j <= 600; j++) { a[i][j] = -inf; f[0][i][j] = -inf; f[1][i][j] = -inf; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; f[0][1][1] = a[1][1]; for (int k = 3; k <= n + n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int x = k % 2, y = 1 - x; if (k - i > n || k - j > n || k - i < 1 || k - j < 1) { f[x][i][j] = -inf; continue; } long long w = -inf; for (int dx = -1; dx <= 0; dx++) for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]); f[x][i][j] = w; if (i != j) f[x][i][j] += a[i][k - i] + a[j][k - j]; else f[x][i][j] += a[i][k - i]; } } } cout << f[0][n][n]; } ```
#include <bits/stdc++.h> using namespace std; int Map[305][305], F[305 << 1][305][305]; int X[4] = {-1, -1, 0, 0}; int Y[4] = {-1, 0, -1, 0}; int main() { int i, j, k, n, t; memset(F, 0xCC, sizeof(F)); for (i = scanf("%d", &n); i <= n; i++) for (j = 1; j <= n; j++) scanf("%d", &Map[i][j]); for (F[0][i = 1][1] = Map[1][1]; i <= 2 * n - 2; i++) for (j = 1; j <= n && j <= i + 1; j++) for (k = j; k <= n && k <= i + 1; k++) { for (t = 0; t < 4; t++) F[i][j][k] = max(F[i][j][k], F[i - 1][j + X[t]][k + Y[t]]); F[i][j][k] += Map[j][i - j + 2] + Map[k][i - k + 2] - (j == k ? Map[k][i - k + 2] : 0); } printf("%d\n", F[2 * n - 2][n][n]); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int Map[305][305], F[305 << 1][305][305]; int X[4] = {-1, -1, 0, 0}; int Y[4] = {-1, 0, -1, 0}; int main() { int i, j, k, n, t; memset(F, 0xCC, sizeof(F)); for (i = scanf("%d", &n); i <= n; i++) for (j = 1; j <= n; j++) scanf("%d", &Map[i][j]); for (F[0][i = 1][1] = Map[1][1]; i <= 2 * n - 2; i++) for (j = 1; j <= n && j <= i + 1; j++) for (k = j; k <= n && k <= i + 1; k++) { for (t = 0; t < 4; t++) F[i][j][k] = max(F[i][j][k], F[i - 1][j + X[t]][k + Y[t]]); F[i][j][k] += Map[j][i - j + 2] + Map[k][i - k + 2] - (j == k ? Map[k][i - k + 2] : 0); } printf("%d\n", F[2 * n - 2][n][n]); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int n, a[303][303], dp[303][303][303], xx[] = {1, 0}, yy[] = {0, 1}, ans; int f(int x, int y, int z) { if (x == n - 1 && y == n - 1) return 0 + a[x][y]; if (dp[x][y][z] != -1e9) return dp[x][y][z]; int ans = -1e9, k = (x + y) - z; for (int i = 0; i < 2; i++) { int tx = x + xx[i], ty = y + yy[i]; for (int j = 0; j < 2; j++) { int tz = z + xx[j], tk = k + yy[j]; if (tx < n && ty < n && tz < n && tk < n) { if (x == z && y == k) ans = max(ans, f(tx, ty, tz) + a[x][y]); else ans = max(ans, f(tx, ty, tz) + a[x][y] + a[z][k]); } } } return dp[x][y][z] = ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int z = 0; z < n; z++) dp[i][j][z] = -1e9; cin >> a[i][j]; } } cout << f(0, 0, 0); }
### Prompt In CPP, your task is to solve the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int n, a[303][303], dp[303][303][303], xx[] = {1, 0}, yy[] = {0, 1}, ans; int f(int x, int y, int z) { if (x == n - 1 && y == n - 1) return 0 + a[x][y]; if (dp[x][y][z] != -1e9) return dp[x][y][z]; int ans = -1e9, k = (x + y) - z; for (int i = 0; i < 2; i++) { int tx = x + xx[i], ty = y + yy[i]; for (int j = 0; j < 2; j++) { int tz = z + xx[j], tk = k + yy[j]; if (tx < n && ty < n && tz < n && tk < n) { if (x == z && y == k) ans = max(ans, f(tx, ty, tz) + a[x][y]); else ans = max(ans, f(tx, ty, tz) + a[x][y] + a[z][k]); } } } return dp[x][y][z] = ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int z = 0; z < n; z++) dp[i][j][z] = -1e9; cin >> a[i][j]; } } cout << f(0, 0, 0); } ```
#include <bits/stdc++.h> using namespace std; const int N = 310; const int INF = 100000000; int n, v[N][N], dp[N * 2][N][N]; bool valid(int x, int y) { if (x <= 0 || x > n || y <= 0 || y > n) return false; return true; } bool same(int x1, int y1, int x2, int y2) { if (x1 == x2 && y1 == y2) return true; return false; } int main() { memset(dp, 0, sizeof(dp)); scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%d", &v[i][j]); dp[0][1][1] = v[1][1]; for (int i = 1; i <= 2 * n - 2; i++) for (int x1 = 1; x1 <= n; x1++) for (int x2 = 1; x2 <= n; x2++) { int y1 = i + 2 - x1; int y2 = i + 2 - x2; int t = 0, ans = -INF; if (!same(x1, y1, x2, y2)) t = v[x1][y1] + v[x2][y2]; else t = v[x1][y1]; if (!valid(x1, y1) || !valid(x2, y2)) continue; if (valid(x1 - 1, y1)) { if (valid(x2 - 1, y2)) ans = max(ans, t + dp[i - 1][x1 - 1][x2 - 1]); if (valid(x2, y2 - 1)) ans = max(ans, t + dp[i - 1][x1 - 1][x2]); } if (valid(x1, y1 - 1)) { if (valid(x2 - 1, y2)) ans = max(ans, t + dp[i - 1][x1][x2 - 1]); if (valid(x2, y2 - 1)) ans = max(ans, t + dp[i - 1][x1][x2]); } dp[i][x1][x2] = ans; } printf("%d\n", dp[2 * n - 2][n][n]); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 310; const int INF = 100000000; int n, v[N][N], dp[N * 2][N][N]; bool valid(int x, int y) { if (x <= 0 || x > n || y <= 0 || y > n) return false; return true; } bool same(int x1, int y1, int x2, int y2) { if (x1 == x2 && y1 == y2) return true; return false; } int main() { memset(dp, 0, sizeof(dp)); scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%d", &v[i][j]); dp[0][1][1] = v[1][1]; for (int i = 1; i <= 2 * n - 2; i++) for (int x1 = 1; x1 <= n; x1++) for (int x2 = 1; x2 <= n; x2++) { int y1 = i + 2 - x1; int y2 = i + 2 - x2; int t = 0, ans = -INF; if (!same(x1, y1, x2, y2)) t = v[x1][y1] + v[x2][y2]; else t = v[x1][y1]; if (!valid(x1, y1) || !valid(x2, y2)) continue; if (valid(x1 - 1, y1)) { if (valid(x2 - 1, y2)) ans = max(ans, t + dp[i - 1][x1 - 1][x2 - 1]); if (valid(x2, y2 - 1)) ans = max(ans, t + dp[i - 1][x1 - 1][x2]); } if (valid(x1, y1 - 1)) { if (valid(x2 - 1, y2)) ans = max(ans, t + dp[i - 1][x1][x2 - 1]); if (valid(x2, y2 - 1)) ans = max(ans, t + dp[i - 1][x1][x2]); } dp[i][x1][x2] = ans; } printf("%d\n", dp[2 * n - 2][n][n]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a[305][305], dp[2 * 305][305][305]; int f(int r1, int c1, int r2, int c2) { if (r1 > n || r2 > n || c1 > n || c2 > n) return -100000000; if (r1 == n && c1 == n) return a[n][n]; int t = r1 + c1 - 1; if (dp[t][c1][c2] != -1) return dp[t][c1][c2]; int ans = f(r1 + 1, c1, r2 + 1, c2); ans = max(ans, f(r1 + 1, c1, r2, c2 + 1)); ans = max(ans, f(r1, c1 + 1, r2 + 1, c2)); ans = max(ans, f(r1, c1 + 1, r2, c2 + 1)); ans += a[r1][c1]; if (r1 != r2 || c1 != c2) ans += a[r2][c2]; return dp[t][c1][c2] = ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { scanf("%d", &a[i][j]); for (int t = 1; t < 2 * n; t++) dp[t][i][j] = -1; } printf("%d\n", f(1, 1, 1, 1)); return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ— n cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1 ≀ n ≀ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≀ ai, j ≀ 1000) is the number written in the cell with coordinates (i, j). Output On a single line print a single number β€” the answer to the problem. Examples Input 1 5 Output 5 Input 2 11 14 16 12 Output 53 Input 3 25 16 25 12 18 19 11 13 8 Output 136 Note Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: <image> Furik's path is marked with yellow, and Rubik's path is marked with pink. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a[305][305], dp[2 * 305][305][305]; int f(int r1, int c1, int r2, int c2) { if (r1 > n || r2 > n || c1 > n || c2 > n) return -100000000; if (r1 == n && c1 == n) return a[n][n]; int t = r1 + c1 - 1; if (dp[t][c1][c2] != -1) return dp[t][c1][c2]; int ans = f(r1 + 1, c1, r2 + 1, c2); ans = max(ans, f(r1 + 1, c1, r2, c2 + 1)); ans = max(ans, f(r1, c1 + 1, r2 + 1, c2)); ans = max(ans, f(r1, c1 + 1, r2, c2 + 1)); ans += a[r1][c1]; if (r1 != r2 || c1 != c2) ans += a[r2][c2]; return dp[t][c1][c2] = ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { scanf("%d", &a[i][j]); for (int t = 1; t < 2 * n; t++) dp[t][i][j] = -1; } printf("%d\n", f(1, 1, 1, 1)); return 0; } ```