We need to parse the problem correctly. There are two armies: Raikage Army and Zetsu Army. Soldiers have unique IDs. We have N dual fights, each between a soldier from Raikage and a soldier from Zetsu. So each edge (A,B) connects two soldiers of opposite sides. We don't know which ID belongs to which side. We need to assign each ID to either Raikage (R) or Zetsu (Z) such that each fight is between R and Z (i.e., for each edge, the two endpoints have opposite assignments). And we want to maximize the number of soldiers assigned to Raikage Army. Output that maximum possible count.
Given that IDs can be up to 2*10^4, N up to 1e5 per test case, T up to 20, total N maybe up to 2e6? But careful: constraints say 1 ≤ N ≤ 10^5 per test case, but T up to 20, so total N could be 2e6. We need efficient solution.
Observation: The assignment constraints: it's a bipartite graph. Since each edge must connect opposite sides, the graph must be bipartite. However, the graph may not be connected; there may be multiple components. For each connected component, the bipartition is forced up to swapping the two sides. Because the graph is bipartite? Actually, it is guaranteed that there exists a valid assignment because the fights are between opposite armies (by problem statement: Given that both A & B will be of different army). That means the actual assignment (the true one) is a valid bipartition. But we don't know which side is which. So the graph is guaranteed to be bipartite. However, there might be multiple possible assignments that satisfy all edges: they are exactly the two colorings of each connected component, but the colors (R or Z) can be swapped per component independently? Wait, if we assign globally: we want to assign each vertex to either Raikage or Zetsu, such that each edge connects opposite sides. That's essentially a 2-coloring of the graph. Since the graph is bipartite, there are two possible colorings for each connected component (if we consider colors as say "color 0" and "color 1"), but the assignment to Raikage vs Zetsu is a labeling of the two colors with the army names. However, there is an additional nuance: The assignment is not required to be consistent across components? Actually, the global assignment is simply a function from vertices to {R, Z} satisfying edge constraints. This is exactly a valid 2-coloring. For each connected component, there are exactly two possible colorings (if the component is bipartite). However, note: In a bipartite graph, the two color classes are determined up to swapping. So overall, the number of valid assignments is 2^(number of connected components) (if each component is connected and bipartite). But we also have to consider isolated vertices? The problem statement says there are only edges from fights. But there might be soldiers that did not fight? Actually, the input only gives IDs that appear in fights. So all soldiers mentioned are part of at least one fight. So there are no isolated vertices. But there could be components that are single edges? Actually, each component has at least one edge. So every vertex is in some edge. So each component is bipartite and has at least 2 vertices.
Now, we want to maximize the number of vertices assigned to Raikage. For each component, we have two choices: either assign the larger color class to Raikage, or the smaller one? Because we can decide which color (say color 0) corresponds to Raikage. If we pick the coloring that yields more R, we would assign the larger partition to R. But we have to be careful: The assignment is not per component independent? Actually, we can choose the assignment per component independently because the colors are not linked across components. In each component, we can decide which side (color) we call Raikage, as there is no constraint linking different components. So the maximum total Raikage count is sum over components of max(size_of_partition1, size_of_partition2). This yields the answer.
But wait, check case 4: Input: 4 edges: (1,2), (2,3), (3,4), (4,5). This graph is a path of 5 vertices: 1-2-3-4-5. The bipartition: one side: vertices 1,3,5 (size 3), other side: 2,4 (size 2). Max is 3. Output is 3. Good.
Case 1: edges: (1,2),(2,3),(2,4),(2,5). Graph: star with center 2 connected to 1,3,4,5. Bipartition: center alone? Since edges: center vs leaves. So one partition: {2} (size1), the other: {1,3,4,5} (size4). Max=4. Output:4.
Case 2: edges: (2,6),(1,5),(3,4),(7,9),(5,8),(4,10). Let's parse: edges: 2-6, 1-5, 3-4, 7-9, 5-8, 4-10. Components:
- component1: vertices {2,6} -> partition sizes 1 and 1 -> max 1.
- component2: vertices {1,5,8}? Actually edges: 1-5 and 5-8 => vertices 1,5,8. This is a path of length 2. Partition: 1 and 8 on one side? Let's check: edge 1-5: assign opposite, say 1=R,5=Z. Edge 5-8: 5=Z so 8=R. So sides: {1,8} vs {5}. Sizes 2 and 1, max 2.
- component3: vertices {3,4,10}? edges: 3-4, 4-10. Path: 3-4-10. Partition: e.g., 3=R,4=Z,10=R => sides {3,10} vs {4} sizes 2 and 1, max 2.
- component4: vertices {7,9}: sizes 1 and 1, max 1.
Sum max: 1+2+2+1 = 6. Output:6. Works.
Case 3: 1 edge: (100,200). Two vertices, each component size max=1, sum=1. Output:1.
So the problem reduces to: given a bipartite graph (guaranteed bipartite), find the sum over components of the larger size of its bipartition.
But wait: is it always guaranteed bipartite? The input says "Given that both A & B will be of different army." That implies there exists a ground truth where each edge connects opposite armies. So the graph is bipartite. However, there might be multiple edges connecting same pair? Possibly but not given. So graph is simple? Not necessarily, but it doesn't matter.
Thus algorithm: For each test case, read N edges. Build graph over vertices that appear. Since vertex IDs up to 20000, we can use adjacency list. However, N up to 1e5, vertices up to 2*10^4, that's okay. But across test cases we should reset. We can do BFS/DFS to color each component with two colors (say 0 and 1). Count number of vertices colored 0 and 1 in each component. Add max to answer.
Edge Cases: Is it possible that a component is not bipartite due to conflicting constraints? The problem statement ensures that there is a valid assignment, but could there be cycles of odd length? If the ground truth is that each edge connects opposite armies, that implies the graph is bipartite. So there will be no odd cycles. However, our algorithm should still handle if there is any inconsistency? Not needed.
We need to ensure we handle multiple test cases efficiently. Use fast I/O.
Complexity: O(V+E) per test case. With V up to 210^4 and E up to 1e5, it's fine. However, there might be up to 210^4 vertices per test case, but total across T could be up to 4e5? Actually, sum of N across test cases may be up to 201e5 = 2e6, so total vertices across test cases up to 2e6? but vertex IDs per test case limited, but total input is manageable.
Implementation details:
We need to collect all distinct vertices from edges. Could store in vector of adjacency lists. Since IDs up to 20000, we could allocate an array of size MAX_ID+1 (20000) for each test case? But T up to 20, we can reinitialize arrays each test case. But careful: IDs can be up to 2*10^4 (i.e., 20000). That's small. So we could use static arrays of size 20001, and a visited array, color array. But we must clear adjacency lists for used vertices only to avoid O(V) reset per test case for all possible IDs. Since V is at most 20000, resetting arrays of size 20001 is okay. However, building adjacency list for each vertex up to 20000 using vector<vector> might be heavy if we allocate for all 20001 each test case. But that's fine: 20001 vectors, each initially empty. We'll need to clear them at the end or reallocate. Since T=20, reinitializing a vector of size 20001 each test case (with default empty vectors) is fine. But the total memory would be 20001 vectors each with some capacity. Alternatively, we can use a map to only store vertices that appear, but simpler to just use arrays sized to max ID. Since max ID is 20000, which is small, this is acceptable.
But wait: constraints say 1 ≤ A, B ≤ 210^4. That's 20000, indeed. So we can set MAX = 20000 maybe plus a bit. But 210^4 = 20000? Actually 2*10^4 = 20000. So max ID is 20000.
Thus we can declare:
const int MAXV = 20000;
But ensure index up to 20000 inclusive, so size 20001.
For each test case:
- vector adj[MAXV+1]; // but this is array of vectors; we need to clear each vector? Actually we can reinitialize: vector adj[MAXV+1] would be declared fresh for each test case? In C++ we can define inside loop: for each test case, create vector adj[MAXV+1]; but MAXV+1 is 20001, that's okay but may cause stack overflow if declared as local variable? Usually, an array of vectors on stack may be heavy because each vector is an object with pointers, but total size maybe 20001 * (something) but it's on stack? Actually, if declared as local variable, the array of vectors is placed on stack, but each vector object is small (like three pointers). So 20001 * 24 bytes ~ 480KB, which is okay for stack. However, some compilers have stack limit of 1MB. It's fine. Alternatively, use dynamic allocation: vector<vector> adj(MAXV+1). That places the vector of vectors on stack but the inner vectors on heap. But local vector of size MAXV+1 will allocate that many vectors on the heap? Actually vector<vector> adj(MAXV+1) constructs MAXV+1 empty vectors. That's okay. Might be simpler to use a global array of vectors and clear them per test case.
Given T up to 20, we can just use global adjacency and visited arrays of size MAXV+1, and for each test case, we need to reset adjacency lists for only those vertices that appear. We can store a list of used vertices to clear later. This avoids resetting all 20001 entries. But resetting all 20001 vectors via adj[i].clear() in a loop takes O(20001) per test case, which is trivial. So we can have global vectors of size MAXV+1. But careful: global array of vectors may be large but fixed. We'll do:
const int MAXV = 20000;
vector adj[MAXV+1];
bool vis[MAXV+1];
int color[MAXV+1]; // 0 or 1
For each test case, read N, then for i from 1..N read A,B, push to adj[A], adj[B]. Also record used vertices in a vector vertices to iterate later. After processing, we do BFS/DFS for each used vertex not visited. For each component, we count size0 and size1. Then ans += max(size0, size1). Output.
After processing test case, we need to clear adjacency lists for used vertices to free memory and reset vis/color for used vertices. We can iterate over vertices list and clear adj[v].clear(), set vis[v]=false, color[v]=0. This is O(total vertices per test case) which is fine.
Potential pitfalls: The graph is guaranteed bipartite, but if there is any isolated vertex (should not happen because it appears in an edge), but still it's fine.
Edge Cases: There could be multiple edges between same pair? Doesn't matter. Our BFS will process duplicates maybe but fine.
BFS approach: Use queue. For each component start, set color[start]=0, vis[start]=true, count0=1, count1=0. Then BFS: for each neighbor, if not visited, set color[neighbor]=1-color[current], increment respective count, mark visited; else if visited and color[neighbor]==color[current], then contradiction (but not expected). Then after BFS, add max.
Alternatively, we can use DFS recursion but recursion depth could be up to number of vertices (20000) which is safe, but we have many edges; recursion might cause stack overflow if deep recursion for long path? 20000 depth is okay for most judges with default stack? Usually recursion depth limit may be around 10^6? Actually recursion depth in C++ can cause stack overflow if not increased. Better to use iterative BFS/DFS to be safe.
Now consider maximum ID: 210^4 = 20000. But wait: it says "1 ≤ A, B ≤ 210^4". That could be interpreted as 210^4 = 20000, yes. But if it's 210^4 meaning 20000? Usually 2*10^4 = 20000. So we set MAXV = 20000. But what about the sample: 100,200 within that range.
Thus solution straightforward.
But let's double-check: The problem wants "maximum possible number of soldiers of Raikage Army." That we computed as sum over components of max(color count). That seems correct.
But is there any twist? Could there be any global constraint that the assignment of armies must be consistent across components? For example, if we swap colors in one component, we are effectively labeling one set as Raikage. But could there be a scenario where we need to ensure that the same ID is not used in two components? IDs are unique, so no. And there is no constraint linking different components: they are independent fights. So yes.
Thus answer is sum over components of max(size0,size1).
However, the problem statement might be ambiguous: "They found the information about all the dual fights. Dual fight means a fight between a member of Raikage Army and a member of Zetsu Army." So each fight is between a Raikage soldier and a Zetsu soldier. They know the IDs of the fighters but don't know which ID belongs to which army. They want to find the maximum possible number of soldiers of Raikage Army. That is exactly as we described.
One might think there is possibility that some soldier could be assigned to either army if not constrained? But each soldier appears in at least one fight? The input only gives fights, but is it possible that some soldier did not fight? The problem says: "They collected the information about all the dual fights." So we have a list of all fights. It might not include all soldiers if some soldiers never fought? But the question is to find the maximum possible number of soldiers of Raikage Army, given only the fight information. If a soldier never appears in any fight, then there is no constraint on his army assignment; we could assign him to Raikage arbitrarily to increase count. However, the input only contains IDs that appear in fights. So we cannot infer existence of other soldiers. Usually, such problems assume that the soldiers mentioned in the fights are the only ones relevant, or that all soldiers are considered? Let's read: "They collected the information about all the dual fights. ... The ninjas of Leaf Village knows the id of the dual fighters, but don't know which id belongs to Raikage Army or a Zetsu Army." So they have IDs from the fight pairs. It doesn't explicitly say that those are all soldiers; but presumably, the fights involve some soldiers, and they know those IDs. Any soldier not involved in any fight would not be known, so they can't count them. So we only consider the IDs given. So we should only consider vertices that appear in the input. So that's fine.
But what about the possibility of multiple fights for same pair? Not important.
Thus algorithm stands.
Now, check complexity: For each test case, we process edges and BFS over all vertices. O(V+E) with V up to 2*10^4, E up to 1e5.
But there is a nuance: If we use global arrays of size 20001, we need to be careful about resetting visited and color for used vertices only. Because if we clear entire arrays of size 20001, that's O(20001) per test case, which is fine (20001 * 20 = 400k). So we could just loop from 1 to MAXV and reset adj[i].clear(), vis[i]=false etc. But that would be O(20001) each test case, which is fine. But note: we might have less than MAXV vertices, but still acceptable. However, adjacency list resetting: we need to clear each adj[i] vector; for i=1..20000, even if we never used that vertex, it's okay to clear empty vector. So we can do a simple for loop. But wait, we need to also rebuild adjacency list: we need to add edges to adj[A] and adj[B]. If we have a global array of vectors of size 20001, we should clear all vectors before each test case to remove previous data. That is O(20001) clears. That is fine. But clearing 20001 vectors might take some time but still trivial. Alternatively, we can store used vertices and only clear those, but not necessary.
But there is a potential memory issue: If we allocate a global array of vectors of size 20001, each test case we add edges, the vectors will accumulate capacity from previous test cases if we just clear (which shrinks size but not necessarily capacity). That's okay; total memory will be O(total edges across all test cases) but each test case we clear, the vectors may retain capacity. However, since T up to 20, and each test case may have up to 1e5 edges, total memory might become high if vectors keep capacity across test cases? Actually, if we do adj[i].clear(), the vector's capacity remains unchanged; it does not free the underlying memory. So after processing test case 1, each adj[i] might have capacity equal to degree of i. Then we clear, but the capacity remains. Then test case 2, we start pushing edges, which may increase capacity further if needed, but if the degrees are similar, it may reuse existing capacity, but also may need to reallocate if capacity insufficient. But if we process test cases sequentially, after clearing, we still hold memory from previous test case until the program ends, which could lead to high memory usage if the sum of capacities across all test cases becomes large. But worst-case: each test case has up to 1e5 edges, and there are 20 test cases. If we keep capacities, the total memory could be up to about 201e5 (some overhead) ≈ 2e6 integers, each 4 bytes = 8 MB, plus vector overhead. That's okay. Actually, 2e6 ints is about 8 MB. And each vector has some overhead (like pointer, size, capacity). That's 20001 vectors * overhead ~ maybe 24 bytes each = 480KB. So total memory ~ 10 MB, which is fine.
So we can use global arrays.
Implementation steps:
- Use fast I/O: ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
- Read T.
- For case_num from 1..T:
- Read N.
- For i=1..N: read A,B. Add to adj[A].push_back(B); adj[B].push_back(A). Also mark used? Actually we will later need to iterate over all vertices that appear. Could use a boolean array present[MAXV+1] to mark which IDs appear. Or we can after reading, BFS only on vertices that appear and are not visited. But we need to know which vertices exist. So we can maintain a vector
verts and mark present[A]=true, etc. But we also could simply iterate i from 1 to MAXV and check if adj[i] is non-empty? However, if a vertex appears but has degree, adj[i] will be non-empty. But what about a vertex that appears but has no edges? Not possible because it appears in an edge. So we can use if (!adj[i].empty() && !vis[i]) to start BFS. However, if we clear adjacency lists at the start of test case, they will be empty. So after reading all edges, vertices that appear will have non-empty adjacency. So we can loop from 1 to MAXV and if adj[i].size()>0 and not visited, do BFS. But we also need to consider vertices that appear but adjacency list is non-empty; that's fine. But is it possible that a vertex appears but adjacency list is empty? No, because each vertex appears in at least one edge, so its adjacency list will have at least one neighbor. So this works. But we still need to mark visited for all vertices. The loop from 1 to MAXV (20000) per test case is fine. So we can avoid storing a separate list of vertices.
But careful: The vertex IDs may not be contiguous; we may loop over 20000 anyway. That's okay.
Thus algorithm per test case:
- Clear adjacency: for (int i=1; i<=MAXV; i++) adj[i].clear(); (But MAXV=20000). Actually we can clear up to 20000. But if we use global arrays and clear only those i that might be used? We can clear all for simplicity.
- Initialize visited and color? We'll only set visited when we BFS; but we need to reset visited for all vertices before next test case? Because visited may be left from previous test case. Since we are clearing adjacency and will BFS only on vertices with adj non-empty, but if visited is left true for a vertex that was used in previous test case but not used in current test case, that vertex's ID may be not used now but visited true may cause issues if we loop over all 1..MAXV and check if !visited and adj non-empty? But if the vertex is not used, adj[i] will be empty, so we won't process it. However, visited[i] might still be true from previous test case, but we won't access it because we only start BFS on vertices that are not visited and adj non-empty. But if a vertex is not used now (adj empty) but visited is true from previous test case, it's irrelevant. However, for safety, we should reset visited for all vertices as well. Because later we might use visited array without checking adj non-empty? But we only use it in BFS and we only start BFS when !vis[i] and adj[i] non-empty. So if visited[i] is true and adj[i] empty, we ignore it. But if visited[i] is true for a vertex that is used in current test case (i.e., adj non-empty), that would be a problem because we might skip it. But after clearing adjacency for all vertices and then reading edges, the adjacency for used vertices will be rebuilt, but visited may still contain old values. So we must reset visited for at least those vertices that will be used. However, resetting visited for all vertices (1..MAXV) is O(20000) per test case, which is fine. So we can do a loop to set visited[i]=false and color[i]=0 for i=1..MAXV.
But careful: visited and color are arrays of size MAXV+1. Resetting all each test case is fine.
So plan:
- const int MAXV = 20000;
- vector adj[MAXV+1];
- bool vis[MAXV+1];
- int col[MAXV+1]; // 0 or 1
For each test case:
// Reset
for (int i=1; i<=MAXV; ++i) {
adj[i].clear();
vis[i] = false;
col[i] = 0;
}
// Read N
for (int i=0; i<N; ++i) {
int A, B; cin >> A >> B;
adj[A].push_back(B);
adj[B].push_back(A);
}
// Process
long long ans = 0; // total max Raikage
for (int i=1; i<=MAXV; ++i) {
if (!vis[i] && !adj[i].empty()) {
// BFS
int cnt0 = 0, cnt1 = 0;
queue q;
q.push(i);
vis[i] = true;
col[i] = 0;
cnt0++;
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v : adj[u]) {
if (!vis[v]) {
vis[v] = true;
col[v] = 1 - col[u];
if (col[v] == 0) cnt0++;
else cnt1++;
q.push(v);
} else {
// Optionally check consistency, not needed but good.
// if (col[v] == col[u]) { // contradiction }
}
}
}
ans += max(cnt0, cnt1);
}
}
// Output
cout << "Case " << case_num << ": " << ans << "\n";
Time complexity: O(MAXV + sum degrees) per test case.
But is there any possibility that a component contains only one vertex? Not possible because each vertex appears in at least one edge, so it must have at least one neighbor, so component size >=2. But what about if there is an edge between same ID? Input says two different integers A,B, so not self-loop.
Now, we should verify with sample.
Case 1: Works.
But we should test on edge cases: Large component, many edges.
Potential issue: The graph may not be connected? Already handled.
Now, is the answer always integer? Yes.
Potential alternative approach: Use DSU with parity. But BFS coloring is simpler.
Now, I want to think if there is any nuance: The problem wants "maximum possible number of soldiers of Raikage Army". Could there be a scenario where, by assigning some soldiers to one army, we affect the count of soldiers in other components? No, independent.
Thus solution is correct.
Now, we must implement fast I/O. Use ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
But note: if we use endl, it flushes. Use '\n'.
Now, note about multiple test cases: The sum of N can be up to 2e6, reading with cin is okay after syncing.
Potential memory: adj as vector adj[MAXV+1]; we need to be careful about reallocation when pushing edges. Each edge adds two pushes. That's okay.
Now, confirm the MAXV: 210^4 = 20000. But is it 210^4 = 20000 or 20,000? Yes. But if it's 210^4 = 20000 exactly, but sometimes they mean 210^4 = 20000? Actually, 2*10^4 = 20000, that's correct. So we set MAXV = 20000.
But sometimes problem constraints say up to 10^5 for IDs, but here they say 2*10^4. So safe.
Now, but we must ensure that we handle IDs exactly as given, no offset.
One more nuance: The answer may be as high as total number of vertices. Because if one partition is larger in each component, sum of max can be up to total vertices. So answer fits within int? Total vertices max: At most 210^4? Actually, number of distinct IDs can be up to 210^4. So answer ≤ 20000. But wait, there could be more vertices than 20000? Since IDs are up to 20000, distinct IDs cannot exceed 20000. So answer ≤ 20000. But we have T test cases, but each test case answer ≤ 20000. So int is fine.
But sample output shows case 2 answer 6, etc.
Edge case: N=0? Possibly? But constraints N≥1? Actually 1 ≤ N ≤ 10^5, so N≥1.
Now, let's double-check problem statement: "Each case contains an integer N denoting the number of dual fights." So N≥1.
Thus fine.
But we need to ensure we parse input correctly: There's an integer T, then for each case, an integer N, then N lines each with two ints. The sample input has 4 test cases, each with N, then edges.
Now, I think the solution is straightforward.
However, I recall a known problem: "Maximum possible members of Raikage Army" maybe is from some contest. This approach is typical.
Now, confirm with more thought: Could there be a scenario where the graph is not bipartite because they gave contradictory info? But problem says "Given that both A & B will be of different army." So the data is consistent with a bipartition. So we assume it's bipartite.
But suppose there is a triangle: 1-2, 2-3, 3-1. Then it's impossible for each edge to be between opposite armies. However, the problem guarantees that such data won't appear because the actual fights are between opposite armies, so the underlying true assignment exists. So graph is bipartite.
Thus solution stands.
Now, let's think about potential optimization: Use adjacency list with vector adj[20001] as said. BFS using queue.
Now, we might also need to handle large number of test cases quickly, but 20 * (20000 + 1e5) = 20 * 1.2e5 = 2.4e6 operations, plus BFS overhead, which is fine.
Now, implement.
But be careful: When we clear adjacency list for all i from 1 to MAXV, we also need to clear vis and col. Doing loops for all 20001 each test case is fine. But maybe we can do only for used vertices by storing them in a vector all_vertices. But not necessary.
But there's a catch: If we clear adjacency for all i from 1..MAXV, that will be O(MAXV) per test case (20k). For T=20, that's 400k operations, trivial.
But we must also read N and edges; that's main cost.
Now, writing code.
Now, some notes: The input might have multiple spaces, newlines. Use cin.
Edge Cases: IDs may be up to 20000, but we set MAXV = 20000, but we need array index up to 20000 inclusive, so size MAXV+1 = 20001.
Now, I'll produce final solution.
But I should also consider using bitset or something? Not needed.
Now, implement with BFS.
Pseudo-code:
#include <bits/stdc++.h>
using namespace std;
const int MAXV = 20000;
vector adj[MAXV+1];
bool vis[MAXV+1];
int col[MAXV+1];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
for (int tc = 1; tc <= T; ++tc) {
// Reset
for (int i = 1; i <= MAXV; ++i) {
adj[i].clear();
vis[i] = false;
col[i] = -1; // or 0, but set to 0 later.
}
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
int A, B;
cin >> A >> B;
adj[A].push_back(B);
adj[B].push_back(A);
}
int ans = 0;
for (int i = 1; i <= MAXV; ++i) {
if (!vis[i] && !adj[i].empty()) {
// BFS
queue<int> q;
q.push(i);
vis[i] = true;
col[i] = 0;
int cnt[2] = {1, 0};
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v : adj[u]) {
if (!vis[v]) {
vis[v] = true;
col[v] = 1 - col[u];
cnt[col[v]]++;
q.push(v);
}
// else check bipartite if needed (col[v]==col[u]) then error.
}
}
ans += max(cnt[0], cnt[1]);
}
}
cout << "Case " << tc << ": " << ans << "\n";
}
return 0;
}
But wait: col[i] may be initialized to 0, but we set it again in BFS. It's fine.
But we must be careful: If there is a vertex that appears but has no edges? Not possible.
Now, test with sample.
I'll simulate quickly.
Case 1: vertices: 1,2,3,4,5. adj[1]=[2], adj[2]=[1,3,4,5], etc. Loop i=1..20000. i=1: !vis[1] && !adj[1].empty() -> BFS from 1. Color 1 as 0. Then BFS: neighbor 2: not visited, color 1-> col[2]=1, cnt[1]++. Then from 2: neighbors 1,3,4,5; 1 visited, 3 new color 0, cnt[0]++; 4 new color0, cnt[0]++; 5 new color0, cnt[0]++. So cnt[0]= (1 from start + 3 from 3,4,5) =4? Actually start: cnt[0]=1 (node1), cnt[1]=0. Then process node2: from u=1, v=2 => cnt[1]=1. Then when u=2: v=3 => cnt[0]=2; v=4 => cnt[0]=3; v=5 => cnt[0]=4. So final cnt[0]=4, cnt[1]=1. max=4. i=2..5 are visited so skip. i=6..20000 skip because adj empty. ans=4. Output "Case 1: 4". Good.
Case 2: as earlier gives 6.
Case 3: N=1: edge 100,200. So vertices 100,200. Loop i from 1..20000. i=100: !vis[100] && !adj[100].empty() -> BFS. Start 100 color0 cnt0=1. neighbor 200: color1 cnt1=1. max=1. Then i=200 visited, skip. ans=1. Output.
Case 4: vertices 1-5 path. BFS from 1: color1=0 cnt0=1; neighbor2 color1=1 cnt1=1; neighbor3 color0 cnt0=2; neighbor4 color1 cnt1=2; neighbor5 color0 cnt0=3. max=3. ans=3. Output.
Works.
Now, note: The BFS from i only if adj[i] not empty, but what about components where the starting vertex may have been visited from a previous BFS? Not possible because we check !vis[i]. So fine.
Now, we must ensure that we process all components even if the graph has multiple components. Since we iterate i from 1 to MAXV, we will eventually hit each component's smallest vertex. Good.
Now, is there any scenario where a vertex has adjacency but the BFS doesn't capture all vertices because we skip due to visited? No.
Now, one more nuance: The graph may be bipartite but not necessarily connected. Yes.
Now, complexity: For each test case, BFS visits each vertex exactly once, each edge twice (once from each endpoint). So O(V+E).
Now, fast I/O: We use cin and cout with sync off and tie off. That should be fast enough for 2e6 lines.
Now, consider the worst-case input: T=20, each N=1e5, each with random IDs up to 20000. That will produce about 1e5 edges, and vertices up to 20000. BFS will traverse all vertices and edges. That's fine.
Now, confirm memory: adj holds degree for each vertex. Total pushes = 2N per test case, up to 2e5 integers. Each integer 4 bytes, so 0.8 MB per test case. Actually, 2e5 ints ~ 0.8 MB. Plus vector overhead: each vector has pointer, size, capacity (24 bytes) * 20001 = 0.48 MB. So total ~1.3 MB per test case. But since we reuse global arrays, the capacity might accumulate but overall memory maybe ~2-3 MB. Fine.
Now, there is a small improvement: We can use bitset for visited? Not needed.
Now, we should ensure we use 'int' for ans, but total max per test case is up to 20000, so int is fine.
Now, let's produce final answer.
But before final, double-check problem statement: "maximum possible number of soldiers of Raikage Army." Could there be any scenario where the assignment is not independent per component because we need to assign globally such that each soldier is either R or Z, but there is no further constraint. So independent.
Thus answer is correct.
Now, final code.
Potential pitfalls: The input may contain leading/trailing spaces. Use cin.
We need to output exactly as "Case X: Y". Sample shows "Case 1: 4" etc.
Now, we might want to handle large T with clearing arrays efficiently. Loop to MAXV each test case is fine.
But note: If MAXV is 20000, but the maximum ID could be 20000. That's okay.
Now, confirm that we didn't miss that A,B are up to 210^4, but 210^4 = 20000, so yes.
But could it be 210^4 = 20000? Actually, 210^4 = 2 * 10^4 = 20000. So yes.
Now, final code.
But hold on: The constraints in problem: 1 ≤ A, B ≤ 2*10^4. But in sample case 2, we have ID 10, which is <= 20000. So okay.
Now, final answer.
But wait, there's a nuance: The problem says "maximum possible number of soldiers of Raikage Army." What if there are multiple possible assignments, but we are to pick the maximum number of soldiers that could belong to Raikage Army. That's exactly sum of max partition sizes.
Now, verify with more complex scenario: Suppose a component has equal partition sizes (like 2 and 2). Then we can assign either side to Raikage, giving 2. So max = 2.
Now, check if any component could be disconnected and we could assign a soldier to both armies? No.
Thus solution stands.
Now, the only potential tricky part: Are there any soldiers that are not involved in any fight but could be assigned arbitrarily? The input only lists fights, and the IDs known are those that appear in fights. So we only count those. The problem statement: "The ninjas of Leaf Village knows the id of the dual fighters" So they only know IDs of those who fought. They don't know about any other soldiers. So they must base their reasoning only on those IDs. So we only consider vertices that appear. So correct.
Now, let's implement.
But I'll consider a more memory-friendly approach: Instead of resetting all 20001 vectors, we could clear them only for used vertices. But it's fine.
But caution: In C++, when we do adj[i].clear(), it doesn't release memory, but it's fine.
Now, one more nuance: The BFS uses queue which can be declared inside the loop for each component. That's okay. But we can also reuse a global queue? Not necessary.
Now, final code.
But before finalizing, test with a scenario where a component has many edges, BFS should handle.
Now, I think it's done.
However, we must consider the possibility that the graph may have multiple edges or parallel edges. That doesn't affect BFS; we may process neighbor multiple times, but visited check prevents infinite loops. It just adds a little overhead. It's okay.
Now, check for any integer overflow? No.
Now, final answer.
I will produce the solution now. ```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXV = 20000; // maximum possible soldier id
vector adj[MAXV + 1];
bool vis[MAXV + 1];
int col[MAXV + 1]; // 0 or 1
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
for (int tc = 1; tc <= T; ++tc) {
// reset structures
for (int i = 1; i <= MAXV; ++i) {
adj[i].clear();
vis[i] = false;
col[i] = -1;
}
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
int A, B;
cin >> A >> B;
adj[A].push_back(B);
adj[B].push_back(A);
}
int ans = 0;
// process each connected component
for (int i = 1; i <= MAXV; ++i) {
if (!vis[i] && !adj[i].empty()) {
// BFS to colour the component
queue<int> q;
q.push(i);
vis[i] = true;
col[i] = 0;
int cnt[2] = {1, 0}; // count of vertices coloured 0 and 1
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : adj[u]) {
if (!vis[v]) {
vis[v] = true;
col[v] = 1 - col[u];
cnt[col[v]]++;
q.push(v);
}
}
}
ans += max(cnt[0], cnt[1]);
}
}
cout << "Case " << tc << ": " << ans << "\n";
}
return 0;
}