如何判重边?
查看原帖
如何判重边?
224927
lei_yu楼主2020/6/29 15:31
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<stack>
using namespace std;
int times,n,m,head[1000001],dfn[1000001],low[1000001],cnt,qlt_cnt,qlt[1000001],d[1000001],ans;
stack<int>q;
struct node
{
	int to,next,from;
}a[1000001];
void add_edge(int from,int to)
{
	a[++cnt].to=to;
	a[cnt].next=head[from];
	a[cnt].from=from;
	head[from]=cnt;
}
void tarjan(int u,int fa)
{
	//cout<<"find"<<u<<endl;
	q.push(u);
	dfn[u]=low[u]=++times;
	for(int i=head[u];i;i=a[i].next)
	{
		int v=a[i].to;
		if(a[i].to!=fa)
		{
			if(!dfn[v])
			{
				tarjan(v,u);
				low[u]=min(low[u],low[v]);
			}
			else low[u]=min(low[u],dfn[v]);
		}
	}
	//cout<<"exit"<<u<<" "<<dfn[u]<<" "<<low[u]<<endl;
	if(dfn[u]==low[u])
	{
		qlt_cnt++;
		while(q.top()!=u)
		{
			qlt[q.top()]=qlt_cnt;
			q.pop();
		}
		qlt[q.top()]=qlt_cnt;
		q.pop();	
	}
}
int main()
{
	int x,y;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>x>>y;
		add_edge(x,y);
		add_edge(y,x);
	}
	for(int i=1;i<=n;i++)
	if(!dfn[i])tarjan(i,0);
	for(int i=1;i<=cnt;i+=2)
	{
		int xx=a[i].from;
		int yy=a[i].to;
		if(qlt[xx]!=qlt[yy])
		{
			d[qlt[xx]]++;
			d[qlt[yy]]++;
			//cout<<d[qlt[xx]]<<" "<<d[qlt[yy]]<<endl;
		}
	}
	for(int i=1;i<=qlt_cnt;i++)
	{		
		//cout<<d[qlt[i]]<<" "<<d[qlt[i]]/2<<endl;
		if(d[i]==1)
		{
			ans++;
		}
	}
	cout<<(ans+1)/2;
}

WA 9 如何判重?或者是其他错误?

2020/6/29 15:31
加载中...