72分求助
查看原帖
72分求助
141335
qwq2519楼主2021/9/25 00:10
#include<iostream>
#include<cstdio>
#define rep(i,j,k) for(register int i(j);i<=k;++i)
#define drp(i,j,k) for(register int i(j);i>=k;--i)
#define repg(x) for(register int i(G.head[x]);i;i=G.next[i])
#define bug cout<<"~~~~~~~~~~~~~"<<'\n';
#define bugout(x) cout<<x<<'\n';
using std::cin;
using std::cout;
typedef long long lxl;
template<typename T> 
inline T  max( T a, T b) {
	return a > b ? a : b;
}
template<typename T> 
inline T  min( T a, T b) {
	return a < b ? a : b;
}

inline char gt() {
	static char buf[1 << 21], *p1 = buf, *p2 = buf;
	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
template <typename T>
inline void  read(T &x) {
	register char ch = gt();
	x = 0;
	int w(0);
	while(!(ch >= '0' && ch <= '9'))w |= ch == '-', ch = gt();
	while(ch >= '0' && ch <= '9')x = x * 10 + (ch & 15), ch = gt();
	w ? x = ~(x - 1) : x;
}
template <typename T>
inline void out(T x,char cc) {
	if(x < 0) x = -x, putchar('-');
	char ch[20];
	int num(0);
	while(x || !num) ch[++num] = x % 10 + '0', x /= 10;
	while(num) putchar(ch[num--]);
	putchar(cc);
}

const int N=5e3+79;
const int M=1e4+79;
int n,m;
struct graph{
	int head[N],tot=1,next[M<<1],ver[M<<1],from[M<<1];
	inline void add(int a,int b){
		ver[++tot]=b;
		from[tot]=a;
		next[tot]=head[a];
		head[a]=tot;
	}
}G;
int dfn[N],low[N],tim;
bool bridge[N];
inline void tarjan(int x,int edge){
	dfn[x]=low[x]=++tim;
	repg(x){
		int y(G.ver[i]);
		if(!dfn[y]){
			tarjan(y,i);
			low[x]=min(low[x],low[y]);
			if(dfn[x]<low[y]){
				bridge[i]=bridge[i^1]=1;
			}
		}
		else if(i!=edge^1){
			low[x]=min(low[x],dfn[y]);
		}
	}
}

int cnt;
int c[N];
int d[N];
inline void dfs(int x){
	c[x]=cnt;
	repg(x){
		int y(G.ver[i]);
		if(c[y]||bridge[i]) continue;
		dfs(y);
	}
}


int main() {
  read(n);read(m);
  int a,b;
  rep(i,1,m){
  	read(a);read(b);
  	G.add(a,b);
  	G.add(b,a);
  }
  
  rep(i,1,n){
  	if(!dfn[i]){
  		tarjan(i,0);
	  }
  }
//      tarjan(1,0);

  rep(i,1,n){
  	if(!c[i]){
  		cnt++;
  		dfs(i);
	  }
  }
  rep(i,1,m){
  	if(c[G.ver[i*2]]!=c[G.from[i*2]]){
  		d[c[G.ver[i*2]]]++;
  		d[c[G.from[i*2]]]++;//!!!
	  }
  }//Ëõµãºóͳ¼Æ¶È 
  int ans(0);
  rep(i,1,cnt){
  	if(d[i]==1) ans++;
  }
  out((ans+1)/2,'\n');
	return 0;
}
2021/9/25 00:10
加载中...