Road Construction (Redundant Paths original title)

http://poj.org/problem?id=3352

https://blog.csdn.net/zstuyyyyccccbbbb/article/details/108904709 (link to original title)

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e3+100;
typedef long long LL;
LL dfn[maxn],low[maxn],fa[maxn],times=0,cnt=0,col[maxn],du[maxn];
vector<LL>g[maxn];
stack<LL>s;
bool ma[maxn][maxn],instack[maxn];
void tarjan2(LL x){
	dfn[x]=low[x]=++times;
	s.push(x);instack[x]=true;
	for(LL i=0;i<g[x].size();i++)
	{
		LL to=g[x][i];
		if(!dfn[to])
		{
			fa[to]=x;
			tarjan2(to);
			low[x]=min(low[x],low[to]);
		}
		else if(to!=fa[x]) low[x]=min(low[x],dfn[to]);
	}
	if(low[x]==dfn[x])
	{
		cnt++;
		LL y;
		do{
			y=s.top();
			col[y]=cnt;
			instack[y]=false;
			s.pop();
		}while(y!=x);
		return;
	}
} 
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;
  cin>>n>>m;
  memset(fa,-1,sizeof(fa));
  for(LL i=1;i<=m;i++){
  	LL u,v;cin>>u>>v;
  	if(!ma[u][v]) g[u].push_back(v),g[v].push_back(u);
  	ma[u][v]++,ma[v][u]++;
  }
  for(LL i=1;i<=n;i++) if(!dfn[i]) tarjan2(i);
  for(LL i=1;i<=n;i++){
  	for(LL j=0;j<g[i].size();j++)
  	{
  		if(col[i]!=col[g[i][j]])	du[col[i]]++,du[col[g[i][j]]]++;
	}
  }
  LL sum=0;
  for(LL i=1;i<=cnt;i++){
  	if(du[i]/2==1) sum++;
  }
  cout<<(sum+1)/2<<endl;
return 0;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/108908170