408368: GYM103107 H Hack DSU!
Description
As we all know, disjoint set union is a useful tool. However, sometimes we must apply the strategy of union by rank or path compression to make it quick. In general, if we have $$$n$$$ operations of merge, we may do all these operations in time complexity of $$$O(n \alpha(n))$$$, where $$$\alpha(n) \leq 4$$$ for $$$n \leq 10^5$$$.
Little Y is the teaching assistant of Data Structure class. One day, one problem was left for practice. There are $$$n$$$ sets numbered from $$$1$$$ to $$$n$$$. Then there are $$$n$$$ pairs of integer $$$(A_i,B_i)$$$. We should merge set $$$A$$$ and $$$B$$$ for each pair $$$(A,B)$$$. The task is to get the count of disjoint sets after all operations.
When reading the homework codes from students, Little Y noticed one implementation of find operation without recursion. Then she thinks this code is not implemented correctly, which may lead to about $$$O(n^2)$$$ times of read or write operations to the array $$$\texttt{parent}$$$. To verify this, she adds a temporary variable called $$$\texttt{counter}$$$ to track the count of write operations.
#include <iostream>
using namespace std;
const int MAXN = 1e5+10;
int parent[MAXN];
long long counter = 0;
int find(int x) {
while (x != parent[x]) {
if (x < parent[x]) {
// Merge-by-rank and Path-compression
parent[x] = parent[parent[x]];
}
x = parent[x];
counter++;
}
return x;
}
void merge(int a, int b) {
a = find(a);
b = find(b);
parent[a] = b;
}
int main() {
int n, A, B, ans = 0;
cin >> n;
for (int i = 1; i <= n; i++)
parent[i] = i;
for (int i = 1; i <= n; i++) {
cin >> A >> B;
merge(A, B);
}
for (int i = 1; i <= n; i++)
if (i == find(i))
ans++;
cout << ans << endl;
return 0;
}
Little Y thinks there exists one input data that makes the $$$\texttt{counter}$$$ large enough. Can you help her to provide one input data to prove that she is right?
For a given $$$n$$$, you should construct $$$n$$$ pairs of integers $$$(A_i, B_i)$$$. When the number $$$n$$$ and $$$n$$$ pairs of integers $$$(A_i, B_i)$$$ is sent to this program, you should guarantee that the final value of $$$\texttt{counter}$$$ should be greater than a certain number $$$T$$$, which means that this program cannot stop within $$$T$$$ write operations.
InputOne line contains two integer $$$n ~ (10 \leq n \leq 10^4)$$$ and $$$T ~ (n \leq T \leq \frac{n^2}{\log_2 n})$$$ denoting the count of initial sets and the least write operations that this program must take.
OutputOutput $$$n$$$ lines.
The $$$i$$$-th line contains two integers $$$A_i, B_i ~ (1 \leq A_i, B_i \leq n)$$$ denoting the $$$i$$$-th pair of number.
ExampleInput10 10Output
1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 1Note
The $$$\texttt{counter}$$$ of sample input is $$$18$$$.