为什么开了O2才能过(不吸氧全wa)
查看原帖
为什么开了O2才能过(不吸氧全wa)
913131
Skicyer楼主2023/7/11 17:23
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e3+5;
const int maxm=1e5+5;
int n,m;
int fst[maxn],nxt[maxm<<1];
struct E{
	int f,to;
}e[maxm<<1];
int cnt,tot,low[maxn],dfn[maxn];
void add(int x,int y){
	nxt[++cnt]=fst[x];
	fst[x]=cnt;
	e[cnt].to=y;
	e[cnt].f=x;
}
int co[maxn],col;
stack<int>s;
void tarjan(int x,int fa){
	dfn[x]=low[x]=++tot;
	s.push(x);
	for(int i=fst[x];i;i=nxt[i]){
		int to=e[i].to;
		if((fa+1)/2==(i+1)/2) continue;
		if(!dfn[to]){
			tarjan(to,i);
			low[x]=min(low[x],low[to]);
		}
		else low[x]=min(low[x],dfn[to]);
	}
	if(dfn[x]==low[x]){
		col++;
		while(1){
			co[s.top()]=col;
			if(s.top()==x) break;
			s.pop();
		}
		s.pop();
	}
}
int in[maxn];
int main() {
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	for(int i=1;i<=n;i++){
		if(!dfn[i]) tarjan(i,-1);
	}
	for(int i=1;i<=cnt;i+=2){
		if(co[e[i].f]!=co[e[i].to]) {
			in[co[e[i].f]]++;
			in[co[e[i].to]]++;
		}
	}
	int ans;
	for(int i=1;i<=col;i++){
		if(in[i]==1) ans++;
	}
	if((ans+1)/2==1) cout<<0<<endl;
	else cout<<(ans+1)/2<<endl;
}
2023/7/11 17:23
加载中...