强联通分量
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct node{
int to,nxt;
}d[N];int head[N],tot=0;
void add(int a,int b){
d[++tot]={b,head[a]};head[a]=tot;
}
int low[N],dfn[N],Stack[N],belong[N];
int Index,top,scc,num[N];
bool instack[N];
void tarjan(int u){
int v;
low[u]=dfn[u]=++Index;
Stack[top++]=u;
instack[u]=true;
for(int i=head[u];i;i=d[i].nxt){
v=d[i].to;
if(!dfn[v]){
tarjan(v);
if(low[u]>low[v]){
low[u]=low[v];
}
}else if(instack[v]&&low[u]>dfn[v]){
low[u]=low[v];
}
}
if(low[u]==dfn[u]){
scc++;
do{
v=Stack[--top];
instack[v]=false;
belong[v]=scc;num[scc]++;
}while(v!=u);
}
return ;
}
void solve(int n){
memset(dfn,0,sizeof dfn);
memset(instack,false,sizeof instack);
memset(num,0,sizeof num);
Index=scc=top=0;
for(int i=1;i<=n;i++){
if(!dfn[i]) tarjan(i);
}
}
void init(){
tot=0;memset(head,0,sizeof head);
}
求割点
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
struct node{
int to,nxt;
}d[N];int head[N],tot=0;
void add(int a,int b){
d[++tot]={b,head[a]};head[a]=tot;
}
int low[N],dfn[N],Stack[N],belong[N];
int Index,top,scc,num[N];
bool instack[N],vis[N];
void tarjan(int u,int fa){
// cout<<u<<endl;
low[u]=dfn[u]=++Index;
int son=0;
for(int i=head[u];i;i=d[i].nxt){
int to=d[i].to;
if(!dfn[to]){
tarjan(to,fa);
low[u]=min(low[u],low[to]);
if(low[to]>=dfn[u]&&u!=fa){
vis[u]=1;
}
if(u==fa) son++;
}
low[u]=min(low[u],dfn[to]);
}
if(son>=2&&u==fa){
vis[u]=1;
}
return ;
}
void solve(int n){
memset(dfn,0,sizeof dfn);
memset(instack,false,sizeof instack);
memset(num,0,sizeof num);
memset(vis,0,sizeof vis);
Index=scc=top=0;
for(int i=1;i<=n;i++){
if(!dfn[i]) tarjan(i,i);
}
}
void init(){
tot=0;memset(head,0,sizeof head);
}
int n,m;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int a,b;scanf("%d%d",&a,&b);add(a,b);add(b,a);
}
int tot=0;
solve(n);
for(int i=1;i<=n;i++){
if(vis[i]==1) tot++;
}
cout<<tot<<endl;
for(int i=1;i<=n;i++){
if(vis[i]) cout<<i<<" ";
}cout<<endl;
}
点双联通分量
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
struct node{
int to,nxt,val;
}d[N];int head[N],tot=-1;
void add(int a,int b,int val){
d[++tot]={b,head[a],val};head[a]=tot;
}
int low[N],dfn[N],Stack[N],belong[N];
int Index,top,scc,num[N];
bool instack[N],used[N];
stack<int> stk;vector<int> ans;
void tarjan(int u,int fa){
dfn[u]=low[u]=++Index;
for(int i=head[u];~i;i=d[i].nxt){
int v=d[i].to;if(v==fa) continue;
if(used[i]==1) continue;
used[i]=used[i^1]=1;
stk.push(i);
if(!dfn[v]){
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<=low[v]){
set<int> verset;
set<int> edgeset;
verset.insert(u);
int x;
do{
x=stk.top();
stk.pop();
edgeset.insert(d[x].val);
verset.insert(d[x].to);
}while(x!=i);
// cout<<edgeset.size()<<endl;
if(edgeset.size()==verset.size()){
for(auto it:edgeset) ans.push_back((it));
}
}
}else low[u]=min(low[u],dfn[v]);
}
}
void solve(int n){
memset(dfn,0,sizeof dfn);
memset(instack,false,sizeof instack);
memset(num,0,sizeof num);
Index=scc=top=0;
for(int i=1;i<=n;i++){
if(!dfn[i]) tarjan(i,i);
}
}
void init(){
tot=-1;memset(head,-1,sizeof head);
memset(used,0,sizeof used);
}
int n,m;
int main(){
init();
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int a,b;scanf("%d%d",&a,&b);add(a,b,i);add(b,a,i);
}
solve(n);
sort(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++){
cout<<ans[i]<<" ";
}cout<<endl;
}