A ski base is planned to be built in Walrusland. Recently, however, the project is still in the constructing phase. A large land lot was chosen for the construction. It contains n ski junctions, numbered from 1 to n. Initially the junctions aren't connected in any way.
In the constructing process m bidirectional ski roads will be built. The roads are built one after another: first the road number 1 will be built, then the road number 2, and so on. The i-th road connects the junctions with numbers ai and bi.
Track is the route with the following properties:
The route is closed, that is, it begins and ends in one and the same junction.
The route contains at least one road.
The route doesn't go on one road more than once, however it can visit any junction any number of times.
Let's consider the ski base as a non-empty set of roads that can be divided into one or more tracks so that exactly one track went along each road of the chosen set. Besides, each track can consist only of roads from the chosen set. Ski base doesn't have to be connected.
Two ski bases are considered different if they consist of different road sets.
After building each new road the Walrusland government wants to know the number of variants of choosing a ski base based on some subset of the already built roads. The government asks you to help them solve the given problem.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). They represent the number of junctions and the number of roads correspondingly. Then on m lines follows the description of the roads in the order in which they were built. Each road is described by a pair of integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers of the connected junctions. There could be more than one road between a pair of junctions.
Output
Print m lines: the i-th line should represent the number of ways to build a ski base after the end of construction of the road number i. The numbers should be printed modulo 1000000009 (109 + 9).
Examples
Input
3 4
1 3
2 3
1 2
1 2
Output
0
0
1
3
Note
Let us have 3 junctions and 4 roads between the junctions have already been built (as after building all the roads in the sample): 1 and 3, 2 and 3, 2 roads between junctions 1 and 2. The land lot for the construction will look like this:
The land lot for the construction will look in the following way:
We can choose a subset of roads in three ways:
In the first and the second ways you can choose one path, for example, 1 - 2 - 3 - 1. In the first case you can choose one path 1 - 2 - 1.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse problem: "Ski base" defined as a non-empty set of roads that can be partitioned into one or more tracks (closed walks) such that each track is a closed walk (starting and ending same vertex, at least one road, no repeated road, but vertices can repeat) and exactly one track uses each road in the set. So essentially a set of edges that can be decomposed into cycles (Eulerian subgraph where each component is Eulerian? But careful: tracks are closed walks that don't reuse edges, but can repeat vertices; a closed walk without repeating edges is essentially a circuit that might traverse vertices multiple times, but in undirected graph, any closed walk that doesn't reuse edges corresponds to an Eulerian trail? Actually, a closed walk that doesn't repeat edges is essentially a cycle decomposition? For undirected graphs, a closed walk that uses each edge at most once, and uses each edge exactly once in the decomposition, so the set of edges must be decomposed into edge-disjoint closed trails (circuits) that may share vertices. That's exactly an Eulerian subgraph: a subgraph where each connected component is Eulerian (all vertices even degree). However, careful: The track is allowed to repeat vertices; but if a component is Eulerian, we can find an Eulerian circuit that covers all edges exactly once. But the definition says "divided into one or more tracks so that exactly one track went along each road of the chosen set." That means we partition the set of edges into edge-disjoint closed walks. Each closed walk can cover a subset of edges; these subsets are edge-disjoint and together cover all edges. This is exactly a decomposition of the edge set into closed trails. In an undirected graph, a set of edges can be partitioned into edge-disjoint cycles (simple cycles) if and only if every vertex has even degree? Not exactly: Euler's theorem says that a connected graph has an Eulerian circuit if and only if all vertices have even degree. However, a graph can be decomposed into edge-disjoint cycles if and only if every vertex has even degree (that's a known fact: any graph where all vertices have even degree can be decomposed into cycles). Actually, it's true that a graph (not necessarily connected) can be decomposed into edge-disjoint cycles if and only if every vertex has even degree. This decomposition is not necessarily into simple cycles? It can be into cycles (simple cycles) because each component is Eulerian and you can perform a cycle decomposition: repeatedly remove cycles until no edges left. So indeed the condition is that each vertex in the chosen edge set has even degree (since each closed walk contributes 2 to degree at each internal vertex? Actually any closed walk will contribute an even number to the degree of each vertex it passes through? Let's think: A closed walk that does not reuse edges but may visit a vertex multiple times; each time it enters and leaves, it uses two incident edges. So overall, the degree of each vertex in the subgraph formed by the edges used by that walk must be even? Actually, for a closed trail (not necessarily simple cycle), the degree of each vertex in the trail is even (since each time we pass through a vertex we use two edges; start and end at same vertex also gives even). So each closed trail is an Eulerian subgraph. If we partition edges into several closed trails, the union also has all vertices even degree because degrees add. Conversely, if a graph (set of edges) has all vertices even degree, then it can be partitioned into edge-disjoint cycles (or closed trails). So the condition for a non-empty set of edges to be a ski base is exactly that the subgraph induced by those edges has all vertices even degree. But is there any additional restriction? The definition: "non-empty set of roads that can be divided into one or more tracks so that exactly one track went along each road of the chosen set. Besides, each track can consist only of roads from the chosen set." So yes, that means the edge set can be partitioned into closed trails. As argued, this is equivalent to every vertex in the subgraph having even degree.
But wait: Could there be a set where all vertices have even degree but cannot be partitioned into closed trails because the graph is disconnected? Actually each component must be Eulerian. For a disconnected graph, each component with all even degrees can be Eulerian? Eulerian circuit requires connectivity; but decomposition into cycles does not require a circuit covering whole component, just into cycles; each component with all even degrees can be decomposed into cycles (since each component is a graph where all vertices have even degree, we can always find a cycle decomposition). So yes.
Thus the problem reduces to: Given an undirected graph (with multiple edges allowed) that is being built incrementally (adding edges one by one), after each addition we need to count the number of non-empty subsets of edges (from the set of edges added so far) such that in the induced subgraph, every vertex has even degree. The count modulo 1e9+9.
Let's denote the current set of edges E_t (first t edges). We need to compute the number of subsets S ⊆ E_t, S ≠ ∅, such that for all vertices v, deg_S(v) is even. That's the number of non-empty even subgraphs (also called Eulerian subgraphs). This is a classic problem: For a graph, the number of even subgraphs (including empty) equals 2^{m - n + c}, where c is the number of connected components? Actually, in a vector space over GF(2), the cycles space dimension is m - n + c (for a graph). The set of even subgraphs corresponds to the cycle space: subsets of edges with even degree at each vertex form a vector space over GF(2) of dimension m - n + c (for a connected component? Actually overall graph may be disconnected; the cycle space of a graph is the set of edge sets that form a disjoint union of cycles; this is the kernel of the incidence matrix modulo 2. The rank of the incidence matrix for a graph with n vertices and c components is n - c (over GF(2)), assuming no isolated vertices? Actually incidence matrix mod 2 has rank n - c (if graph has no isolated vertices? The rank of the incidence matrix over GF(2) for a graph with c components is n - c, because each component contributes (size of component - 1) to rank, and each row sum is 0 mod 2. So nullity = m - (n - c) = m - n + c. So the number of even subgraphs (including empty) is 2^{m - n + c}. But careful: This holds for the entire graph? The vector space is the set of edge subsets with even degree at all vertices. Indeed, the incidence matrix A (size n x m) over GF(2) maps a subset of edges (characteristic vector) to the vector of degrees mod 2 at each vertex. The kernel is the set of subsets with all degrees even. The rank of A over GF(2) is n - c (assuming graph has no isolated vertices? But isolated vertices have zero rows? Actually if a vertex is isolated (degree 0), its row is all zeros, so it's linearly dependent. The rank is n - c' where c' is number of connected components of the graph after removing isolated vertices? Let's recall standard result: For a graph with n vertices, m edges, and c connected components (including isolated vertices), the rank of the incidence matrix over GF(2) is n - c. Because for each component (even isolated), we have a relation that the sum of rows in that component is 0 (since each edge contributes 2 to sum mod2? Actually each edge connects two vertices in same component; summing all rows for a component gives 0 because each edge contributes 1+1=0 mod2. Also, any one row can be expressed as sum of others? The dimension of row space is n - c. This holds even if there are isolated vertices: for an isolated vertex, the row is all zeros, so it's dependent and effectively reduces rank? Actually with isolated vertices, each isolated vertex is a component, and its row is zero, so it does not contribute to rank; thus total rank is (sum over components of (size_i - 1)) = n - c. So yes, formula holds for all graphs with c components (including isolated). So nullity = m - (n - c) = m - n + c. So the number of even subgraphs is 2^{m - n + c}. This includes the empty set. So number of non-empty even subgraphs is 2^{m - n + c} - 1, provided m - n + c >= 0 (which is always true? For a graph with no cycles, m = n - c, so nullity=0, only empty set is even; that is consistent). So after each edge addition, we need to compute c (number of connected components) of the current graph (including isolated vertices? c is number of components. Graph initially has n vertices and no edges, so c = n. As edges added, c may decrease. Also m increases by 1 each step. But we also need to consider multiple edges? The formula holds for multigraphs as well, because incidence matrix still works; multiple edges are separate columns. So nullity dimension = m - n + c remains valid. However, careful: If there are self-loops? But input says ai ≠ bi, so no self-loops. So we can apply.
Thus after each edge addition, we could compute the current number of edges m_t = t, and the number of components c_t. Then answer = (2^{t - n + c_t} - 1) mod MOD.
Check with example: n=3, m=4. After building 4 edges, we have edges: (1,3), (2,3), (1,2), (1,2). Let's compute. Graph: vertices 1,2,3. Edges: three vertices all connected? Actually edges: 1-3, 2-3, 1-2, and another 1-2. So the graph is connected (c=1). m=4. So t - n + c = 4 - 3 + 1 = 2. So 2^2 - 1 = 4 - 1 = 3. That matches output after fourth edge (line 4 says 3). After third edge: edges: 1-3, 2-3, 1-2. Then c=1, m=3 => 3-3+1=1 => 2^1-1=1. Output after third edge is 1. After first edge: only edge (1,3). Graph: components? vertices: 1 and 3 are connected; vertex 2 isolated. So c = 2 (components: {1,3} and {2}). m=1 => 1-3+2=0 => 2^0-1=0. Output 0. After second edge: edges (1,3) and (2,3). Now vertices 1,2,3 all connected? 1-3-2, so component {1,2,3}, c=1? Actually there is path from 1 to 2 via 3, so graph is connected (all three vertices). So c=1. m=2 => 2-3+1=0 => 2^0-1=0. Output 0. This matches sample.
Thus problem seems simple: after each edge addition, compute current number of connected components in the graph. Since n,m up to 1e5, we can use union-find (DSU) to maintain components. Each edge added may merge two components or not (if already in same component). Then c_t = number of components.
But careful: The formula uses c_t = number of connected components of the graph (including isolated vertices). However, when we have multiple edges between same vertices, adding an edge between vertices already in same component does not change component count. So answer increments when an edge is added that connects two previously separate components? Actually t - n + c changes: t increases by 1, c may decrease by 1 (if edge connects two different components) or stay same. So we can compute exponent e = t - n + c. Then answer = (2^e - 1) mod MOD.
But wait: In union-find we maintain number of components initially n. For each new edge (u,v), we union(u,v) and if they were in different components, we decrement component count. So we can compute exponent incrementally: initially before any edges, we would have e = 0 - n + n = 0? But we only answer after each edge. So we can compute:
Let comp = n.
Let edges_added = 0.
For each new edge:
edges_added += 1
if find(u) != find(v): comp -= 1; union.
exponent = edges_added - n + comp
ans = (pow2[exponent] - 1) mod MOD
output ans
We need pow2 up to max exponent. Max exponent when? edges_added = m (max 1e5), comp >= 1, so exponent max = m - n + 1 (if graph becomes connected) which could be up to 1e5 - 2 + 1 ~ 1e5. Actually worst-case n=2, m=1e5 => exponent = 1e5 - 2 + 1 = 99999, so within 1e5. So we can precompute pow2 up to 1e5.
But wait: There might be case where exponent becomes negative? Let's check: initially after first edge, if n large and we add an edge, edges_added=1, comp might be n-1 (if u and v were different components? Actually initially each vertex is isolated; adding an edge between two distinct vertices merges two components, so comp becomes n-1). So exponent = 1 - n + (n-1) = 0. So non-negative. If we add an edge between vertices already in same component, comp unchanged, exponent = t - n + c. Since t increases, c unchanged, but t can be less than n - c? Actually when graph is already connected, c=1, exponent = t - n + 1. For t < n-1, it could be negative? But if graph is connected, then t must be at least n-1 (minimum edges to connect n vertices). However, we might add edges in an order that causes graph to become connected before having n-1 edges? Actually to become connected, you need at least n-1 edges, but it could become connected exactly at n-1 edges. So before that, c > 1, and exponent = t - n + c. Since c >= n - t? Not necessarily. Let's examine: For any graph, the number of components c satisfies c >= n - t, because each edge can reduce components by at most 1. So c >= n - t. Thus t - n + c >= t - n + (n - t) = 0. So exponent is always >= 0. Equality when c = n - t (i.e., graph is a forest). So indeed exponent non-negative.
Thus solution appears trivial. But is there any nuance? Let's double-check problem statement: "Ski base" defined as a non-empty set of roads that can be divided into one or more tracks... The tracks must be closed routes that contain at least one road, cannot go on one road more than once. But is it allowed for a track to use a road twice? It says "doesn't go on one road more than once" so each edge used at most once per track. In the decomposition, each road appears in exactly one track. So yes, it's exactly an edge-disjoint decomposition into closed trails. Even degree condition is necessary and sufficient. However, there might be a subtlety: The tracks are required to be "closed" and "contain at least one road". That means a track cannot be empty. But if we have a set that can be partitioned into tracks, each track is non-empty. So that's fine.
But there is also a condition: "Ski base doesn't have to be connected." So even degree condition still holds.
Thus the counting reduces to even subgraphs.
But let's double-check with more complex cases: Suppose we have a graph with a vertex of degree 0 in the chosen set? That vertex just not incident to any chosen edge. That's fine. The even-degree condition is vacuously true for vertices not incident to any chosen edge (degree 0 even). So indeed the condition is that all vertices incident to the chosen edges have even degree. That's exactly the kernel condition.
Thus the answer is 2^{m - n + c} - 1.
But wait: The problem statement says "two ski bases are considered different if they consist of different road sets." So we are counting subsets of edges. So formula is correct.
Now we need to output after each edge. So we can maintain DSU and component count. But there is a catch: The graph may have parallel edges. DSU handles them fine; union only if different sets. So that's fine.
But are we missing something about the fact that the tracks can be any closed walks, not necessarily simple cycles? That might allow some sets that are not even-degree? Consider a set consisting of a single edge? A single edge: deg(u)=1, deg(v)=1, not even. Can we form a track from a single edge? A track must be closed and contain at least one road. A closed walk using a single edge would need to traverse that edge and return, but you cannot traverse same edge twice because track cannot go on one road more than once. So a single edge cannot form a closed walk. So not allowed. So yes, even-degree condition holds.
Consider two edges that share a vertex, like edges (1,2) and (2,3). That's a path: degrees: deg(1)=1, deg(2)=2, deg(3)=1 -> not all even. Can we partition into tracks? Could we have a closed walk that uses both edges? Starting at 2, go to 1, back to 2 (using (2,1) and then (1,2)? That would reuse edge (1,2) twice? Not allowed. Could we have two tracks? Each track must be closed and use disjoint edges. But we have only two edges; they share a vertex; any closed walk would need to use at least two edges; but if we try to make one track using both edges, we need to start at 1, go to 2, go to 3. To close, we would need to return from 3 to 1, but no edge. So impossible. Could we have two tracks each using one edge? But each track must be closed; a single edge cannot make a closed walk. So not allowed. So indeed not even.
Consider a triangle (3 edges). All vertices degree 2, even. Can be decomposed into one cycle. Good.
Consider two disjoint cycles: each component Eulerian, works.
Thus formula stands.
But there is nuance: The dimension of cycle space is m - n + c for any graph, but that counts subsets of edges that satisfy even degree condition, including empty set. So number of non-empty even subgraphs is 2^{m - n + c} - 1. However, is this always true for graphs with multiple edges? Yes, because the incidence matrix columns for parallel edges are independent; they are separate columns. The rank of incidence matrix might be affected? Consider two parallel edges between same vertices u and v. The rows for u and v each have two 1's. The rank of A (mod 2) is still n - c? Let's test: n=2, c=1? Actually graph with two vertices and two parallel edges. A = [1 1; 1 1] mod2. Each row is [1,1]. Rows are equal, rank =1. n - c = 2-1=1. So nullity = m - rank = 2-1=1. So even subgraphs: sets of edges such that deg(u) even, deg(v) even. Since deg(u) mod2 = sum of edges incident to u mod2 = (#edges selected) mod2 (since both edges incident to u). So we need #selected even. So there are 2 possibilities: select none or both. That's 2 = 2^1, and non-empty =1. So fits formula: m=2, n=2, c=1 => exponent=1 => 2^1-1=1. Good.
Consider three parallel edges: m=3, rank? A = [1 1 1; 1 1 1] rank=1. nullity=3-1=2 => 4 even subgraphs: subsets with even number of edges: 0,2 edges (choose any 2 of 3). That's 1+3=4. So exponent = m - n + c = 3 -2 +1=2, 2^2=4. Good.
So it's consistent.
Thus the solution is simple DSU plus precomputed powers.
But is that the whole problem? It seems too easy for a problem from Codeforces? Actually I recall a Codeforces problem "Ski Base" (CF 117C? Not sure). I think it is indeed known that the answer is 2^{added_edges - n + components} - 1. However, I recall there being a twist: The number of ways modulo 1000000009, but the exponent could be huge? But here m up to 1e5, so fine.
Wait, but is there any nuance about "tracks" can be any closed route, not necessarily a cycle (simple cycle) but could be like figure-8? However that still results in even degrees.
Thus solution: maintain DSU, count components, compute exponent = edges_added - n + comp, output pow2[exponent] - 1 mod MOD.
But we need to be careful about modulo: MOD = 1000000009 (1e9+9). Precompute pow2 up to m (or up to m maybe plus n? Actually max exponent is m - n + comp_min. Since comp_min is 1, exponent max = m - n + 1. Since n>=2, exponent <= m-1? If n=2, m=1e5, exponent = 100000 - 2 + 1 = 99999 < 1e5. So we can precompute up to 1e5 (or maybe 2e5 to be safe). But what if n is large, m is also large? n up to 1e5, m up to 1e5. So exponent max <= m = 1e5 (when n=c? Actually if n=c? That would be when graph has no edges? But after adding edges, exponent can be at most m? Let's find max possible: comp >=1, so exponent = m - n + comp ≤ m - n + n = m? Actually comp ≤ n, so exponent ≤ m - n + n = m. But comp = n when no edges, but then m=0 so exponent=0. For m>0, comp can be at most n but then m small. So exponent ≤ m. For m=1e5, exponent ≤ 1e5. So precomputing up to 1e5 is fine.
But careful: exponent could be negative? We argued it's always non-negative, but DSU will handle.
Edge cases: If exponent = 0, then ans = 0.
Now, is there any trick about cycles being counted multiple times? No, we count subsets.
Thus solution is straightforward.
But wait, there is another interpretation: Could it be that a ski base is a set of roads that can be partitioned into tracks, but each track is a closed route that doesn't go on one road more than once, but can go through any junction multiple times. This is exactly a circuit. So again.
Thus I'm confident.
However, I recall a Codeforces problem "Ski Base" (maybe problem 198C?) that required something about number of cycles modulo something and had answer 2^{something} - 1. Many people solved it with DSU.
Let's search memory: There is Codeforces Round #91 (Div. 2) problem D "Ski Base"? Actually I think it's problem 117C? Let me think: There is problem "Ski Base" from Codeforces Beta Round #91 (Div. 1 Only) or something. I think the solution is indeed based on the concept of "number of independent cycles". So it's correct.
But one nuance: The modulo is 1e9+9, which is prime? Actually 1e9+9 is prime. So we can compute pow2 using fast exponentiation or precomputation.
Thus the algorithm:
Read n, m.
Initialize DSU with n elements.
comp = n.
edges = 0.
Precompute pow2[0..m] (maybe up to m) as pow2[0] = 1; for i=1..m: pow2[i] = (pow2[i-1] * 2) % MOD.
For each of the m edges:
read u, v.
edges++.
if find(u) != find(v): comp--; union(u,v).
exponent = edges - n + comp.
ans = (pow2[exponent] - 1 + MOD) % MOD.
print ans.
That's O(m α(n)). Good.
But let's double-check with the sample:
n=3,m=4. Precompute pow2 up to 4 maybe more: pow2[0]=1,1=2,2=4,3=8,4=16? Actually exponent max 2 so okay.
Step1: edges=1, find(1)!=find(3) => comp=2, exponent=1-3+2=0, ans=0.
Step2: edges=2, find(2)!=find(3)? After first union, 1-3 in same set. find(2) diff => comp=1, exponent=2-3+1=0, ans=0.
Step3: edges=3, find(1)==find(2) (since all connected) => comp=1, exponent=3-3+1=1, ans=2^1-1=1.
Step4: edges=4, find(1)==find(2) => comp=1, exponent=4-3+1=2, ans=4-1=3. Good.
Thus matches.
Edge case: multiple edges between same vertices from the start? That's fine.
One more test: n=2, m=1: edge 1-2. Initially comp=2, edges=1 => find diff => comp=1, exponent=1-2+1=0 => ans=0. Is that correct? With one edge, no ski base. Yes.
n=2, m=2: edges: 1-2, 1-2 (parallel). After first: ans0. After second: edges=2, comp=1, exponent=2-2+1=1 => ans=1. Valid ski base: choose both edges to have even degrees (each vertex degree 2). That set can be partitioned? Could be one track: start at 1, go to 2 via first edge, then back to 1 via second edge -> closed walk using both edges, each exactly once. So valid. Also could choose any other? Only non-empty subsets: {first edge} not valid, {second} not valid, {both} valid. So answer 1. Good.
n=2, m=3: edges all 1-2. After first:0; second:1; third: edges=3, comp=1, exponent=3-2+1=2 => ans=3. Even subgraphs: subsets with even number of edges: 0,2,? Actually choose 2 edges out of 3: C(3,2)=3, and also 0. So non-empty even: 3. Good.
Thus formula works.
Now we must implement DSU for up to 1e5 nodes. Use path compression and union by size/rank.
Edge addition order: We process edges sequentially, updating DSU.
Potential pitfalls: The graph may have many edges, but DSU handles.
But wait: There's a nuance: The number of connected components c in the formula is the number of connected components in the graph formed by all edges (including multiple edges). DSU gives that.
But is it correct to count components based purely on connectivity ignoring isolated vertices? DSU will treat each vertex initially as its own component. When we union, we merge. So comp is number of DSU sets. That's correct.
Thus solution is correct.
But let's read problem statement again: "Ski base doesn't have to be connected." That's covered.
Now, are there any constraints about modulo? MOD = 1000000009, we must output modulo.
Thus the solution is trivial.
But I recall a problem "Ski Base" from Codeforces that had a twist: The answer is 2^{m - n + number_of_components} - 1, but they also required to consider that the government asks after each new road, and there might be a large number of ways, but we need to output modulo. Yes.
Thus the solution is indeed simple.
But before finalizing, double-check if there is any hidden condition: Could a track be allowed to be any closed route, but not necessarily a cycle decomposition? For example, consider a set of edges that forms a single vertex with a loop? But loops are not allowed.
Also, could there be a track that uses edges in a way that some vertex has odd degree but still closed? No, as argued.
Thus it's fine.
Thus code can be written quickly.
Now some notes: The DSU find with path compression; union by size.
We also need to precompute powers up to m (maybe m+5). exponent could be up to m, so array size m+5.
Edge case: exponent = 0 leads to ans = 0.
Implementation steps:
const MOD = 1000000009.
read n, m.
init parent array of size n+1, size or rank array.
comp = n.
edges = 0.
precompute pow2[0..m] (maybe up to m) using pow2[0]=1; for i in 1..m: pow2[i] = (pow2[i-1]*2)%MOD.
For i in 1..m:
read u, v.
edges += 1.
ru = find(u); rv = find(v)
if ru != rv:
comp -= 1
union(ru, rv) (by size)
exponent = edges - n + comp
ans = (pow2[exponent] - 1) % MOD
if ans < 0: ans += MOD
print ans
Testing with sample: ok.
Now, we must ensure that exponent is within precomputed range. edges - n + comp could be up to m (since comp >=1, edges=m, so exponent <= m - n + n = m). So safe.
But if n is very large (1e5) and m is also 1e5, exponent might be exactly m - n + comp. Since n up to 1e5, comp at least 1, exponent could be as low as 0 but at most m. So we need pow2 up to m. We have precomputed up to m, so fine.
Now, what about memory: pow2 array of size m+1 ~ 1e5+1, fine.
Now, we need to think about potential overflow: pow2 exponent as integer, no problem.
Now, is there any hidden nuance regarding multiple edges and the cycle space dimension? The dimension of cycle space is m - n + c. That is correct for multigraphs. So we are good.
Thus solution is O(m α(n)).
Now finalize.
But wait, there is one more nuance: The government wants number of ski bases based on some subset of the already built roads. That means we consider all subsets of the set of roads built so far. That's exactly what we did.
Now, we can finalize.
But just to be thorough, let's test with more examples.
Test 1: n=4, m=3 edges: (1,2), (2,3), (3,4). After each:
Edge1: edges=1, comp from 4 to 3? Actually 1-2 merges two comps: comp=3. exponent=1-4+3=0 => ans0.
Edge2: (2,3). After edge1, sets: {1,2}, {3}, {4}. Union 2 and 3: 2 is in {1,2}, 3 is in {3}, so merge => comp=2. exponent=2-4+2=0 => ans0.
Edge3: (3,4). Now sets: {1,2,3} and {4}. Union => comp=1. exponent=3-4+1=0 => ans0. Actually with 3 edges, graph is a path: 1-2-3-4, still not a cycle, so no even subgraph except empty. So correct.
Now add edge4: (4,1). That creates a cycle of length 4. After edge4: edges=4, comp stays 1, exponent=4-4+1=1 => ans=1. Indeed only non-empty even subgraph is the whole cycle (set of all 4 edges). Also could we have a subset? Possibly two disjoint cycles? No other. So 1 is correct.
Now add edge5: another (1,2). Then graph has edges: cycle (1-2-3-4-1) plus extra parallel edge 1-2. After 5 edges, comp=1, exponent=5-4+1=2 => ans=3. Even subgraphs: subsets with even degree? Let's list all subsets (non-empty) of 5 edges: The condition: each vertex even deg. Compute degrees: if we select edges, we need parity even. Since all vertices are in one component. Number of even subgraphs = 2^{2}=4, non-empty=3. Which subsets? (1) The whole set? Let's test: whole set has deg(1): edges: (1,2)x2, (4,1) => three? Actually cycle edges: (1,2), (2,3), (3,4), (4,1). Plus extra (1,2) so edges incident to 1: (1,2) from cycle, extra (1,2), and (4,1) => 3 edges -> odd, so not even. So whole set not included. Need to compute properly: The even subgraph space dimension 2. Bases? Possibly two independent cycles: one fundamental cycle from spanning tree? Actually we can compute. But the number should be 3 non-empty even subgraphs. Let's find them: (a) the 4-edge cycle (set of edges that form the cycle). (b) the two parallel edges 1-2? Actually set of the two parallel edges (1,2) and (1,2) gives deg(1)=2, deg(2)=2, others 0 -> even. So that's a second. (c) maybe the cycle plus both parallel edges? That's 6 edges but we have only 5, can't. Actually we have 5 edges total. The even subgraphs are those where the XOR of cycles yields even. Since dimension 2, there are 4: empty, cycle, double parallel, and cycle XOR double parallel? What is cycle XOR double parallel? Cycle edges: {e1, e2, e3, e4}; double parallel: {p1, p2} (both parallel). XOR yields edges: {e1, e2, e3, e4, p1, p2} but that's 6 edges, not within our set? Wait, XOR of subsets is defined as symmetric difference. But if we take symmetric difference of cycle set and double parallel set, we get edges that are in exactly one of them. Since the double parallel set has two edges between 1 and 2, and the cycle contains one edge between 1 and 2. So the symmetric difference would have: the cycle's (1,2) edge is present, and also the two parallel edges? Actually careful: The sets are disjoint in terms of edge indices. The cycle has one specific edge (say edge index 1) connecting 1-2. The double parallel set includes two other edges (indices 5 and maybe? Actually we have 5 edges total: edges: e1(1-2), e2(2-3), e3(3-4), e4(4-1), e5(1-2) extra. Suppose cycle uses e1,e2,e3,e4. double parallel uses e1? Actually double parallel would need two edges between 1-2. There are only two edges between 1-2 total: e1 and e5. So double parallel = {e1, e5}. Then cycle = {e1,e2,e3,e4}. Symmetric difference = {e2,e3,e4,e5}. That's 4 edges: e2,e3,e4,e5. This is a subset of our 5 edges (contains e5 and the three edges e2,e3,e4). Does that give even degrees? Check: deg(1): edges e5 (1-2) only? Actually e5 is (1,2). e4 is (4,1) present? In symmetric difference, e4 is (4,1) present, yes. So deg(1) = 2 (e5 and e4) even; deg(2): e2 (2-3) and e5 (1-2) => 2 even; deg(3): e2 (2-3), e3 (3-4) =>2 even; deg(4): e3,e4 =>2 even. So it's valid. So that's the third non-empty even subgraph. So indeed 3. Good.
Thus formula holds.
Now the problem is solved.
But before we finalize, ensure that we didn't misinterpret the problem: The problem statement: "Let's consider the ski base as a non-empty set of roads that can be divided into one or more tracks so that exactly one track went along each road of the chosen set. Besides, each track can consist only of roads from the chosen set. Ski base doesn't have to be connected."
Another way: Could there be a case where the set of edges can be partitioned into tracks, but not necessarily every vertex has even degree? Consider a set consisting of a single closed track that goes like 1-2, 2-3, 3-1? That's a triangle, even degrees. But consider a closed track that is not a simple cycle, like a figure-8: two cycles sharing a vertex. That vertex would have degree 4, others degree 2. Still even. So even degree condition holds.
But consider a closed track that traverses edges in a way that visits a vertex multiple times, but overall each time you enter and leave, so degree contributed is 2 each time. So total degree even. So condition holds.
Thus the equivalence stands.
One more nuance: Could a set be partitioned into tracks but not satisfy even degree condition if tracks are allowed to start and end at different vertices? But they must be closed, so start=end. So each track is a closed walk, so degrees from that track are even. Summed across tracks, still even. So necessary.
Sufficiency: If all vertices even degree, can we partition the edge set into edge-disjoint closed trails? Yes, as we can decompose into cycles. However, the definition of track requires that it is a closed route, but does it require that the route is simple? It says "doesn't go on one road more than once, however it can visit any junction any number of times." So it allows non-simple cycles (like a figure-8, which is not a simple cycle because vertex visited multiple times). The decomposition into cycles (simple cycles) yields tracks that satisfy the condition (they are closed, don't reuse edges, may revisit vertices? A simple cycle does not revisit vertices except start/end same, but that's allowed. So yes, any even-degree graph can be decomposed into cycles (simple cycles). So it's sufficient.
Thus equivalence is sound.
Hence solution is DSU.
Now, the problem might be considered too easy? But perhaps there is a twist: The answer can be huge, but we mod. And the exponent could be large? But m <= 1e5, so fine.
But wait: In the problem constraints, m up to 1e5. But what about n up to 1e5. So exponent <= 1e5. So pow2 precomputation works.
Thus final code.
But there's a nuance: The DSU must handle 1-indexed vertices. We'll implement as arrays.
Now, is there any need to use long long? Use int with mod.
Now finalize.
But let's double-check the modulo: 1000000009 (1e9+9). It's prime. Fine.
Implementation details:
Use vector parent(n+1), size(n+1,1). Initialize for i 1..n: parent[i]=i.
find with path compression.
union: if ru != rv: if size[ru] < size[rv]: swap; parent[rv] = ru; size[ru] += size[rv].
Now loop over edges: read a,b; e++; if (find(a) != find(b)) { comp--; union(a,b); } exponent = e - n + comp; ans = (pow2[exponent] - 1 + MOD) % MOD; print ans.
But ensure exponent is within [0,m]; we have pow2 array of size m+1, exponent could be m exactly? If e=m, comp=1, n maybe 2, exponent = m - 2 +1 = m -1, not m. If n=1? But n>=2, so exponent <= m-1? Actually if n=2, m=100000, comp=1, exponent = 100000 -2 +1 = 99999. So max exponent = m - 1? But what if n=100000, m=100000, comp might be >1? The maximum exponent could be when n is minimal? But since n >=2, m <= 1e5, exponent maximum is m - n + comp. comp can be at most n, but if comp = n, then e likely small. To get exponent = m, we would need m - n + comp = m => -n + comp = 0 => comp = n. But if comp = n, then no edges have been added, but e=m? That's impossible because when e=m>0, comp < n unless all edges are loops? But loops not allowed. So exponent cannot reach m. But it's safe to have pow2 up to m anyway; maybe exponent could be m if n=1? But n>=2. So it's safe.
But just in case, we can allocate pow2 up to m (or maybe m+5) and compute exponent; if exponent > m (unlikely) we can compute via fast exponentiation; but not needed.
Better to precompute up to m because exponent can be at most m. Actually consider n=2, m=1e5, comp=1, exponent = 1e5 -2 +1 = 99999. So exponent <= m - 1. For n=3, exponent max = m - 3 + 1 = m-2. So as n increases, exponent decreases. So exponent <= m - 1. But if n = 100000, m = 100000, then to have exponent high, comp must be as large as possible? Actually exponent = m - n + comp. Since comp <= n, max exponent <= m - n + n = m. But to achieve m, we need comp = n and m - n + n = m, but comp = n means no merges, which implies all edges are within the same component? Actually if comp = n, then no two vertices from different components have been merged. However, edges could be added between vertices in the same component without merging, but then comp remains n. But if we start with n isolated vertices, and we add an edge between two vertices, that merges them, comp becomes n-1, not n. So after adding any edge, comp decreases unless the edge connects two vertices already in same component (which can't happen initially). So after first edge, comp < n. So for e>=1, comp <= n-1. Then exponent = e - n + comp <= e - n + (n-1) = e - 1. Since e <= m, exponent <= m - 1. So exponent is at most m-1. So pow2 up to m-1 is safe. But we can allocate m+1 anyway.
Thus all good.
Now write code.
But there is one more nuance: The problem might be tricky: The number of even subgraphs is 2^{m - n + c} only if we consider the graph as a whole. However, that counts all subsets of edges that satisfy even degree condition, including empty. That's what we need.
Now, I should verify with a more complicated example to be sure.
Consider graph with two components: one is a tree (no cycles) and another is a cycle. Suppose after some edges, we have component A with vertices, edges: a tree with 3 vertices and 2 edges (path), component B with a triangle (3 vertices, 3 edges). Total n=6, m=5, components? Tree component: vertices 3, edges 2, not connected? Actually tree is connected, so overall components: tree component (3 vertices) and triangle component (3 vertices) => c=2. Then exponent = 5 - 6 + 2 = 1. So 2^1 -1 =1. Which even subgraph exists? The only non-empty even subgraph is the triangle (cycle) itself. The tree part has no even subgraph except empty. So total 1. That matches.
Now consider two cycles disconnected: n=6, two triangles, m=6, c=2, exponent = 6-6+2=2 => 3 non-empty even subgraphs: each triangle individually (2), and both triangles together (1) => total 3. Indeed.
Thus correct.
Now, the problem seems solved.
However, reading the note in problem statement: The sample after building all 4 roads, they gave three ways. That matches.
Now, is there any chance that the answer could be something else? For example, some subsets might be counted multiple times if there are multiple ways to partition into tracks? But the definition says "can be divided into one or more tracks". So as long as there exists at least one division, it counts as a ski base. We don't care about number of ways to divide. So each subset is counted once. So it's exactly the set of subsets satisfying condition. So our counting is correct.
Thus the solution is straightforward.
But wait: There might be a nuance about the definition of "track": It is a route with properties: closed, contains at least one road, doesn't go on one road more than once, but can visit any junction any number of times. This is a closed trail. But does it allow the trail to use a road in one direction only? Since roads are bidirectional, that's fine.
Now, the equivalence to even-degree subgraphs is standard.
So final answer: DSU + precomputation.
Now I'll write the solution.
But also note: Since m up to 1e5, we can also compute answer on the fly without precomputation by maintaining exponent and multiplying by 2 when exponent increases, etc. But precomputation is simpler.
Now, confirm modulo: (pow2[exponent] - 1) may be negative if pow2[exponent] is 0 mod MOD? But pow2[exponent] is never 0 mod MOD because MOD is prime and 2 is invertible, so pow2[exponent] is non-zero. So subtract 1 yields non-negative? Actually if pow2[exponent] = 0? Not possible. So it's safe to compute (pow2[exponent] - 1 + MOD) % MOD.
Now, code.
I'll implement DSU with path compression and union by size.
Potential pitfalls: Input reading speed for large input? Use scanf or fast I/O.
Now produce final answer.
But before finalize, re-evaluate: Is there any scenario where even-degree condition is not sufficient due to requirement that each track must be a closed route that contains at least one road? If the subset is empty, it's excluded. But our formula subtracts 1, so fine.
If the subset consists of a disjoint union of cycles, we can assign each cycle as a track, that's valid.
But consider a subset that is a single vertex with two self-loops? Not allowed because no self-loops.
Consider a subset that is a single edge with both ends same vertex (loop). That's not allowed as ai != bi. So loops not present.
Thus fine.
Now confirm using a random test: n=5, m=... we can simulate.
Thus solution is correct.
Now provide final code.
But wait: There is an alternative approach: Count the number of independent cycles (cyclomatic number) and answer = 2^{cycles} - 1. Because m - n + c is exactly the cyclomatic number (also known as circuit rank). So indeed we are counting the number of independent cycles. So after each edge, we track the number of independent cycles.
Thus we can think of it as adding edges: if edge connects two vertices in same component, it creates a new independent cycle, increasing the cyclomatic number by 1; if it connects different components, it doesn't create a cycle but reduces components, so the cyclomatic number (m - n + c) remains the same? Actually if edge connects different components: m increases by 1, c decreases by 1 => (m - n + c) increases by 0. If edge connects same component: m increases by 1, c unchanged => (m - n + c) increases by 1. So exponent = number of edges added that close a cycle. So answer is 2^{#cycle-creating edges} - 1. That's also a known interpretation. So we could simply count number of times we add an edge within the same component. However, careful: Initially components = n. When we add an edge between two vertices in different components, we merge them, component count decreases by 1. So (m - n + c) stays same? Let's compute: before adding edge, we have m, c. After adding, m' = m+1. If we merge, c' = c-1. So change = (m+1 - n + (c-1)) - (m - n + c) = 0. If we don't merge (same component), c' = c, so change = 1. So exponent equals the count of edges that are added within already connected components. So answer = 2^{# such edges} - 1. That is also evident from the sample: first two edges connect different components (no cycle), third edge creates first cycle (exponent becomes 1, answer 1), fourth edge is also within same component (exponent becomes 2, answer 3). So we could compute by simply counting how many edges so far have been added that connect vertices already in same DSU set. That's even simpler: we don't need to compute exponent = edges - n + comp; we can maintain a variable cycles = 0, and each time we add edge, if find(u) == find(v) then cycles++; else union. Then answer = (2^{cycles} - 1) mod MOD. But is that always equivalent? Let's verify: exponent = m - n + c. Initially before any edges, m=0, c=n => exponent=0. After each edge, if edge connects different components, m increases by 1, c decreases by 1 => exponent unchanged. If edge connects same component, m increases by 1, c unchanged => exponent increases by 1. So indeed exponent equals number of edges that were added within same component. But careful: After merging components, subsequent edges that connect vertices from the same component (which now includes many vertices) will count as cycle-creating. So cycles variable exactly counts number of such edges. So answer = 2^{cycles} - 1. Let's test with sample: edges: first (1,3): different, cycles=0; second (2,3): different (after first union, 2 and 3 are in different? Actually after first, {1,3} and {2}. second connects 2 and 3, which are in different sets => cycles remains 0; third (1,2): now all in one set, same component => cycles=1; fourth (1,2): same component => cycles=2. So answer after third: 2^1-1=1; after fourth: 2^2-1=3. Works.
Test earlier: n=2, m=3 all parallel: first edge: different => cycles=0, answer0; second: now both vertices same set (since first union), so same component => cycles=1, answer1; third: same component => cycles=2, answer 3. That matches.
Thus we can simplify: just count number of edges that create cycles (i.e., connecting vertices already in same component). Then answer = (2^{cycles} - 1) mod MOD.
But is this always equivalent to exponent = m - n + c? Let's examine: Initially cycles = 0, exponent = 0. When we add an edge that merges components, cycles unchanged, exponent unchanged. When we add edge inside same component, cycles increases by 1, exponent increases by 1. So they stay equal throughout. So yes, cycles = exponent.
Thus we don't even need to compute m and c. Just maintain a counter cycles. Also DSU to determine if edge connects same set. This avoids needing to compute n in exponent. However, we need to ensure that after each edge, cycles = m - n + c. Let's derive: Initially, cycles=0. After each merge edge, cycles unchanged; after each cycle edge, cycles++. At any point, number of merge edges = number of times we union different components. Since we start with n components, after processing t edges, we have performed some number of successful unions (merges). Let merges = number of edges that connected different components. Then cycles = t - merges. Also components c = n - merges (since each merge reduces components by 1). So exponent = t - n + c = t - n + (n - merges) = t - merges = cycles. So indeed.
Thus we can just use cycles counter.
Therefore solution becomes: DSU, cycles = 0, for each edge: if find(u) == find(v): cycles++; else union(u,v); answer = (pow2[cycles] - 1) % MOD.
But careful: After union, we still need to union. So code simpler.
Check with sample: cycles after 1: 0, after2:0, after3:1, after4:2. Yes.
Now, precompute pow2 up to m because cycles <= m (since each edge may be a cycle edge). Actually cycles <= m, but cycles could be m if all edges are cycle edges? Initially components: n; first edge? If first edge connects two vertices, they are in different components initially, so it cannot be a cycle edge. So at most m - (n-1) cycle edges? Because to create cycles, we need to have at least n-1 edges to connect all vertices? Actually we can have multiple cycles within same component after it's connected. The maximum cycles = m - (n - c_final). But c_final could be >1 if graph remains disconnected. For maximum cycles, we want as many edges as possible to be within same components. In worst case, we could have a graph that becomes connected after n-1 edges, then the remaining m - (n-1) edges are all within that component, so cycles = m - (n-1). Since n up to 1e5, m up to 1e5, cycles could be up to m (if n=2, then first edge merges, so that edge is not a cycle, but cycles count for remaining edges: first edge: merge (not cycle), second edge: cycle, so cycles = m-1. So max cycles = m-1 (if n>=2). Actually if n=2, m=1e5, then first edge is merge, cycles=0; subsequent edges are cycles => cycles = m-1 = 99999. So cycles <= m-1. But we can precompute up to m to be safe.
Thus we can precompute pow2 up to m.
Now answer for each step is based on current cycles count. However, note: after an edge that merges components, cycles does not change, but answer may change? Actually answer = 2^{cycles} - 1. If cycles doesn't change, answer remains same as previous? Let's test: after first edge (merge) cycles=0 => ans=0. after second edge (merge) cycles=0 => ans=0. after third edge (cycle) cycles=1 => ans=1. after fourth edge (cycle) cycles=2 => ans=3. So yes, answer stays same if cycles unchanged. That matches output: first two lines both 0.
So this is simpler.
Now, but we need to be careful: The DSU should also update component sizes maybe for union by size. But not needed for cycle detection. So we can just do DSU.
Thus final algorithm:
Read n, m.
DSU init.
cycles = 0.
Precompute pow2[0..m] modulo MOD.
For each i in 1..m:
read u, v.
if find(u) == find(v):
cycles++;
else:
union(u, v);
ans = (pow2[cycles] - 1) % MOD;
print ans.
That's it.
Double-check with sample: correct.
Now test with another scenario: n=4, edges: (1,2) -> cycles? find(1)!=find(2) => union, cycles=0, ans=0. (2,3) -> find diff => union, cycles=0, ans=0. (3,4) -> diff? After two unions: sets? Let's compute: after first: {1,2}, {3}, {4}. after second: union 2 and 3 -> set {1,2,3}, {4}. Third: edge (3,4): find(3) set {1,2,3}, find(4) set {4} => different => union, cycles=0, ans=0. (1,4) -> now all in one set, same component => cycles=1, ans=1. (1,4) again (parallel) -> same component => cycles=2, ans=3. So answers: 0,0,0,1,3. Check if formula with cycles works: cycles after first=0, second=0, third=0, fourth=1, fifth=2. So answers accordingly. Let's also compute using exponent: after 4 edges, m=4, n=4, comp=1, exponent=1 => ans=1; after 5 edges, exponent=2 => ans=3. Good.
Thus we can just track cycles.
Now, note that we must compute pow2[cycles] quickly. cycles increments each time we see an edge within same component. So precomputation up to m.
Now, is there any scenario where cycles might be large but answer needs modulo? Yes.
Now, one more nuance: The answer is printed after each edge. So we compute per edge.
Thus solution is O(m α(n)).
Now, we should also consider if there is any hidden condition about the graph having multiple edges and the DSU union operation. For union when vertices are in different sets, we merge. That's fine.
Now, let's implement.
Potential issues:
Input reading: We need to handle up to 1e5 lines, so scanf or fast I/O fine.
DSU: parent array of size n+1, maybe also size array for union by size. Could also do without size, just random linking.
pow2 array: use long long or int.
Now, code.
But wait: The problem might have a twist: The government asks after each new road, the number of variants of choosing a ski base based on some subset of the already built roads. That is exactly as we did.
Now, verify with edge cases:
n=100000, m=100000, edges all connecting vertex 1 and 2 (parallel). First edge: union, cycles=0, ans=0. Subsequent edges: each is same component => cycles increments. So after 100k edges, cycles = 99999. ans = 2^{99999} - 1 mod MOD. That is fine. However, we need to precompute pow2 up to 100k. That's fine.
n=100000, m=1, only edge between two random vertices: cycles=0, ans=0. Good.
Now, let's double-check the modulo: 1e9+9. Use #define MOD 1000000009.
Now, final code in C++:
code
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000009;
const int MAXM = 100000;
vector<int> parent, sz;
int find(int x) {
while (parent[x] != x) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
void unite(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() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
parent.resize(n+1);
sz.resize(n+1, 1);
for (int i = 1; i <= n; ++i) parent[i] = i;
vector<long long> pow2(m+1);
pow2[0] = 1;
for (int i = 1; i <= m; ++i) {
pow2[i] = (pow2[i-1] * 2) % MOD;
}
int cycles = 0;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
if (find(u) == find(v)) {
cycles++;
} else {
unite(u, v);
}
long long ans = (pow2[cycles] - 1 + MOD) % MOD;
cout << ans << "\n";
}
return 0;
}
Will this work? Let's test with sample.
Now, check if any issue: In DSU find function, I wrote while loop with path compression. Might be fine.
Alternatively, use recursive find.
Now, ensure that when cycles increments, pow2[cycles] is within array; cycles can be up to m, but cycles start at 0, after m edges could be m? As argued, cycles <= m-1 for n>=2? If n=2, first edge merges, so cycles max = m-1, so cycles <= m-1, but we allocated pow2 up to m, so safe. In case n=1? But n>=2 per constraints. So fine.
Edge case: m=1, cycles=0 => pow2[0]=1, ans=0.
Now, the solution is extremely simple.
But I'd like to double-check the problem statement: "Track is the route with the following properties: The route is closed, that is, it begins and ends in one and the same junction. The route contains at least one road. The route doesn't go on one road more than once, however it can visit any junction any number of times." So yes.
Now, there is a subtlety: Could a track be something like a single vertex with a loop? That would be a road from a junction to itself, but not allowed. So ignore.
Thus solution stands.
Now, we must output after each road. So we do.
Now, confirm with the sample note: They have images but we don't need.
Thus final answer.
But before finalizing, consider alternative scenario: Could a set of edges that is even-degree be partitioned into tracks in a way that each track is a single edge? No, single edge cannot be closed. So the decomposition must be into closed trails. But if the set consists of two parallel edges between same vertices, we can make one track that goes from u to v on first edge and back on second edge, that's a closed trail of length 2, using each edge exactly once. So it's valid. For a set consisting of a single cycle, we can make one track. For a set consisting of two disjoint cycles, we can make two tracks. For a set consisting of a figure-8 (two cycles sharing a vertex), we could either make one track that traverses both cycles (like go around first cycle, then second) which would be a closed trail using all edges exactly once (possible because vertex degree 4, we can do an Eulerian circuit). So it's valid. So indeed every even subgraph can be covered by a set of closed trails.
Now, is there any even subgraph that cannot be covered by a set of closed trails? For example, an empty set is excluded. A set that is a union of cycles but some vertex of degree >2? Eulerian trail exists for each component as long as all degrees even, we can find an Eulerian circuit covering all edges of that component. That circuit is a closed trail that uses each edge exactly once. So we could have one track per component. That satisfies the condition. So indeed any even subgraph can be partitioned into tracks (one per component). So it's equivalent.
Thus the counting is correct.
Therefore, we can deliver solution.
Now, I'll produce final answer as code.
But given the problem might have expected the formula using DSU, it's fine.
Now, I think it's solved.
However, let's check the constraints again: 2 ≤ n ≤ 10^5, 1 ≤ m ≤ 10^5. Good.
Now, about modulo: 1000000009. Precompute powers mod.
Now, final code.
One more test: n=5, m=7 edges: (1,2), (2,3), (3,1) (cycle), (4,5), (4,5) (parallel), (4,5) (another), (1,4). Let's compute step by step manually:
after 1: cycles 0 (merge), ans0
after 2: (2,3): find(2) and find(3) different? After first union, set {1,2}, {3}, {4}, {5}. (2,3) merges => cycles 0, ans0
after 3: (3,1): now all of 1,2,3 in same set? Actually after 2: union 2 and 3 => set {1,2,3}, {4}, {5}. Edge (3,1) connects vertices in same set => cycles=1, ans=1
after 4: (4,5): different sets => union, cycles=1, ans=1
after 5: (4,5): now 4 and 5 same set => cycles=2, ans=3
after 6: (4,5) again: same set => cycles=3, ans=7 (2^3-1=7)
after 7: (1,4): now 1 in set {1,2,3} and 4 in set {4,5}? Actually after edge5 we had set {4,5} and {1,2,3} separate. So edge (1,4) connects different sets => union, cycles remains 3, ans=7.
Let's compute via formula exponent = m - n + c at each step:
Initially: n=5.
After 1: m=1, c=4? Actually after first union, c=4. exponent=1-5+4=0.
After 2: m=2, c=3 => exponent=0.
After 3: m=3, c=3? Actually after third edge, it's cycle within component, so c remains 3 (since we didn't merge). So exponent=3-5+3=1.
After 4: m=4, now merge {4} and {5} => c becomes 2? Wait before edge4, sets: {1,2,3}, {4}, {5} => c=3. After union 4 and 5, c=2. So exponent=4-5+2=1.
After 5: m=5, c=2 (since edge5 connects 4-5 same set, no merge) => exponent=5-5+2=2.
After 6: m=6, c=2 => exponent=6-5+2=3.
After 7: m=7, now edge7 merges two sets ( {1,2,3} and {4,5} ), c becomes 1 => exponent=7-5+1=3.
So exponent matches cycles count. Good.
Now, answer after 6: 2^3-1=7. after 7: 7. So it's fine.
Thus solution holds.
Now, I think we can produce final answer.
But wait: Is there any trick with modulo? We subtract 1, add MOD to ensure positivity.
Now, final code.
Potential improvements: Could use iterative find.
Now, I'll output the code as solution.
But let's consider if there is any case where cycles variable overflow? It's int up to 1e5.
Now, final.
Double-check: The problem might have originally required modulo 1000000009 (which is 10^9+9). Yes.
Now, finalize.
But I remember there was a Codeforces problem called "Ski Base" with same sample and everything. In editorial, they also used DSU and counted number of edges that create cycles.
Thus I'm confident.
One more nuance: The problem asks for "the number of variants of choosing a ski base based on some subset of the already built roads." That's exactly the number of non-empty subsets of edges that can be partitioned into tracks. So our answer.
Now, I'll write final answer.
But there is a subtle detail: The definition of track: "doesn't go on one road more than once, however it can visit any junction any number of times." This is essentially an Eulerian circuit. But we must ensure that if we have a component that is Eulerian, we can indeed produce a track that uses each edge exactly once. That's true because an Eulerian circuit exists for each connected component with all vertices even degree. However, there is a nuance: An Eulerian circuit requires that the graph is connected (the component). But our component is connected by definition (since it's a connected component). So yes, we can have one track per component. But what if the component is not connected? But component is connected by definition. So fine.
Thus final.
Now, I'll produce the final code.
But before that, I want to think about if any edge case violates: Could there be a subgraph that is even but not a union of cycles? Actually any even graph can be decomposed into cycles, as known. So fine.
Now, final answer.
But wait: Is the graph considered as simple or multigraph? Multigraph allowed. The cycle decomposition works for multigraphs as well.
Thus final.
Now, I'll implement in C++.
Note: I need to use long long for pow2 to avoid overflow when multiplying, but mod operations keep it within range.
Now, final code. ```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000009;
const int MAXM = 100000;
vector parent, sz;
int find(int x) {
while (parent[x] != x) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
void unite(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() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int n, m;
cin >> n >> m;
parent.resize(n + 1);
sz.assign(n + 1, 1);
for (int i = 1; i <= n; ++i) parent[i] = i;
// precompute powers of two modulo MOD
vector<long long> pow2(m + 1);
pow2[0] = 1;
for (int i = 1; i <= m; ++i) {
pow2[i] = (pow2[i - 1] * 2) % MOD;
}
int cycles = 0;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
if (find(u) == find(v)) {
++cycles;
} else {
unite(u, v);
}
long long ans = (pow2[cycles] - 1 + MOD) % MOD;
cout << ans << '\n';
}
return 0;