We need to compute the absolute difference between number of villages (N) and number of cities after mergers. A city is formed by merging two or more villages; a village that is not merged (i.e., not connected to any other via merge info) remains a village. So after processing the merges, we have components. Some components have size 1 (village), size >=2 (city). But the problem says: "Two or more merged villages will become one new city, and villages that are not merged with any village will remain villages." So if we have a component of size >=2, it's a city. If a component of size 1, it's a village.
But careful: The problem might treat "villages" as the original entities, but after merger, some become cities. The output is absolute difference between number of cities and number of villages after the merger. So if we have C cities and V villages, output |C - V|. But note that the total number of original villages N = (sum of sizes of all components) = number of villages that are still villages (V) + number of villages that belong to cities (those are part of cities, not counted as separate villages). However, the problem statement says: "output the absolute value of the difference between the number of cities and the number of villages after the merger." So after the merger, we have some number of cities (each is a merged group of at least 2 villages) and some number of villages (which are singleton villages that did not merge). So we need to compute that difference.
Thus answer = | (# of components of size >= 2) - (# of components of size 1) |.
But wait, examples:
Example 1:
N=3, M=1, merge 1-2. So components: {1,2} (size2 city), {3} (size1 village). So cities=1, villages=1, diff=0. Output 0. matches.
Example 2:
N=4, M=2, (1,4),(2,3). Two components: {1,4} city, {2,3} city. No singleton. cities=2, villages=0, diff=2. Output 2. matches.
Example 3:
N=5, M=0. No merges. All villages singleton: 5 villages, 0 cities. diff=5. Output 5. matches.
Example 4:
N=3, M=3, edges: (1,2),(2,3),(3,1). All three in one component: city size3. cities=1, villages=0, diff=1. Output 1. matches.
Example 5:
N=3, M=2, (1,2),(2,3). All three connected? Yes, because 1-2-3 => component of size3. So city=1, villages=0, diff=1. Output 1. matches.
Example 6:
N=5, M=4, edges: 1-2,2-3,3-4,4-5 -> one component of size5. So cities=1, villages=0, diff=1. Output 1. matches.
Example 7:
N=10, M=5, edges: 3-4,1-2,9-6,2-6,2-9.
Let's compute components:
Edges: (3,4) -> component A: {3,4}
(1,2) -> component B: {1,2}
(9,6) -> component C: {9,6}
(2,6) connects B and C: B {1,2} and C {6,9} merge => {1,2,6,9}
(2,9) already same.
So final components: A: {3,4} (size2 city), combined: {1,2,6,9} (size4 city), and the rest villages: we have N=10, villages covered: 3,4,1,2,6,9 = 6 nodes, remaining: 5,7,8,10 = 4 nodes singleton. So cities = 2 (size2 and size4), villages = 4. diff = |2-4| = 2. Output 2. matches.
Thus our interpretation is correct.
Therefore solution: Use union-find (disjoint set) to group villages based on given merges. After processing, count number of components of each size. For each root, get size. Count cities = number of components with size >=2; villages = number of components with size ==1. Then compute abs(cities - villages).
Edge Cases: M could be 0, all components singleton; cities=0, villages=N; diff=N. M up to 100, N up to 1000. So union-find is fine.
Implementation: Read N, M. Initialize parent and size arrays. For i=1..M, read a, b, union them. Then after all, for each node i, find root. Use a map (or array) to accumulate counts per root. Actually we can just compute root and then increment component size count in an array that counts size per root. Then after counting, iterate roots: if size[root] == 1 then villages++ else cities++. Compute diff.
Alternatively, we can count number of distinct roots and number of singleton roots. But need to differentiate size.
We need to be careful: If a component has size >1 but there is only one node in the union? Actually merges always combine two different villages. So if there is at least one edge, component size >1.
Thus answer computed.
Potential nuance: Could there be a scenario where after merges, a component that has exactly one village is considered a village? Yes. That's what we did.
Thus solution is simple.
Now we need to generate final answer.
Let's confirm with all examples.
Thus problem solved.
Now produce code in C++.
Steps:
- Read N, M.
- Initialize parent[i]=i, size[i]=1.
- Define find function with path compression.
- Define unionSets that merges by size.
- For each of M lines: read a,b; union(a,b).
- After processing, create an array compSize of size N+1 initialized to 0.
- For i=1 to N: root = find(i); compSize[root]++ (or maybe use an array to accumulate counts).
But we need final sizes per component. We can iterate and increment compSize[root] after find. Use an array "compCount" of length N+1, but we only need root's size. Alternatively, we can just use the size array from union-find, but careful: union-find size array might not be accurate for all roots because when we compress path, we might not update size for all; but we maintain size only for root. After all unions, for each node we find root, then we can get size[root] directly from the union-find size array that we maintain (which is accurate for roots). However, if we do path compression, some nodes might not point directly to root, but we call find to get root. So we can: for i=1..N, root = find(i); and then we can increment some counter for that root? But we only need to count components; we can use a boolean visited array for roots: if not visited[root] then record size[root] and mark visited. That's simpler.
Implementation:
int parent[1001], sz[1001];
init for i=1..N: parent[i]=i, sz[i]=1;
After unions:
bool visited[1001]={false};
int cities=0, villages=0;
for(int i=1;i<=N;i++){
int r = find(i);
if(!visited[r]){
visited[r]=true;
if(sz[r]==1) villages++;
else cities++;
}
}
ans = abs(cities - villages);
print ans.
Edge: In unionSets, when merging, we update sz of new root. So sz array holds correct component size for roots.
That's it.
Time: O(N + M * α(N)) trivial.
Now produce the final answer in required format: just output integer.
Let's test with given examples.
Example 1:
N=3, M=1 union(1,2). After union: sz[2]=2 (if root 2) or 1? Implementation specific. Then find: roots: 1's root 2, visited[2] => size 2, city++; 3's root 3 visited => size1 village++. So cities=1, villages=1 diff=0.
Example 2: N=4 unions: (1,4),(2,3). After: two components sizes 2,2 => cities=2, villages=0 diff=2.
Example 3: N=5 M=0: all sz=1, visited each root individually: 5 villages, 0 cities diff=5.
Example 4: N=3 all connected: size of root=3 => city1, villages0 diff=1.
Example 5: N=3 connections: 1-2,2-3 => root size=3 => city1 diff=1.
Example 6: N=5 1-2-3-4-5 => size5 => city1 diff=1.
Example 7: N=10, compute: after unions, root for 1,2,6,9 should be same, size4; root for 3,4 size2; other singletons 5,7,8,10. So visited: root1? maybe, then city1, then root3? city2, then 5 village,7,8,10 villages. So villages=4, cities=2 diff=2.
Matches.
Thus solution.
Now produce final answer.
Potential pitfalls: Input format: For M=0, there is only first line? In example 3: Input: "5 0" and then nothing else. So we need to handle M=0: just read first line, then no further lines. Code should work.
We'll read N, M; then loop M times reading pairs. Works.
Thus final C++ code:
#include
#include
using namespace std;
const int MAXN = 1000;
int parent[MAXN+1];
int sz[MAXN+1];
int find(int x) {
if (parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
void unionSets(int a, int b) {
int ra = find(a), rb = find(b);
if (ra == rb) return;
if (sz[ra] < sz[rb]) swap(ra, rb);
parent[rb] = ra;
sz[ra] += sz[rb];
}
int main() {
int N, M;
cin >> N >> M;
for (int i = 1; i <= N; ++i) {
parent[i] = i;
sz[i] = 1;
}
for (int i = 0; i < M; ++i) {
int a, b;
cin >> a >> b;
unionSets(a, b);
}
bool visited[MAXN+1] = {false};
int cities = 0, villages = 0;
for (int i = 1; i <= N; ++i) {
int r = find(i);
if (!visited[r]) {
visited[r] = true;
if (sz[r] == 1) villages++;
else cities++;
}
}
cout << abs(cities - villages) << endl;
return 0;
}
That's it.
But let's ensure we include for abs? Actually std::abs from or . But we can just do int diff = cities > villages ? cities - villages : villages - cities; to avoid include.
Simplify: int ans = cities > villages ? cities - villages : villages - cities; cout << ans.
But fine.
Now produce final answer. ```cpp
#include
using namespace std;
const int MAX_N = 1000;
int parent[MAX_N + 1];
int compSize[MAX_N + 1];
// Find with path compression
int find(int x) {
if (parent[x] != x)
parent[x] = find(parent[x]);
return parent[x];
}
// Union by size
void unite(int a, int b) {
int ra = find(a), rb = find(b);
if (ra == rb) return;
if (compSize[ra] < compSize[rb]) swap(ra, rb);
parent[rb] = ra;
compSize[ra] += compSize[rb];
}
int main() {
int N, M;
cin >> N >> M;
// Initialisation
for (int i = 1; i <= N; ++i) {
parent[i] = i;
compSize[i] = 1;
}
// Process merges
for (int i = 0; i < M; ++i) {
int a, b;
cin >> a >> b;
unite(a, b);
}
// Count cities (size >= 2) and villages (size == 1)
bool visited[MAX_N + 1] = {false};
int cities = 0, villages = 0;
for (int i = 1; i <= N; ++i) {
int root = find(i);
if (!visited[root]) {
visited[root] = true;
if (compSize[root] == 1)
++villages;
else
++cities;
}
}
int ans = (cities > villages) ? cities - villages : villages - cities;
cout << ans << endl;
return 0;
}