Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; int n, m, vis[500010]; int cur[500010]; vector<int> G[500010]; vector<int> bbc[500010]; queue<int> Q; queue<int> X; int check(int x, int y) { int L = 0, R = G[x].size() - 1; while (L <= R) { int M = (L + R) / 2; if (G[x][M] == y) { return 1; } else if (G[x][M] < y) { L = M + 1; } else { R = M - 1; } } return 0; } int main() { while (scanf("%d%d", &n, &m) != EOF) { int i, j, u, v, cnt = 0; for (i = 0; i <= n; i++) { G[i].clear(); bbc[i].clear(); cur[i] = 1; } for (i = 0; i < m; i++) { scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } for (i = 1; i <= n; i++) { sort(G[i].begin(), G[i].end()); vis[i] = 0; X.push(i); } for (i = 1; i <= n; i++) { if (!vis[i]) { vis[i] = 1; bbc[cnt].push_back(i); Q.push(i); while (!Q.empty()) { u = Q.front(); Q.pop(); m = X.size(); for (j = 0; j < m; j++) { v = X.front(); X.pop(); if (vis[v]) { Q.push(v); continue; } if (!check(v, u)) { Q.push(v); vis[v] = 1; bbc[cnt].push_back(v); } else { X.push(v); } } } cnt++; } } printf("%d\n", cnt); for (i = 0; i < cnt; i++) { printf("%d", bbc[i].size()); for (j = 0; j < bbc[i].size(); j++) { printf(" %d", bbc[i][j]); } printf("\n"); } } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; int i, j, k, n, m, x, y, T, ans, big, cas, num, len; bool flag; vector<int> G[500005], out[500005]; set<int> st; void bfs(int u) { queue<int> q; q.push(u); while (!q.empty()) { u = q.front(); q.pop(); out[ans].push_back(u); for (set<int>::iterator it = st.begin(); it != st.end();) { int v = *it; it++; if (!binary_search(G[u].begin(), G[u].end(), v)) { q.push(v); st.erase(v); } } } } int main() { scanf("%d%d", &n, &m); for (i = 1; i <= m; i++) { scanf("%d %d", &x, &y); G[x].push_back(y); G[y].push_back(x); } for (i = 1; i <= n; i++) { st.insert(i); sort(G[i].begin(), G[i].end()); } ans = 0; while (!st.empty()) { int u = *(st.begin()); st.erase(u); ans++; bfs(u); } printf("%d\n", ans); for (i = 1; i <= ans; i++) { int size = out[i].size(); printf("%d ", size); for (j = 0; j < size - 1; j++) { printf("%d ", out[i][j]); } printf("%d\n", out[i][size - 1]); } return 0; }
CPP
190_E. Counter Attack
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.
2
11
import java.util.List; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.ArrayList; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.TreeSet; import java.util.StringTokenizer; import java.math.BigInteger; import java.util.Collections; import java.util.Collection; import java.io.InputStream; /** * TEST * * Built using CHelper plug-in * Actual solution is at the top * @author AlexFetisov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE_Counter solver = new TaskE_Counter(); solver.solve(1, in, out); out.close(); } } class TaskE_Counter { Graph g; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); g = new Graph(); g.initGraph(n, m); for (int i = 0; i < m; ++i) { g.addEdge(in.nextInt() - 1, in.nextInt() - 1); } TreeSet<Integer> contains = new TreeSet<Integer>(); for (int i = 0; i < n; i++) { contains.add(i); } int[] queue = new int[n]; int size, iv, i, v, j, to; List<Integer> color; List<List<Integer>> result = new ArrayList<List<Integer>>(); for (iv = 0; iv < n; ++iv) { if (contains.contains(iv)) { size = 0; queue[size++] = iv; color = new ArrayList<Integer>(); contains.remove(iv); for (i = 0; i < size; ++i) { color.add(queue[i] + 1); TreeSet<Integer> toRemove = new TreeSet<Integer>(); for (j = g.first[queue[i]]; j != -1; j = g.next[j]) { to = g.to[j]; if (contains.contains(to)) { toRemove.add(to); contains.remove(to); } } for (int x : contains) { queue[size++] = x; } if (contains.size() > 0) { contains.clear(); } contains.addAll(toRemove); } result.add(color); } } out.println(result.size()); for (List<Integer> list : result) { out.print(list.size()); for (int vv : list) { out.print(" " + vv); } out.println(); } } } class Graph { public int[] from; public int[] to; public int[] first; public int[] next; public int nVertex; public int nEdges; public int curEdge; public Graph() {} public void initGraph(int n, int m) { curEdge = 0; nVertex = n; nEdges = m; from = new int[m * 2]; to = new int[m * 2]; first = new int[n]; next = new int[m * 2]; Arrays.fill(first, -1); } public void addEdge(int a, int b) { next[curEdge] = first[a]; first[a] = curEdge; to[curEdge] = b; from[curEdge] = a; ++curEdge; next[curEdge] = first[b]; first[b] = curEdge; to[curEdge] = a; from[curEdge] = b; ++curEdge; } } class InputReader { private BufferedReader reader; private StringTokenizer stt; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() { try { return reader.readLine().trim(); } catch (IOException e) { return null; } } public String nextString() { while (stt == null || !stt.hasMoreTokens()) { stt = new StringTokenizer(nextLine()); } return stt.nextToken(); } public int nextInt() { return Integer.parseInt(nextString()); } }
JAVA
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; char buf[1 << 21], *p1 = buf, *p2 = buf; template <class T> void read(T &x) { x = 0; int c = getchar(); int flag = 0; while (c < '0' || c > '9') flag |= (c == '-'), c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); if (flag) x = -x; } template <class T> T _max(T a, T b) { return b < a ? a : b; } template <class T> T _min(T a, T b) { return a < b ? a : b; } template <class T> bool checkmax(T &a, T b) { return a < b ? a = b, 1 : 0; } template <class T> bool checkmin(T &a, T b) { return b < a ? a = b, 1 : 0; } const int N = 500005, M = 1000005; int n, m; int e = 0, first[N], enxt[M << 1], point[M << 1]; int pre[N], nxt[N]; int cnt = 0, vis[N], cov[N]; int q[N]; vector<int> v[N]; void add_edge(int x, int y) { enxt[++e] = first[x]; first[x] = e; point[e] = y; } void remove(int x) { nxt[pre[x]] = nxt[x]; pre[nxt[x]] = pre[x]; } void init() { read(n); read(m); memset(first, -1, sizeof first); for (int i = 0; i < n; ++i) nxt[i] = i + 1; for (int i = 1; i <= n; ++i) pre[i] = i - 1; for (int i = 1, u, v; i <= m; ++i) { read(u); read(v); add_edge(u, v); add_edge(v, u); } } void bfs(int idx) { int head = 1, tail = 0; q[++tail] = idx; remove(idx); while (head <= tail) { int x = q[head++]; v[cnt].push_back(x); for (int i = first[x]; ~i; i = enxt[i]) { int to = point[i]; cov[to] = 1; } for (int t = nxt[0]; t; t = nxt[t]) { if (cov[t]) cov[t] = 0; else { remove(t); vis[t] = 1; q[++tail] = t; } } } } void solve() { for (int i = 1; i <= n; ++i) if (!vis[i]) ++cnt, bfs(i); printf("%d\n", cnt); for (int i = 1; i <= cnt; ++i) { sort(v[i].begin(), v[i].end()); printf("%d", (int)v[i].size()); for (int j = 0; j < (int)v[i].size(); ++j) { printf(" %d", v[i][j]); } printf("\n"); } } int main() { init(); solve(); return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; int const MN = 5e5 + 100; int n, m, c; vector<int> ans[MN], vis, adj[MN]; bool mark[MN]; queue<int> q; int main() { scanf("%d %d", &n, &m); for (int i = 0, u, v; i < m; i++) { scanf("%d %d", &u, &v); u--, v--; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 0; i < n; i++) { sort(adj[i].begin(), adj[i].end()); vis.push_back(i); } for (int i = 0; i < n; i++) { if (mark[i]) continue; ans[c].push_back(i); mark[i] = 1; q.push(i); while (!q.empty()) { int v = q.front(); q.pop(); vector<int> nw; for (auto u : vis) { int p = lower_bound(adj[v].begin(), adj[v].end(), u) - adj[v].begin(); if (!mark[u] && (adj[v].empty() || u != adj[v][p])) { mark[u] = 1; ans[c].push_back(u); q.push(u); } else nw.push_back(u); } vis.swap(nw); } c++; } printf("%d\n", c); for (int i = 0; i < c; i++) { printf("%d ", ans[i].size()); for (auto x : ans[i]) printf("%d ", x + 1); printf("\n"); } }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adjlist; int main() { int n, m; scanf("%d %d", &n, &m); adjlist.assign(n, vector<int>()); for (int i = 0; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); a--; b--; adjlist[a].push_back(b); adjlist[b].push_back(a); } map<int, int> candidate; map<int, int> nottaken; vector<vector<int> > ans(500000); for (int i = 0; i < n; ++i) nottaken[i]; int gr = 0; while (!nottaken.empty()) { int s = nottaken.begin()->first; nottaken.erase(nottaken.begin()); for (typeof((nottaken).begin()) it = (nottaken).begin(); it != (nottaken).end(); ++it) it->second = 0; vector<int>& group = ans[gr]; group.push_back(s); bool grown = true; int p = 0; while (grown && !nottaken.empty()) { grown = false; candidate = nottaken; nottaken.clear(); for (; p < group.size(); ++p) { int u = group[p]; for (int i = 0; i < adjlist[u].size(); ++i) { int v = adjlist[u][i]; typeof(candidate.begin()) it; if ((it = candidate.find(v)) != candidate.end()) { ++it->second; if (it->second == group.size()) { nottaken[it->first] = it->second; candidate.erase(it); } } } } for (typeof((candidate).begin()) it = (candidate).begin(); it != (candidate).end(); ++it) { group.push_back(it->first); grown = true; } } gr++; } printf("%d\n", gr); for (int g = 0; g < gr; ++g) { printf("%d", ans[g].size()); for (int i = 0; i < ans[g].size(); ++i) printf(" %d", ans[g][i] + 1); puts(""); } return 0; }
CPP
190_E. Counter Attack
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.
2
11
import java.util.*; import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.io.InputStream; /** * TEST * * Built using CHelper plug-in * Actual solution is at the top * @author AlexFetisov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE_Counter solver = new TaskE_Counter(); solver.solve(1, in, out); out.close(); } } class TaskE_Counter { Graph g; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); g = new Graph(); g.initGraph(n, m); for (int i = 0; i < m; ++i) { g.addEdge(in.nextInt() - 1, in.nextInt() - 1); } Set<Integer> contains = new LinkedHashSet<>(); for (int i = 0; i < n; i++) { contains.add(i); } int[] queue = new int[n]; int size, iv, i, v, j, to; List<Integer> color; List<List<Integer>> result = new ArrayList<List<Integer>>(); for (iv = 0; iv < n; ++iv) { if (contains.contains(iv)) { size = 0; queue[size++] = iv; color = new ArrayList<Integer>(); contains.remove(iv); for (i = 0; i < size; ++i) { color.add(queue[i] + 1); Set<Integer> toRemove = new HashSet<>(); for (j = g.first[queue[i]]; j != -1; j = g.next[j]) { to = g.to[j]; if (contains.contains(to)) { toRemove.add(to); contains.remove(to); } } for (int x : contains) { queue[size++] = x; } if (contains.size() > 0) { contains.clear(); } contains.addAll(toRemove); } result.add(color); } } out.println(result.size()); for (List<Integer> list : result) { out.print(list.size()); for (int vv : list) { out.print(" " + vv); } out.println(); } } } class Graph { public int[] from; public int[] to; public int[] first; public int[] next; public int nVertex; public int nEdges; public int curEdge; public Graph() {} public void initGraph(int n, int m) { curEdge = 0; nVertex = n; nEdges = m; from = new int[m * 2]; to = new int[m * 2]; first = new int[n]; next = new int[m * 2]; Arrays.fill(first, -1); } public void addEdge(int a, int b) { next[curEdge] = first[a]; first[a] = curEdge; to[curEdge] = b; from[curEdge] = a; ++curEdge; next[curEdge] = first[b]; first[b] = curEdge; to[curEdge] = a; from[curEdge] = b; ++curEdge; } } class InputReader { private BufferedReader reader; private StringTokenizer stt; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() { try { return reader.readLine().trim(); } catch (IOException e) { return null; } } public String nextString() { while (stt == null || !stt.hasMoreTokens()) { stt = new StringTokenizer(nextLine()); } return stt.nextToken(); } public int nextInt() { return Integer.parseInt(nextString()); } }
JAVA
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 500 * 1000 + 100; const long long mod = 1e9 + 7; const long long inf = 2 * 1e18; long long n, m, sz; vector<int> com[maxn]; vector<int> adj[maxn]; set<int> un_marked; bool mark[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; 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++) { sort(adj[i].begin(), adj[i].end()); un_marked.insert(i); } for (int i = 1; i <= n; i++) { if (un_marked.find(i) != un_marked.end()) { queue<int> q; q.push(i); sz++; un_marked.erase(i); while (q.size()) { com[sz].push_back(q.front()); vector<int> for_del; auto it = un_marked.begin(); while (it != un_marked.end()) { if (!binary_search(adj[q.front()].begin(), adj[q.front()].end(), *it)) { q.push(*it); for_del.push_back(*it); } it++; } for (auto x : for_del) un_marked.erase(x); q.pop(); } } } cout << sz << endl; for (int i = 1; i <= sz; i++) { cout << com[i].size() << " "; for (auto x : com[i]) { cout << x << " "; } cout << endl; } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const int inf = 2000000000; const long double eps = 1e-07; int n, m; vector<int> g[501000]; int p[501000]; int sz[501000]; set<int> vv; int getanc(int v) { if (v == p[v]) return v; return p[v] = getanc(p[v]); } void merge(int a, int b) { int x = getanc(a); int y = getanc(b); if (x == y) return; if (rand() & 1) { p[x] = y; sz[y] += sz[x]; } else { p[y] = x; sz[x] += sz[y]; } } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; ++i) { int x, y; scanf("%d %d", &x, &y); --x; --y; g[x].push_back(y); g[y].push_back(x); } for (int i = 0; i < n; ++i) { p[i] = i; sz[i] = 1; vv.insert(i); } for (int i = 0; i < n; ++i) { vector<int> tmp, tmp2; tmp.resize(g[i].size()); for (int j = 0; j < g[i].size(); ++j) { tmp[j] = getanc(g[i][j]); } sort(tmp.begin(), tmp.end()); int x = 0; int y; while (x < tmp.size()) { int sum = 0; y = x; while (y < tmp.size() && tmp[y] == tmp[x]) { ++y; ++sum; } if (sz[tmp[x]] == sum) { vv.erase(tmp[x]); tmp2.push_back(tmp[x]); } x = y; } set<int>::iterator it = vv.begin(); while (it != vv.end()) { merge(i, *it); ++it; } vv.clear(); vv.insert(getanc(i)); for (int j = 0; j < tmp2.size(); ++j) { vv.insert(tmp2[j]); } } cout << vv.size(); vector<pair<int, int>> tmp; for (int i = 0; i < n; ++i) { tmp.push_back(make_pair(getanc(i), i)); } sort(tmp.begin(), tmp.end()); for (int i = 0; i < tmp.size(); ++i) { if ((i == 0) || (tmp[i].first != tmp[i - 1].first)) { printf("\n%d ", sz[tmp[i].first]); } printf("%d ", tmp[i].second + 1); } return 0; }
CPP
190_E. Counter Attack
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.
2
11
import java.io.*; import java.util.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { int g [][]; int deg []; int edges [][]; ArrayList<ArrayList<Integer>> answer = new ArrayList<ArrayList<Integer>>(); public void solve () throws Exception { int n = nextInt(); int m = nextInt(); g = new int [n][]; deg = new int [n]; edges = new int [m][2]; for (int i = 0; i < m; i++) { int v1 = nextInt() - 1; int v2 = nextInt() - 1; deg[v1]++; deg[v2]++; edges[i][0] = v1; edges[i][1] = v2; } for (int i = 0; i < n; i++) { g [i] = new int [deg[i]]; deg[i] = 0; } for (int i = 0; i < m; i++) { int v1 = edges[i][0]; int v2 = edges[i][1]; g[v1][deg[v1]++] = v2; g[v2][deg[v2]++] = v1; } TreeSet<Integer> set = new TreeSet<Integer>(); for (int i = 0; i < n; i++) { set.add(i); } LinkedList<Integer> q = new LinkedList<Integer>(); for (int i = 0; i < n; i++) { if (set.contains(i)) { q.add(i); ArrayList<Integer> list = new ArrayList<Integer>(); set.remove(i); while (!q.isEmpty()) { int v = q.pollFirst(); list.add(v + 1); TreeSet<Integer> removeSet = new TreeSet<Integer>(); for (int j = 0; j < g[v].length; j++) { int to = g[v][j]; if (set.contains(to)) { removeSet.add(to); set.remove(to); } } for (int to : set) { q.add(to); } if (set.size() > 0) { set.clear(); } for (int rmv : removeSet) { set.add(rmv); } } answer.add(list); } } out.println(answer.size()); for (int i = 0; i < answer.size(); i++) { ArrayList<Integer> list = answer.get(i); out.print(list.size()); for (int v : list) { out.print(" "+v); } out.println(); } } static final String fname = ""; static long stime = 0; BufferedReader in; PrintWriter out; StringTokenizer st; private String nextToken () throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } private int nextInt () throws Exception { return Integer.parseInt(nextToken()); } private long nextLong () throws Exception { return Long.parseLong(nextToken()); } private double nextDouble () throws Exception { return Double.parseDouble(nextToken()); } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve (); } catch (Exception e) { throw new RuntimeException(e); } finally { out.close(); } } public static void main(String[] args) { new Thread(null, new Solution(), "", 1<<26).start(); } }
JAVA
190_E. Counter Attack
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.
2
11
#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); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; const int N = 5e5 + 7; set<int> unvis; unordered_set<int, custom_hash> adj[N]; vector<vector<int> > ans; void dfs(int u) { ans.back().emplace_back(u); for (auto it = unvis.begin(); it != unvis.end();) { if (adj[u].find(*it) == adj[u].end()) { 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(v), adj[v].emplace(u); } for (int i = 0; i < n; ++i) unvis.emplace(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() << ' '; for (auto& j : i) cout << j + 1 << ' '; cout << '\n'; } }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pll = pair<ll, ll>; ll power(ll x, ll y); const ll MOD = 1e9 + 7; const ll INF = 1e18 + 18; const int inf = 2e9; const int N = 5e5 + 5; vector<int> v[N]; vector<int> adj[N]; vector<int> res; set<int> st; int bfs(int s) { queue<int> q; q.push(s); while (q.size()) { auto p = q.front(); q.pop(); st.erase(p); adj[s].emplace_back(p); auto temp = st; for (auto i : v[p]) temp.erase(i); for (auto i : temp) { q.push(i); st.erase(i); } } return s; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int I = 1; I <= m; I++) { int A, B; cin >> A >> B; v[A].emplace_back(B), v[B].emplace_back(A); }; for (int i = 1; i <= n; i++) st.insert(i); for (int i = 1; i <= n; i++) { if (st.count(i)) res.emplace_back(bfs(i)); } cout << res.size() << "\n"; for (auto i : res) { cout << adj[i].size() << " "; for (auto j : adj[i]) cout << j << " "; cout << "\n"; } } ll power(ll x, ll y) { ll res = 1; x %= MOD; while (y > 0) { if (y & 1) res = (res * x) % MOD; y = y >> 1, x = (x * x) % MOD; } return res; }
CPP
190_E. Counter Attack
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.
2
11
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Collection; import java.util.InputMismatchException; import java.io.IOException; import java.util.Random; import java.util.ArrayList; import java.util.NoSuchElementException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CounterAttack solver = new CounterAttack(); solver.solve(1, in, out); out.close(); } static class CounterAttack { int n; int m; EzIntArrayList[] bad; EzIntTreeSet nVis; void dfs(int u, EzIntArrayList comp) { comp.add(u); nVis.remove(u); for (int v = nVis.getFirst(); !nVis.returnedNull(); v = nVis.higher(v)) { if (!bad[u].binarySearch(v)) { dfs(v, comp); } } } public void solve(int testNumber, InputReader in, PrintWriter out) { n = in.nextInt(); m = in.nextInt(); bad = new EzIntArrayList[n + 1]; Arrays.setAll(bad, i -> new EzIntArrayList()); for (int i = 0; i < m; i++) { int u = in.nextInt(); int v = in.nextInt(); bad[u].add(v); bad[v].add(u); } for (EzIntArrayList a : bad) { a.sort(); } nVis = new EzIntTreeSet(); for (int i = 1; i <= n; i++) { nVis.add(i); } ArrayList<EzIntArrayList> comps = new ArrayList<>(); for (int i = 1; i <= n; i++) { if (nVis.contains(i)) { EzIntArrayList comp = new EzIntArrayList(); dfs(i, comp); comps.add(comp); } } out.println(comps.size()); for (EzIntArrayList comp : comps) { out.print(comp.size() + " "); for (EzIntIterator it = comp.iterator(); it.hasNext(); ) { int u = it.next(); out.print(u + " "); } out.println(); } } } static final class PrimitiveHashCalculator { private PrimitiveHashCalculator() { } public static int getHash(int x) { return x; } } static interface EzIntIterator { boolean hasNext(); int next(); } static class EzIntArrayList implements EzIntList, EzIntStack { private static final int DEFAULT_CAPACITY = 10; private static final double ENLARGE_SCALE = 2.0; private static final int HASHCODE_INITIAL_VALUE = 0x811c9dc5; private static final int HASHCODE_MULTIPLIER = 0x01000193; private int[] array; private int size; public EzIntArrayList() { this(DEFAULT_CAPACITY); } public EzIntArrayList(int capacity) { if (capacity < 0) { throw new IllegalArgumentException("Capacity must be non-negative"); } array = new int[capacity]; size = 0; } public EzIntArrayList(EzIntCollection collection) { size = collection.size(); array = new int[size]; int i = 0; for (EzIntIterator iterator = collection.iterator(); iterator.hasNext(); ) { array[i++] = iterator.next(); } } public EzIntArrayList(int[] srcArray) { size = srcArray.length; array = new int[size]; System.arraycopy(srcArray, 0, array, 0, size); } public EzIntArrayList(Collection<Integer> javaCollection) { size = javaCollection.size(); array = new int[size]; int i = 0; for (int element : javaCollection) { array[i++] = element; } } public int size() { return size; } public EzIntIterator iterator() { return new EzIntArrayListIterator(); } public boolean add(int element) { if (size == array.length) { enlarge(); } array[size++] = element; return true; } private void enlarge() { int newSize = Math.max(size + 1, (int) (size * ENLARGE_SCALE)); int[] newArray = new int[newSize]; System.arraycopy(array, 0, newArray, 0, size); array = newArray; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; EzIntArrayList that = (EzIntArrayList) o; if (size != that.size) { return false; } for (int i = 0; i < size; i++) { if (array[i] != that.array[i]) { return false; } } return true; } public int hashCode() { int hash = HASHCODE_INITIAL_VALUE; for (int i = 0; i < size; i++) { hash = (hash ^ PrimitiveHashCalculator.getHash(array[i])) * HASHCODE_MULTIPLIER; } return hash; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < size; i++) { sb.append(array[i]); if (i < size - 1) { sb.append(", "); } } sb.append(']'); return sb.toString(); } public void sort() { EzIntSort.safeArraysSort(array, 0, size); } public int lowerBound(int val) { int ret = -1; for (int jump = size; jump >= 1; jump /= 2) { while (ret + jump < size && array[ret + jump] < val) { ret += jump; } } return ret + 1; } public boolean binarySearch(int val) { int lb = lowerBound(val); return lb < size && array[lb] == val; } private class EzIntArrayListIterator implements EzIntIterator { private int curIndex = 0; public boolean hasNext() { return curIndex < size; } public int next() { if (curIndex == size) { throw new NoSuchElementException("Iterator doesn't have more elements"); } return array[curIndex++]; } } } static final class EzIntSort { private static final Random rnd = new Random(); private EzIntSort() { } private static void randomShuffle(int[] a, int left, int right) { for (int i = left; i < right; i++) { int j = i + rnd.nextInt(right - i); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } public static void safeArraysSort(int[] a, int left, int right) { if (left > right || left < 0 || right > a.length) { throw new IllegalArgumentException( "Incorrect range [" + left + ", " + right + ") was specified for sorting, length = " + a.length); } randomShuffle(a, left, right); Arrays.sort(a, left, right); } } static class EzIntTreeSet implements EzIntSortedSet { private static final int DEFAULT_CAPACITY = 10; private static final double ENLARGE_SCALE = 2.0; private static final int HASHCODE_INITIAL_VALUE = 0x811c9dc5; private static final int HASHCODE_MULTIPLIER = 0x01000193; private static final boolean BLACK = false; private static final boolean RED = true; private static final int NULL = 0; private static final int DEFAULT_NULL_ELEMENT = (new int[1])[0]; private int[] key; private int[] left; private int[] right; private int[] p; private boolean[] color; private int size; private int root; private boolean returnedNull; public EzIntTreeSet() { this(DEFAULT_CAPACITY); } public EzIntTreeSet(int capacity) { if (capacity < 0) { throw new IllegalArgumentException("Capacity must be non-negative"); } capacity++; key = new int[capacity]; left = new int[capacity]; right = new int[capacity]; p = new int[capacity]; color = new boolean[capacity]; color[NULL] = BLACK; size = 0; root = NULL; returnedNull = false; } public EzIntTreeSet(EzIntCollection collection) { this(collection.size()); for (EzIntIterator iterator = collection.iterator(); iterator.hasNext(); ) { add(iterator.next()); } } public EzIntTreeSet(int[] srcArray) { this(srcArray.length); for (int element : srcArray) { add(element); } } public EzIntTreeSet(Collection<Integer> javaCollection) { this(javaCollection.size()); for (int element : javaCollection) { add(element); } } public int size() { return size; } public boolean contains(int element) { int x = root; while (x != NULL) { if (element < key[x]) { x = left[x]; } else if (element > key[x]) { x = right[x]; } else { return true; } } return false; } public EzIntIterator iterator() { return new EzIntTreeSetIterator(); } public boolean add(int element) { int y = NULL; int x = root; while (x != NULL) { //noinspection SuspiciousNameCombination y = x; if (element < key[x]) { x = left[x]; } else if (element > key[x]) { x = right[x]; } else { return false; } } if (size == color.length - 1) { enlarge(); } int z = ++size; key[z] = element; p[z] = y; if (y == NULL) { root = z; } else { if (element < key[y]) { left[y] = z; } else { right[y] = z; } } left[z] = NULL; right[z] = NULL; color[z] = RED; fixAfterAdd(z); return true; } public boolean remove(int element) { int z = root; while (z != NULL) { if (element < key[z]) { z = left[z]; } else if (element > key[z]) { z = right[z]; } else { removeNode(z); return true; } } return false; } private void removeNode(int z) { int y = (left[z] == NULL || right[z] == NULL) ? z : successorNode(z); int x = (left[y] != NULL) ? left[y] : right[y]; p[x] = p[y]; if (p[y] == NULL) { root = x; } else { if (y == left[p[y]]) { left[p[y]] = x; } else { right[p[y]] = x; } } if (y != z) { key[z] = key[y]; } //noinspection PointlessBooleanExpression if (color[y] == BLACK) { fixAfterRemove(x); } // Swap with last if (y != size) { // copy fields key[y] = key[size]; left[y] = left[size]; right[y] = right[size]; p[y] = p[size]; color[y] = color[size]; // fix the children's parents p[left[size]] = y; p[right[size]] = y; // fix one of the parent's children if (left[p[size]] == size) { left[p[size]] = y; } else { right[p[size]] = y; } // fix root if (root == size) { root = y; } } size--; } private int successorNode(int x) { if (right[x] != NULL) { x = right[x]; while (left[x] != NULL) { x = left[x]; } return x; } else { int y = p[x]; while (y != NULL && x == right[y]) { //noinspection SuspiciousNameCombination x = y; y = p[y]; } return y; } } private void fixAfterAdd(int z) { while (color[p[z]] == RED) { if (p[z] == left[p[p[z]]]) { int y = right[p[p[z]]]; if (color[y] == RED) { color[p[z]] = BLACK; color[y] = BLACK; color[p[p[z]]] = RED; z = p[p[z]]; } else { if (z == right[p[z]]) { z = p[z]; rotateLeft(z); } color[p[z]] = BLACK; color[p[p[z]]] = RED; rotateRight(p[p[z]]); } } else { int y = left[p[p[z]]]; if (color[y] == RED) { color[p[z]] = BLACK; color[y] = BLACK; color[p[p[z]]] = RED; z = p[p[z]]; } else { if (z == left[p[z]]) { z = p[z]; rotateRight(z); } color[p[z]] = BLACK; color[p[p[z]]] = RED; rotateLeft(p[p[z]]); } } } color[root] = BLACK; } private void fixAfterRemove(int x) { while (x != root && color[x] == BLACK) { if (x == left[p[x]]) { int w = right[p[x]]; if (color[w] == RED) { color[w] = BLACK; color[p[x]] = RED; rotateLeft(p[x]); w = right[p[x]]; } if (color[left[w]] == BLACK && color[right[w]] == BLACK) { color[w] = RED; x = p[x]; } else { if (color[right[w]] == BLACK) { color[left[w]] = BLACK; color[w] = RED; rotateRight(w); w = right[p[x]]; } color[w] = color[p[x]]; color[p[x]] = BLACK; color[right[w]] = BLACK; rotateLeft(p[x]); x = root; } } else { int w = left[p[x]]; if (color[w] == RED) { color[w] = BLACK; color[p[x]] = RED; rotateRight(p[x]); w = left[p[x]]; } if (color[left[w]] == BLACK && color[right[w]] == BLACK) { color[w] = RED; x = p[x]; } else { if (color[left[w]] == BLACK) { color[right[w]] = BLACK; color[w] = RED; rotateLeft(w); w = left[p[x]]; } color[w] = color[p[x]]; color[p[x]] = BLACK; color[left[w]] = BLACK; rotateRight(p[x]); x = root; } } } color[x] = BLACK; } private void rotateLeft(int x) { int y = right[x]; right[x] = left[y]; if (left[y] != NULL) { p[left[y]] = x; } p[y] = p[x]; if (p[x] == NULL) { root = y; } else { if (x == left[p[x]]) { left[p[x]] = y; } else { right[p[x]] = y; } } left[y] = x; p[x] = y; } private void rotateRight(int x) { int y = left[x]; left[x] = right[y]; if (right[y] != NULL) { p[right[y]] = x; } p[y] = p[x]; if (p[x] == NULL) { root = y; } else { if (x == right[p[x]]) { right[p[x]] = y; } else { left[p[x]] = y; } } right[y] = x; p[x] = y; } private void enlarge() { int newLength = Math.max(color.length + 1, (int) (color.length * ENLARGE_SCALE)); key = Arrays.copyOf(key, newLength); left = Arrays.copyOf(left, newLength); right = Arrays.copyOf(right, newLength); p = Arrays.copyOf(p, newLength); color = Arrays.copyOf(color, newLength); } private int firstNode() { int x = root; while (left[x] != NULL) { x = left[x]; } return x; } public int getFirst() { if (root == NULL) { returnedNull = true; return DEFAULT_NULL_ELEMENT; } final int x = firstNode(); returnedNull = false; return key[x]; } public int higher(int element) { int x = root; while (x != NULL) { if (element < key[x]) { if (left[x] != NULL) { x = left[x]; } else { returnedNull = false; return key[x]; } } else { if (right[x] != NULL) { x = right[x]; } else { int y = p[x]; while (y != NULL && x == right[y]) { //noinspection SuspiciousNameCombination x = y; y = p[y]; } if (y == NULL) { returnedNull = true; return DEFAULT_NULL_ELEMENT; } else { returnedNull = false; return key[y]; } } } } returnedNull = true; return DEFAULT_NULL_ELEMENT; } public boolean returnedNull() { return returnedNull; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; EzIntTreeSet that = (EzIntTreeSet) o; if (size != that.size) { return false; } for (int x = firstNode(), y = that.firstNode(); x != NULL; //noinspection SuspiciousNameCombination x = successorNode(x), y = that.successorNode(y) ) { if (key[x] != that.key[y]) { return false; } } return true; } public int hashCode() { int hash = HASHCODE_INITIAL_VALUE; for (int x = firstNode(); x != NULL; x = successorNode(x)) { hash = (hash ^ PrimitiveHashCalculator.getHash(key[x])) * HASHCODE_MULTIPLIER; } return hash; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (int x = firstNode(); x != NULL; x = successorNode(x)) { if (sb.length() > 1) { sb.append(", "); } sb.append(key[x]); } sb.append(']'); return sb.toString(); } private class EzIntTreeSetIterator implements EzIntIterator { private int x; private EzIntTreeSetIterator() { x = firstNode(); } public boolean hasNext() { return x != NULL; } public int next() { if (x == NULL) { throw new NoSuchElementException("Iterator doesn't have more elements"); } final int result = key[x]; x = successorNode(x); return result; } } } static interface EzIntStack extends EzIntCollection { int size(); EzIntIterator iterator(); boolean equals(Object object); int hashCode(); String toString(); } static interface EzIntList extends EzIntCollection { int size(); EzIntIterator iterator(); boolean equals(Object object); int hashCode(); String toString(); } static interface EzIntCollection { int size(); EzIntIterator iterator(); boolean equals(Object object); int hashCode(); String toString(); } static interface EzIntSortedSet extends EzIntSet { int size(); EzIntIterator iterator(); boolean equals(Object object); int hashCode(); String toString(); } static interface EzIntSet extends EzIntCollection { int size(); EzIntIterator iterator(); boolean equals(Object object); int hashCode(); String toString(); } static class InputReader { private final InputStream is; private final byte[] inbuf = new byte[1024]; private int lenbuf = 0; private int ptrbuf = 0; public InputReader(InputStream stream) { is = stream; } private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } }
JAVA
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; inline long long modadd(long long n, long long m, long long p = 998244353) { return ((n + m) % p + p) % p; } inline long long modsub(long long n, long long m, long long p = 998244353) { return ((n - m + p) % p + p) % p; } inline long long modpro(long long n, long long m, long long p = 998244353) { return ((n * m) % p + p) % p; } inline unsigned long long int powe(long long first, long long second) { unsigned long long int res = 1; while (second > 0) { if (second & 1) res = res * first; second = second >> 1; first = first * first; } return res; } inline long long modpow(long long first, long long second, long long p = 998244353) { long long res = 1; while (second > 0) { if (second & 1) res = modpro(res, first, p); second = second >> 1; first = modpro(first, first, p); } return res; } inline long long modInverse(long long n, long long p = 998244353) { return modpow(n, p - 2, p); } inline long long modadd3(long long first, long long second, long long z, long long p = 998244353) { return modadd(modadd(first, second, p), z, p); } inline long long modadd4(long long first, long long second, long long z, long long w, long long p = 998244353) { return modadd(modadd(first, second, p), modadd(z, w, p), p); } template <typename T> inline T max3(T first, T second, T z) { return max(max(first, second), z); } template <typename T> inline T max4(T first, T second, T z, T w) { return max(max3(first, second, w), z); } template <typename T> inline T min3(T first, T second, T z) { return min(min(first, second), z); } template <typename T> inline T min4(T first, T second, T z, T w) { return min(min3(first, second, w), z); } template <typename T> void printArr(T *arr, int n) { for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; } vector<int> adj[500005]; class Graph { public: int V; Graph(int V) { this->V = V; } void addEdge(int a, int b) { adj[a].push_back(b); adj[b].push_back(a); } }; vector<int> parent, rank_, size_; set<int> reps; void makeSet(int first) { parent[first] = first; rank_[first] = 0; size_[first] = 1; reps.insert(first); } int findSet(int first) { if (first != parent[first]) parent[first] = findSet(parent[first]); return parent[first]; } void unionSets(int first, int second) { int px = findSet(first); int py = findSet(second); if (px == py) return; if (rank_[px] < rank_[py]) { parent[px] = py; reps.erase(px); size_[py] += size_[px]; } else if (rank_[py] < rank_[px]) { parent[py] = px; reps.erase(py); size_[px] += size_[py]; } else { parent[py] = px; rank_[px]++; reps.erase(py); size_[px] += size_[py]; } } int creps[500005]; vector<int> v2; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int erer = 1; for (int(erer2) = (1); (erer2) < (erer + 1); (erer2)++) { int n, m; cin >> n >> m; Graph G(n + 2); parent.resize(n + 2); rank_.resize(n + 2); size_.resize(n + 2); int u, v; for (int(i) = (0); (i) < (m); (i)++) { cin >> u >> v; G.addEdge(u, v); } for (int(i) = (1); (i) < (n + 1); (i)++) sort(adj[i].begin(), adj[i].end()); for (int(i) = (1); (i) < (n + 1); (i)++) { makeSet(i); for (int first : adj[i]) { creps[findSet(first)]++; } for (int first : reps) { if (first > i) break; if (size_[first] > creps[first]) { v2.push_back(first); } creps[first] = 0; } for (int first : v2) unionSets(first, i); v2.clear(); } map<int, vector<int> > ma; for (int(i) = (1); (i) < (n + 1); (i)++) { ma[findSet(i)].push_back(i); } cout << ((long long)(ma).size()) << endl; for (auto first : ma) { cout << ((long long)(first.second).size()) << " "; for (int second : first.second) cout << second << " "; cout << endl; } } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 1; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq; set<pair<int, int> > st; set<int> ML[N]; vector<int> adj[N]; bitset<N> B; int n, m, a, b, tmp, x, D[N]; pair<int, int> p, p2; bool C(int a, int b) { int tmp = lower_bound(adj[a].begin(), adj[a].end(), b) - adj[a].begin(); if ((int)adj[a].size() == tmp || ((int)adj[a].size() != tmp && adj[a][tmp] != b)) return (1); return (0); } int main() { scanf("%d %d", &n, &m); while (m--) { scanf("%d %d", &a, &b); adj[a].push_back(b), adj[b].push_back(a), D[a]++, D[b]++; } for (int i = 1; i <= n; i++) st.insert({D[i], i}), sort(adj[i].begin(), adj[i].end()); while (st.size()) { x++; pair<int, int> tmppi = *st.begin(); pq.push(tmppi); st.erase(st.begin()); ML[x].insert(tmppi.second); while (pq.size()) { p = pq.top(), pq.pop(); auto it = st.begin(); while (it != st.end()) { p2 = *it; if (C(p.second, p2.second)) { ML[x].insert(p2.second); st.erase(p2); it = st.upper_bound(p2); pq.push(p2); continue; } else it++; } } } printf("%d\n", x); for (int i = 1; i <= x; i++) { printf("%d ", (int)ML[i].size()); for (auto x : ML[i]) printf("%d ", x); printf("\n"); } }
CPP
190_E. Counter Attack
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.
2
11
// package cf190; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import java.util.function.IntConsumer; import java.util.stream.Collectors; public class CFE { private static final long MOD = 1_000_000_007; private static final String INPUT = "4 4\n" + "1 2\n" + "1 3\n" + "4 2\n" + "4 3\n"; private PrintWriter out; private FastScanner sc; public static void main(String[] args) { new CFE().run(); } public void run() { sc = new FastScanner(oj ? System.in : new ByteArrayInputStream(INPUT.getBytes())); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); try { solve(); } catch (ExitException ignored) { } out.flush(); tr(System.currentTimeMillis() - s + "ms"); } static class IntArray { int[] array; int size; IntArray() { this(20); } IntArray(int capacity) { this.array = new int[capacity]; } void add(int n) { ensureCapacity(size + 1); array[size++] = n; } void sort() { shuffle(); Arrays.sort(array, 0, size); } void shuffle() { for (int i = 0; i < size; i++) { int r = i + (int) (Math.random() * (size - i)); { int tmp = array[i]; array[i] = array[r]; array[r] = tmp; } } } boolean contains(int n) { int lo = -1; int hi = size; while (hi-lo > 1) { int med = (hi+lo)>>1; int mValue = array[med]; if (mValue == n) return true; else if (mValue < n) lo = med; else hi = med; } return false; } private void ensureCapacity(int cap) { if (cap > array.length) grow(cap); } private void grow(int cap) { int oldCapacity = array.length; int newCapacity = oldCapacity + (oldCapacity >> 1); array = Arrays.copyOf(array, newCapacity); } } int n; IntArray cities; IntArray[] ways; public void solve() { n = sc.nextInt(); int m = sc.nextInt(); cities = new IntArray(n); for (int i = 1; i <= n; i++) cities.add(i); int[] as = new int[m]; int[] bs = new int[m]; int[] deg = new int[n+1]; for (int i = 0; i < m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); as[i] = a; bs[i] = b; deg[a]++; deg[b]++; } ways = new IntArray[n+1]; int mincity = 0; int mindeg = Integer.MAX_VALUE; for (int i = 1; i <= n; i++) { int degree = deg[i]; ways[i] = new IntArray(degree); if (degree < mindeg) { mindeg = degree; mincity = i; } } for (int i = 0; i < m; i++) { int a = as[i]; int b = bs[i]; ways[a].add(b); ways[b].add(a); } DSU dsu = new DSU(n); int[] minways = ways[mincity].array; Arrays.sort(minways); int minlength = minways.length; for (int i = 1, p = 0; i <= n; i++) { if (p < minlength && minways[p] == i) { p++; } else { dsu.union(mincity, i); } } for (int next : minways) { int[] cways = ways[next].array; Arrays.sort(cways); int clength = cways.length; for (int i = 1, p = 0; i <= n; i++) { if (p < clength && cways[p] == i) { p++; } else { dsu.union(next, i); } } } List<IntArray> ans = new ArrayList<>(); for (int i = 1; i <= n; i++) { int root = dsu.find(i); if (dsu.ans[root] == null) { ans.add(dsu.ans[root] = new IntArray(dsu.size[root])); } dsu.ans[root].add(i); } out.println(ans.size()); for (IntArray an : ans) { out.print(an.size); int[] array = an.array; for (int i = 0; i < an.size; i++) { out.print(" " + array[i]); } out.println(); } } private void dfs(int ccity, IntArray cans) { cans.add(ccity); IntArray newCities = new IntArray(cities.size); IntArray edges = ways[ccity]; IntArray nexts = new IntArray(); int ie = 0; int nEdge = ie < edges.size ? edges.array[ie++] : Integer.MAX_VALUE; int[] array = cities.array; for (int i = 0, length = cities.size; i < length; i++) { int city = array[i]; if (city == ccity) continue; while (city > nEdge) nEdge = ie < edges.size ? edges.array[ie++] : Integer.MAX_VALUE; if (city < nEdge) nexts.add(city); else newCities.add(city); } cities = newCities; for (int i = 0; i < nexts.size; i++) { dfs(nexts.array[i], cans); } } static class DSU { int[] parent; int[] size; IntArray[] ans; DSU(int n) { parent = new int[n+1]; for (int i = 1; i <= n; i++) parent[i] = i; size = new int[n+1]; Arrays.fill(size, 1); ans = new IntArray[n+1]; } int find(int v) { if (v == parent[v]) return v; return parent[v] = find(parent[v]); } void union(int a, int b) { a = find(a); b = find(b); if (a != b) { if (size[a] < size[b]) { int tmp = a; a = b; b = tmp; } parent[b] = a; size[a] += size[b]; } } } //******************************************************************************************** //******************************************************************************************** //******************************************************************************************** private void times(int n, IntConsumer consumer) { for (int i = 0; i < n; i++) { try { consumer.accept(i); } catch (ExitException ignored) { } } } private static class ExitException extends RuntimeException { } private void answer(Object ans) { out.println(ans); throw new ExitException(); } private static int lowerBound(long[] arr, long key) { int lo = 0; int hi = arr.length - 1; while (lo < hi) { int mid = (lo + hi) / 2; if (key <= arr[mid]) { hi = mid - 1; } else { lo = mid + 1; } } return arr[lo] < key ? lo + 1 : lo; } private static int upperBound(long[] arr, long key) { int lo = 0; int hi = arr.length - 1; while (lo < hi) { int mid = (lo + hi) / 2; if (key >= arr[mid]) { lo = mid + 1; } else { hi = mid; } } return arr[lo] <= key ? lo + 1 : lo; } private static int ceil(double d) { int ret = (int) d; return ret == d ? ret : ret + 1; } private static int round(double d) { return (int) (d + 0.5); } private static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } private static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } private int[] readIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = sc.nextInt(); } return res; } private long[] readLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = sc.nextLong(); } return res; } @SuppressWarnings("unused") static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; FastScanner(InputStream stream) { this.stream = stream; } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
JAVA
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 100; template <typename T> inline T read() { T s = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { s = (s << 3) + (s << 1) + ch - 48; ch = getchar(); } return s * f; } set<int> st; int vis[N]; int color[N]; vector<int> v[N], one[N]; int tot = 0; int main() { int n = read<int>(), m = read<int>(); for (int i = 1; i <= m; ++i) { int x = read<int>(), y = read<int>(); one[x].push_back(y); one[y].push_back(x); } for (int i = 1; i <= n; ++i) { st.insert(i); sort(one[i].begin(), one[i].end()); one[i].push_back(1e6); } queue<int> q; int ans = 0; for (int i = 1; i <= n; ++i) { if (vis[i]) continue; q.push(i); st.erase(i); ++ans; color[i] = ans; v[ans].push_back(i); while (!q.empty()) { int now = q.front(); q.pop(); if (vis[now]) continue; vis[now] = 1; for (auto it = st.begin(); it != st.end();) { if (vis[*it]) continue; auto item = lower_bound(one[now].begin(), one[now].end(), *it); if (item == one[i].end() || *item != *it) { color[*it] = color[now]; v[color[now]].push_back(*it); q.push(*it); it = st.erase(it); } else ++it; } } } printf("%d\n", ans); for (int i = 1; i <= ans; ++i) { printf("%d", v[i].size()); for (auto to : v[i]) { printf(" %d", to); } printf("\n"); } }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; vector<int> e[500500]; vector<int> res[500500]; bool visited[500500]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--; v--; e[u].push_back(v); e[v].push_back(u); } list<int> sp; for (int i = 0; i < n; i++) sp.push_back(i); int k = 0; while (sp.begin() != sp.end()) { list<int> curr; curr.push_back(sp.front()); sp.pop_front(); while (curr.begin() != curr.end()) { int u = curr.front(); curr.pop_front(); sort(e[u].begin(), e[u].end()); int p = 0; list<int>::iterator it = sp.begin(); while (it != sp.end()) { int v = *it; while (p < e[u].size() && e[u][p] < v) p++; if (p < e[u].size() && e[u][p] == v) { it++; continue; } curr.push_back(v); list<int>::iterator temp = it; it++; sp.erase(temp); } res[k].push_back(u); } k++; } printf("%d\n", k); for (int i = 0; i < k; i++) { printf("%d", res[i].size()); for (int j = 0; j < res[i].size(); j++) printf(" %d", res[i][j] + 1); printf("\n"); } return 0; }
CPP
190_E. Counter Attack
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.
2
11
import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Task2 { public static void main(String[] args) throws IOException { new Task2().solve(); } int mod = 1000000007; PrintWriter out; int n; int m; ArrayList<Integer>[] g; void solve() throws IOException { //Reader in = new Reader("in.txt"); //out = new PrintWriter( new BufferedWriter(new FileWriter("output.txt")) ); Reader in = new Reader(); PrintWriter out = new PrintWriter( new BufferedWriter(new OutputStreamWriter(System.out)) ); //BufferedReader br = new BufferedReader( new FileReader("in.txt") ); //BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); int n = in.nextInt(); int m = in.nextInt(); Pair[] edge = new Pair[m]; int[] inDeg = new int[n]; for (int i = 0; i < m; i++) { int x = in.nextInt()-1; int y = in.nextInt()-1; edge[i] = new Pair(x, y); inDeg[x]++; inDeg[y]++; } ArrayList<Integer> need = new ArrayList<>(); boolean[] isNeed = new boolean[n]; int minV = 0; for (int i = 0; i < n; i++) if (inDeg[i] < inDeg[minV]) minV = i; HashSet<Integer>[] g = new HashSet[n]; for (int i = 0; i < n; i++) { g[i] = new HashSet<>(); } for (int i = 0; i < m; i++) { if (edge[i].a == minV) { need.add(edge[i].b); isNeed[edge[i].b] = true; } if (edge[i].b == minV) { need.add(edge[i].a); isNeed[edge[i].a] = true; } } int[] sz = new int[n]; int[] ver = new int[n]; for (int i = 0; i < need.size(); i++) ver[need.get(i)] = i; int[][] a = new int[need.size()][need.size()]; for (int i = 0; i < m; i++) { int v = edge[i].a; int u = edge[i].b; if (isNeed[v] && isNeed[u]) { a[ver[v]][ver[u]] = 1; a[ver[u]][ver[v]] = 1; } else if (isNeed[u]) { sz[u]++; } else if (isNeed[v]) { sz[v]++; } } DSU dsu = new DSU(n); int cntNotNeed = 0; for (int i = 0; i < n; i++) if (!isNeed[i]) { cntNotNeed++; dsu.union(minV, i); } for (int v : need) { if (sz[v] < cntNotNeed) dsu.union(v, minV); for (int i : need) if (a[ver[v]][ver[i]] == 0) dsu.union(v, i); } ArrayList<Integer>[] tans = new ArrayList[n]; for (int i = 0; i < n; i++) tans[i] = new ArrayList<>(); for (int i = 0; i < n; i++) { int x = dsu.get(i); tans[x].add(i+1); } int acnt = 0; for (int i = 0; i < n; i++) if (tans[i].size() > 0) acnt++; out.println(acnt); for (int i = 0; i < n; i++) { if (tans[i].size() == 0) continue; out.print(tans[i].size() + " "); for (int u : tans[i]) out.print(u+" "); out.println(); } out.flush(); out.close(); } class DSU { int[] set; int[] size; DSU(int n) { set = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { set[i] = i; size[i] = 1; } } int get(int a) { if (set[a] == a) return a; return set[a] = get(set[a]); } void union(int a, int b) { a = get(a); b = get(b); if (a == b) return; if (size[a] >= size[b]) { set[b] = a; size[a] += size[b]; } else { set[a] = b; size[b] += size[a]; } } boolean compare(int a, int b) { return get(a) == get(b); } } class Pair implements Comparable<Pair>{ int a; int b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair p) { if (b < p.b) return 1; if (b > p.b) return -1; return 0; } // @Override // public boolean equals(Object o) { // Pair p = (Pair) o; // return a == p.a && b == p.b; // } // // @Override // public int hashCode() { // return Integer.valueOf(a).hashCode() + Integer.valueOf(b).hashCode(); // } } class Reader { BufferedReader br; StringTokenizer tok; Reader(String file) throws IOException { br = new BufferedReader( new FileReader(file) ); } Reader() throws IOException { br = new BufferedReader( new InputStreamReader(System.in) ); } String next() throws IOException { while (tok == null || !tok.hasMoreElements()) tok = new StringTokenizer(br.readLine()); return tok.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.valueOf(next()); } long nextLong() throws NumberFormatException, IOException { return Long.valueOf(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.valueOf(next()); } String nextLine() throws IOException { return br.readLine(); } } }
JAVA
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; set<int> ss1; set<int>::iterator pos; vector<int> g[500010]; queue<int> q; struct Edge { int v, pre; }; Edge e[2000010]; int use[500010]; int ecnt, hh[500010], p[500010]; void addEdge(int u, int v) { e[ecnt].v = v; e[ecnt].pre = hh[u]; hh[u] = ecnt++; } int main() { int n, m; while (scanf("%d%d", &n, &m) == 2) { ss1.clear(); for (int i = 1; i <= n; i++) ss1.insert(i); memset(hh, -1, sizeof(hh)); memset(use, 0, sizeof(use)); ecnt = 0; for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); addEdge(u, v); addEdge(v, u); } int cnts = 0, anss = 0, now = 0; while (cnts != n) { int num = *ss1.begin(), cnt = 0; while (!q.empty()) q.pop(); q.push(num); ss1.erase(num); g[anss].clear(); while (!q.empty()) { now++; int cnt = 0; int num = q.front(); g[anss].push_back(num); cnts++; q.pop(); for (int i = hh[num]; i != -1; i = e[i].pre) { int v = e[i].v; use[v] = now; } pos = ss1.begin(); while (pos != ss1.end()) { if (use[*pos] != now) { q.push(*pos); p[cnt++] = *pos; } pos++; } for (int i = 0; i < cnt; i++) ss1.erase(p[i]); } anss++; } printf("%d\n", anss); for (int i = 0; i < anss; i++) { int len = g[i].size(); printf("%d", len); for (int j = 0; j < len; j++) printf(" %d", g[i][j]); puts(""); } } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 500 * 1000 + 10; int n, m, cnt; set<int> sv; vector<int> adj[N], ans[N], tmp; queue<int> q; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> m; for (int i = 0, u, v; i < m; i++) cin >> u >> v, adj[u].push_back(v), adj[v].push_back(u); for (int i = 1; i <= n; i++) sv.insert(i), sort(adj[i].begin(), adj[i].end()); while (!sv.empty()) { if (q.empty()) q.push(*sv.begin()), ans[++cnt].push_back(q.front()), sv.erase(sv.begin()); int v = q.front(), bs; q.pop(); for (int u : sv) { if (adj[v].empty()) bs = -1; else bs = upper_bound(adj[v].begin(), adj[v].end(), u) - adj[v].begin() - 1; if ((bs ^ -1 ? adj[v][bs] : -1) ^ u) tmp.push_back(u); } for (int u : tmp) q.push(u), ans[cnt].push_back(u), sv.erase(u); tmp.clear(); } cout << cnt << '\n'; for (int i = 1; i <= cnt; i++, cout << '\n') { cout << ans[i].size() << ' '; for (int u : ans[i]) cout << u << ' '; } }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 500050; set<int> Free; vector<int> E[N], sol[N]; void DFS(int u, int c) { Free.erase(u); sol[c].push_back(u); int nxt = N; for (int i = (signed)E[u].size() - 1; ~i; i--) { for (int v = *Free.upper_bound(E[u][i]); v < nxt; v = *Free.upper_bound(E[u][i])) DFS(v, c); nxt = E[u][i]; } } int c; int main() { int n, m, u, v, i, j; scanf("%i %i", &n, &m); while (m--) scanf("%i %i", &u, &v), E[u].push_back(v), E[v].push_back(u); for (i = 1; i <= n; i++) Free.insert(i), E[i].push_back(0), sort(E[i].begin(), E[i].end()); Free.insert(N + 1000); for (i = 1; i <= n; i++) if (Free.count(i)) c++, DFS(i, c); printf("%i\n", c); for (i = 1; i <= c; i++) { printf("%i ", sol[i].size()); for (j = 0; j < sol[i].size(); j++) printf("%i ", sol[i][j]); printf("\n"); } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; constexpr int N = 1e6 + 10; int n, m, pos, u, v, z, ver, mn, mx; vector<int> ans[N]; vector<pair<int, int>> edge; deque<int> ve; bool mark[N]; bool bin(int b, int e) { if (b + 1 == e) { if (edge[b].first == mn && edge[b].second == mx) return false; return true; } int mid = (b + e) >> 1; if (edge[mid].first < mn || (edge[mid].first == mn && edge[mid].second <= mx)) return bin(mid, e); return bin(b, mid); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; edge.push_back({0, 0}); for (int i = 0; i < m; i++) { cin >> u >> v; if (u > v) swap(u, v); edge.push_back({u, v}); } sort(edge.begin(), edge.end()); for (int i = 1; i <= n; i++) ve.push_back(i); mark[1] = true; while (ve.size()) { ver = ve.front(); ve.pop_front(); queue<int> q; q.push(ver); while (q.size()) { z = q.front(); q.pop(); ans[pos].push_back(z); for (int i = 0; i < (int)ve.size(); i++) { if (z < ve[i]) { mn = z; mx = ve[i]; } else { mn = ve[i]; mx = z; } if (bin(0, edge.size()) && !mark[ve[i]]) { q.push(ve[i]); mark[ve[i]] = true; ve[i] = ve[ve.size() - 1]; ve.pop_back(); i--; } } } pos++; } cout << pos << '\n'; for (int i = 0; i < pos; i++) { cout << ans[i].size() << ' '; for (int j = 0; j < (int)ans[i].size(); j++) cout << ans[i][j] << ' '; cout << '\n'; } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; int n, m; const int maxN = 5e5 + 5; vector<int> g[maxN]; set<int> a; vector<vector<int>> ans; void dfs(int node) { a.erase(a.find(node)); ans.back().push_back(node); for (int i = 0; i < g[node].size(); i++) { auto it = a.lower_bound(g[node][i] + 1); while (it != a.end()) { if (i == g[node].size() - 1 || *it < g[node][i + 1]) { dfs(*it); it = a.lower_bound(g[node][i] + 1); } else break; } } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } for (int i = 0; i < n; i++) { a.insert(i); g[i].push_back(-1); sort(g[i].begin(), g[i].end()); } for (int i = 0; i < n; i++) { if (a.find(i) != a.end()) { ans.emplace_back(vector<int>(0)); dfs(i); } } cout << ans.size() << '\n'; for (auto el : ans) { cout << el.size() << " "; for (auto v : el) { cout << v + 1 << " "; } cout << '\n'; } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18 + 10; const int maxn = 5e5 + 30; const int maxq = 1e5 + 10; const int alf = 26; const long long dlm = 1e9 + 7; const long long del = 179425601; const int eps = 1e-7; string O[] = {"YES", "NO", "Yes", "No"}; vector<int> g[maxn]; int com1[maxn], pr[maxn], d[maxn], x[maxn], mark[maxn]; vector<int> ans[maxn]; vector<int> ans2; int cnt = 1; int find_par(int v) { if (pr[v] == 0) return v; return pr[v] = find_par(pr[v]); } void union_dsu(int v, int u) { if (find_par(v) == find_par(u)) return; pr[find_par(v)] = find_par(u); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int v, u; cin >> v >> u; g[v].push_back(u); g[u].push_back(v); } int first_good = 0; for (int i = 1; i <= n; i++) { if (!(n - g[i].size() > g[i].size())) { continue; } mark[i] = 1; if (first_good == 0) { first_good = i; continue; } union_dsu(i, first_good); } for (int i = 1; i <= n; i++) { if (mark[i] == 1) continue; for (int j = 0; j < maxn; j++) d[j] = 0; for (int j = 0; j < g[i].size(); j++) { int u = g[i][j]; d[u] = 1; } for (int j = 1; j <= n; j++) { if (i == j) continue; if (d[j] == 1) continue; union_dsu(i, j); } } for (int i = 1; i <= n; i++) { int v = find_par(i); if (x[v] == 0) { x[v] = cnt; cnt++; } ans[x[v]].push_back(i); } cout << cnt - 1 << "\n"; for (int i = 1; i < cnt; i++) { cout << ans[i].size() << " "; for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << " "; cout << "\n"; } return 0; }
CPP
190_E. Counter Attack
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.
2
11
import java.io.*; import java.util.*; public class Main { static FastReader in; static PrintWriter out; static int n, m; static int[][] edges; static int[] deg; static int[][] g; static HashSet<Integer> s; static LinkedList<Integer> q; static ArrayList<ArrayList<Integer>> ans; static void solve() { n = in.nextInt(); m = in.nextInt(); edges = new int[m][2]; deg = new int[n]; for (int i = 0; i < m; i++) { int v = in.nextInt() - 1; int u = in.nextInt() - 1; edges[i][0] = v; edges[i][1] = u; deg[v]++; deg[u]++; } g = new int[n][]; for (int i = 0; i < n; i++) { g[i] = new int[deg[i]]; deg[i] = 0; } for (int i = 0; i < m; i++) { int v = edges[i][0]; int u = edges[i][1]; g[v][deg[v]++] = u; g[u][deg[u]++] = v; } ans = new ArrayList<>(); s = new HashSet<>(); for (int i = 0; i < n; i++) { s.add(i); } q = new LinkedList<>(); for (int i = 0; i < n; i++) { if (s.contains(i)) { ArrayList<Integer> ansi = new ArrayList<>(); q.add(i); s.remove(i); while (!q.isEmpty()) { int v = q.poll(); ansi.add(v); HashSet<Integer> temp = new HashSet<>(); for (int u : g[v]) { if (s.contains(u)) { temp.add(u); s.remove(u); } } for (int u : s) { q.add(u); } s = temp; } ans.add(ansi); } } out.println(ans.size()); for (ArrayList<Integer> ansi : ans) { out.print(ansi.size()); for (int v : ansi) { out.print(" " + (v + 1)); } out.print('\n'); } } public static void main(String[] args) { in = new FastReader(System.in); // in = new FastReader(new FileInputStream("input.txt")); out = new PrintWriter(System.out); // out = new PrintWriter(new FileOutputStream("output.txt")); int t = 1; while (t-- > 0) solve(); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } Integer nextInt() { return Integer.parseInt(next()); } Long nextLong() { return Long.parseLong(next()); } Double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } String nextLine() { String s = ""; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } } }
JAVA
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 7; vector<int> adj[N]; set<int> st, tmp; vector<int> ans[N]; queue<int> q; int main() { int n, m; scanf("%d %d", &n, &m); 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); } for (int i = 0; i < n; i++) st.insert(i); int cnt = 0; while (!st.empty()) { int v = *st.begin(); st.erase(st.begin()); q.push(v); while (!q.empty()) { int v = q.front(); ans[cnt].push_back(v); q.pop(); for (auto u : adj[v]) { auto it = st.find(u); if (it != st.end()) { tmp.insert(*it); st.erase(it); } } while (!st.empty()) { q.push(*st.begin()); st.erase(st.begin()); } st.swap(tmp); } cnt++; } 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"); } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; void dfs(int s, set<int> &v, set<pair<int, int>> &edge, vector<int> &nodes) { v.erase(s); nodes.push_back(s); vector<int> q; for (auto i : v) { if (edge.count({min(i, s), max(i, s)}) == 0) { q.push_back(i); } } for (auto i : q) v.erase(i); for (auto i : q) dfs(i, v, edge, nodes); } void solve() { int n, m, ans = 0; cin >> n >> m; set<pair<int, int>> edge; vector<vector<int>> groups; vector<int> nodes; set<int> v; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; edge.insert({min(a, b), max(a, b)}); } for (int i = 1; i <= n; i++) v.insert(i); for (int i = 1; i <= n; i++) { if (v.count(i) == 0) continue; ans++; dfs(i, v, edge, nodes); groups.push_back(nodes); nodes.clear(); } cout << signed(groups.size()) << "\n"; for (auto i : groups) { cout << signed(i.size()) << " "; for (auto x : i) cout << x << " "; cout << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t = 1; while (t--) solve(); return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 5000 * 100 + 5; const int mod = 1000 * 1000 * 1000 + 7; vector<int> nadj[maxn]; vector<int> comp[maxn]; int par[maxn]; int h[maxn]; int tmp[maxn]; int getpar(int x) { if (par[x] == x) return x; par[x] = getpar(par[x]); return par[x]; } void join(int u, int v) { u = getpar(u); v = getpar(v); if (u == v) return; if (h[u] > h[v]) swap(u, v); par[u] = v; h[v] = max(h[v], h[u] + 1); } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; nadj[u].push_back(v); nadj[v].push_back(u); } int mini = 0; for (int i = 0; i < n; i++) { par[i] = i; if (nadj[i].size() < nadj[mini].size()) mini = i; } for (int i = 0; i < (int)nadj[mini].size(); i++) tmp[nadj[mini][i]] = 1; for (int i = 0; i < n; i++) if (tmp[i] == 0) join(mini, i); for (int i = 0; i < (int)nadj[mini].size(); i++) { int u = nadj[mini][i]; for (int j = 0; j < n; j++) tmp[j] = 0; for (int j = 0; j < (int)nadj[u].size(); j++) tmp[nadj[u][j]] = 1; for (int j = 0; j < n; j++) if (tmp[j] == 0) join(u, j); } for (int i = 0; i < n; i++) comp[getpar(i)].push_back(i); int cnt = 0; for (int i = 0; i < n; i++) if (comp[i].size() != 0) cnt++; cout << cnt << endl; for (int i = 0; i < n; i++) if (comp[i].size() != 0) { cout << comp[i].size() << " "; for (int j = 0; j < (int)comp[i].size(); j++) cout << comp[i][j] + 1 << " "; cout << "\n"; } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 500100; unordered_set<int> grafo[MAXN], s; vector<int> vec; void dfs(int x) { vec.push_back(x); vector<int> v; for (auto i : s) { if (grafo[x].find(i) != grafo[x].end()) continue; v.push_back(i); } for (int i = 0; i < v.size(); i++) s.erase(v[i]); for (int i = 0; i < v.size(); i++) dfs(v[i]); } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) s.insert(i); for (int i = 1; i <= m; i++) { int x, y; scanf("%d %d", &x, &y); grafo[x].insert(y); grafo[y].insert(x); } int cnt = 0; vector<int> resp; for (int i = 1; i <= n; i++) { if (s.find(i) == s.end()) continue; vec.clear(); s.erase(i); dfs(i); cnt++; resp.push_back((int)vec.size()); for (int j = 0; j < vec.size(); j++) resp.push_back(vec[j]); resp.push_back(-1); } printf("%d\n", cnt); for (int i = 0; i < resp.size(); i++) { if (resp[i] == -1) printf("\n"); else printf("%d ", resp[i]); } }
CPP
190_E. Counter Attack
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.
2
11
import java.io.*; import java.util.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { int g [][]; int deg []; int edges [][]; ArrayList<ArrayList<Integer>> answer = new ArrayList<ArrayList<Integer>>(); public void solve () throws Exception { int n = nextInt(); int m = nextInt(); g = new int [n][]; deg = new int [n]; edges = new int [m][2]; for (int i = 0; i < m; i++) { int v1 = nextInt() - 1; int v2 = nextInt() - 1; deg[v1]++; deg[v2]++; edges[i][0] = v1; edges[i][1] = v2; } for (int i = 0; i < n; i++) { g [i] = new int [deg[i]]; deg[i] = 0; } for (int i = 0; i < m; i++) { int v1 = edges[i][0]; int v2 = edges[i][1]; g[v1][deg[v1]++] = v2; g[v2][deg[v2]++] = v1; } TreeSet<Integer> set = new TreeSet<Integer>(); for (int i = 0; i < n; i++) { set.add(i); } Queue<Integer> q = new ArrayDeque<Integer>(n); ArrayList<Integer> removeSet = new ArrayList<Integer>(n); for (int i = 0; i < n; i++) { if (set.contains(i)) { q.add(i); ArrayList<Integer> list = new ArrayList<Integer>(); set.remove(i); while (!q.isEmpty()) { int v = q.poll(); list.add(v + 1); if(removeSet.size() > 0) { removeSet.clear(); } for (int j = 0; j < g[v].length; j++) { int to = g[v][j]; if (set.contains(to)) { removeSet.add(to); set.remove(to); } } q.addAll(set); if (set.size() > 0) { set.clear(); } set.addAll(removeSet); } answer.add(list); } } out.println(answer.size()); for (int i = 0; i < answer.size(); i++) { ArrayList<Integer> list = answer.get(i); out.print(list.size()); for (int v : list) { out.print(" "+v); } out.println(); } } static final String fname = ""; static long stime = 0; BufferedReader in; PrintWriter out; StringTokenizer st; private String nextToken () throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } private int nextInt () throws Exception { return Integer.parseInt(nextToken()); } private long nextLong () throws Exception { return Long.parseLong(nextToken()); } private double nextDouble () throws Exception { return Double.parseDouble(nextToken()); } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve (); } catch (Exception e) { throw new RuntimeException(e); } finally { out.close(); } } public static void main(String[] args) { new Thread(null, new Solution(), "", 1<<26).start(); } }
JAVA
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; vector<int> v[500005], sol[500005], to_del; int DUMMY1 = 0, DUMMY2 = 500005; struct lista { int pr, nx, apare; } ls[500010]; inline void sterge(int val) { int pre = ls[val].pr; int nxt = ls[val].nx; ls[pre].nx = nxt; ls[nxt].pr = pre; ls[val].apare = 0; } inline void adauga(int val) { ls[val].apare = 1; ls[val].pr = DUMMY1; ls[val].nx = ls[DUMMY1].nx; ls[DUMMY1].nx = val; ls[ls[val].nx].pr = val; } int care(int val) { return ls[val].apare; } int viz[500005], contor = 0, f[500005]; int sta[500005]; int main() { ios_base::sync_with_stdio(false); int n, m, a, b; cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } vector<int> to_add; int cnt = 0; ls[DUMMY1].pr = DUMMY1; ls[DUMMY1].nx = DUMMY2; ls[DUMMY2].pr = DUMMY1; ls[DUMMY2].nx = DUMMY2; ls[DUMMY1].apare = 1; ls[DUMMY2].apare = 1; for (int j = 1; j <= n; j++) { adauga(j); } for (int i = 1; i <= n; i++) { if (viz[i] == 0) { sterge(i); ++contor; sta[++cnt] = i; while (cnt != 0) { int nod = sta[cnt]; cnt--; viz[nod] = 1; sol[contor].push_back(nod); for (auto u : v[nod]) { if (ls[u].apare == 1) { sterge(u); } } for (int j = ls[DUMMY1].nx; j != DUMMY2; j = ls[j].nx) { if (viz[j] == 0) { viz[j] = 1; sta[++cnt] = j; sterge(j); } } for (auto u : v[nod]) { if (viz[u] == 0) { adauga(u); } } } } } cout << contor << "\n"; for (int i = 1; i <= contor; i++) { cout << sol[i].size() << " "; for (auto u : sol[i]) { cout << u << " "; } cout << "\n"; } return 0; }
CPP
190_E. Counter Attack
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.
2
11
//package round120; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Map; public class E { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(), m= ni(); int[] from = new int[m]; int[] to = new int[m]; for(int i = 0;i < m;i++){ from[i] = ni()-1; to[i] = ni()-1; } int[][] g = packU(n, from, to); int[][] clus = decompose(g); out.println(clus.length); for(int[] c : clus){ out.print(c.length); for(int v : c){ out.print(" " + (v+1)); } out.println(); } } static int[][] packU(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int f : from) p[f]++; for (int t : to) p[t]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]]; for (int i = 0; i < from.length; i++) { g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } public static int[][] decompose(int[][] g) { int n = g.length; int mindeg = n+1; int arg = -1; for(int i = 0;i < n;i++){ if(g[i].length < mindeg){ mindeg = g[i].length; arg = i; } } int[] garg = Arrays.copyOf(g[arg], g[arg].length); Arrays.sort(garg); boolean[][] h = filterDense(g, garg); int m = h.length; // get complement for(int i = 0;i < m;i++){ for(int j = 0;j < m;j++){ if(i != j)h[i][j] = !h[i][j]; } } int[] clus = classify(h); boolean[] connectedWithZero = new boolean[m]; for(int i = 0;i < m;i++){ int x = garg[i]; if(connectedWithZero[clus[i]])continue; if(g[x].length < n-m){ connectedWithZero[clus[i]] = true; }else{ int disconnectedWithOuterThanH = 0; for(int e : g[x]){ if(Arrays.binarySearch(garg, e) < 0){ disconnectedWithOuterThanH++; } } connectedWithZero[clus[i]] = disconnectedWithOuterThanH < n-m; } } int[][] ret = new int[n][]; ret[0] = new int[n]; int p = 0, q = 0; for(int i = 0;i < n;i++){ if(p < garg.length && garg[p] == i){ if(connectedWithZero[clus[p]]){ ret[0][q++] = i; } p++; }else{ ret[0][q++] = i; } } ret[0] = Arrays.copyOf(ret[0], q); int retgen = 1; int[][] b = makeBuckets(clus); for(int i = 0;i < b.length;i++){ if(b[i].length > 0 && !connectedWithZero[i]){ for(int j = 0;j < b[i].length;j++){ b[i][j] = garg[b[i][j]]; } ret[retgen++] = b[i]; } } return Arrays.copyOf(ret, retgen); } static int[] classify(boolean[][] g) { int n = g.length; int[] q = new int[n]; int[] clus = new int[n]; Arrays.fill(clus, -1); int clusgen = 0; for(int i = 0;i < n;i++){ if(clus[i] == -1){ int qp = 0; clus[i] = clusgen; q[qp++] = i; for(int r = 0;r < qp;r++){ int cur = q[r]; for(int j = 0;j < n;j++){ if(g[cur][j] && clus[j] == -1){ clus[j] = clusgen; q[qp++] = j; } } } clusgen++; } } return clus; } static boolean[][] filterDense(int[][] g, int[] valids) { boolean[][] h = new boolean[valids.length][valids.length]; int m = valids.length; Map<Integer, Integer> gtoh = new HashMap<>(); for(int i = 0;i < m;i++)gtoh.put(valids[i], i); for(int i = 0;i < m;i++){ for(int e : g[valids[i]]){ if(gtoh.containsKey(e))h[i][gtoh.get(e)] = true; } } return h; } public static int[][] makeBuckets(int[] a) { int n = a.length; int sup = 0; for(int v : a)sup = Math.max(sup, v); int[][] bucket = new int[sup+1][]; int[] bp = new int[sup+1]; for(int i = 0;i < n;i++)bp[a[i]]++; for(int i = 0;i <= sup;i++)bucket[i] = new int[bp[i]]; for(int i = n-1;i >= 0;i--)bucket[a[i]][--bp[a[i]]] = i; return bucket; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
JAVA
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; int n, m; int fa[500010], ind[500010]; struct node { int to, next; } edge[500010 * 10]; int head[500010], sumedge, poi[500010], tot, pos[500010]; int cnt, num[500010]; vector<int> v[500010]; bool used[500010], g[2005][2005]; map<pair<int, int>, bool> mmm; int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); } void add(int u, int v) { edge[++sumedge].to = v; edge[sumedge].next = head[u]; head[u] = sumedge; } void merge(int u, int v) { fa[getfa(u)] = getfa(v); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { fa[i] = i; } int ui, vi; for (int i = 1; i <= m; ++i) { scanf("%d%d", &ui, &vi); if (ui == vi) continue; if (vi < ui) swap(ui, vi); if (mmm.count(make_pair(ui, vi))) continue; add(ui, vi); add(vi, ui); ind[ui]++; ind[vi]++; mmm[make_pair(ui, vi)] = 1; } for (int i = 1; i <= n; ++i) fa[i] = i; int id = 1, rem = 0; for (int i = 2; i <= n; ++i) { if (ind[i] < ind[id]) id = i; } for (int i = head[id]; i; i = edge[i].next) { int v = edge[i].to; if (used[v]) continue; used[v] = 1; poi[++tot] = v; pos[v] = tot; } used[id] = 1; poi[++tot] = id; pos[id] = tot; for (int i = 1; i <= n; ++i) { if (!used[i]) { merge(id, i); ++rem; } } memset(g, 1, sizeof(g)); for (int i = 1; i <= tot; ++i) { int u = poi[i]; for (int j = head[u]; j; j = edge[j].next) { int v = edge[j].to; if (used[v]) { g[pos[v]][pos[u]] = 0; g[pos[u]][pos[v]] = 0; } } } for (int i = 1; i <= tot; ++i) { int t = 0; for (int j = 1; j <= tot; ++j) { if (i == j) continue; if (g[i][j]) { merge(poi[i], poi[j]); } else { ++t; } } if (ind[poi[i]] - t < rem) merge(poi[i], id); } for (int i = 1; i <= n; ++i) { if (i == getfa(i)) num[i] = ++cnt; } for (int i = 1; i <= n; ++i) { v[getfa(i)].push_back(i); } printf("%d\n", cnt); for (int i = 1; i <= n; ++i) { if (i == getfa(i)) { int s = v[i].size(); printf("%d", s); for (int j = 0; j < s; ++j) { printf(" %d", v[i][j]); } putchar('\n'); } } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> const double eps = 1e-10; using namespace std; using ll = long long; using ul = unsigned long long; using PII = pair<int, int>; const int NN = 1011101; const int NZ = 511100; const int MM = 151; const int need = (1 << 30) - 1; int n, m, s, x, i, j, t, a, b, k, c, r, col[NN]; int l; ll in[NN]; int comp[NN]; PII gr[NN]; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", &a, &b); a--, b--; gr[i + i] = {a, b}; gr[i + i + 1] = {b, a}; } set<int> sv; for (int i = 0; i < n; i++) sv.insert(i); m += m; sort(gr, gr + m); vector<vector<int>> answww; while (!sv.empty()) { int ff = *sv.begin(); sv.erase(ff); queue<int> qq; qq.push(ff); vector<int> _comp; while (!qq.empty()) { int v = qq.front(); qq.pop(); _comp.push_back(v); vector<int> rv; for (auto it : sv) { int vv = it; if (!binary_search(gr, gr + m, make_pair(v, it))) { qq.push(vv); rv.push_back(vv); } } for (int i = 0; i < rv.size(); i++) { sv.erase(rv[i]); } } answww.push_back(_comp); } printf("%d\n", answww.size()); for (int i = 0; i < answww.size(); i++) { printf("%d ", answww[i].size()); for (int j = 0; j < answww[i].size(); j++) { printf(" %d", answww[i][j] + 1); } puts("\n"); } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5; int n, m, mind, sub[maxn + 10], br, no_edge[maxn + 10], par[maxn + 10], sz[maxn + 10]; vector<int> vect[maxn + 10], rez[maxn + 10]; vector<int> mapa[maxn + 10]; int fnd(int x, int y) { int l = 0; int r = mapa[x].size() - 1; int sr; int ret = -1; while (l <= r) { sr = (l + r) / 2; if (mapa[x][sr] <= y) { ret = mapa[x][sr]; l = sr + 1; } else r = sr - 1; } if (ret == y) return 1; else return 0; } int root(int x) { if (par[x] != x) par[x] = root(par[x]); return par[x]; } void join(int x, int y) { x = root(x); y = root(y); if (sz[x] < sz[y]) swap(x, y); if (x == y) return; sz[x] += sz[y]; par[y] = x; } void init() { for (int i = 1; i <= n; i++) { par[i] = i; sz[i] = 1; } } int main() { scanf("%d %d", &n, &m); init(); for (int i = 1; i <= m; i++) { int u, v; scanf("%d %d", &u, &v); vect[u].push_back(v); vect[v].push_back(u); mapa[u].push_back(v); mapa[v].push_back(u); } for (int i = 1; i <= n; i++) sort(mapa[i].begin(), mapa[i].end()); int minn = 1e9; for (int i = 1; i <= n; i++) { if (minn > vect[i].size()) { minn = vect[i].size(); mind = i; } } for (int i = 0; i < vect[mind].size(); i++) { int id = vect[mind][i]; sub[++br] = id; no_edge[id] = 1; } no_edge[mind] = 1; for (int i = 1; i <= n; i++) { if (no_edge[i]) continue; join(mind, i); } for (int i = 1; i <= br; i++) { for (int j = i + 1; j <= br; j++) { int id1 = sub[i]; int id2 = sub[j]; if (fnd(id1, id2)) continue; join(id1, id2); } for (int j = 1; j <= n; j++) { if (no_edge[j]) continue; if (fnd(sub[i], j)) continue; join(sub[i], j); } } for (int i = 1; i <= n; i++) rez[root(i)].push_back(i); int pp = 0; for (int i = 1; i <= n; i++) if (rez[i].size() > 0) pp++; printf("%d\n", pp); for (int i = 1; i <= n; i++) { if (rez[i].size() == 0) continue; printf("%d ", rez[i].size()); for (int j = 0; j < rez[i].size(); j++) printf("%d ", rez[i][j]); printf("\n"); } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 10, M = 1e6 + 100, MOD = 1e9 + 7, ML = 25; int a[M], ne[M]; vector<int> ve[M], adj[M]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } for (int i = 1; i <= n; i++) { adj[i].push_back(INF); sort(adj[i].begin(), adj[i].end()); ne[i] = i + 1; } ne[n] = -1; int l = 0, r = 0, cnt = 0, link = 1; while (link != -1) { int la = r; a[r++] = link; link = ne[link]; while (l < r) { int v = a[l++]; ve[cnt].push_back(v); int u = link, la = -1; while (u != -1) { int z = *lower_bound(adj[v].begin(), adj[v].end(), u); if (z != u) { if (u == link) link = ne[link]; if (la != -1) ne[la] = ne[u]; a[r++] = u; } else la = u; u = ne[u]; } } cnt++; } cout << cnt << "\n"; for (int i = 0; i < cnt; i++) { cout << ve[i].size() << " "; for (int u : ve[i]) cout << u << " "; cout << "\n"; } }
CPP
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; long long int n, m; vector<long long int> a[500005]; set<long long int> vis; vector<vector<long long int> > ans; vector<long long int> vv; inline void dfs(long long int node) { vis.erase(node); vv.push_back(node); long long int cur = 0; while (!vis.empty()) { auto it = vis.upper_bound(cur); if (it == vis.end()) break; long long int tmp = (*it); if (!binary_search((a[node]).begin(), (a[node]).end(), tmp)) { dfs(tmp); } cur = (tmp); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int i, j; cin >> n >> m; for (i = 0; i < m; i++) { long long int u, v; cin >> u >> v; a[u].push_back(v); a[v].push_back(u); } for (i = 1; i <= n; i++) { vis.insert(i); sort((a[i]).begin(), (a[i]).end()); } long long int cnt = 0; for (i = 1; i <= n; i++) { if (vis.find(i) != vis.end()) { cnt++; vv.clear(); dfs(i); ans.push_back(vv); } } cout << cnt << "\n"; for (auto it : ans) { cout << (long long int)(it.size()) << " "; for (auto iit : it) cout << iit << " "; cout << "\n"; } return 0; }
CPP
190_E. Counter Attack
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.
2
11
#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; unordered_set<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 (adj[u].find(*it) == adj[u].end()) { 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(v), adj[v].emplace(u); } for (int i = 0; i < n; ++i) unvis.emplace(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() << ' '; for (auto& j : i) cout << j + 1 << ' '; cout << '\n'; } }
CPP
190_E. Counter Attack
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.
2
11
//package prac; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Round120E { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(), m = ni(); int[] from = new int[m]; int[] to = new int[m]; for(int i = 0;i < m;i++){ from[i] = ni()-1; to[i] = ni()-1; } int[][] g = packU(n, from, to); DJSet ds = enumConnectedComponentFromComplementGraph(g); int[][] h = ds.makeUp(); out.println(ds.count()); for(int i = 0;i < n;i++){ if(h[i] != null){ out.print(h[i].length); for(int v : h[i]){ out.print(" " + (v+1)); } out.println(); } } } public static class DJSet { public int[] upper; public DJSet(int n) { upper = new int[n]; Arrays.fill(upper, -1); } public int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } public boolean equiv(int x, int y) { return root(x) == root(y); } public boolean union(int x, int y) { x = root(x); y = root(y); if (x != y) { if (upper[y] < upper[x]) { int d = x; x = y; y = d; } upper[x] += upper[y]; upper[y] = x; } return x == y; } public int count() { int ct = 0; for (int u : upper) if (u < 0) ct++; return ct; } public int[][] makeUp() { int n = upper.length; int[][] ret = new int[n][]; int[] rp = new int[n]; for(int i = 0;i < n;i++){ if(upper[i] < 0)ret[i] = new int[-upper[i]]; } for(int i = 0;i < n;i++){ int r = root(i); ret[r][rp[r]++] = i; } return ret; } } static int[][] packU(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int f : from) p[f]++; for (int t : to) p[t]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]]; for (int i = 0; i < from.length; i++) { g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } public DJSet enumConnectedComponentFromComplementGraph(int[][] g) { int n = g.length; int[] top = new int[n]; for(int i = 0;i < n;i++)top[i] = i; boolean[] processed = new boolean[n]; DJSet ds = new DJSet(n); int ntop = top.length; long[] S = new long[4]; // find mindegree node A int mind = 99999999; int argmind = -1; for(int u = 0;u < ntop;u++){ int i = top[u]; if(g[i].length < mind){ mind = g[i].length; argmind = i; } } Arrays.sort(g[argmind]); // connect A to nodes not in g[A] for(int u = 0;u < ntop;u++){ int i = top[u]; if(Arrays.binarySearch(g[argmind], i) < 0){ ds.union(argmind, i); } } // connect Bs(in g[A]) to nodes not in g[B] // O(m/n*n*alpha*log n) for(int e : g[argmind]){ if(processed[ds.root(e)])continue; Arrays.sort(g[e]); for(int u = 0;u < ntop;u++){ int i = top[u]; if(Arrays.binarySearch(g[e], i) < 0){ ds.union(e, i); } } } processed[ds.root(argmind)] = true; // filter nodes to be processed int p = 0; for(int i = 0;i < ntop;i++){ if(!ds.equiv(argmind, top[i])){ top[p++] = top[i]; } } ntop = p; return ds; } void run() throws Exception { // int n = 1400, m = n*(n-1)/2; // Random gen = new Random(); // StringBuilder sb = new StringBuilder(); // sb.append(n + " "); // sb.append(m + " "); // for (int i = 0; i < n; i++) { // for(int j = i+1;j < n;j++){ // sb.append(i+1 + " "); // sb.append(j+1 + " "); // } // } // INPUT = sb.toString(); is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Round120E().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
JAVA
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; const long long N = 1e5 + 5; unordered_set<long long> ss[5 * N]; unordered_set<long long> s2; vector<long long> ans; void bfs(long long uu) { queue<long long> qq; qq.push(uu); s2.erase(uu); while (!qq.empty()) { long long u = qq.front(); qq.pop(); ans.push_back(u); vector<long long> ans1; for (long long v : s2) { if (ss[u].find(v) == ss[u].end()) { ans1.push_back(v); qq.push(v); } } for (long long j : ans1) { s2.erase(j); } } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; long long a, b; for (long long i = 0; i < m; i++) { cin >> a >> b; ss[a].insert(b); ss[b].insert(a); } for (long long i = 1; i <= n; i++) { s2.insert(i); } vector<vector<long long> > res; for (long long i = 1; i <= n; i++) { if (s2.find(i) != s2.end()) { bfs(i); res.push_back(ans); ans.clear(); } } cout << (long long)res.size() << "\n"; for (vector<long long> i : res) { cout << (long long)i.size() << " "; for (long long j : i) { cout << j << " "; } cout << "\n"; } return 0; }
CPP
190_E. Counter Attack
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.
2
11
import java.util.*; import java.io.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; public class CounterAttack { private static InputReader in; private static PrintWriter out; public static void main(String[] args) { new CounterAttack().run(); } private static class InputReader { private final InputStream is; private final byte[] inbuf = new byte[1024]; private int lenbuf = 0; private int ptrbuf = 0; public InputReader(InputStream stream) { is = stream; } private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public double nextDouble() { return Double.parseDouble(nextString()); } public char nextChar() { return (char) skip(); } public String nextString() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public char[] nextString(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } private static final boolean oj = System.getProperty("ONLINE_JUDGE") != null; private static void debug(Object... o) { if (!oj) { System.out.println(Arrays.deepToString(o)); } } /*==================================================================================================================*/ int n, m; int[][] bad; ArrayList<ArrayList<Integer>> comps; TreeSet<Integer> nVis; void dfs(int u, ArrayList<Integer> comp) { comp.add(u); nVis.remove(u); for (Integer v = nVis.isEmpty() ? null : nVis.first(); v != null; v = nVis.higher(v)) { if (Arrays.binarySearch(bad[u], v) < 0) { dfs(v, comp); } } } void solve() { n = in.nextInt(); m = in.nextInt(); int[] from = new int[m]; int[] to = new int[m]; for (int i = 0; i < m; i++) { int u = in.nextInt(); int v = in.nextInt(); from[i] = u; to[i] = v; } bad = packU(n + 1, from, to); for (int[] a : bad) { Arrays.sort(a); } nVis = new TreeSet<>(); for (int i = 1; i <= n; i++) { nVis.add(i); } comps = new ArrayList<>(); for (int i = 1; i <= n; i++) { if (nVis.contains(i)) { ArrayList<Integer> comp = new ArrayList<>(); dfs(i, comp); comps.add(comp); } } out.println(comps.size()); for (ArrayList<Integer> comp : comps) { out.print(comp.size() + " "); for (int u : comp) { out.print(u + " "); } out.println(); } } static int[][] packU(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int f : from) p[f]++; for (int t : to) p[t]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]]; for (int i = 0; i < from.length; i++) { g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } public void run() { in = new InputReader(System.in); out = new PrintWriter(System.out); int tt = 1; // int tt = in.nextInt(); for (int i = 1; i <= tt; i++) { solve(); } out.flush(); } }
JAVA
190_E. Counter Attack
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.
2
11
#include <bits/stdc++.h> using namespace std; const int MAX_N = 5e5 + 10; vector<int> cc[MAX_N]; vector<int> adj[MAX_N]; vector<int> rem; set<int> seen; queue<int> q; void bfs(int u, int num) { q.push(u), seen.erase(u); cc[num].push_back(u + 1); while (((int)(q).size())) { u = q.front(); rem.clear(); q.pop(); int p = 0; for (auto i : seen) { while (p < ((int)(adj[u]).size()) && adj[u][p] < i) p++; if (p < ((int)(adj[u]).size()) && adj[u][p] == i) continue; rem.push_back(i); } for (auto i : rem) { q.push(i), seen.erase(i); cc[num].push_back(i + 1); } } } int main() { ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; while (m--) { int u, v; cin >> u >> v; u--, v--; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 0; i < n; i++) { seen.insert(i); sort(adj[i].begin(), adj[i].end()); } int ans = 0; for (int i = 0; i < n; i++) if (seen.count(i)) bfs(i, ans++); cout << ans << endl; for (int i = 0; i < ans; i++) { cout << ((int)(cc[i]).size()) << ' '; for (auto j : cc[i]) cout << j << ' '; cout << endl; } return 0; }
CPP
190_E. Counter Attack
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.
2
11
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Collection; import java.util.InputMismatchException; import java.io.IOException; import java.util.Random; import java.util.NoSuchElementException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CounterAttack solver = new CounterAttack(); solver.solve(1, in, out); out.close(); } static class CounterAttack { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); EzIntArrayList[] bad = new EzIntArrayList[n + 1]; Arrays.setAll(bad, i -> new EzIntArrayList()); for (int i = 0; i < m; i++) { int u = in.nextInt(); int v = in.nextInt(); bad[u].add(v); bad[v].add(u); } CounterAttack.DSU dsu = new CounterAttack.DSU(n); int minV = 1; int minD = Integer.MAX_VALUE / 2; for (int i = 1; i <= n; i++) { if (bad[i].size() < minD) { minV = i; minD = bad[i].size(); } } bad[minV].sort(); for (int i = 1; i <= n; i++) { if (!bad[minV].binarySearch(i)) { dsu.unite(minV, i); } } // 2mlog(2m) + 2mlog(2m) + nlog(2m) for (EzIntIterator it = bad[minV].iterator(); it.hasNext(); ) { int v = it.next(); bad[v].sort(); for (int i = 1; i <= n; i++) { if (!bad[v].binarySearch(i)) { dsu.unite(v, i); } } } EzIntArrayList[] comps = new EzIntArrayList[n + 1]; Arrays.setAll(comps, i -> new EzIntArrayList()); for (int i = 1; i <= n; i++) { int ri = dsu.find(i); comps[ri].add(i); } out.println(dsu.numSets); for (EzIntArrayList comp : comps) { if (!comp.isEmpty()) { out.print(comp.size() + " "); for (EzIntIterator it = comp.iterator(); it.hasNext(); ) { int u = it.next(); out.print(u + " "); } out.println(); } } } static class DSU { int[] link; int[] sz; int numSets; DSU(int n) { link = new int[n + 1]; Arrays.setAll(link, i -> i); sz = new int[n + 1]; Arrays.fill(sz, 1); numSets = n; } int find(int u) { while (u != link[u]) { u = link[u]; } return u; } void unite(int u, int v) { u = find(u); v = find(v); if (u != v) { if (sz[u] > sz[v]) { int tmp = u; u = v; v = tmp; } link[u] = v; sz[v] += sz[u]; numSets--; } } } } static final class EzIntSort { private static final Random rnd = new Random(); private EzIntSort() { } private static void randomShuffle(int[] a, int left, int right) { for (int i = left; i < right; i++) { int j = i + rnd.nextInt(right - i); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } public static void safeArraysSort(int[] a, int left, int right) { if (left > right || left < 0 || right > a.length) { throw new IllegalArgumentException( "Incorrect range [" + left + ", " + right + ") was specified for sorting, length = " + a.length); } randomShuffle(a, left, right); Arrays.sort(a, left, right); } } static class InputReader { private final InputStream is; private final byte[] inbuf = new byte[1024]; private int lenbuf = 0; private int ptrbuf = 0; public InputReader(InputStream stream) { is = stream; } private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } static final class PrimitiveHashCalculator { private PrimitiveHashCalculator() { } public static int getHash(int x) { return x; } } static class EzIntArrayList implements EzIntList, EzIntStack { private static final int DEFAULT_CAPACITY = 10; private static final double ENLARGE_SCALE = 2.0; private static final int HASHCODE_INITIAL_VALUE = 0x811c9dc5; private static final int HASHCODE_MULTIPLIER = 0x01000193; private int[] array; private int size; public EzIntArrayList() { this(DEFAULT_CAPACITY); } public EzIntArrayList(int capacity) { if (capacity < 0) { throw new IllegalArgumentException("Capacity must be non-negative"); } array = new int[capacity]; size = 0; } public EzIntArrayList(EzIntCollection collection) { size = collection.size(); array = new int[size]; int i = 0; for (EzIntIterator iterator = collection.iterator(); iterator.hasNext(); ) { array[i++] = iterator.next(); } } public EzIntArrayList(int[] srcArray) { size = srcArray.length; array = new int[size]; System.arraycopy(srcArray, 0, array, 0, size); } public EzIntArrayList(Collection<Integer> javaCollection) { size = javaCollection.size(); array = new int[size]; int i = 0; for (int element : javaCollection) { array[i++] = element; } } public int size() { return size; } public boolean isEmpty() { return size == 0; } public EzIntIterator iterator() { return new EzIntArrayListIterator(); } public boolean add(int element) { if (size == array.length) { enlarge(); } array[size++] = element; return true; } private void enlarge() { int newSize = Math.max(size + 1, (int) (size * ENLARGE_SCALE)); int[] newArray = new int[newSize]; System.arraycopy(array, 0, newArray, 0, size); array = newArray; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; EzIntArrayList that = (EzIntArrayList) o; if (size != that.size) { return false; } for (int i = 0; i < size; i++) { if (array[i] != that.array[i]) { return false; } } return true; } public int hashCode() { int hash = HASHCODE_INITIAL_VALUE; for (int i = 0; i < size; i++) { hash = (hash ^ PrimitiveHashCalculator.getHash(array[i])) * HASHCODE_MULTIPLIER; } return hash; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < size; i++) { sb.append(array[i]); if (i < size - 1) { sb.append(", "); } } sb.append(']'); return sb.toString(); } public void sort() { EzIntSort.safeArraysSort(array, 0, size); } public int lowerBound(int val) { int ret = -1; for (int jump = size; jump >= 1; jump /= 2) { while (ret + jump < size && array[ret + jump] < val) { ret += jump; } } return ret + 1; } public boolean binarySearch(int val) { int lb = lowerBound(val); return lb < size && array[lb] == val; } private class EzIntArrayListIterator implements EzIntIterator { private int curIndex = 0; public boolean hasNext() { return curIndex < size; } public int next() { if (curIndex == size) { throw new NoSuchElementException("Iterator doesn't have more elements"); } return array[curIndex++]; } } } static interface EzIntIterator { boolean hasNext(); int next(); } static interface EzIntList extends EzIntCollection { int size(); EzIntIterator iterator(); boolean equals(Object object); int hashCode(); String toString(); } static interface EzIntCollection { int size(); EzIntIterator iterator(); boolean equals(Object object); int hashCode(); String toString(); } static interface EzIntStack extends EzIntCollection { int size(); EzIntIterator iterator(); boolean equals(Object object); int hashCode(); String toString(); } }
JAVA
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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"; } }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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"; } }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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"); } }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
import java.util.*; import java.io.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; public class CounterAttackDSU { private static InputReader in; private static PrintWriter out; public static void main(String[] args) { new CounterAttackDSU().run(); } private static class InputReader { private final InputStream is; private final byte[] inbuf = new byte[1024]; private int lenbuf = 0; private int ptrbuf = 0; public InputReader(InputStream stream) { is = stream; } private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public double nextDouble() { return Double.parseDouble(nextString()); } public char nextChar() { return (char) skip(); } public String nextString() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public char[] nextString(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } private static final boolean oj = System.getProperty("ONLINE_JUDGE") != null; private static void debug(Object... o) { if (!oj) { System.out.println(Arrays.deepToString(o)); } } /*==================================================================================================================*/ void solve() { int n = in.nextInt(); int m = in.nextInt(); int[] from = new int[m]; int[] to = new int[m]; for (int i = 0; i < m; i++) { int u = in.nextInt(); int v = in.nextInt(); from[i] = u; to[i] = v; } int[][] bad = packU(n + 1, from, to); DSU dsu = new DSU(n); int minV = 1; int minD = Integer.MAX_VALUE / 2; for (int i = 1; i <= n; i++) { if (bad[i].length < minD) { minV = i; minD = bad[i].length; } } Arrays.sort(bad[minV]); for (int i = 1, ptr = 0; i <= n; i++) { if (Arrays.binarySearch(bad[minV], i) < 0) { dsu.unite(minV, i); } } for (int v : bad[minV]) { Arrays.sort(bad[v]); for (int i = 1, ptr = 0; i <= n; i++) { if (Arrays.binarySearch(bad[v], i) < 0) { dsu.unite(v, i); } } } ArrayList<Integer>[] comps = new ArrayList[n + 1]; Arrays.setAll(comps, i -> new ArrayList<>()); for (int i = 1; i <= n; i++) { int ri = dsu.find(i); comps[ri].add(i); } out.println(dsu.numSets); for (ArrayList<Integer> comp : comps) { if (!comp.isEmpty()) { out.print(comp.size() + " "); for (int u : comp) { out.print(u + " "); } out.println(); } } } static class DSU { int[] link, sz; int numSets; DSU(int n) { link = new int[n + 1]; Arrays.setAll(link, i -> i); sz = new int[n + 1]; Arrays.fill(sz, 1); numSets = n; } boolean same(int u, int v) { return find(u) == find(v); } int find(int u) { while (u != link[u]) { u = link[u]; } return u; } void unite(int u, int v) { u = find(u); v = find(v); if (u != v) { if (sz[u] > sz[v]) { int tmp = u; u = v; v = tmp; } link[u] = v; sz[v] += sz[u]; numSets--; } } } static int[][] packU(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int f : from) p[f]++; for (int t : to) p[t]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]]; for (int i = 0; i < from.length; i++) { g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } public void run() { in = new InputReader(System.in); out = new PrintWriter(System.out); int tt = 1; // int tt = in.nextInt(); for (int i = 1; i <= tt; i++) { solve(); } out.flush(); } }
JAVA
190_E. Counter Attack
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.
2
11
#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'; } } }
CPP
190_E. Counter Attack
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.
2
11
#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'; } }
CPP
190_E. Counter Attack
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.
2
11
import java.util.*; import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.io.InputStream; /** * TEST * * Built using CHelper plug-in * Actual solution is at the top * @author AlexFetisov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE_Counter solver = new TaskE_Counter(); solver.solve(1, in, out); out.close(); } } class TaskE_Counter { Graph g; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); g = new Graph(); g.initGraph(n, m); for (int i = 0; i < m; ++i) { g.addEdge(in.nextInt() - 1, in.nextInt() - 1); } Set<Integer> contains = new HashSet<Integer>(); for (int i = 0; i < n; i++) { contains.add(i); } int[] queue = new int[n]; int size, iv, i, v, j, to; List<Integer> color; List<List<Integer>> result = new ArrayList<List<Integer>>(); for (iv = 0; iv < n; ++iv) { if (contains.contains(iv)) { size = 0; queue[size++] = iv; color = new ArrayList<Integer>(); contains.remove(iv); for (i = 0; i < size; ++i) { color.add(queue[i] + 1); Set<Integer> toRemove = new HashSet<>(); for (j = g.first[queue[i]]; j != -1; j = g.next[j]) { to = g.to[j]; if (contains.contains(to)) { toRemove.add(to); contains.remove(to); } } for (int x : contains) { queue[size++] = x; } if (contains.size() > 0) { contains.clear(); } contains.addAll(toRemove); } result.add(color); } } out.println(result.size()); for (List<Integer> list : result) { out.print(list.size()); for (int vv : list) { out.print(" " + vv); } out.println(); } } } class Graph { public int[] from; public int[] to; public int[] first; public int[] next; public int nVertex; public int nEdges; public int curEdge; public Graph() {} public void initGraph(int n, int m) { curEdge = 0; nVertex = n; nEdges = m; from = new int[m * 2]; to = new int[m * 2]; first = new int[n]; next = new int[m * 2]; Arrays.fill(first, -1); } public void addEdge(int a, int b) { next[curEdge] = first[a]; first[a] = curEdge; to[curEdge] = b; from[curEdge] = a; ++curEdge; next[curEdge] = first[b]; first[b] = curEdge; to[curEdge] = a; from[curEdge] = b; ++curEdge; } } class InputReader { private BufferedReader reader; private StringTokenizer stt; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() { try { return reader.readLine().trim(); } catch (IOException e) { return null; } } public String nextString() { while (stt == null || !stt.hasMoreTokens()) { stt = new StringTokenizer(nextLine()); } return stt.nextToken(); } public int nextInt() { return Integer.parseInt(nextString()); } }
JAVA
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
import java.io.*; import java.util.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { int g [][]; int deg []; int edges [][]; ArrayList<ArrayList<Integer>> answer = new ArrayList<ArrayList<Integer>>(); public void solve () throws Exception { int n = nextInt(); int m = nextInt(); g = new int [n][]; deg = new int [n]; edges = new int [m][2]; for (int i = 0; i < m; i++) { int v1 = nextInt() - 1; int v2 = nextInt() - 1; deg[v1]++; deg[v2]++; edges[i][0] = v1; edges[i][1] = v2; } for (int i = 0; i < n; i++) { g [i] = new int [deg[i]]; deg[i] = 0; } for (int i = 0; i < m; i++) { int v1 = edges[i][0]; int v2 = edges[i][1]; g[v1][deg[v1]++] = v2; g[v2][deg[v2]++] = v1; } HashSet<Integer> set = new HashSet<Integer>(); for (int i = 0; i < n; i++) { set.add(i); } LinkedList<Integer> q = new LinkedList<Integer>(); for (int i = 0; i < n; i++) { if (set.contains(i)) { q.add(i); ArrayList<Integer> list = new ArrayList<Integer>(); set.remove(i); while (!q.isEmpty()) { int v = q.pollFirst(); list.add(v + 1); HashSet<Integer> removeSet = new HashSet<Integer>(); for (int j = 0; j < g[v].length; j++) { int to = g[v][j]; if (set.contains(to)) { removeSet.add(to); set.remove(to); } } for (int to : set) { q.add(to); } if (set.size() > 0) { set.clear(); } for (int rmv : removeSet) { set.add(rmv); } } answer.add(list); } } out.println(answer.size()); for (int i = 0; i < answer.size(); i++) { ArrayList<Integer> list = answer.get(i); out.print(list.size()); for (int v : list) { out.print(" "+v); } out.println(); } } static final String fname = ""; static long stime = 0; BufferedReader in; PrintWriter out; StringTokenizer st; private String nextToken () throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } private int nextInt () throws Exception { return Integer.parseInt(nextToken()); } private long nextLong () throws Exception { return Long.parseLong(nextToken()); } private double nextDouble () throws Exception { return Double.parseDouble(nextToken()); } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve (); } catch (Exception e) { throw new RuntimeException(e); } finally { out.close(); } } public static void main(String[] args) { new Thread(null, new Solution(), "", 1<<26).start(); } }
JAVA
190_E. Counter Attack
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.
2
11
#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"); } }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
import java.util.*; import java.io.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; public class CounterAttackDSU { private static InputReader in; private static PrintWriter out; public static void main(String[] args) { new CounterAttackDSU().run(); } private static class InputReader { private final InputStream is; private final byte[] inbuf = new byte[1024]; private int lenbuf = 0; private int ptrbuf = 0; public InputReader(InputStream stream) { is = stream; } private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public double nextDouble() { return Double.parseDouble(nextString()); } public char nextChar() { return (char) skip(); } public String nextString() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public char[] nextString(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } private static final boolean oj = System.getProperty("ONLINE_JUDGE") != null; private static void debug(Object... o) { if (!oj) { System.out.println(Arrays.deepToString(o)); } } /*==================================================================================================================*/ void solve() { int n = in.nextInt(); int m = in.nextInt(); ArrayList<Integer>[] bad = new ArrayList[n + 1]; Arrays.setAll(bad, i -> new ArrayList<>()); for (int i = 0; i < m; i++) { int u = in.nextInt(); int v = in.nextInt(); bad[u].add(v); bad[v].add(u); } DSU dsu = new DSU(n); int minV = 1; int minD = Integer.MAX_VALUE / 2; for (int i = 1; i <= n; i++) { if (bad[i].size() < minD) { minV = i; minD = bad[i].size(); } } Collections.sort(bad[minV]); for (int i = 1, ptr = 0; i <= n; i++) { if (ptr < bad[minV].size() && bad[minV].get(ptr) == i) { ptr++; } else { dsu.unite(minV, i); } } for (int v : bad[minV]) { Collections.sort(bad[v]); for (int i = 1, ptr = 0; i <= n; i++) { if (ptr < bad[v].size() && bad[v].get(ptr) == i) { ptr++; } else { dsu.unite(v, i); } } } ArrayList<Integer>[] comps = new ArrayList[n + 1]; Arrays.setAll(comps, i -> new ArrayList<>()); for (int i = 1; i <= n; i++) { int ri = dsu.find(i); comps[ri].add(i); } out.println(dsu.numSets); for (ArrayList<Integer> comp : comps) { if (!comp.isEmpty()) { out.print(comp.size() + " "); for (int u : comp) { out.print(u + " "); } out.println(); } } } static class DSU { int[] link, sz; int numSets; DSU(int n) { link = new int[n + 1]; Arrays.setAll(link, i -> i); sz = new int[n + 1]; Arrays.fill(sz, 1); numSets = n; } boolean same(int u, int v) { return find(u) == find(v); } int find(int u) { while (u != link[u]) { u = link[u]; } return u; } void unite(int u, int v) { u = find(u); v = find(v); if (u != v) { if (sz[u] > sz[v]) { int tmp = u; u = v; v = tmp; } link[u] = v; sz[v] += sz[u]; numSets--; } } } public void run() { in = new InputReader(System.in); out = new PrintWriter(System.out); int tt = 1; // int tt = in.nextInt(); for (int i = 1; i <= tt; i++) { solve(); } out.flush(); } }
JAVA
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
import java.io.*; import java.util.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { int g [][]; int deg []; int edges [][]; public void solve () throws Exception { int n = nextInt(); int m = nextInt(); g = new int [n][]; deg = new int [n]; edges = new int [m][2]; for (int i = 0; i < m; i++) { int v1 = nextInt() - 1; int v2 = nextInt() - 1; deg[v1]++; deg[v2]++; edges[i][0] = v1; edges[i][1] = v2; } for (int i = 0; i < n; i++) { g [i] = new int [deg[i]]; deg[i] = 0; } for (int i = 0; i < m; i++) { int v1 = edges[i][0]; int v2 = edges[i][1]; g[v1][deg[v1]++] = v2; g[v2][deg[v2]++] = v1; } HashSet<Integer> set = new HashSet<Integer>(); for (int i = 0; i < n; i++) { set.add(i); } int q [] = new int [n]; int mark [] = new int [n]; int remove [] = new int [n]; int k = 0; for (int i = 0; i < n; i++) { if (mark[i] == 0) { k++; int tail = 1; int head = 0; q [0] = i; set.remove(i); while (head < tail) { int v = q[head++]; mark [v] = k; int rsize = 0; for (int j = 0; j < g[v].length; j++) { int to = g[v][j]; if (set.contains(to)) { remove[rsize++] = to; set.remove(to); } } for (int to : set) { q[tail++] = to; } if (set.size() > 0) { set.clear(); } for (int j = 0; j < rsize; j++) { set.add(remove [j]); } } } } int count [] = new int [k]; for (int i = 0; i < n; i++) { count [mark[i] - 1]++; } int ans [][] = new int [k][]; for (int i = 0; i < k; i++) { ans [i] = new int [count[i]]; count[i] = 0; } for (int i = 0; i < n; i++) { ans[mark[i] - 1][count[mark[i] - 1]++] = i + 1; } out.println(k); for (int i = 0; i < k; i++) { out.print(count[i]); for (int j = 0; j < ans[i].length; j++) { out.print(" "+ans[i][j]); } out.println(); } } static final String fname = ""; static long stime = 0; BufferedReader in; PrintWriter out; StringTokenizer st; private String nextToken () throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } private int nextInt () throws Exception { return Integer.parseInt(nextToken()); } private long nextLong () throws Exception { return Long.parseLong(nextToken()); } private double nextDouble () throws Exception { return Double.parseDouble(nextToken()); } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve (); } catch (Exception e) { throw new RuntimeException(e); } finally { out.close(); } } public static void main(String[] args) { new Thread(null, new Solution(), "", 1<<26).start(); } }
JAVA
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
import java.util.List; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.ArrayList; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.TreeSet; import java.util.StringTokenizer; import java.math.BigInteger; import java.util.Collections; import java.util.Collection; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author AlexFetisov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE_Counter solver = new TaskE_Counter(); solver.solve(1, in, out); out.close(); } } class TaskE_Counter { Graph g; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); g = new Graph(); g.initGraph(n, m); for (int i = 0; i < m; ++i) { g.addEdge(in.nextInt() - 1, in.nextInt() - 1); } TreeSet<Integer> contains = new TreeSet<Integer>(); for (int i = 0; i < n; i++) { contains.add(i); } int[] queue = new int[n]; int size, iv, i, v, j, to; List<Integer> color; List<List<Integer>> result = new ArrayList<List<Integer>>(); for (iv = 0; iv < n; ++iv) { if (contains.contains(iv)) { size = 0; queue[size++] = iv; color = new ArrayList<Integer>(); contains.remove(iv); for (i = 0; i < size; ++i) { color.add(queue[i] + 1); TreeSet<Integer> toRemove = new TreeSet<Integer>(); for (j = g.first[queue[i]]; j != -1; j = g.next[j]) { to = g.to[j]; if (contains.contains(to)) { toRemove.add(to); contains.remove(to); } } for (int x : contains) { queue[size++] = x; } if (contains.size() > 0) { contains.clear(); } contains.addAll(toRemove); } result.add(color); } } out.println(result.size()); for (List<Integer> list : result) { out.print(list.size()); for (int vv : list) { out.print(" " + vv); } out.println(); } } } class Graph { public int[] from; public int[] to; public int[] first; public int[] next; public int nVertex; public int nEdges; public int curEdge; public Graph() {} public void initGraph(int n, int m) { curEdge = 0; nVertex = n; nEdges = m; from = new int[m * 2]; to = new int[m * 2]; first = new int[n]; next = new int[m * 2]; Arrays.fill(first, -1); } public void addEdge(int a, int b) { next[curEdge] = first[a]; first[a] = curEdge; to[curEdge] = b; from[curEdge] = a; ++curEdge; next[curEdge] = first[b]; first[b] = curEdge; to[curEdge] = a; from[curEdge] = b; ++curEdge; } } class InputReader { private BufferedReader reader; private StringTokenizer stt; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() { try { return reader.readLine().trim(); } catch (IOException e) { return null; } } public String nextString() { while (stt == null || !stt.hasMoreTokens()) { stt = new StringTokenizer(nextLine()); } return stt.nextToken(); } public int nextInt() { return Integer.parseInt(nextString()); } }
JAVA
190_E. Counter Attack
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.
2
11
#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"); } }
CPP
190_E. Counter Attack
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.
2
11
#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'; } }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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"; } }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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"; } }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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'; } }
CPP
190_E. Counter Attack
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.
2
11
#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; } }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
#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"); } }
CPP
190_E. Counter Attack
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.
2
11
#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; }
CPP
190_E. Counter Attack
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.
2
11
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static class FastReader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static long mod = 998244353; public static long[] fac; public static int N = (int) 1e5; public static long[] dp; public static List<Integer>[] edges; public static void main(String[] args) throws IOException { // TODO Auto-generated method stub FastReader sc = new FastReader(); int n = sc.nextInt(); int m = sc.nextInt(); List<Integer>[] edges = new ArrayList[n+1]; for(int i=0;i<=n;++i) edges[i] = new ArrayList<>(); for(int i=1;i<=m;++i) { int u = sc.nextInt(); int v = sc.nextInt(); edges[u].add(v); edges[v].add(u); } DSU dsu = new DSU(n+1); // to enhance for(int i=1;i<=n;++i) Collections.sort(edges[i]); HashSet<Integer> allComp = new HashSet<>(); //contain one element from each comp for(int i=1;i<=n;++i) { Map<Integer,Integer> map = new HashMap<>(); for(int node : edges[i]) { /* * till now comp. are made from nodes * less than i so there is no use of * node having value greater than i */ if(node >= i) break; int root = dsu.findRoot(node); /* * storing the number of node present in some white comp * but connected to i with a black edge */ map.put(root,map.getOrDefault(root,0)+1); } List<Integer> list = new ArrayList<>(); for(int node : allComp) { int root = dsu.findRoot(node); /* * iterating over all comps * if we find any comp having at least one node * which are connected to i by a white edge * then we can merge i to that comp */ if(dsu.size[root] > map.getOrDefault(root,0)) { dsu.unite(i, root); list.add(node); } } /* * as now i is merged into diff comp * so we delete the elements of that comp * and insert i in place of them */ for(int x : list) allComp.remove(x); allComp.add(i); } HashMap<Integer,List<Integer>> map = new HashMap<>(); for(int i=1;i<=n;++i) { add(map,dsu.findRoot(i),i); } out.println(map.size()); for(int x : map.keySet()) { out.print(map.get(x).size()+" "); for(int v : map.get(x)) { out.print(v+" "); } out.println(); } out.close(); } static void add(Map<Integer,List<Integer>> map, int root, int v) { if(map.containsKey(root)) map.get(root).add(v); else { List<Integer> temp = new ArrayList<>(); temp.add(v); map.put(root, temp); } } static class DSU { int n; int[] parent, size; public DSU(int v) { n = v; parent = new int[n]; size = new int[n]; for(int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } public int findRoot(int curr) { if(curr == parent[curr]) return curr; return parent[curr] = findRoot(parent[curr]); } public boolean unite(int a, int b) { int rootA = findRoot(a); int rootB = findRoot(b); if(rootA == rootB) return true; if(size[rootA] > size[rootB]) { parent[rootB] = rootA; size[rootA] += size[rootB]; } else { parent[rootA] = rootB; size[rootB] += size[rootA]; } return false; } } }
JAVA
190_E. Counter Attack
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.
2
11
#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; }
CPP