很久沒有更新博客了,主要是時間不允許…
然後昨天覆現了CCPC秦皇島的題目,發現一個點雙的題寫了倆小時…遂學習一下點雙和邊雙和強連通分量。
求割點模板
還是tarjan,分爲兩種情況:
1.選一個節點爲根,從該點進行tarjan,如果這個點有>=2個子樹,那麼一定是割點。
2.否則low[v]>=dfn[u],且u不爲root,則u爲割點。
下面是代碼:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
vector<int> G[maxn];
int low[maxn],dfn[maxn],cut[maxn];
int tot,root,rs,n,m;
void tarjan(int x,int fa)
{
low[x] = dfn[x] = ++tot;
for(int v : G[x])
{
if(!dfn[v])
{
tarjan(v,x);
low[x] = min(low[x],low[v]);
if(low[v]>=dfn[x])
{
if(x!=root) cut[x] = 1;
else rs++;
}
}
else if(v!=fa) low[x] = min(low[x],dfn[v]);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
for(int i=1;i<=n;i++)
{
if(!dfn[i])
{
root = i;
rs = 0;
tarjan(i,i);
if(rs>=2) cut[i] = 1;
}
}
int ans = 0;
for(int i=1;i<=n;i++)
{
if(cut[i]) ans++;
}
printf("%d\n",ans);
for(int i=1;i<=n;i++)
{
if(cut[i]) printf("%d ",i);
}
puts("");
return 0;
}