#include<bits/stdc++.h>
using namespace std;
const int N = 505, M = 1e4+5;
int par[2][M][N], sz[2][M][N]; // for prefix
int root(int v, int i, bool j)
{
if(par[j][i][v]==-1) return v;
return par[j][i][v] = root(par[j][i][v], i, j);
}
inline void merge(int u, int v, int i, bool j)
{
u = root(u, i, j);
v = root(v, i, j);
if(v==u)
return;
if(sz[j][i][u] > sz[j][i][v])
swap(u, v);
par[j][i][u] = v;
sz[j][i][v] += sz[j][i][u];
}
int main()
{
int n, m;
cin >> n >> m;
for(int i=0;i<n;i++)
{
sz[1][m][i] = sz[0][0][i] = 1;
par[1][m][i] = par[0][0][i] = -1;
}
vector<pair<int,int> > e;
for(int i=0;i<m;i++)
{
int u, v;
cin >> u >> v;
u--;v--;
for(int j=0;j<n;j++)
{
sz[0][i+1][j] = sz[0][i][j];
par[0][i+1][j] = par[0][i][j];
}
merge(u, v, i+1, 0);
e.push_back({u, v});
}
while(e.size())
{
int u = e.back().first, v = e.back().second;
e.pop_back();
int j = e.size();
for(int i=0;i<n;i++)
{
sz[1][j][i] = sz[1][j+1][i];
par[1][j][i] = par[1][j+1][i];
}
merge(u, v, j, 1);
}
int k;
cin >> k;
for(int i=0;i<k;i++)
{
int l, r;
cin >> l >> r;
int cnt = 0;
// use par[0][M-1][N-1] and sz[0][M-1][N-1]
for(int i=0;i<n;i++)
{
par[0][M-1][i] = -1;
sz[0][M-1][i] = 1;
}
for(int i=0;i<n;i++)
{
if(par[0][l-1][i]!=-1)
merge(i, par[0][l-1][i], M-1, 0);
if(par[1][r][i]!=-1)
merge(i, par[1][r][i], M-1, 0);
}
for(int i=0;i<n;i++)
cnt += (par[0][M-1][i]==-1);
cout << cnt << endl;
}
return 0;
}
1699A - The Third Three Number Problem | 1617B - GCD Problem |
841A - Generous Kefa | 1690B - Array Decrements |
1692C - Where's the Bishop | 104A - Blackjack |
1438A - Specific Tastes of Andre | 1711C - Color the Picture |
1194C - From S To T | 110B - Lucky String |
1114A - Got Any Grapes | 224B - Array |
125B - Simple XML | 567B - Berland National Library |
431B - Shower Line | 282C - XOR and OR |
1582B - Luntik and Subsequences | 609A - Флеш-карты |
1207A - There Are Two Types Of Burgers | 371C - Hamburgers |
343B - Alternating Current | 758B - Blown Garland |
1681B - Card Trick | 1592A - Gamer Hemose |
493D - Vasya and Chess | 1485A - Add and Divide |
337B - Routine Problem | 1392D - Omkar and Bed Wars |
76E - Points | 762C - Two strings |