ural1671 Anansi's Cobweb

时间:2022-03-19 17:23:00

Anansi's Cobweb

Time limit: 1.0 second
Memory limit: 64 MB
Usatiy-Polosatiy XIII decided to destroy Anansi's home — his cobweb. The cobweb consists of Nnodes, some of which are connected by threads. Let us say that two nodes belong to the same piece if it is possible to get from one node to the other by threads. Usatiy-Polosatiy has already decided which threads and in what order he would tear and now wants to know the number of pieces in cobweb after each of his actions.

Input

The first line contains integers N and M — the number of nodes and threads in the cobweb, respectively(2 ≤ N ≤ 100000; 1 ≤ M ≤ 100000). Each of the next M lines contains two different integers — the 1-based indices of nodes connected by current thread. The threads are numbered from 1 to M in the order of description. Next line contains an integer Q which denotes the quantity of threads Usatiy-Polosatiy wants to tear (1 ≤ Q ≤ M). The last line contains numbers of these threads — different integers separated by spaces.

Output

Output Q integers — the number of pieces in Anansi's cobweb after each of Usatiy-Polosatiy's action. Separate numbers with single spaces.

Samples

input output
4 4
1 2
2 3
1 3
3 4
3
2 4 3
1 2 3
3 1
1 2
1
1
3

分析:倒着写并查集;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,p[maxn],ans[maxn],query[maxn],coco[maxn];
struct node
{
int x,y;
}a[maxn];
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n)p[i]=i;
rep(i,,m)scanf("%d%d",&a[i].x,&a[i].y);
scanf("%d",&t);
rep(i,,t)scanf("%d",&query[i]),coco[query[i]]=;
ans[t]=n;
rep(i,,m)
{
if(!coco[i])
{
int fa=find(a[i].x),fb=find(a[i].y);
if(fa!=fb)p[fa]=fb,ans[t]--;
}
}
for(i=t-;i>=;i--)
{
int fa=find(a[query[i+]].x),fb=find(a[query[i+]].y);
if(fa!=fb)p[fa]=fb,ans[i]=ans[i+]-;
else ans[i]=ans[i+];
}
printf("%d",ans[]);
rep(i,,t)printf(" %d",ans[i]);
printf("\n");
//system("pause");
return ;
}