题目地址


#include<cstdio>
#include<iostream>
using namespace std;
const int MAXN=1e5,MAXM=1e6;
struct Edge{
	int from,to,nxt;
}e[MAXM];
int head[MAXN],edgeCnt=1;
void addEdge(int u,int v){
	e[++edgeCnt].from=u;
	e[edgeCnt].to=v;
	e[edgeCnt].nxt=head[u];
	head[u]=edgeCnt;
}
int dfn[MAXN],low[MAXN],dfnCnt=0;
int stck[MAXN],top=0;
int dccCnt=0;
void tarjan(int x,int nowRoot) {
	dfn[x]=low[x]=++dfnCnt;
	stck[++top]=x;
	if(x==nowRoot&&(!head[x])){
		dccCnt++;
		return;
	}
	int flag=0;
	for(int i=head[x];i;i=e[i].nxt){
		int nowV=e[i].to;
		if(!dfn[nowV]){
			tarjan(nowV,nowRoot);
			if(low[nowV]>=dfn[x]){
				flag++;
				dccCnt++;
				int y;
				do{
					y=stck[top--];
				}while(y!=nowV);
			}
			low[x]=min(low[x],low[nowV]);
		}else low[x]=min(low[x],dfn[nowV]);
	}
}
int main() {
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		addEdge(u,v);
		addEdge(v,u);
	}
	for(int i=1;i<=n;i++)
		if(!dfn[i]){
			tarjan(i,i);
		}
	printf("%d\n",dccCnt);
	return 0;
}