首页 > Web开发 > 详细

Anansi's Cobweb

时间:2016-08-18 18:24:19      阅读:280      评论:0      收藏:0      [点我收藏+]

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

inputoutput
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+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}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,1,n)p[i]=i;
    rep(i,1,m)scanf("%d%d",&a[i].x,&a[i].y);
    scanf("%d",&t);
    rep(i,1,t)scanf("%d",&query[i]),coco[query[i]]=1;
    ans[t]=n;
    rep(i,1,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-1;i>=1;i--)
    {
        int fa=find(a[query[i+1]].x),fb=find(a[query[i+1]].y);
        if(fa!=fb)p[fa]=fb,ans[i]=ans[i+1]-1;
        else ans[i]=ans[i+1];
    }
    printf("%d",ans[1]);
    rep(i,2,t)printf(" %d",ans[i]);
    printf("\n");
    //system("pause");
    return 0;
}

Anansi's Cobweb

原文:http://www.cnblogs.com/dyzll/p/5784932.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!