2019CCPC秦皇岛赛区 Forest Program Tarjan求点的双联通分量

Tarjan模板题

自诩为图论选手,结果没学Tarjan,打下了新赛季第一铁

第二天:这不是Tarjan点双模板题吗(摔!)

背起这口锅,给队友叩头了

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<stack>
#include<cstring>
#include<set>
#include<iterator>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<cmath>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<iomanip>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int N = 3e5 + 10;
const int M = (5e5 + 10) * 2;
int head[N], Next[M], ver[M];
int tot, top, cnt, num;
vector<int> dcc[N];
void addedge(int x, int y)
{
    ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
    ver[++tot] = x, Next[tot] = head[y], head[y] = tot;
}
int dfn[N], low[N];
int sta[N];
int root;
void tarjan(int x)
{
    dfn[x] = low[x] = ++num;
    sta[++top] = x;
    if (x == root && head[x] == 0)
    {
        dcc[++cnt].push_back(x);
        return;
    }
    for (int i = head[x]; i; i = Next[i])
    {

        int y = ver[i];
        if (!dfn[y])
        {
            tarjan(y);
            low[x] = min(low[x], low[y]);
            if (low[y] >= dfn[x])
            {
                cnt++;
                int z;
                do
                {
                    z = sta[top--];
                    dcc[cnt].push_back(z);
                } while (z != y);
                dcc[cnt].push_back(x);
            }
        }
        else
            low[x] = min(low[x], dfn[y]);
    }
}
ll p[N];
void init()
{
    memset(head, 0, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    //memset(low, 0, sizeof(low));
    for (int i = 1; i <= cnt; i++)
    {
        dcc[i].clear();
    }
    tot = num = cnt = top = 0;
}
int main()
{
    p[0] = 1;
    for (int i = 1; i <= N - 1; i++)
    {
        p[i] = p[i - 1] * 2 % mod;
    }
        init();
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= m; i++)
        {
            int x, y;
            cin >> x >> y;
            addedge(x, y);
        }
        for (int i = 1; i <= n; i++)
        {
            if (!dfn[i])
            {
                top = 0;
                root = i;
                tarjan(i);
            }
        }
        ll ans = 1;
        for (int i = 1; i <= cnt; i++)
        {
            //cout << "size == " << dcc[i].size() << endl;
            if (dcc[i].size() >= 3)
            {
                ans = ans * (p[dcc[i].size()] - 1 + mod) % mod;
                m -= dcc[i].size();
            }
        }
        if (m > 0)
            ans = ans * p[m] % mod;
        cout << ans << endl;
        //printf("%164d\n", ans);
    return 0;
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值