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;
}