[CSP-S Simulation Test]: (diameter of the bipartite graph of FIG. +) Simple operation

Title Description

Once there was a $ points comprising n-$, $ m $ edges, since no free edge of the ring and a weight of FIG.
For two points $ u not directly connected to the edge, v $, you can merge them. Specifically, you can delete the $ u, v $ and all sides to them as endpoints, then add a new point $ x $, it will be with all original with u and v have even point directly connected to the side edges.
You need to judge whether a merge operation such that the picture can be a number of times one strand, if you need to claim maximum length of this chain.


Input Format

Read data from files $ merge.in $ in.
The first line of two positive integers $ n, m $, showing the points and edges FIG.
Subsequently m rows, each row two positive integers $ u, v $, expressed between U $ $ a $ and $ V undirected edges.


Output Format

Output to a file in $ merge.out $.
If the merge operation such that the picture can be several times one strand, the maximum length of the chain output, otherwise the output $ $ -1.


Sample

Sample input 1:

5 4
1 2
2 3
3 4
3 5

Sample output 1:

3

Sample input 2:

4 6
1 2
2 3
1 3
3 4
2 4
1 4

Sample Output 2:

-1


Data range and tips

For $ 30 \% $ data, $ n <10 $
to $ 70 \% $ data, $ n <2,000 $
for $ 100 \% $ data, $ n \ leqslant 1,000, m \ leqslant 10 ^ 5 $


answer

FIG painting will find, if it occurs a certain odd ring no solution; FIG diameter is the longest chain, the answer is that every link block and diameter.

Staining bipartite graph can then seek a diameter enough FIG.

Time complexity: $ \ Theta (n ^ 2) $.

期望得分:$100$分。

实际得分:$100$分。


代码时刻

#include<bits/stdc++.h>
using namespace std;
int n,m;
struct rec{int nxt,to;}e[200001];
int head[1001],cnt,tot;
int col[1001],bel[1001],len[1001];
int dis[1001];
bool vis[1001];
int ans;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;
void add(int x,int y)
{
	e[++cnt].nxt=head[x];
	e[cnt].to=y;
	head[x]=cnt;
}
void dfs(int x,int c,int p)
{
	col[x]=c;bel[x]=p;
	for(int i=head[x];i;i=e[i].nxt)
	{
		if(!col[e[i].to])dfs(e[i].to,-c,p);
		if(col[x]==col[e[i].to]){puts("-1");exit(0);}
	}
}
int Dij(int x)
{
	int res=0;
	memset(dis,0x3f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	dis[x]=0;
	q.push(make_pair(0,x));
	while(!q.empty())
	{
		int flag=q.top().second;
		q.pop();
		if(vis[flag])continue;
		vis[flag]=1;
		res=max(res,dis[flag]);
		for(int i=head[flag];i;i=e[i].nxt)
			if(dis[e[i].to]>dis[flag]+1)
			{
				dis[e[i].to]=dis[flag]+1;
				q.push(make_pair(dis[e[i].to],e[i].to));
			}
	}
	return res;
}
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(!col[i])dfs(i,1,++tot);
	for(int i=1;i<=n;i++)
		len[bel[i]]=max(len[bel[i]],Dij(i));
	for(int i=1;i<=tot;i++)
		ans+=len[i];
	printf("%d",ans);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11670807.html