We are asked: "You are given an undirected graph with N vertices and 0 edges. Process Q queries of the following types.
0 u v: Add an edge (u, v).
1 u v: Print 1 if u and v are in the same connected component, 0 otherwise.
Constraints 1 ≤ N, Q ≤ 200,000.
This is a classic dynamic connectivity problem, but only adding edges (no deletions). So it's just union-find (disjoint set union) with union operations and find queries. The queries are online, we just process each query sequentially. For type 0, union(u, v). For type 1, check if find(u)==find(v). That's it.
But wait, the problem might be trivial? Possibly they want to test knowledge of DSU. However, the problem statement originally from AtCoder? Actually, it looks like AtCoder's Disjoint Set Union practice problem: A - Disjoint Set Union or something. The constraints are typical. So we just implement DSU with path compression and union by size/rank.
But we should note: "You are given an undirected graph with N vertices and 0 edges. Process Q queries ..." There's no delete. So yes.
But sometimes there is a twist: The input can be large. Use fast I/O. Use union-find with size.
Let's confirm: The sample:
4 7
1 0 1 -> query type 1: check 0 and 1, initially no edges -> 0 (output 0)
0 0 1 -> add edge (0,1) -> connect components
0 2 3 -> add edge (2,3) -> connect 2 and 3
1 0 1 -> now 0 and 1 are connected -> 1 (output 1)
1 1 2 -> 1 (in component {0,1}) and 2 (in component {2,3}) are disconnected -> 0 (output 0)
0 0 2 -> add edge (0,2) -> merges two components: {0,1,2,3}
1 1 3 -> 1 and 3 now connected -> 1 (output 1)
Matches sample.
So implement.
We need to output for each type 1 query, the answer.
Implementation details:
Use 0-indexed vertices (given 0 ≤ u, v < N)
DSU:
parent[i] = i initially
size[i] = 1
find(x): path compression
unite(u, v):
root_u = find(u), root_v = find(v)
if root_u != root_v: merge by size (attach smaller to larger), update size.
For type 1: find(u) == find(v) -> 1 else 0.
Because Q up to 200k, N up to 200k, it's fine.
We'll output each answer on a new line.
Thus answer: Provide C++ code with fast I/O.
But the user didn't specify language? They said "出力" (output). The problem statement is generic. We are ChatGPT, we should provide explanation and code. Possibly the expected answer is code? But they might want explanation. We'll produce a solution explanation and then code.
Given the "in the response" format, I'll produce a solution in C++.
I'll use:
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector parent, size;
DSU(int n) {
parent.resize(n);
size.resize(n,1);
for(int i=0;i<n;++i) parent[i]=i;
}
int find(int x) {
if(parent[x]==x) return x;
return parent[x]=find(parent[x]);
}
bool same(int u, int v) {
return find(u)==find(v);
}
void unite(int u, int v) {
int ru=find(u), rv=find(v);
if(ru==rv) return;
if(size[ru]<size[rv]) swap(ru,rv);
parent[rv]=ru;
size[ru]+=size[rv];
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N,Q;
cin>>N>>Q;
DSU dsu(N);
while(Q--){
int t,u,v;
cin>>t>>u>>v;
if(t==0){
dsu.unite(u,v);
} else {
cout<<dsu.same(u,v)<<"\n";
}
}
return 0;
}
That's it.
But also note: AtCoder's problem might require mod 2? No.
Thus final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
struct DisjointSetUnion {
vector parent, size;
DisjointSetUnion(int n) {
parent.resize(n);
size.resize(n, 1);
for (int i = 0; i < n; ++i) parent[i] = i;
}
code
// find with path compression
int find(int x) {
if (parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
bool same(int u, int v) {
return find(u) == find(v);
}
void unite(int u, int v) {
int ru = find(u), rv = find(v);
if (ru == rv) return;
// union by size
if (size[ru] < size[rv]) swap(ru, rv);
parent[rv] = ru;
size[ru] += size[rv];
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int N, Q;
cin >> N >> Q;
DisjointSetUnion dsu(N);
while (Q--) {
int t, u, v;
cin >> t >> u >> v;
if (t == 0) {
dsu.unite(u, v);
} else {
cout << dsu.same(u, v) << "\n";
}
}
return 0;