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
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); long val = in.nextLong(); long res = 6 * val * val - 6 * val + 1; System.out.println(res); } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> int main() { int a; scanf("%d", &a); long long ans = 1; for (int i = 1; i < a; ++i) ans += 12LL * i; printf("%I64d\n", ans); return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; int a; int main() { cin >> a; cout << 6 * a * (a - 1) + 1 << endl; return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
print(1+sum(12*i for i in range(1,int(input()))))
PYTHON3
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
n=input() sum=1 for i in range(1,n): sum+=12*i print sum
PYTHON
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
import math import sys input = sys.stdin.readline def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) n = inp() print(n*(n-1)*6 + 1)
PYTHON3
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
def star(n, starn_prev): if n == 1: return 1 elif n == 2: return 13 else: return starn_prev + 18 + 6 * (1 + 2 * (n - 3)) class CodeforcesTask171BSolution: def __init__(self): self.result = '' self.a = 0 def read_input(self): self.a = int(input()) def process_task(self): current = 0 for x in range(1, self.a + 1): prev = current current = star(x, prev) self.result = str(current) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask171BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
PYTHON3
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
k = int(raw_input()) sum = ans = 0 for i in xrange(k - 1): sum += 12 ans += sum print ans + 1
PYTHON
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; long long val[18260]; void preproce() { val[1] = 1; for (int i = int(2); i < int(18258); i++) val[i] = val[i - 1] + 9 * (i - 1) + 3 * (i - 1); } int main() { preproce(); int n; scanf("%d", &n); cout << val[n] << endl; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
import java.io.*; import java.util.*; public class Main { public static void main (String[]args)throws IOException { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); System.out.println(6*n*(n-1)+1); } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; public class PrB { public static long time; public static void main(String[] args) throws Exception { time = System.currentTimeMillis(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(go(br)); br.close(); System.exit(0); } public static int go(BufferedReader br) throws Exception { int a = Integer.parseInt(br.readLine()); a --; return 1 + 12 * (a * (a + 1) / 2); } public static void checkTime() { System.out.println(System.currentTimeMillis() - time); time = System.currentTimeMillis(); } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long int dp[20000] = {0}; dp[1] = 1; for (int i = 2; i <= 18257; i++) dp[i] = dp[i - 1] + 12 * (i - 1); cout << dp[n] << endl; return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
import java.io.*; import java.lang.Math; import java.util.*; public class Main { public BufferedReader in; public PrintStream out; public boolean log_enabled = true; public void test() { long n = readLong(); long r = 1 + 6 * n*(n-1); out.println(r); } public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = System.out; //in = new BufferedReader(new FileReader("in.txt")); //out = new PrintStream(new File("out.txt")); } catch (Exception e) { return; } //while (true) { //int t = readInt(); for (int i=0; i<t; i++) { test(); } } } public static void main(String args[]) { new Main().run(); } private StringTokenizer tokenizer = null; public int readInt() { return Integer.parseInt(readToken()); } public long readLong() { return Long.parseLong(readToken()); } public double readDouble() { return Double.parseDouble(readToken()); } public String readLn() { try { String s; while ((s = in.readLine()).length()==0); return s; } catch (Exception e) { return ""; } } public String readToken() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } catch (Exception e) { return ""; } } public int[] readIntArray(int n) { int[] x = new int[n]; readIntArray(x, n); return x; } public void readIntArray(int[] x, int n) { for (int i=0; i<n; i++) { x[i] = readInt(); } } public void logWrite(String format, Object... args) { if (!log_enabled) { return; } out.printf(format, args); } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; template <typename Tp> inline void outarr(Tp _begin, Tp _end, const char* _delim = " ") { for (Tp current = _begin; current != _end; ++current) { std::cout << *current << _delim; } std::cout << '\n'; } using ll = int64_t; using pii = std::pair<int, int>; constexpr int INF = 0x3f3f3f3f; constexpr int MOD = static_cast<const int>(1e9 + 7); int F(int x) { if (x == 1) { return 1; } return 12 * (x - 1); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int a; cin >> a; ll ans = 0; for (int i = 1; i <= a; ++i) { ans += F(i); } cout << ans << "\n"; return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
a=int(raw_input()) sum=1 for i in range(1,a): sum+=i*12 print sum
PYTHON
171_B. Star
<image> Input The input contains a single integer a (1 ≀ a ≀ 18257). Output Print a single integer output (1 ≀ output ≀ 2Β·109). Examples Input 2 Output 13
2
8
from math import * a= int(raw_input()) ans=1 for i in range(2,2*a,2): ans+=6*i print ans
PYTHON
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 + 1000; vector<int> G[maxn], ans[maxn]; set<int> s; set<int>::iterator ite; int num[maxn], cnt, res; void bfs(int x) { s.erase(x); queue<int> que; ans[res].push_back(x); que.push(x); cnt = 0; while (que.size()) { int cur = que.front(); que.pop(); cnt = 0; for (ite = s.begin(); ite != s.end(); ite++) { int next = *ite; if (!binary_search(G[cur].begin(), G[cur].end(), *ite)) { que.push(next); ans[res].push_back(next); num[cnt++] = next; } } for (int i = 0; i < cnt; i++) s.erase(num[i]); } res++; } int main() { int n, m; while (scanf("%d%d", &n, &m) != EOF) { for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); G[a].push_back(b); G[b].push_back(a); } for (int i = 1; i <= n; i++) { s.insert(i); sort(G[i].begin(), G[i].end()); } res = 1; for (int i = 1; i <= n; i++) { if (s.count(i)) bfs(i); } printf("%d\n", res - 1); for (int i = 1; i <= res; i++) { if (ans[i].size()) { printf("%d ", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) { if (j == ans[i].size() - 1) printf("%d\n", ans[i][j]); else 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> graph[500005]; set<int> all; int vis[500005]; vector<int> ans[500005]; vector<pair<int, int> > sequence; int n, m, x, y, i, cnt, k = 0; void graph_in() { scanf("%d %d", &n, &m); for (i = 0; i < m; i++) { scanf("%d %d", &x, &y); graph[x].push_back(y); graph[y].push_back(x); } } void init_values() { for (i = 1; i <= n; i++) { all.insert(i); sort(graph[i].begin(), graph[i].end()); } } void bfs(int from) { ans[k].push_back(from); queue<int> q; all.erase(from); q.push(from); vector<int> to_del; vis[from] = 1; int cur, l; while (!q.empty()) { cur = q.front(); q.pop(); for (auto &it : all) { if (!binary_search(graph[cur].begin(), graph[cur].end(), it)) { to_del.push_back(it); ans[k].push_back(it); vis[it] = 1; q.push(it); } } for (l = 0; l < to_del.size(); l++) all.erase(to_del[l]); to_del.clear(); } k++; } void print_ans() { k = 0; int sz; while (ans[k].size()) { printf("%d ", ans[k].size()); for (i = 0; i < ans[k].size(); i++) printf("%d ", ans[k][i]); puts(""); k++; } } int main() { int h; graph_in(); init_values(); for (i = 0; i < n; i++) { if (!vis[i + 1]) { cnt++; bfs(i + 1); } } printf("%d\n", cnt); print_ans(); }
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 comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T lcm(T a, T b) { a = abs(a); b = abs(b); return (a / gcd(a, b)) * b; } template <class T, class X> inline bool getbit(T a, X i) { T t = 1; return ((a & (t << i)) > 0); } template <class T, class X> inline T setbit(T a, X i) { T t = 1; return (a | (t << i)); } template <class T, class X> inline T resetbit(T a, X i) { T t = 1; return (a & (~(t << i))); } inline long long getnum() { char c = getchar(); long long num, sign = 1; for (; c < '0' || c > '9'; c = getchar()) if (c == '-') sign = -1; for (num = 0; c >= '0' && c <= '9';) { c -= '0'; num = num * 10 + c; c = getchar(); } return num * sign; } inline long long power(long long a, long long b) { long long multiply = 1; for (int i = (0); i < (b); i++) { multiply *= a; } return multiply; } int n, m, cnt = 0; vector<int> take[5 * 100002]; set<int> all; vector<int> forbid[5 * 100002]; void dfs(int u) { all.erase(u); take[cnt].push_back(u); auto it = all.begin(); while (it != all.end()) { int v = *it; if (binary_search(forbid[u].begin(), forbid[u].end(), v)) { it++; continue; } dfs(v); it = all.lower_bound(v); } } int main() { int test, cases = 1; scanf("%d%d", &n, &m); int u, v; for (int i = (0); i < (m); i++) { u = getnum(); v = getnum(); forbid[u].push_back(v); forbid[v].push_back(u); } for (int i = (1); i < (n + 1); i++) all.insert(i), sort(forbid[i].begin(), forbid[i].end()); for (int i = (1); i < (n + 1); i++) { if (all.find(i) != all.end()) { cnt++; dfs(i); } } cout << cnt << "\n"; for (int i = (1); i < (cnt + 1); i++) { printf("%d ", (int)take[i].size()); for (int j = (0); j < (take[i].size()); j++) printf("%d ", take[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 i, j, n, m; int ind[500010], last[1000100 * 2], e[1000100 * 2], t; void adde(int a, int b) { t++; e[t] = b; last[t] = ind[a]; ind[a] = t; } vector<int> nxt[2]; vector<int> pp; int ha[500010]; int que[500010], qt; int nt; struct Myset { int ind[500010]; int last[500010]; int e[500010]; int t; int n; void inc() { n++; } void add(int b) { add(n, b); } void add(int a, int b) { t++; last[t] = ind[a]; e[t] = b; ind[a] = t; } void output() { printf("%d\n", n); int num; for (i = 1; i <= n; i++) { num = 0; for (j = ind[i]; j; j = last[j]) { num++; } printf("%d", num); for (j = ind[i]; j; j = last[j]) { printf(" %d", e[j]); } printf("\n"); } } } myset; void make2(set<int>& p) { while (p.size()) { pp.clear(); nxt[nt].push_back(*p.begin()); p.erase(p.begin()); myset.inc(); while (nxt[nt].size()) { for (auto i : nxt[nt]) { myset.add(i); } vector<int>& nextnext = nxt[1 - nt]; for (auto ii : nxt[nt]) { for (i = ind[ii]; i; i = last[i]) { if (p.count(e[i])) { pp.push_back(e[i]); p.erase(e[i]); } } for (auto jj : p) { nextnext.push_back(jj); } p.clear(); p.insert(pp.begin(), pp.end()); pp.clear(); } nxt[nt].clear(); nt = 1 - nt; } } } void make() { int depth = 0; for (i = 1; i <= n; i++) { que[i] = i; } qt = n; while (qt) { myset.inc(); int lastnext = 0; myset.add(que[qt]); qt--; while (myset.ind[myset.n] != lastnext) { i = myset.ind[myset.n]; while (i != lastnext) { depth++; for (j = ind[myset.e[i]]; j; j = last[j]) { ha[e[j]]++; } i = myset.last[i]; } lastnext = myset.ind[myset.n]; i = 1; while (i <= qt) { if (ha[que[i]] != depth) { myset.add(que[i]); swap(que[i], que[qt]); qt--; } else { i++; } } } } } int main() { scanf("%d %d", &n, &m); int a, b; while (m--) { scanf("%d %d", &a, &b); adde(a, b); adde(b, a); } make(); myset.output(); 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 + 5; vector<int> graf[maxn], pos[maxn]; int main() { int n, m; scanf("%d %d", &n, &m); while (m--) { int a, b; scanf("%d %d", &a, &b); graf[a].push_back(b); graf[b].push_back(a); } for (int i = 1; i <= n; ++i) sort(graf[i].begin(), graf[i].end()); set<int> cand; for (int i = 1; i <= n; ++i) cand.insert(i); int c = 0; while (cand.size()) { int s = *cand.begin(); cand.erase(s); ++c; pos[c].push_back(s); queue<int> q; q.push(s); while (q.size()) { int x = q.front(); q.pop(); auto it = cand.begin(); while (it != cand.end()) { if (graf[x].size() > 0 && graf[x].back() >= *it && *(lower_bound(graf[x].begin(), graf[x].end(), *it)) == *it) { ++it; continue; } pos[c].push_back(*it); q.push(*it); it = cand.erase(it); } } } printf("%d\n", c); for (int i = 1; i <= c; ++i) { printf("%d ", pos[i].size()); for (int j = 0; j < pos[i].size(); ++j) printf("%d ", pos[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 maxn = 500010; int pa[maxn], sz[maxn]; vector<int> e[maxn]; set<int> grp, tmp; map<int, int> lft; int getpa(int u) { return u == pa[u] ? u : (pa[u] = getpa(pa[u])); } int main() { int n, m; while (~scanf("%d%d", &n, &m)) { grp.clear(); for (int i = 0; i < (n); ++i) pa[i] = i, sz[i] = 1, e[i].clear(), grp.insert(i); for (int i = 0; i < (m); ++i) { int a, b; scanf("%d%d", &a, &b); a--, b--; e[a].push_back(b), e[b].push_back(a); } for (int i = 0; i < (n); ++i) { sort(e[i].begin(), e[i].end()); e[i].resize(unique(e[i].begin(), e[i].end()) - e[i].begin()); } for (int i = 0; i < (n); ++i) { lft.clear(); for (int j = 0; j < (e[i].size()); ++j) lft[getpa(e[i][j])]++; tmp = grp; for (typeof((grp).begin()) it = (grp).begin(); it != (grp).end(); ++it) if (tmp.find(*it) != tmp.end() && lft[*it] < sz[*it]) { int p1 = getpa(i), p2 = *it; if (p1 != p2) { sz[p1] += sz[p2], pa[p2] = p1; tmp.erase(p2); } } grp = tmp; } for (int i = 0; i < (n); ++i) e[i].clear(); int tot = 0; for (int i = 0; i < (n); ++i) { e[getpa(i)].push_back(i); if (getpa(i) == i) tot++; } printf("%d\n", tot); for (int i = 0; i < (n); ++i) if (e[i].size() > 0) { sort(e[i].begin(), e[i].end()); printf("%d", e[i].size()); for (int j = 0; j < (e[i].size()); ++j) printf(" %d", e[i][j] + 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.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.TreeSet; 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; TreeSet<Integer> nVis; void dfs(int u, EzIntArrayList comp) { comp.add(u); nVis.remove(u); for (Integer v = nVis.isEmpty() ? null : nVis.first(); v != null; v = nVis.higher(v)) { int lb = bad[u].lowerBound(v); if (lb == bad[u].size() || bad[u].get(lb) != 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 TreeSet<>(); 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 interface EzIntList 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(); } } } 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; } public int get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index " + index + " is out of range, size = " + size); } return array[index]; } 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; } 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 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(); } static final class PrimitiveHashCalculator { private PrimitiveHashCalculator() { } public static int getHash(int x) { return x; } } 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); } } }
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; long long modpow(long long a, long long b, long long mod = (long long)(1e9 + 7)) { if (!b) return 1; a %= mod; return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int mxn = 5e5; int n, m; deque<int> nxt; unordered_set<int> adj[mxn]; void bfs(int v, vector<int> &st) { queue<int> pq; pq.push(v); while (pq.size()) { int x = pq.front(); pq.pop(); st.push_back(x); int sz = int((nxt).size()); while (sz--) { int w = nxt.back(); nxt.pop_back(); if (adj[x].count(w)) nxt.push_front(w); else pq.push(w); } } } void solve() { scanf("%d", &n); scanf("%d", &m); for (int i = 0; i < n; i++) nxt.push_back(i); for (int _ = 0; _ < m; _++) { int x, y; scanf("%d", &x); scanf("%d", &y); adj[--x].insert(--y); adj[y].insert(x); } int sz = 0; stringstream ss; while (!nxt.empty()) { int i = nxt.back(); nxt.pop_back(); vector<int> st; bfs(i, st); ss << st.size(); for (int w : st) ss << " " << w + 1; ss << endl; sz++; } cout << sz << endl; cout << ss.str(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); 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 = 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> graph[500005]; set<int> all; int vis[500005]; vector<int> ans[500005]; vector<pair<int, int> > sequence; int n, m, x, y, i, cnt, k = 0; void graph_in() { scanf("%d %d", &n, &m); for (i = 0; i < m; i++) { scanf("%d %d", &x, &y); graph[x].push_back(y); graph[y].push_back(x); } } void init_values() { for (i = 1; i <= n; i++) { all.insert(i); sort(graph[i].begin(), graph[i].end()); } } queue<int> q; void bfs(int from) { ans[k].push_back(from); all.erase(from); q.push(from); vector<int> to_del; vis[from] = 1; int cur, l; while (!q.empty()) { cur = q.front(); q.pop(); for (auto &it : all) { if (binary_search(graph[cur].begin(), graph[cur].end(), it)) continue; else { to_del.push_back(it); ans[k].push_back(it); vis[it] = 1; q.push(it); } } for (l = 0; l < to_del.size(); l++) all.erase(to_del[l]); to_del.clear(); } k++; } void print_ans() { k = 0; int sz; while (ans[k].size()) { sz = ans[k].size(); printf("%d ", sz); for (i = 0; i < sz; i++) printf("%d ", ans[k][i]); puts(""); k++; } } int main() { int h; graph_in(); init_values(); for (i = 0; i < n; i++) { if (!vis[i + 1]) { cnt++; bfs(i + 1); } } printf("%d\n", cnt); print_ans(); }
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
#include <bits/stdc++.h> using namespace std; vector<vector<int>> compos; int complement_component(vector<vector<int>> &g) { int n = g.size(), ans = 0; set<int> S; for (int i = 0; i < n; ++i) S.insert(i); while (S.size()) { ++ans; int theCompo = 0; queue<int> q; q.push(*S.begin()); S.erase(S.begin()); compos.push_back(vector<int>{}); while (q.size()) { int u = q.front(); q.pop(); compos.back().push_back(u); if (g[u].size()) { while (S.size() and *S.begin() < g[u][0]) { q.push(*S.begin()); S.erase(S.begin()); } int it = 0; while (S.size() and it < g[u].size()) { auto cur = S.upper_bound(g[u][it]); while (cur != S.end() and (it + 1 == g[u].size() or *cur < g[u][it + 1])) { q.push(*cur), S.erase(cur); cur = S.upper_bound(g[u][it]); } ++it; } } else { compos.back() = vector<int>(n); iota(compos.back().begin(), compos.back().end(), 0); return 1; } } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int N, M, u, v; cin >> N >> M; vector<vector<int>> g(N); while (M--) { cin >> u >> v, --u, --v; g[u].push_back(v), g[v].push_back(u); } for (int i = 0; i < N; ++i) sort(g[i].begin(), g[i].end()); cout << complement_component(g) << '\n'; sort(compos.begin(), compos.end()); for (auto &compo : compos) { cout << compo.size() << ' '; for (int i = 0; i < compo.size(); ++i) cout << compo[i] + 1 << " \n"[i == compo.size() - 1]; } }
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 MAX = 500005; int n, m; vector<int> adj[MAX]; vector<int> g[MAX]; int f[MAX]; int cnt; int find(int u) { return f[u] = (f[u] == u) ? u : find(f[u]); } void dfs(int u) { f[u] = u + 1; g[cnt].push_back(u); for (int i = find(1), j = 0; i <= n; i = find(i + 1)) { for (; j < (int)adj[u].size() && adj[u][j] < i; ++j) ; if (j == (int)adj[u].size() || adj[u][j] != i) dfs(i); } } int main() { scanf("%d%d", &n, &m); int u, v; for (int i = 0; i < m; ++i) { scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n + 1; ++i) { f[i] = i; sort(adj[i].begin(), adj[i].end()); } for (int i = 1; i <= n; ++i) if (f[i] == i) { ++cnt; dfs(i); } printf("%d\n", cnt); for (int i = 1; i <= cnt; ++i) if (g[i].size()) { printf("%d", (int)g[i].size()); for (int j = 0; j < (int)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
#include <bits/stdc++.h> using namespace std; int n, m, x, y, K; set<int> s; set<pair<int, int> > a; vector<int> ans[500005]; inline void dfs(int nod) { set<int>::iterator it; for (it = s.begin(); it != s.end();) { if (!a.count(make_pair(min(*it, nod), max(*it, nod)))) { int x = *it; s.erase(x); dfs(x); it = s.lower_bound(x); } else it++; } ans[K].push_back(nod); } int main() { ios::sync_with_stdio(false); scanf("%d %d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d %d", &x, &y); if (x > y) swap(x, y); a.insert({x, y}); } for (int i = 1; i <= n; ++i) s.insert(i); while (!s.empty()) { x = *s.begin(); s.erase(x); K++; dfs(x); } printf("%d\n", K); for (int i = 1; i <= K; ++i) { printf("%d ", ans[i].size()); for (auto it : ans[i]) printf("%d ", it); 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 const mxn = 5e5 + 10; vector<int> adj[mxn], v1, v2; set<int> x, y; vector<vector<int> > ans; int mark[mxn]; int main() { ios_base::sync_with_stdio(false); int n, m, v; 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++) { if (mark[i] == 2) continue; v1.clear(); v = i; mark[v] = 2; v1.push_back(v); for (int j = 1; j <= n; j++) { if (mark[j] == 0) y.insert(j); } while (true) { v2.clear(); for (int j = 0; j < adj[v].size(); j++) { int u = adj[v][j]; if (!mark[u]) mark[u] = 1; } for (auto j : y) { if (mark[j] == 0) { mark[j] = 1; x.insert(j); v2.push_back(j); } else mark[j] = 0; } for (int p = 0; p < v2.size(); p++) y.erase(v2[p]); if (x.size() == 0) break; v = *x.begin(); mark[v] = 2; v1.push_back(v); x.erase(x.begin()); } y.clear(); ans.push_back(v1); } cout << ans.size() << "\n"; for (int i = 0; i < ans.size(); 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; int comps[500010]; set<int> notVis; vector<vector<int> > adjList; void dfs(int n, int cmp) { comps[n] = cmp; vector<int> toVis; int idx = 0; for (auto it = notVis.begin(); it != notVis.end();) { while (idx < adjList[n].size() && (*it) > adjList[n][idx]) idx++; if (idx < adjList[n].size() && (*it) == adjList[n][idx]) idx++, it++; else { toVis.push_back((*it)); it = notVis.erase(it); } } for (int i = 0; i < toVis.size(); i++) dfs(toVis[i], cmp); } int main() { ios_base::sync_with_stdio(false); int N, M; cin >> N >> M; adjList.resize(N); for (int i = 0; i < M; i++) { int x, y; cin >> x >> y; x--; y--; adjList[x].push_back(y); adjList[y].push_back(x); } for (int i = 0; i < N; i++) { notVis.insert(i); sort(adjList[i].begin(), adjList[i].end()); } int cmp = 1; while (notVis.size() != 0) { auto it = notVis.begin(); int n = *it; notVis.erase(it); dfs(n, cmp); cmp++; } cout << cmp - 1 << endl; vector<vector<int> > grps(cmp - 1); for (int i = 0; i < N; i++) grps[comps[i] - 1].push_back(i + 1); for (int i = 0; i < grps.size(); i++) { cout << grps[i].size() << " "; for (int j = 0; j < grps[i].size(); j++) cout << grps[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 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()) { q.push(*sv.begin()), ans[++cnt].push_back(q.front()), sv.erase(sv.begin()); while (!q.empty()) { 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 << ans[i].size() << ' '; for (int u : ans[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; set<int> ver; int vis[500100], sz[500100], c; vector<int> comp[500100], g[500100]; void dfs(int u, int p) { comp[c].push_back(u); sz[c]++; for (int i = 0; i < g[u].size(); i++) vis[g[u][i]] = 1; vector<int> t; for (set<int>::iterator it = ver.begin(); it != ver.end(); it++) { if (!vis[*it]) { t.push_back(*it); } } for (int i = 0; i < g[u].size(); i++) vis[g[u][i]] = 0; for (int i = 0; i < t.size(); i++) ver.erase(t[i]); for (int i = 0; i < t.size(); i++) dfs(t[i], u); } int main() { ios::sync_with_stdio(0); int n, m, i, u, j, v; scanf("%d %d", &n, &m); for (i = 1; i <= m; i++) { scanf("%d %d", &u, &v); g[u].push_back(v); g[v].push_back(u); } for (i = 1; i <= n; i++) ver.insert(i); for (i = 1; i <= n; i++) { if (ver.find(i) == ver.end()) continue; ver.erase(i); c++; dfs(i, 0); } printf("%d\n", c); for (i = 1; i <= c; i++) { printf("%d ", sz[i]); for (j = 0; j < sz[i]; j++) printf("%d ", comp[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 N = 5e5 + 1; int n, m, a, b, par[N], d[N], o = N, k, y, anss, cnt; vector<int> adj[N], ans[N]; bool vis[N]; int get_par(int u) { if (u == par[u]) return u; return par[u] = get_par(par[u]); } void add(int u, int v) { u = get_par(u); v = get_par(v); if (u == v) return; if (d[v] > d[u]) par[u] = v; else par[v] = u; if (d[u] == d[v]) d[u]++; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= n; i++) if (adj[i].size() < o) { o = adj[i].size(); k = i; } for (int i = 0; i < adj[k].size(); i++) vis[adj[k][i]] = 1; for (int i = 1; i <= n; i++) par[i] = i; for (int i = 1; i <= n; i++) if (!vis[i]) add(k, i); for (int i = 0; i < adj[k].size(); i++) { y = adj[k][i]; adj[y].push_back(0); adj[y].push_back(n + 1); sort(adj[y].begin(), adj[y].end()); cnt = -1; for (int j = 0; j < adj[y].size() - 1; j++) { if (!vis[adj[y][j]]) cnt++; for (int l = adj[y][j] + 1; l < adj[y][j + 1]; l++) if (vis[l]) add(l, y); } if (cnt < n - adj[k].size()) add(y, k); } for (int i = 1; i <= n; i++) par[i] = get_par(i); for (int i = 1; i <= n; i++) ans[par[i]].push_back(i); for (int i = 1; i <= n; i++) if (ans[i].size() > 0) anss++; printf("%d\n", anss); for (int i = 1; i <= n; i++) { if (ans[i].size() > 0) { 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
//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; DJSet ds = new DJSet(n); // find mindegree node A int mind = 99999999; int argmind = -1; for(int i = 0;i < n;i++){ 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 i = 0, p = 0;i < n;i++){ if(p < g[argmind].length && g[argmind][p] == i){ p++; }else{ 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]){ Arrays.sort(g[e]); int p = 0; for(int i = 0;i < n;i++){ if(p < g[e].length && g[e][p] == i){ p++; }else{ ds.union(e, i); } } } 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> 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]; vector<PII> gr; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", &a, &b); a--, b--; gr.push_back({min(a, b), max(a, b)}); } sort(gr.begin(), gr.end()); set<int> sv; for (int i = 0; i < n; i++) sv.insert(i); vector<vector<int>> reswww; while (!sv.empty()) { int ff = *sv.begin(); sv.erase(ff); vector<int> comp; queue<int> qq; qq.push(ff); while (!qq.empty()) { int v = qq.front(); qq.pop(); comp.push_back(v); vector<int> trm; for (auto it : sv) { int x = it; int a = min(x, v); int b = max(x, v); if (!binary_search(gr.begin(), gr.end(), make_pair(a, b))) { trm.push_back(x); qq.push(x); } } for (int i = 0; i < trm.size(); i++) { sv.erase(trm[i]); } } reswww.push_back(comp); } printf("%d\n", reswww.size()); for (int i = 0; i < reswww.size(); i++) { printf("%d", reswww[i].size()); for (int j = 0; j < reswww[i].size(); j++) { printf(" %d", reswww[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; set<int> ele[500007]; set<int> ble; vector<int> cle[500003]; int cnt = 0, pr = 0; void dfs(int x) { std::vector<int> ans; for (auto i : ble) { if (ele[x].find(i) == ele[x].end() && ele[i].find(x) == ele[i].end()) { ans.push_back(i); cle[cnt].push_back(i); } } for (auto i : ans) ble.erase(i); for (auto i : ans) dfs(i); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) ble.insert(i); int ttt = 0, flag = 0; for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); ttt++; ele[x].insert(y); } set<int>::iterator it; while (1) { it = ble.begin(); { dfs(*it); cnt++; if (ble.size() == 0) break; } } printf("%d\n", cnt); for (int i = 0; i < cnt; i++) { printf("%d ", cle[pr].size()); for (auto j : cle[pr]) printf("%d ", j); pr++; 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> #pragma GCC optimize("Ofast") using namespace std; const long double eps = 1e-7; const int inf = 1000000010; const long long INF = 10000000000000010LL; const int mod = 1000000007; const int MAXN = 500010; struct DSU { int par[MAXN]; int sz[MAXN]; DSU() { for (int i = 1; i < MAXN; i++) par[i] = i; for (int i = 1; i < MAXN; i++) sz[i] = 1; } int get(int x) { if (par[x] == x) return x; return par[x] = get(par[x]); } void join(int x, int y) { x = get(x); y = get(y); if (x == y) return; if (sz[x] > sz[y]) swap(x, y); par[x] = y; sz[y] += sz[x]; } } dsu; int n, m, k, u, v, x, y, t, a, b, root = 1; int mark[MAXN]; vector<int> G[MAXN]; vector<int> out[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; while (m--) { cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } for (int i = 2; i <= n; i++) if (G[i].size() < G[root].size()) root = i; for (int i = 0; i <= n; i++) mark[i] = 2; for (int i : G[root]) mark[i] = 1; for (int i = 1; i <= n; i++) if (mark[i] == 2) dsu.join(root, i); for (int i : G[root]) mark[i] = 0; for (int i : G[root]) { int tmp = 0; for (int j : G[i]) { if (mark[j] == 2) tmp++; else mark[j] = 1; } if (tmp < n - G[root].size()) dsu.join(root, i); for (int j : G[root]) if (!mark[j]) dsu.join(i, j); for (int j : G[i]) if (mark[j] != 2) mark[j] = 0; } for (int i = 1; i <= n; i++) { if (dsu.get(i) == i) t++; out[dsu.get(i)].push_back(i); } cout << t << '\n'; for (int i = 1; i <= n; i++) { if (out[i].empty()) continue; cout << out[i].size() << ' '; for (int j : out[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> #pragma GCC optimize("O3") 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
#include <bits/stdc++.h> using namespace std; unordered_map<long long, int> mp; long long temp; vector<int> ans[500010]; int bel[500010], pre[500010], nex[500010], q[500010]; int main() { int n, m, x, y; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { pre[i] = i - 1; nex[i] = i + 1; } nex[0] = 1; pre[n + 1] = n; for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); temp = x * 1000000000ll + y; mp[temp] = 1; temp = y * 1000000000ll + x; mp[temp] = 1; } int s = 1, now = 0; while (1) { for (int i = s; i <= n + 1; i++) { if (bel[i] == 0) { s = i; break; } } if (s > n) break; now++; pre[nex[s]] = pre[s]; nex[pre[s]] = nex[s]; int rear = 0, front = 0; q[rear++] = s; while (rear > front) { int u = q[front++]; bel[u] = now; ans[now].push_back(u); for (int j = nex[0]; j <= n; j = nex[j]) { temp = u * 1000000000ll + j; if (!mp[temp]) { q[rear++] = j; pre[nex[j]] = pre[j]; nex[pre[j]] = nex[j]; } } } s++; } printf("%d\n", now); for (int i = 1; i <= now; i++) { int num = ans[i].size(); printf("%d", num); for (int j = 0; j < num; 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 MAXN = 5 * 1e5 + 1; vector<vector<int> > G(MAXN); set<int> st; bool m_binary(vector<int> &v, int number) { int l = 0, r = v.size() - 1; while (l <= r) { const int middle = (l + r) >> 1; if (v[middle] == number) return true; if (v[middle] < number) { l = middle + 1; } else { r = middle - 1; } } return false; } vector<int> BFS(int &u) { vector<int> ret; queue<int> Q; Q.push(u); while (!Q.empty()) { int v = Q.front(); Q.pop(); set<int>::iterator it = st.begin(); while (it != st.end()) { if (!m_binary(G[v], *it)) { std::set<int>::iterator current = it++; Q.push(*current); st.erase(current); } else { ++it; } } ret.push_back(v); } return ret; } int main() { int N, M; cin >> N >> M; int v, u; while (M--) { scanf("%d %d", &v, &u); G[v].push_back(u); G[u].push_back(v); } for (int i = 1; i <= N; i++) { st.insert(i); sort(G[i].begin(), G[i].end()); } vector<vector<int> > ans; while (!st.empty()) { int now = *st.begin(); st.erase(st.begin()); ans.push_back(BFS(now)); } printf("%d\n", ans.size()); for (unsigned int i = 0; i < ans.size(); i++) { printf("%d ", ans[i].size()); for (unsigned int j = 0; j < ans[i].size(); j++) { printf("%d ", ans[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 parent[500001]; int size[500001]; vector<vector<int> > neighbours; int get_parent(int i) { if (parent[i] == i) return i; else return parent[i] = get_parent(parent[i]); } bool merge(int a, int b) { int parent_a = get_parent(a); int parent_b = get_parent(b); if (parent_a == parent_b) return false; if (size[parent_a] > size[parent_b]) { parent[parent_b] = parent_a; size[parent_a] += size[parent_b]; } else { parent[parent_a] = parent_b; size[parent_b] += size[parent_a]; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<int> temp; for (int i = 0; i <= n; i++) { neighbours.push_back(temp); } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; if (a < b) { neighbours[b].push_back(a); } else { neighbours[a].push_back(b); } } for (int i = 1; i <= 500000; i++) { parent[i] = i; size[i] = 1; } set<int> components; for (int i = 1; i <= n; i++) { map<int, int> edges_with_components; for (int j = 0; j < neighbours[i].size(); j++) { int vertex = neighbours[i][j]; int parent_of_vertex = get_parent(vertex); edges_with_components[parent_of_vertex]++; } map<int, int>::iterator it = edges_with_components.begin(); map<int, bool> merged_with_components; for (set<int>::iterator it = components.begin(); it != components.end(); it++) { int parent = get_parent(*it); int size_of_parent = size[parent]; int edges_with_parent_component = edges_with_components[parent]; if (edges_with_parent_component < size_of_parent) { merge(i, parent); merged_with_components[parent] = true; } } if (merged_with_components.size() == 0) { components.insert(i); } else if (merged_with_components.size() == 1) { continue; } else { int max_size = -1; int max_component = -1; for (map<int, bool>::iterator it = merged_with_components.begin(); it != merged_with_components.end(); it++) { if (size[it->first] > max_size) { max_size = size[it->first]; max_component = it->first; } } for (map<int, bool>::iterator it = merged_with_components.begin(); it != merged_with_components.end(); it++) { if (it->first != max_component) { components.erase(it->first); } } } } map<int, bool> present; for (int i = 1; i <= n; i++) { present[get_parent(i)] = true; } cout << present.size() << endl; map<int, vector<int> > connected_components; for (int i = 1; i <= n; i++) { connected_components[parent[i]].push_back(i); } for (map<int, vector<int> >::iterator it = connected_components.begin(); it != connected_components.end(); it++) { cout << it->second.size() << " "; for (int i = 0; i < it->second.size(); i++) { cout << it->second[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
#include <bits/stdc++.h> using namespace std; const int N = 2000 * 1000 + 2; int n, m, x, y, cnt = 0, par[N], sum, s[N], deg[N], mo[N]; vector<int> adj[N], A, B, ans[N]; bool vis[N], is[N]; int get_par(int v) { if (par[v] == v) return v; return par[v] = get_par(par[v]); } void add(int u, int v) { u = get_par(u); v = get_par(v); if (u == v) return; if (s[u] < s[v]) swap(u, v); par[v] = u; s[u] += s[v]; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d", &x, &y); adj[x].push_back(y); adj[y].push_back(x); deg[x]++; deg[y]++; } int u = min_element(deg + 1, deg + n + 1) - deg; for (int i = 0; i < (int((adj[u]).size())); i++) { vis[adj[u][i]] = true; A.push_back(adj[u][i]); } for (int i = 1; i <= n; i++) if (!vis[i]) B.push_back(i); for (int i = 1; i <= n; i++) s[i] = 1; s[u] = (int((B).size())); for (int i = 1; i <= n; i++) par[i] = (vis[i]) ? i : u; for (int i = 0; i < (int((A).size())); i++) { int v = A[i]; for (int j = 0; j < (int((adj[v]).size())); j++) is[adj[v][j]] = true; for (int j = 0; j < (int((A).size())); j++) if (!is[A[j]]) add(v, A[j]); for (int j = 0; j < (int((adj[v]).size())); j++) is[adj[v][j]] = false; } for (int i = 0; i < (int((A).size())); i++) { int v = A[i]; for (int j = 0; j < (int((adj[v]).size())); j++) is[adj[v][j]] = true; for (int j = 0; j < (int((B).size())); j++) if (!is[B[j]]) add(v, B[j]); for (int j = 0; j < (int((adj[v]).size())); j++) is[adj[v][j]] = false; } for (int i = 1; i <= n; i++) { int k = get_par(i); if (mo[k] == 0) mo[k] = ++cnt; ans[mo[k]].push_back(i); } printf("%d\n", cnt); for (int i = 1; i <= cnt; i++) { printf("%d ", (int((ans[i]).size()))); for (int j = 0; j < (int((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 MAXN = 5e5 + 10; set<int> rem; queue<int> q; vector<int> ng[MAXN]; bool mark[MAXN]; vector<int> comps[MAXN]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); x--; y--; ng[x].push_back(y); ng[y].push_back(x); } for (int i = 0; i < n; i++) rem.insert(i), sort(ng[i].begin(), ng[i].end()); int cntId = 0; while (!rem.empty()) { int newX = *rem.begin(); mark[newX] = true; rem.erase(rem.begin()); q.push(newX); int compId = cntId++; while (!q.empty()) { int cur = q.front(); comps[compId].push_back(cur); q.pop(); vector<int> newlyAdd; for (set<int>::iterator it = rem.begin(); it != rem.end(); it++) { int u = *it; if (!binary_search(ng[cur].begin(), ng[cur].end(), u)) newlyAdd.push_back(u), mark[u] = true, q.push(u); } for (int i = 0; i < newlyAdd.size(); i++) rem.erase(newlyAdd[i]); } } printf("%d\n", cntId); for (int i = 0; i < cntId; i++) { printf("%d", (int)comps[i].size()); for (int j = 0; j < comps[i].size(); j++) printf(" %d", comps[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; const int MAXN = 500010; const int inf = (1 << 30); int n, m; vector<int> adj[MAXN]; int p[MAXN], cmpn[MAXN]; int find(int i) { return p[i] == i ? i : p[i] = find(p[i]); } void merge(int u, int v) { u = find(u); v = find(v); p[u] = v; } vector<int> ans[MAXN]; bool check(int u, int v) { int lo = 0, hi = (int)adj[u].size() - 1; while (lo < hi && lo < adj[u].size()) { int mid = lo + (hi - lo) / 2; if (adj[u][mid] >= v) { hi = mid; } else { lo = mid + 1; } } if (lo == hi && lo >= 0 && lo < adj[u].size() && adj[u][lo] == v) { return true; } return false; } int main() { scanf("%d%d", &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++) { sort(adj[i].begin(), adj[i].end()); p[i] = i; } set<int> s; for (int i = 1; i <= n; i++) { s.insert(i); } while (!s.empty()) { queue<int> q; q.push(*s.begin()); s.erase(s.begin()); while (!q.empty()) { int u = q.front(); q.pop(); for (set<int>::iterator it = s.begin(); it != s.end();) { if (check(u, *it)) { it++; continue; } q.push(*it); merge(u, *it); s.erase(it++); } } } int sze = 0; for (int i = 1; i <= n; i++) { int u = find(i); if (ans[u].size() == 0) sze++; ans[u].push_back(i); } printf("%d\n", sze); for (int i = 1; i <= n; i++) { if (ans[i].size() > 0) { 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> using namespace std; const int MAXN = 5 * 100 * 1000 + 13; queue<int> bfsq; set<int> bfsS; set<pair<int, int> > forbid; vector<int> compV[MAXN], toDel; int main() { ios_base::sync_with_stdio(false), 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; forbid.insert(make_pair(min(a, b), max(a, b))); } for (int i = 1; i <= n; i++) bfsS.insert(i); int comp = 0; while (!bfsS.empty()) { comp++; bfsq.push(*(bfsS.begin())); bfsS.erase(bfsS.begin()); while (!bfsq.empty()) { int u = bfsq.front(); compV[comp].push_back(u); bfsq.pop(); for (auto v : bfsS) if (forbid.find(make_pair(min(u, v), max(u, v))) == forbid.end()) { toDel.push_back(v); bfsq.push(v); } for (auto u : toDel) bfsS.erase(u); toDel.clear(); } } cout << comp << endl; for (int i = 1; i <= comp; i++) { cout << compV[i].size() << " "; for (auto u : compV[i]) cout << u << " "; 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; int n, m; pair<int, int> edges[1000000]; int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < (int)(m); i++) { int u, v; cin >> u >> v; edges[i] = make_pair(--u, --v); } sort(edges, edges + m); set<int> a; for (int i = 0; i < (int)(n); i++) a.insert(i); vector<vector<int> > ret; while (!a.empty()) { int v = *a.begin(); a.erase(v); vector<int> vs; queue<int> q; q.push(v); while (!q.empty()) { int x = q.front(); q.pop(); vs.push_back(x); for (set<int>::iterator itr = a.begin(); itr != a.end();) { int y = *itr; if (binary_search(edges, edges + m, make_pair(x, y)) || binary_search(edges, edges + m, make_pair(y, x))) ++itr; else a.erase(itr++), q.push(y); } } ret.push_back(vs); } cout << ret.size() << endl; for (int i = 0; i < (int)(ret.size()); i++) { cout << ret[i].size() << " "; for (int j = 0; j < (int)(ret[i].size()); j++) cout << ret[i][j] + 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 double PI = acos(-1.0); template <class T> T SQR(const T &a) { return a * a; } vector<int> g[500100]; int pred[500100]; vector<int> Ans[500100]; int findPred(int a) { if (pred[a] != a) pred[a] = findPred(pred[a]); return pred[a]; } int P[500100] = {0}; void run() { int n, m; cin >> n >> m; for (int i = (0), ei = (m); i < ei; i++) { int a, b; scanf("%d%d", &a, &b); --a; --b; g[a].push_back(b); g[b].push_back(a); } int minSt = 0; for (int i = (0), ei = (n); i < ei; i++) if (g[i].size() < g[minSt].size()) minSt = i; int p = 0; vector<int> vec; for (int i = (0), ei = (n); i < ei; i++) pred[i] = i; for (int i = (0), ei = (n); i < ei; i++) sort((g[i]).begin(), (g[i]).end()); vec.push_back(minSt); int cnt = 0; for (int i = (0), ei = (n); i < ei; i++) if (p < g[minSt].size() && g[minSt][p] == i) { vec.push_back(i); p++; } else { cnt++; pred[i] = minSt; for (int j = (0), ej = (g[i].size()); j < ej; j++) P[g[i][j]]++; } g[minSt].clear(); for (int i = (0), ei = (n); i < ei; i++) if (P[i] == cnt) g[minSt].push_back(i); for (int i = (0), ei = (vec.size()); i < ei; i++) for (int j = (0), ej = (vec.size()); j < ej; j++) if (i != j && !binary_search((g[vec[i]]).begin(), (g[vec[i]]).end(), vec[j])) { int pa = findPred(vec[i]); int pb = findPred(vec[j]); pred[pa] = pb; } map<int, vector<int> *> mp; for (int i = (0), ei = (n); i < ei; i++) { int pr = findPred(i); if (mp.find(pr) == mp.end()) mp[pr] = &Ans[mp.size()]; mp[pr]->push_back(i); } printf("%d\n", mp.size()); for (int i = (0), ei = (mp.size()); i < ei; i++) { printf("%d", Ans[i].size()); for (int j = (0), ej = (Ans[i].size()); j < ej; j++) printf(" %d", Ans[i][j] + 1); printf("\n"); } } int main() { time_t beg = clock(); run(); 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 comment(linker, "/STACK:16777216") using namespace std; const int sz = 500500; int B[sz], dp[sz], gr, n, m; vector<int> A[sz]; vector<int> res[sz]; queue<pair<int, int> > Q; int next(int v) { if (!B[v]) return v; else return dp[v] = next(dp[v]); } void bfs(int v) { pair<int, int> cur; int l, r, nv; gr++; Q.push(make_pair(v, v)); while (!Q.empty()) { cur = Q.front(); Q.pop(); l = cur.first; r = cur.second; nv = next(l); while (nv <= r) { B[nv] = 1; res[gr].push_back(nv); dp[nv] = next(nv + 1); if (A[nv].size() > 0) { if (A[nv][0] > 1) Q.push(make_pair(1, A[nv][0] - 1)); for (int j = 1; j < A[nv].size(); j++) if (A[nv][j] > (A[nv][j - 1] + 1)) { Q.push(make_pair(A[nv][j - 1] + 1, A[nv][j] - 1)); } if (n > A[nv].back()) { Q.push(make_pair(A[nv].back() + 1, n)); } } else { Q.push(make_pair(1, n)); } nv = next(nv); } } } int main() { int a, b; scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d", &a, &b); A[a].push_back(b); A[b].push_back(a); } for (int i = 1; i <= n; i++) { sort(A[i].begin(), A[i].end()); dp[i] = i; } for (int i = 1; i <= n; i++) if (!B[i]) bfs(i); printf("%d\n", gr); for (int i = 1; i <= gr; i++) { printf("%d", (int)res[i].size()); for (int j = 0; j < res[i].size(); j++) printf(" %d", res[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 int64_t MOD = 1e9 + 7; int64_t n, m; vector<int64_t> adj[500001]; vector<vector<int64_t> > ans; vector<int64_t> candi; set<int64_t> st; void dfs(int64_t i) { candi.push_back(i); st.erase(i); vector<int64_t> connect; for (const int64_t &j : st) { if (!binary_search((adj[i]).begin(), (adj[i]).end(), j)) { connect.push_back(j); } } for (const int64_t &j : connect) st.erase(j); for (const int64_t &j : connect) dfs(j); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int64_t i = 1; i <= m; i++) { int64_t a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int64_t i = 1; i <= n; i++) { st.insert(i); sort((adj[i]).begin(), (adj[i]).end()); } for (int64_t i = 1; i <= n; i++) { if (st.find(i) != st.end()) { candi.clear(); dfs(i); ans.push_back(candi); } } cout << ans.size() << "\n"; for (const vector<int64_t> &v : ans) { cout << v.size() << " "; for (const int64_t &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
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)) { int lb = bad[u].lowerBound(v); if (lb == bad[u].size() || bad[u].get(lb) != 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 interface EzIntList extends EzIntCollection { int size(); EzIntIterator iterator(); boolean equals(Object object); int hashCode(); String toString(); } 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; } public int get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index " + index + " is out of range, size = " + size); } return array[index]; } 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; } 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 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(); } 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 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 interface EzIntIterator { boolean hasNext(); int next(); } static interface EzIntSortedSet extends EzIntSet { int size(); EzIntIterator iterator(); boolean equals(Object object); int hashCode(); String toString(); } static final class PrimitiveHashCalculator { private PrimitiveHashCalculator() { } public static int getHash(int x) { return x; } } static interface EzIntSet 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> using namespace std; int N, M; vector<int> edge[500005], ans[500005]; int pA; struct UFS { int fa[500005]; void init(int n) { int i; for (i = 0; i < n; i++) fa[i] = i; } int find(int x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); } void uni(int x, int y) { fa[find(x)] = find(y); } } ufs; void dfs(int x) { int i; ufs.uni(x, x + 1); ans[pA].push_back(x); for (i = ufs.find(1); i <= N; i = ufs.find(i + 1)) { vector<int>::iterator it = lower_bound(edge[x].begin(), edge[x].end(), i); if (it == edge[x].end() || *it != i) dfs(i); } } int main() { int i, j; scanf("%d%d", &N, &M); for (i = 0; i < M; i++) { int s, f; scanf("%d%d", &s, &f); edge[s].push_back(f); edge[f].push_back(s); } for (i = 1; i <= N; i++) sort(edge[i].begin(), edge[i].end()); ufs.init(N + 2); for (i = ufs.find(1), pA = 0; i <= N; i = ufs.find(i + 1)) dfs(i), pA++; printf("%d\n", pA); for (i = 0; i < pA; i++) { printf("%d ", ((int)(ans[i]).size())); for (j = 0; j < ((int)(ans[i]).size()) - 1; j++) printf("%d ", ans[i][j]); printf("%d\n", 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; const long long N = 5e5 + 5; unordered_set<long long> g[N], s; vector<long long> component; void dfs(long long node) { component.push_back(node); s.erase(node); vector<long long> to_del; for (auto it : s) { if (g[node].find(it) == g[node].end()) { to_del.push_back(it); } } for (auto it : to_del) s.erase(it); for (auto it : to_del) dfs(it); } int32_t main() { ios::sync_with_stdio(false), cin.tie(nullptr); ; long long n, m; cin >> n >> m; for (long long i = 0; i < m; i++) { long long u, v; cin >> u >> v; g[u].insert(v); g[v].insert(u); } for (long long i = 1; i <= n; i++) s.insert(i); vector<vector<long long>> ans; for (long long i = 1; i <= n; i++) { if (s.find(i) != s.end()) { component.clear(); dfs(i); ans.push_back(component); } } cout << (long long)(ans).size() << "\n"; for (auto vec : ans) { cout << (long long)(vec).size() << " "; for (auto v : vec) cout << v << " "; 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.io.*; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskE solver = new TaskE(); solver.solve(in, out); out.close(); } } class TaskE { private List<List<Integer>> result = new ArrayList<>(); int [][] graph; int [][] edges; int [] degree; public void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); int m = in.nextInt(); prepareGraph(in, n, m); calculateNumberOfComponents(n); out.printLine(result.size()); for(List<Integer> list : result) { out.print(list.size()+" "); for(int element : list) { out.print((element+1) + " "); } out.printLine(); } } private void prepareGraph(InputReader in, int n, int m) { graph = new int[n][]; degree = new int[n]; edges = new int[m][2]; int from, to; for(int i=0;i<m;i++) { from = in.nextInt() - 1; to = in.nextInt() - 1; degree[from]++; degree[to]++; edges[i][0] = from; edges[i][1] = to; } for(int i=0;i<n;i++) { graph[i] = new int[degree[i]]; degree[i] = 0; } for(int i=0;i<m;i++) { from = edges[i][0]; to = edges[i][1]; graph[from][degree[from]++] = to; graph[to][degree[to]++] = from; } } private void calculateNumberOfComponents(int n) { Set<Integer> remaining = new HashSet<>(); for (int i = 0; i < n; i++) { remaining.add(i); } Queue<Integer> queue = new ArrayDeque<>(); Set<Integer> newlyAdded; List<Integer> partialResult; int vertex; for(int i=0;i<n;i++) { if(!remaining.contains(i)) continue; partialResult = new ArrayList<>(); remaining.remove(i); queue.add(i); while(!queue.isEmpty()) { vertex = queue.remove(); partialResult.add(vertex); newlyAdded = new HashSet<>(); for(int neighbour : graph[vertex]) { if(remaining.contains(neighbour)) { newlyAdded.add(neighbour); remaining.remove(neighbour); } } for(int v : remaining) queue.add(v); remaining.clear(); remaining.addAll(newlyAdded); } result.add(partialResult); } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { String line = reader.readLine(); if ( line == null ) { return false; } tokenizer = new StringTokenizer(line); } catch (IOException e) { throw new RuntimeException(e); } } return true; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next());} } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if ( i != 0 ) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
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, x, K, y; set<int> s; set<pair<int, int> > a; vector<int> ans[500005]; inline void dfs(int nod) { set<int>::iterator it; for (it = s.begin(); it != s.end();) { if (!a.count(make_pair(min(*it, nod), max(*it, nod)))) { int x = *it; s.erase(x); dfs(x); it = s.lower_bound(x); } else it++; } ans[K].push_back(nod); } int main() { ios::sync_with_stdio(false); scanf("%d %d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d %d", &x, &y); if (x > y) swap(x, y); a.insert({x, y}); } for (int i = 1; i <= n; ++i) s.insert(i); while (!s.empty()) { x = *s.begin(); s.erase(x); K++; dfs(x); } printf("%d\n", K); for (int i = 1; i <= K; ++i) { printf("%d ", ans[i].size()); for (auto it : ans[i]) printf("%d ", it); 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 + 8; int n, m, k, a, b, cnt; vector<int> mark[N], nei[N], v; set<int> s; queue<int> q; bool c[N]; void bfs(int u) { q.push(u); c[u] = 1; mark[cnt].push_back(u); s.erase(u); while (!q.empty()) { k = q.front(); for (int i = 0; i < nei[k].size(); i++) { s.erase(nei[k][i]); v.push_back(nei[k][i]); } for (auto h : s) { c[h] = 1; mark[cnt].push_back(h); ; q.push(h); } s.clear(); for (int i = 0; i < v.size(); i++) { if (!c[v[i]]) { s.insert(v[i]); } } v.clear(); q.pop(); } } void bfs_all() { for (int i = 0; i < n; i++) s.insert(i); while (!s.empty()) { bfs(*s.begin()); cnt++; } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; nei[a - 1].push_back(b - 1); nei[b - 1].push_back(a - 1); } bfs_all(); cout << cnt << endl; for (int i = 0; i < cnt; i++) { cout << mark[i].size() << " "; for (int j = 0; j < mark[i].size(); j++) cout << mark[i][j] + 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; const int64_t N = 5e5 + 1e1; bool mark[N]; int64_t n, m, ans, par[N], sz[N]; vector<int64_t> mol[N], adj[N]; int64_t root(int64_t x) { return par[x] = (x == par[x] ? x : root(par[x])); } int64_t mrg(int64_t x, int64_t y) { x = root(x); y = root(y); if (x == y) { return 0; } if (sz[x] < sz[y]) { swap(x, y); } par[y] = x; sz[x] += sz[y]; return 1; } void connect(int64_t v) { memset(mark, 0, sizeof mark); for (int64_t u : adj[v]) { mark[u] = true; } for (int64_t i = 1; i <= n; i++) { if (!mark[i]) { ans -= mrg(v, i); } } } int32_t main() { ios_base::sync_with_stdio(true == false); cin >> n >> m; for (int64_t i = 1; i <= n; i++) { par[i] = i; sz[i] = 1; } ans = n; int64_t a, b; for (int64_t i = 0; i < m; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } int64_t mn = 1; for (int64_t i = 2; i <= n; i++) { if (adj[i].size() < adj[mn].size()) { mn = i; } } connect(mn); for (int64_t u : adj[mn]) { connect(u); } cout << ans << '\n'; for (int64_t i = 1; i <= n; i++) { mol[root(i)].push_back(i); } for (int64_t i = 1; i <= n; i++) { if (mol[i].size()) { cout << mol[i].size() << ' '; for (int64_t u : mol[i]) { cout << u << ' '; } 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
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 left, int right, int val) { int ret = left - 1; for (int jump = right - left; jump >= 1; jump /= 2) { while (ret + jump < right && array[ret + jump] < val) { ret += jump; } } return ret + 1; } public boolean binarySearch(int val) { return binarySearch(0, size, val); } public boolean binarySearch(int left, int right, int val) { int lb = lowerBound(left, right, 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 MX5 = 5e5 + 10; int n, m, ANS = 0, Ans[MX5], pointer = 0, P2 = 0, SZANS[MX5], u, v; vector<int> G[MX5]; set<int> st; bitset<MX5> mark; void DFS(int u) { st.erase(u); mark[u] = 1; Ans[pointer] = u; pointer++; ANS++; std::set<int>::iterator itr = st.begin(); while (itr != st.end()) { int V = *itr; auto JJ = lower_bound(G[u].begin(), G[u].end(), V); if (JJ == G[u].end() || *(JJ) != V) { DFS(V); } itr = st.upper_bound(V); } return; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } for (int i = 1; i <= n; i++) { st.insert(i); sort(G[i].begin(), G[i].end()); } int t = 0; for (int i = 1; i <= n; i++) { if (!mark[i]) { t++; ANS = 0; DFS(i); SZANS[P2] = ANS; P2++; } } cout << t << '\n'; int p = 0; for (int i = 0; i < P2; i++) { int t = 0; cout << SZANS[i] << ' '; while (t < SZANS[i]) { cout << Ans[p] << ' '; p++; t++; } 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 visited[500010], n, m, mk[500010], pt[500010]; int mat[6010][6010]; vector<int> graph[500010], comp[500010], cur; int nope = 0; void dfs1(int s) { visited[s] = 1; cur.push_back(s); for (int i = 1; i <= n; i++) { if (i != s && !mat[s][i] && !visited[i]) { dfs1(i); } } } void dfs2(int s) { if (!mk[s]) { nope = 1; return; } visited[s] = 1; cur.push_back(s); for (auto to : comp[s]) { if (!visited[to]) { dfs2(to); } } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; vector<vector<int> > ans; if (n <= 6000) { for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; mat[a][b] = 1; mat[b][a] = 1; } for (int i = 1; i <= n; i++) { if (!visited[i]) { cur.clear(); dfs1(i); ans.push_back(cur); } } } else { int ansr = 0; for (int i = 1; i <= n; i++) { if (i * (n - i) > m) { ansr = n - (i - 1); break; } } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } vector<int> spec; for (int i = 1; i <= n; i++) { if (graph[i].size() >= ansr) { mk[i] = 1; spec.push_back(i); } } for (int i = 0; i < spec.size(); i++) { vector<int> pres(n + 1); for (auto val : graph[spec[i]]) { pres[val] = 1; } for (int j = 1; j <= n; j++) { if (!pres[j]) { comp[spec[i]].push_back(j); comp[j].push_back(spec[i]); } } } for (int i = 0; i < spec.size(); i++) { if (!visited[spec[i]]) { cur.clear(); nope = 0; dfs2(spec[i]); if (!nope) { ans.push_back(cur); for (auto val : cur) { pt[val] = 1; } } } } vector<int> bg; for (int i = 1; i <= n; i++) { if (!pt[i]) bg.push_back(i); } if (bg.size()) ans.push_back(bg); } cout << ans.size() << '\n'; for (int i = 0; i < ans.size(); i++) { cout << ans[i].size() << ' '; for (int j = 0; j < ans[i].size(); j++) cout << ans[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 = 5e5 + 5; vector<int> G[N]; int n, m, L[N], R[N]; bool vis[N]; queue<int> Q; vector<int> cur; vector<vector<int> > ans; void Delete(int x) { R[L[x]] = R[x]; L[R[x]] = L[x]; } bool Find(int u, int v) { vector<int>::iterator it = lower_bound(G[u].begin(), G[u].end(), v); return it != G[u].end() && *it == v; } void BFS(int S) { cur.clear(); vis[S] = 1; Q.push(S); Delete(S); while (!Q.empty()) { int u = Q.front(); Q.pop(); cur.push_back(u); for (int i = R[0]; i <= n; i = R[i]) { if (Find(u, i)) continue; Delete(i); vis[i] = 1; Q.push(i); } } ans.push_back(cur); } int main() { scanf("%d%d", &n, &m); for (int u, v, 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()); for (int i = 0; i <= n; ++i) { L[i] = i - 1; R[i] = i + 1; } for (int i = 1; i <= n; ++i) { if (vis[i]) continue; BFS(i); } printf("%d\n", ans.size()); for (int i = 0; i < (int)ans.size(); ++i) { printf("%d", ans[i].size()); for (int j = 0; j < (int)ans[i].size(); ++j) printf(" %d", ans[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; pair<int, int> e[2 * 10 * 100001]; int h[5 * 100001], sv[2][5 * 100001], ret[5 * 100001], w[2], t, run, Left, top, k, s[5 * 100001], u, v; int main() { int n, m; scanf("%d%d", &n, &m); for (long i = long(1); i <= long(m); i++) scanf("%d%d", &e[i].first, &e[i].second); for (long i = long(m + 1); i <= long(m + m); i++) { e[i].first = e[i - m].second; e[i].second = e[i - m].first; } sort(e + 1, e + 2 * m + 1); fill(h + 1, h + n + 2, 0); h[n + 1] = 2 * m + 1; for (long i = long(1); i <= long(2 * m); i++) if (i == 1 || e[i].first != e[i - 1].first) h[e[i].first] = i; for (long i = long(n); i >= long(1); i--) if (!h[i]) h[i] = h[i + 1]; run = top = k = t = 0; for (long i = long(1); i <= long(n); i++) sv[t][i] = i; w[0] = n; while (w[t]) { bool ok = true; k++; Left = 0; ret[++top] = sv[t][++Left]; s[k] = 1; while (top - run) { if (!ok) Left = 0; w[t ^ 1] = 0; u = ret[++run]; for (long i = long(h[u]); i <= long(h[u + 1] - 1); i++) { v = e[i].second; while (Left + 1 <= w[t] && sv[t][Left + 1] < v) { s[k]++; ret[++top] = sv[t][++Left]; } if ((Left + 1 <= w[t]) && (sv[t][Left + 1] == v)) sv[t ^ 1][++w[t ^ 1]] = sv[t][++Left]; } while (Left + 1 <= w[t]) { s[k]++; ret[++top] = sv[t][++Left]; } t ^= 1; ok = false; } } printf("%d\n", k); run = 0; for (long i = long(1); i <= long(k); i++) { printf("%d ", s[i]); for (long j = long(1); j <= long(s[i]); j++) printf("%d ", ret[++run]); 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; struct _ { _() { ios_base::sync_with_stdio(0); cin.tie(0); } stringstream ss; } _; template <class A, class B> ostream &operator<<(ostream &o, pair<A, B> t) { o << "(" << t.first << ", " << t.second << ")"; return o; } template <class T> inline string tostring(T first) { _.ss.clear(); _.ss.str(""); _.ss << first; return _.ss.str(); } template <class T> inline T convert(const string &s) { char *p; if (is_integral<T>()) return strtoll(s.c_str(), &p, 10); else return strtod(s.c_str(), &p); } template <class T> void PV(T a, T b, int p = 0, int w = 0, string s = " ") { int c = 0; while (a != b) { cout << setw(w) << *a++; cout << ((a != b && (!p || ++c % p)) ? s : "\n"); } cout.flush(); } template <class T> inline bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; } template <class T> inline bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; } const long long linf = 0x3f3f3f3f3f3f3f3fLL; const int inf = 0x3f3f3f3f; const int mod = int(1e9) + 7; const int N = 500010; vector<int> g[N]; int n, m; const int T = 10000; bool d[T][T]; struct DSU { int par[N + 1], cnt[N + 1]; DSU(int n = N) { init(n); } void init(int n) { for (int i = 0; i <= n; i++) { par[i] = i; cnt[i] = 1; } } int find(int first) { if (par[first] != first) par[first] = find(par[first]); return par[first]; } bool merge(int first, int second) { first = find(first), second = find(second); if (first == second) return 0; if (cnt[first] < cnt[second]) swap(first, second); par[second] = first; cnt[first] += cnt[second]; return 1; } } dsu; int in() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int ret = 0; while (c >= '0' && c <= '9') { ret = 10 * ret + c - 48; c = getchar(); } return ret; } int degree[N]; int main() { n = in(); m = in(); for (int i = 0; i < m; i++) { int a = in(); int b = in(); a--, b--; g[a].push_back(b); g[b].push_back(a); if (a < T && b < T) d[a][b] = d[b][a] = 1; degree[a]++; degree[b]++; } map<int, vector<int> > res; if (n < T) { dsu.init(n); for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) { if (!d[i][j]) dsu.merge(i, j); } for (int i = 0; i < n; i++) res[dsu.find(i)].push_back(i); } else { int minv = inf, pos = -1; for (int i = 0; i < n; i++) if (chmin(minv, degree[i])) pos = i; assert(pos != -1); bool v[N] = {}; memset(v, true, sizeof(v)); ; for (int i : g[pos]) v[i] = 0; int tot = 0; for (int i = 0; i < n; i++) if (v[i]) tot++; for (int i = 0; i < n; i++) { if (v[i] == 0) { int cnt = 0; for (int j : g[i]) { if (v[j] == 1) cnt++; } if (cnt < tot) { v[i] = 1; tot++; } } } vector<int> other; vector<int> id(n, -1); for (int i = 0; i < n; i++) { if (v[i] == 0) { id[i] = other.size(); other.push_back(i); } } int m = other.size(); for (int i = 0; i < n; i++) { if (id[i] == -1) continue; for (int j : g[i]) { if (id[j] == -1) continue; d[id[i]][id[j]] = 1; } } dsu.init(m); for (int i = 0; i < m; i++) for (int j = i + 1; j < m; j++) { if (!d[i][j]) dsu.merge(i, j); } for (int i = 0; i < n; i++) if (v[i]) res[0].push_back(i); for (int i = 0; i < m; i++) res[dsu.find(i) + 1].push_back(other[i]); } cout << res.size() << "\n"; for (auto &t : res) { sort((t.second).begin(), (t.second).end()); cout << t.second.size(); for (int i : t.second) cout << " " << i + 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
//package round120; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.HashMap; import java.util.List; import java.util.Map; public class E3 { InputStream is; PrintWriter out; String INPUT = "5 9 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5"; // String INPUT = "6 11 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 4 6 5 6"; 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); for(int[] r : g){ Arrays.sort(r); } BitSet con = new BitSet(); con.flip(0, n); DJSet ds = new DJSet(n); int min = 9999999; int mini = -1; for(int i = con.nextSetBit(0);i != -1;i = con.nextSetBit(i + 1)){ if(g[i].length < min){ min = g[i].length; mini = i; } } for(int e : g[mini]){ con.clear(e); } for(int i = con.nextSetBit(0);i != -1;i = con.nextSetBit(i + 1)){ ds.union(mini, i); } for(int e : g[mini]){ int ct = 0; for(int f : g[e]){ if(con.get(f)){ ct++; } } if(ct < con.cardinality()){ ds.union(e, mini); } for(int f : g[mini]){ if(Arrays.binarySearch(g[e], f) < 0){ ds.union(e, f); } } } // tr(ret); Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>(); for(int i = 0;i < n;i++){ int r = ds.root(i); if(!map.containsKey(r))map.put(r, new ArrayList<Integer>()); map.get(r).add(i); } out.println(map.size()); for(List<Integer> l : map.values()){ out.print(l.size()); for(int u : l){ out.print(" " + (u+1)); } out.println(); } } public 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 void 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;}} public int count(){ int ct = 0; for(int i = 0;i < upper.length;i++){ if(upper[i] < 0)ct++; } return ct; }} public 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; } 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 E3().run(); } public int ni() { try { int num = 0; boolean minus = false; while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-')); if(num == '-'){ num = 0; minus = true; }else{ num -= '0'; } while(true){ int b = is.read(); if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } } } catch (IOException e) { } return -1; } public long nl() { try { long num = 0; boolean minus = false; while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-')); if(num == '-'){ num = 0; minus = true; }else{ num -= '0'; } while(true){ int b = is.read(); if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } } } catch (IOException e) { } return -1; } public String ns() { try{ int b = 0; StringBuilder sb = new StringBuilder(); while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' ')); if(b == -1)return ""; sb.append((char)b); while(true){ b = is.read(); if(b == -1)return sb.toString(); if(b == '\r' || b == '\n' || b == ' ')return sb.toString(); sb.append((char)b); } } catch (IOException e) { } return ""; } public char[] ns(int n) { char[] buf = new char[n]; try{ int b = 0, p = 0; while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n')); if(b == -1)return null; buf[p++] = (char)b; while(p < n){ b = is.read(); if(b == -1 || b == ' ' || b == '\r' || b == '\n')break; buf[p++] = (char)b; } return Arrays.copyOf(buf, p); } catch (IOException e) { } return null; } double nd() { return Double.parseDouble(ns()); } boolean oj = System.getProperty("ONLINE_JUDGE") != null; 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 MAX_N = 500 * 1000 + 10; vector<int> adj[MAX_N], vec[MAX_N]; int n, m, cnt; queue<int> q; set<int> s; void readInput() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--, v--; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 0; i < n; i++) sort(adj[i].begin(), adj[i].end()), s.insert(i); } void solve() { while (!s.empty()) { q.push(*s.begin()), s.erase(s.begin()); while (!q.empty()) { int u = q.front(); q.pop(), vec[cnt].push_back(u); int cur = 0; vector<int> tmp; for (auto v : s) { while (cur < adj[u].size() && adj[u][cur] < v) cur++; if (cur == adj[u].size() || adj[u][cur] != v) tmp.push_back(v); } for (auto v : tmp) q.push(v), s.erase(v); } cnt++; } } void writeOutput() { cout << cnt << endl; for (int i = 0; i < cnt; i++) { cout << vec[i].size() << ' '; for (auto u : vec[i]) cout << u + 1 << ' '; cout << endl; } } int main() { ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); readInput(), solve(), writeOutput(); 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> G[MAXN], s_; vector<int> v; int n, m, a, b; void dfs(int s) { v.push_back(s); std::vector<int> cur; for (auto c : s_) { if (G[s].find(c) != G[s].end()) continue; cur.push_back(c); } for (auto sd : cur) s_.erase(sd); for (auto sd : cur) dfs(sd); } void Not_Stable() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) s_.insert(i); for (int i = 1; i <= m; i++) { scanf("%d %d", &a, &b); G[a].insert(b); G[b].insert(a); } int ans = 0; vector<int> resp; for (int i = 1; i <= n; i++) { if (s_.find(i) == s_.end()) continue; v.clear(); s_.erase(i); dfs(i); ++ans; resp.push_back((int)v.size()); for (auto sd : v) resp.push_back(sd); resp.push_back(-1); } printf("%d\n", ans); for (int i = 0; i < resp.size(); i++) { if (resp[i] == -1) printf("\n"); else printf("%d ", resp[i]); } } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); ; int g = 1; while (g--) Not_Stable(); 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 + 5; int n, m, cnt, belong[N]; vector<int> g[N], ans[N]; int get(int x) { return belong[x] ? belong[x] = get(belong[x]) : x; } void dfs(int i) { ans[cnt].push_back(i); belong[i] = i + 1; for (int j = get(1), k = 0; j <= n; j = get(j + 1)) { for (; k < g[i].size() && g[i][k] < j; k++) ; if (k < g[i].size() && g[i][k] == j) continue; dfs(j); } } int main() { scanf("%d%d", &n, &m); while (m--) { int a, b; scanf("%d%d", &a, &b); g[a].push_back(b); g[b].push_back(a); } for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); for (int i = 1; i <= n; i++) if (!belong[i]) { dfs(i); cnt++; } printf("%d\n", cnt); for (int i = 0; i < cnt; i++) { printf("%u", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) printf(" %d", ans[i][j]); puts(""); } }
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 + 1000; int n, m, ans, par[MAXN]; vector<int> adj[MAXN], out[MAXN]; bool mark[MAXN]; int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void dfs(int x) { mark[x] = 1; par[x] = x + 1; for (int i = 0; i < adj[x].size(); i++) { int u = adj[x][i] + 1; while (find(u) < (i == adj[x].size() - 1 ? n : adj[x][i + 1])) { out[ans].push_back(find(u)); dfs(find(u)); } } } int main() { ios::sync_with_stdio(false); cin >> n >> m; 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); } for (int i = 0; i < n; i++) adj[i].push_back(-1); for (int i = 0; i < n; i++) sort(adj[i].begin(), adj[i].end()); for (int i = 0; i < n + 100; i++) par[i] = i; for (int i = 0; i < n; i++) if (mark[i] == 0) { out[++ans].push_back(i); dfs(i); } cout << ans << endl; for (int i = 1; i < ans + 1; i++) { cout << out[i].size(); for (int j = 0; j < out[i].size(); j++) cout << ' ' << out[i][j] + 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; class Unvis { vector<int> next; vector<bool> isVis; public: Unvis(int n) { next = vector<int>(n + 1); isVis = vector<bool>(n + 1, false); iota(next.begin(), next.end(), 0); } int next_node(int u) { return not isVis[u] ? u : next[u] = next_node(next[u]); } void vis(int u) { assert(not isVis[u]); isVis[u] = 1; next[u] = next_node(u + 1); } }; class Solution { vector<vector<int>> g, compo; int n, m; Unvis *unvisit; void dfs(int u) { compo.back().push_back(u); unvisit->vis(u); for (int i = 0; g[u][i] != n; ++i) { int l = g[u][i] + 1, r = g[u][i + 1]; if (r > l) { while (unvisit->next_node(l) < r) { l = unvisit->next_node(l); dfs(l); } } } } public: void run() { cin >> n >> m; g.assign(n, vector<int>{-1, n}); unvisit = new Unvis(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a, --b; g[a].push_back(b), g[b].push_back(a); } for (int i = 0; i < n; ++i) sort(g[i].begin(), g[i].end()); int cur = unvisit->next_node(0); while (cur != n) { compo.push_back(vector<int>(0)); dfs(cur); cur = unvisit->next_node(0); } cout << compo.size() << '\n'; for (auto &A : compo) { cout << A.size() << ' '; for (int i = 0; i < A.size(); ++i) cout << A[i] + 1 << " \n"[i == A.size() - 1]; } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); Solution().run(); }
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 + 7; int n, m, u, v; set<int> q; vector<int> ans[maxn], g[maxn]; int tot; int cnt, del[maxn]; void solve(int u) { q.erase(u); ans[++tot].push_back(u); queue<int> qq; qq.push(u); while (!qq.empty()) { int u = qq.front(); qq.pop(); cnt = 0; for (auto &it : q) { int v = it; if (!binary_search(g[u].begin(), g[u].end(), v)) { ans[tot].push_back(v); qq.push(v); del[++cnt] = v; } } for (int i = 1; i <= cnt; i++) { q.erase(del[i]); } } } int main() { scanf("%d %d", &n, &m); for (int i = 1; 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++) { q.insert(i); sort(g[i].begin(), g[i].end()); } for (int i = 1; i <= n; i++) { if (q.count(i)) { solve(i); } } printf("%d\n", tot); for (int i = 1; i <= tot; i++) { int len = ans[i].size(); printf("%d ", len); for (int j = 0; j < len; j++) { printf("%d%c", ans[i][j], j == len - 1 ? '\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; set<int> st; vector<int> mp[500600], ans[500600]; int del[500600]; int n, m, tot; void Bfs(int x) { ans[tot].push_back(x); st.erase(x); queue<int> s; s.push(x); while (!s.empty()) { int u = s.front(); s.pop(); int cont = 0; for (set<int>::iterator it = st.begin(); it != st.end(); it++) { int to = *it; if (!binary_search(mp[u].begin(), mp[u].end(), to)) { s.push(to); ans[tot].push_back(to); del[cont++] = to; } } for (int i = 0; i < cont; i++) st.erase(del[i]); } tot++; } int main() { while (~scanf("%d%d", &n, &m)) { tot = 0; st.clear(); for (int i = 1; i <= n; i++) mp[i].clear(), ans[i].clear(); for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); mp[x].push_back(y); mp[y].push_back(x); } for (int i = 1; i <= n; i++) st.insert(i), sort(mp[i].begin(), mp[i].end()); for (int i = 1; i <= n; i++) { if (st.count(i)) Bfs(i); } printf("%d\n", tot); for (int i = 0; i < tot; 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> using namespace std; const long long INF = 1e9 + 10, M = 1e6 + 100, MOD = 1e9 + 7, ML = 25; int a[M], ne[M]; map<int, bool> mp[M]; vector<int> ve[M], adj[M]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &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++; } printf("%d\n", cnt); for (int i = 0; i < cnt; i++) { printf("%d ", ve[i].size()); for (int u : ve[i]) printf("%d ", u); 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 = (int)1e6 + 123; const long long INF = (long long)1e18 + 123; const int inf = (int)1e9 + 123; const int MOD = (int)1e9 + 7; void megaRandom() { unsigned int FOR; asm("rdtsc" : "=A"(FOR)); srand(FOR); } int n, m; vector<int> g[N]; int col; bool u[N]; set<int> st; void dfs(int x) { u[x] = 1; for (auto to : g[x]) if (!u[to] && to >= 1 && to <= n) dfs(to); } vector<int> comp[N]; void jfs(int root, int x) { st.erase(x); comp[root].push_back(x); for (int i = 0; i < (int)(g[x].size()) - 1; i++) { auto to = st.upper_bound(g[x][i]); while (to != st.end() && *to < g[x][i + 1]) { jfs(root, *to); to = st.upper_bound(g[x][i]); } } } int main() { megaRandom(); scanf("%d%d", &n, &m); for (int i = 1, a, b; i <= m; i++) { scanf("%d%d", &a, &b); g[a].push_back(b), g[b].push_back(a); } for (int i = 1; i <= n; i++) { if (!u[i]) { ++col; dfs(i); } } if (col > 1) { cout << "1\n" << n << " "; for (int i = 1; i <= n; i++) cout << i << " "; return 0; } for (int i = 1; i <= n; i++) { st.insert(i); g[i].push_back(0); sort(g[i].begin(), g[i].end()); g[i].push_back(n + 1); } for (int i = 1; i <= n; i++) { if (st.count(i)) { jfs(i, i); } } int cnt = 0; for (int i = 1; i <= n; i++) if ((int)(comp[i].size())) cnt++; cout << cnt << "\n"; for (int i = 1; i <= n; i++) { if (!(int)(comp[i].size())) continue; cout << (int)(comp[i].size()) << " "; for (auto j : comp[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; 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
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); ArrayList<Integer> removeSet = new ArrayList<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); } } 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
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>(); ArrayList<Integer> removeSet = new ArrayList<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); 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 N = 5e5 + 5; unordered_set<int> notInGraph[N]; set<int> notVis; vector<int> cities; void bfs(int i) { notVis.erase(i); queue<int> q; q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); cities.push_back(u); vector<int> used; for (int v : notVis) { if (notInGraph[u].count(v)) continue; q.push(v); used.push_back(v); } for (int vertex : used) { notVis.erase(vertex); } } } signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; while (m--) { int x, y; cin >> x >> y; notInGraph[x].insert(y); notInGraph[y].insert(x); } for (int i = 1; i <= n; i++) { notVis.insert(i); } vector<vector<int>> res; for (int i = 1; i <= n; i++) { if (notVis.count(i)) { cities.clear(); bfs(i); res.push_back(cities); } } cout << (int)res.size() << '\n'; for (auto i : res) { cout << (int)i.size() << ' '; for (int 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
#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; int inf_int = 2e9; long long inf_ll = 2e18; const double pi = 3.1415926535898; template <typename T, typename T1> void prin(vector<pair<T, T1> >& a) { for (int i = 0; i < a.size(); i++) { cout << a[i].first << " " << a[i].second << "\n"; } } template <typename T, typename T1> void prin(set<pair<T, T1> >& a) { for (auto it = a.begin(); it != a.end(); it++) { cout << it->first << " " << it->second << "\n"; } } template <typename T> void prin(vector<T>& a) { for (int i = 0; i < a.size(); i++) { cout << a[i]; if (i < a.size() - 1) cout << " "; else cout << "\n"; } } template <typename T> void prin(set<T>& a) { for (auto it = a.begin(); it != a.end(); it++) { cout << *it << " "; } } template <typename T> void prin_new_line(vector<T>& a) { for (int i = 0; i < a.size(); i++) { cout << a[i] << "\n"; } } template <typename T, typename T1> void prin_new_line(vector<pair<T, T1> >& a) { for (int i = 0; i < a.size(); i++) { cout << a[i].first << " " << a[i].second << "\n"; } } int sum_vec(vector<int>& a) { int s = 0; for (int i = 0; i < a.size(); i++) { s += a[i]; } return s; } template <typename T> T max(vector<T>& a) { T ans = a[0]; for (int i = 1; i < a.size(); i++) { ans = max(ans, a[i]); } return ans; } template <typename T> T min(vector<T>& a) { T ans = a[0]; for (int i = 1; i < a.size(); i++) { ans = min(ans, a[i]); } return ans; } template <typename T> T min(T a, T b, T c) { return min(a, min(b, c)); } template <typename T> T max(T a, T b, T c) { return max(a, max(b, c)); } long double s_triangle(long double x1, long double y1, long double x2, long double y2, long double x3, long double y3) { return abs(((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) / 2); } bool overflow(long long a, long long b) { if (a * b / b != a) return true; return false; } bool debug = 0; const int maxn = 5e5 + 100; long long mod = 1e9 + 7; vector<int> sor[maxn]; vector<int> g[maxn]; int ver[maxn]; int h; char used[maxn]; vector<vector<int> > ans; vector<int> com; char bad[maxn]; void bfs(int v) {} void solve() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); sor[b].push_back(a); sor[a].push_back(b); } for (int i = 1; i <= n; i++) { for (int e = 0; e < sor[i].size(); e++) { g[sor[i][e]].push_back(i); } } for (int i = 1; i <= n; i++) { ver[i] = i; } h = n; for (int i = 1; i <= n; i++) { if (!used[i]) { com.clear(); int v = i; used[v] = true; queue<int> q; q.push(v); while (!q.empty()) { int v = q.front(); com.push_back(v); q.pop(); for (int to : g[v]) { bad[to] = true; } for (int i = h; i >= 1; i--) { int to = ver[i]; if (!used[to] && !bad[to]) { q.push(to); used[to] = true; swap(ver[i], ver[h]); h--; } } for (int to : g[v]) { bad[to] = false; } } ans.push_back(com); } } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); i++) { printf("%d ", ans[i].size()); for (int v : ans[i]) { printf("%d ", v); } printf("\n"); } } int main() { if (!debug) { } 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 long long maxn = 5e5 + 500; vector<long long> ger[maxn]; long long moa[maxn]; long long n; void okeyed(long long a) { long long savea = a; vector<long long> ham; vector<long long> ham2; vector<long long> tofale; for (long long i = 0; i < ger[a].size(); i++) { ham.push_back(ger[a][i]); } ham.push_back(maxn * 50); tofale.push_back(a); long long j = 0, v, u = 0; while (tofale.size() || j < n) { if (tofale.empty()) { while (u < ham.size() && j < n) { if (ham[u] == j) { u++; j++; } else if (ham[u] > j) { if (moa[j] == -1) { tofale.push_back(j); break; } j++; } else if (ham[u] < j) { u++; } } } else { long long ppu = ham[u]; ham.pop_back(); u = -1; v = tofale.back(); tofale.pop_back(); moa[v] = savea; long long p1 = 0; long long p2 = 0; while (p1 < ham.size() && p2 < ger[v].size()) { if (ham[p1] == ger[v][p2]) { if (ger[v][p2] >= ppu) { u = ham2.size(); ppu = maxn * 50; } ham2.push_back(ger[v][p2]); p1++; p2++; } else if (ham[p1] < ger[v][p2]) { tofale.push_back(ham[p1]); p1++; } else { p2++; } } while (p1 < ham.size()) { tofale.push_back(ham[p1]); p1++; } if (u == -1) { u = ham2.size(); } ham.clear(); swap(ham, ham2); ham.push_back(maxn * 50); } } } vector<long long> ans[maxn]; int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); long long m, k, v, u; for (long long j = 0; j < maxn; j++) { moa[j] = -1; } cin >> n >> m; for (long long i = 0; i < m; i++) { cin >> v >> u; ger[--u].push_back(--v); ger[v].push_back(u); } for (long long i = 0; i < n; i++) { sort(ger[i].begin(), ger[i].end()); } for (long long i = 0; i < n; i++) { if (moa[i] == -1) { okeyed(i); } } for (long long i = 0; i < n; i++) { if (moa[i] < 0 || moa[i] >= maxn) { cout << "SALAM" << ' ' << moa[i]; return 0; } ans[moa[i]].push_back(i); } long long ko = 0; for (long long i = 0; i < maxn; i++) { if (ans[i].size() > 0) ko++; } cout << ko << endl; for (long long i = 0; i < maxn; i++) { if (ans[i].size() > 0) { cout << ans[i].size() << ' '; for (long long j = 0; j < ans[i].size(); j++) { cout << ans[i][j] + 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
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>>(); while (!contains.isEmpty()) { iv = contains.iterator().next(); 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; int n, region, deg[500500], d[500500], r[500500], l[500500]; vector<int> ans[500500], D[500500], a[500500]; void remove(int x) { l[r[x]] = l[x]; r[l[x]] = r[x]; } int connect(int x, int y) { if (deg[x] > deg[y]) swap(x, y); if (!deg[x]) return 1; return *(lower_bound(a[x].begin(), a[x].end(), y)) != y; } void bfs(int x) { queue<int> q; q.push(x); d[x] = ++region; remove(x); while (!q.empty()) { x = q.front(); q.pop(); for (int y = r[0]; y <= n; y = r[y]) if (connect(x, y)) { q.push(y); d[y] = region; remove(y); } } } int main() { int x, y, m; cin >> n >> m; while (m--) { scanf("%d%d", &x, &y); a[x].push_back(y); deg[x]++; a[y].push_back(x); deg[y]++; } for (int i = 0; i <= n; i++) r[i] = i + 1, l[i + 1] = i; for (int i = 1; i <= n; i++) { D[deg[i]].push_back(i); sort(a[i].begin(), a[i].end()); } for (int i = 0; i < n; i++) for (int j = 0; j < int(D[i].size()); j++) { int x = D[i][j]; if (!d[x]) bfs(x); } for (int i = 1; i <= n; i++) if (!d[i]) ans[region + 1].push_back(i); else ans[d[i]].push_back(i); cout << (ans[region + 1].empty() ? region : region + 1) << endl; for (int i = 1; i <= region + 1; i++) if (ans[i].empty()) break; else { cout << ans[i].size() << ' '; for (int j = 0; j < int(ans[i].size()); j++) printf("%d ", ans[i][j]); puts(""); } }
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 comment(linker, "/STACK:16000000") using namespace std; const long long MAXN = 10000000; long long N, M, a, b; vector<vector<int> > G, comp; set<int> S; int k = 0; void dfs(int u) { comp[k].push_back(u); for (set<int>::iterator it = S.begin(); it != S.end();) { int q = *it; if (!G[u].empty()) { int j = int(lower_bound(G[u].begin(), G[u].end(), q) - G[u].begin()); if (G[u][0] <= q && G[u].back() >= q && (j >= 0 && j < G[u].size() && G[u][j] == q)) { it++; continue; } } if (q == u) { it++; continue; } S.erase(S.find(q)); dfs(q); it = S.lower_bound(q); if (S.empty() || it == S.end()) break; } } int order[MAXN]; int main() { cin >> N >> M; G.resize(N); comp.resize(N); for (int i = 0; i < M; i++) { scanf("%d%d", &a, &b); a--; b--; G[a].push_back(b); G[b].push_back(a); } for (int i = 0; i < N; i++) order[i] = i; random_shuffle(order, order + N); for (int i = 0; i < N; i++) sort(G[i].begin(), G[i].end()); for (int i = 0; i < N; i++) S.insert(i); for (int i = 0; i < N; i++) if (S.find(order[i]) != S.end()) { S.erase(S.find(order[i])); dfs(order[i]); k++; } cout << k << endl; for (int i = k - 1; i >= 0; i--) { cout << comp[i].size() << " "; for (int j = 0; j < comp[i].size(); j++) cout << comp[i][j] + 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 N = 5e5 + 5; vector<int> adj[N], ans[N], st; bool mark[N]; 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.push_back(i); int cnt = 0; while (!st.empty()) { vector<int> &q = ans[cnt], tmp; q.push_back(st.back()); st.pop_back(); mark[q.back()] = true; for (int i = 0; i < (int)q.size(); i++) { int v = q[i]; for (int j = 0; j < (int)adj[v].size(); j++) { int u = adj[v][j]; if (!mark[u]++) tmp.push_back(u); } for (int j = 0; j < (int)st.size(); j++) if (!mark[st[j]]++) q.push_back(st[j]); st.clear(); tmp.swap(st); for (int j = 0; j < (int)st.size(); j++) mark[st[j]] = false; } cnt++; } printf("%d\n", cnt); for (int i = 0; i < cnt; i++) { printf("%d ", (int)ans[i].size()); for (int j = 0; j < (int)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> const int MAXN = 500000; int n, m; std::set<std::pair<int, int>> G; std::set<int> notVisitedYet; std::vector<int> C[MAXN]; int component = 0; void DFS(int node, std::set<int>::iterator& nodeIt) { notVisitedYet.erase(nodeIt); C[component].push_back(node); auto it = notVisitedYet.begin(); while (it != notVisitedYet.end()) { int v = *it; if (G.find({std::min(node, v), std::max(node, v)}) == G.end()) DFS(v, it); it = notVisitedYet.upper_bound(v); } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); std::cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; std::cin >> u >> v; G.insert({std::min(u, v), std::max(u, v)}); } for (int i = 1; i <= n; i++) notVisitedYet.insert(i); for (int i = 1; i <= n; i++) { std::set<int>::iterator it = notVisitedYet.find(i); if (it != notVisitedYet.end()) { DFS(i, it); component++; } } std::cout << component << '\n'; for (int i = 0; i < component; i++) { std::cout << C[i].size() << ' '; for (int v : C[i]) std::cout << v << ' '; 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> 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 + 5; const int mod = 1e9 + 7; const int inf = 1e9 + 7; int n, m, i, x, y, j, ct; int ata[N], S[N], S2[N], H[N]; vector<int> ans[N], V[N]; set<int> s; int atabul(int x) { return x == ata[x] ? x : ata[x] = atabul(ata[x]); } int main() { scanf("%d %d", &n, &m); for (i = 1; i <= m; i++) { scanf("%d %d", &x, &y); if (x > y) swap(x, y); V[y].push_back(x); } for (i = 1; i <= n; i++) { S[i] = 1; ata[i] = i; for (j = 0; j < V[i].size(); j++) S2[atabul(V[i][j])]--; for (__typeof((s).begin()) it = s.begin(); it != s.end();) { if (S2[*it]) { S[i] += S[*it]; ata[*it] = i; __typeof((s).begin()) it2 = it++; s.erase(it2); } else it++; } s.insert(i); for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) S2[*it] = S[*it]; } for (i = 1; i <= n; i++) { x = atabul(i); if (!H[x]) { H[x] = 1; ct++; } ans[x].push_back(i); } printf("%d\n", ct); for (i = 1; i <= n; i++) if (ans[i].size()) { cout << ans[i].size() << ' '; for (j = 0; j < ans[i].size(); j++) cout << ans[i][j] << ' '; 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; int n, k; vector<int> v[int(6e5) + 5], w; int vis[int(6e5) + 5]; set<int> s; void dfs(int k) { bool flag = 1; if (s.find(k) != s.end()) { s.erase(k); w.push_back(k); if (s.empty()) return; } for (auto const x : s) { if (!binary_search(v[k].begin(), v[k].end(), x)) { flag = false; dfs(x); break; } } if (!flag) dfs(k); } int main() { ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(0); cin >> n >> k; for (long long int i = 0; i < k; i++) { int x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } for (long long int i = 1; i < n + 1; i++) { sort(v[i].begin(), v[i].end()); s.insert(i); } vector<vector<int>> ans; while (!s.empty()) { int val = *(s.begin()); w.clear(); dfs(val); ans.push_back(w); } cout << ans.size() << '\n'; for (long long int i = 0; i < ans.size(); i++) { cout << ans[i].size() << " "; for (long long 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; queue<int> q; vector<int> gr[500020], er, ans[500020]; set<int> r; int mark[500020]; int isval; int f; int x = 0; int mm = 1; void bfs(int v) { while (!q.empty()) { f = q.front(); q.pop(); for (auto i : r) { isval = 0; if (gr[f].size() == 0) { q.push(i); mark[i] = mm; er.push_back(i); } else { if (gr[f][lower_bound(gr[f].begin() + x, gr[f].end(), i) - gr[f].begin() - x] == i) { isval = 1; } int x = lower_bound(gr[f].begin() + x, gr[f].end(), i) - gr[f].begin() - x; if (isval == 0) { q.push(i); mark[i] = mm; er.push_back(i); } } } for (int j = 0; j < er.size(); j++) { ans[mm].push_back(er[j]); r.erase(er[j]); } er.clear(); } mm++; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { r.insert(i); } for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; gr[x].push_back(y); gr[y].push_back(x); } for (int i = 1; i <= n; i++) { sort(gr[i].begin(), gr[i].begin() + gr[i].size()); } for (int i = 1; i <= n; i++) { if (mark[i] == 0) { mark[i] = mm; ans[mm].push_back(i); q.push(i); r.erase(i); bfs(i); } } cout << mm - 1 << endl; for (int i = 1; i < mm; 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; inline int read() { int k = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { k = k * 10 + ch - '0'; ch = getchar(); } return k * f; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } inline void writeln(int x) { write(x); puts(""); } set<int> s; queue<int> q; int ans[1000010], cnt = 0; vector<int> Ans[1000010]; set<int>::iterator it; bool vis[1000010], viss[1000010]; int nedge = 0, p[2000010], nex[2000010], head[2000010], n, m; int rk[1000010], Cnt; inline void addedge(int a, int b) { p[++nedge] = b; nex[nedge] = head[a]; head[a] = nedge; } inline void bfs() { for (int i = 1; i <= n; i++) if (!vis[i]) { vis[i] = 1; q.push(i); ans[++cnt] = 1; Ans[cnt].push_back(i); s.erase(i); while (!q.empty()) { int now = q.front(); q.pop(); for (int k = head[now]; k; k = nex[k]) viss[p[k]] = 1; Cnt = 0; for (it = s.begin(); it != s.end(); it++) if (!viss[*it]) { int k = *it; ans[cnt]++; Ans[cnt].push_back(k); vis[k] = 1; q.push(k); rk[++Cnt] = k; } else viss[*it] = 0; for (int i = 1; i <= Cnt; i++) s.erase(rk[i]); } } } int main() { n = read(); m = read(); for (int i = 1; i <= m; i++) { int x = read(), y = read(); addedge(x, y); addedge(y, x); } for (int i = 1; i <= n; i++) s.insert(i); bfs(); writeln(cnt); for (int i = 1; i <= cnt; i++) { write(ans[i]); putchar(' '); for (int j = 0; j < ans[i]; j++) write(Ans[i][j]), putchar(' '); 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 + 5; queue<int> q; set<int> myset; vector<int> g[N]; int n, m, cnt, c[N]; vector<int> e, C[N]; set<int>::iterator it; bool bs(int v, int u) { if (g[v].size() == 0) return true; int up = g[v].size(), dw = 0; while (up - dw > 1) { int mid = (up + dw) / 2; if (g[v][mid] > u) up = mid; else dw = mid; } return (g[v][dw] != u); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); 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++) sort(g[i].begin(), g[i].end()); for (int i = 1; i <= n; i++) myset.insert(i); while (!myset.empty()) { cnt++; c[*myset.begin()] = cnt; C[cnt].push_back(*myset.begin()); q.push(*myset.begin()); myset.erase(*myset.begin()); while (!q.empty()) { int u = q.front(); q.pop(); for (it = myset.begin(); it != myset.end(); it++) { if (bs(u, *it)) { c[*it] = c[u]; C[c[u]].push_back(*it); q.push(*it); e.push_back(*it); } } while (!e.empty()) { myset.erase(e.back()); e.pop_back(); } } } cout << cnt << endl; for (int i = 1; i <= cnt; i++) { cout << C[i].size() << " "; for (int j : C[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
#include <bits/stdc++.h> using namespace std; template <class T> bool uin(T& a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T& a, T b) { return a < b ? (a = b, true) : false; } struct dsu { vector<int> p; dsu(int n) : p(n, -1) {} inline int size(int x) { return -p[get(x)]; } inline bool same_set(int a, int b) { return get(a) == get(b); } int get(int x) { return p[x] < 0 ? x : p[x] = get(p[x]); } inline bool unite(int a, int b) { a = get(a); b = get(b); if (a == b) return false; if (p[a] > p[b]) swap(a, b); p[a] += p[b]; p[b] = a; return true; } }; const int nax = 5e5 + 10; vector<int> comp[nax]; vector<int> edge[nax]; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, M; cin >> N >> M; for (int i = 0; i < M; ++i) { int a, b; cin >> a >> b; --a, --b; edge[a].push_back(b); edge[b].push_back(a); } dsu UF(N); for (int i = 0; i < N; ++i) sort(edge[i].begin(), edge[i].end()); int r = 0; for (int i = 0; i < N; ++i) if (edge[i].size() < edge[r].size()) r = i; for (int i = 0; i < N; ++i) if (!binary_search(edge[r].begin(), edge[r].end(), i)) UF.unite(r, i); for (int x : edge[r]) { int cnt = 0; for (int y : edge[r]) { if (binary_search(edge[x].begin(), edge[x].end(), y)) ++cnt; else UF.unite(x, y); } if (cnt + N != (int)edge[x].size() + (int)edge[r].size()) UF.unite(x, r); } for (int i = 0; i < N; ++i) comp[UF.get(i)].push_back(i); int ans = 0; for (int i = 0; i < N; ++i) if (!comp[i].empty()) ++ans; cout << ans << '\n'; for (int i = 0; i < N; ++i) { if (comp[i].empty()) continue; cout << comp[i].size() << ' '; for (int j : comp[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; int nxt[500004]; int find(int x) { return (x == nxt[x]) ? x : nxt[x] = find(nxt[x]); } vector<int> sol[500004]; vector<int> cur; vector<int> adj[500004]; int n; int sz = 0; void dfs(int x) { nxt[x] = x + 1; sol[sz].push_back(x); int j = 0, omar = adj[x].size(); for (int i = find(1); i <= n; i = find(i)) { while (j < omar && adj[x][j] < i) ++j; if (j == omar || adj[x][j] > i) dfs(i); ++i; } } int main() { ; int m; scanf("%d%d", &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 + 1; ++i) nxt[i] = i, sort(adj[i].begin(), adj[i].end()); for (int i = 1; i <= n; ++i) { if (nxt[i] == i) { dfs(i); ++sz; } } printf("%d\n", sz); for (int i = 0; i < sz; ++i) { int omar = sol[i].size(); printf("%d ", omar); for (int k = 0; k < omar; ++k) printf("%d ", sol[i][k]); 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 MM = 5 * 100000; const int N = 1e6; vector<int> adj[N]; vector<int> ans[MM]; set<int> s; int cnt, n, m; queue<int> q; bool mark[MM]; 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
// 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]; for (int i = 1; i <= n; i++) ways[i] = new IntArray(deg[i]); for (int i = 0; i < m; i++) { int a = as[i]; int b = bs[i]; ways[a].add(b); ways[b].add(a); } for (int i = 1; i <= n; i++) if (ways[i].size > 0) ways[i].sort(); List<IntArray> ans = new ArrayList<>(); for (int i = 1; i <= n; i++) { if (cities.contains(i)) { IntArray cans = new IntArray(10); dfs(i, cans); ans.add(cans); } } 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); } } //******************************************************************************************** //******************************************************************************************** //******************************************************************************************** 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> #pragma GCC optimize("Ofast") using namespace std; const int MAXN = 500010; struct DSU { int par[MAXN]; int sz[MAXN]; DSU() { for (int i = 1; i < MAXN; i++) par[i] = i; for (int i = 1; i < MAXN; i++) sz[i] = 1; } int get(int x) { if (par[x] == x) return x; return par[x] = get(par[x]); } void join(int x, int y) { x = get(x); y = get(y); if (x == y) return; if (sz[x] > sz[y]) swap(x, y); par[x] = y; sz[y] += sz[x]; } } dsu; int n, m, u, v, x, t, root = 1; int mark[MAXN]; vector<int> G[MAXN]; vector<int> out[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; while (m--) { cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } for (int i = 2; i <= n; i++) if (G[i].size() < G[root].size()) root = i; for (int i = 0; i <= n; i++) mark[i] = 2; for (int i : G[root]) mark[i] = 1; for (int i = 1; i <= n; i++) if (mark[i] == 2) dsu.join(root, i); for (int i : G[root]) { int tmp = 0; for (int j : G[i]) { if (mark[j] == 2) tmp++; else mark[j] = 0; } if (tmp < n - G[root].size()) dsu.join(root, i); for (int j : G[root]) if (mark[j] == 1) dsu.join(i, j); for (int j : G[i]) if (mark[j] != 2) mark[j] = 1; } for (int i = 1; i <= n; i++) { if (dsu.get(i) == i) t++; out[dsu.get(i)].push_back(i); } cout << t << '\n'; for (int i = 1; i <= n; i++) if (out[i].size()) { cout << out[i].size() << ' '; for (int j : out[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; const int Maxn = 500 * 1000 + 10; int n, m, moal[Maxn], cnt, numd, mark[Maxn]; vector<int> adj[Maxn], ans[Maxn]; bool markadj[Maxn]; void dfs(int v, int a) { mark[v] = a; int pt = 0; for (int i = 0; i < n; i++) { if (i != v) { if (pt < (int)adj[v].size() && adj[v][pt] == i) pt++; else { if (markadj[i]) { if (mark[i] != a) { moal[i] = moal[v]; dfs(i, a); } } if (moal[i] == 0) moal[v] = moal[i]; } } } return; } void dfsAll() { cnt = 1; for (int i = 0; i < (int)adj[numd].size(); i++) if (mark[adj[numd][i]] == 0) { moal[adj[numd][i]] = cnt; dfs(adj[numd][i], 1); dfs(adj[numd][i], 2); if (moal[adj[numd][i]] != 0) cnt++; } return; } int main() { scanf("%d%d", &n, &m); int u, v; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); u--; v--; adj[u].push_back(v); adj[v].push_back(u); } int del = INT_MAX; for (int i = 0; i < n; i++) { sort(adj[i].begin(), adj[i].end()); if ((int)adj[i].size() < del) { del = (int)adj[i].size(); numd = i; } } for (int i = 0; i < (int)adj[numd].size(); i++) markadj[adj[numd][i]] = true; dfsAll(); for (int i = 0; i < n; i++) ans[moal[i]].push_back(i + 1); cout << cnt << endl; for (int i = 0; i < cnt; i++) { printf("%d ", (int)ans[i].size()); for (int j = 0; j < (int)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 N = 5e5 + 5; vector<int> adj[N], ans[N], st; int n, m, cnt; bool mark[N]; int main() { 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.push_back(i); while (!st.empty()) { vector<int> &q = ans[cnt], tmp; q.push_back(st.back()); st.pop_back(); mark[q.back()] = true; for (int i = 0; i < (int)q.size(); i++) { int v = q[i]; for (auto u : adj[v]) if (!mark[u]++) tmp.push_back(u); for (auto u : st) if (!mark[u]++) q.push_back(u); st.clear(); tmp.swap(st); for (auto u : st) mark[u] = false; } cnt++; } printf("%d\n", cnt); for (int i = 0; i < cnt; i++) { printf("%d ", (int)ans[i].size()); for (auto v : ans[i]) printf("%d ", v + 1); printf("\n"); } return 0; }
CPP