加载中…
个人资料
JosiahChiu
JosiahChiu
  • 博客等级:
  • 博客积分:0
  • 博客访问:7,096
  • 关注人气:87
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

CF 91C Ski Base图中任意时刻有多少个环

(2011-10-08 15:59:31)
标签:

cf

91c

多少个环

it

分类: 图论

题目描述:

n个点,m条边,每加一条边,问当前图中有多少个不同的环。

解题报告:

直接上结论:

用并查集维护

如果加边的两点已经是联通的,ans = (ans * 2 + 1) % mod

否则并查集相连即可。

代码如下;
#include<iostream>

#include<cstring>

#include<vector>

#include<algorithm>

#include<cmath>

#include<cstdio>

using namespace std;

#define SIZE 100005

#define mod 1000000009

int fa[SIZE], ans;

int find(int id)

{

    if (fa[id] == -1) return id;

    return fa[id] = find(fa[id]);

}

void un(int a, int b)

{

    int aa = find(a), bb = find(b);

    if (aa == bb) ans = (ans * 2 + 1) % mod;

    else fa[aa] = bb;

}

int main()

{

    int n, m;scanf("%d%d", &n, &m); ans = 0;

    memset(fa, -1, sizeof(fa));

    while(m--)

    {

        int a, b;scanf("%d%d", &a, &b);

        un(a, b);

        printf("%d\n", ans);

    }

         return 0;

}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有