[NOIP simulation test]: Star Trek (Euler Road)

Topic Portal (internal question 4)


Input Format

The first line of two integers n, m, and represents the number of planets wormhole.
Next m lines of two integers u, v, indicating the presence of a bidirectional direct connection wormhole u and v.
Each wormhole will be described more than once.


Output Format

Line an integer representing the number of unused routes nature.


Sample

Sample input

5 4
1 2
1 3
1 4
1 5

Sample Output

6


Data range and tips

Sample explained:
essentially different routes 6:
2-1-3-1-4-1-5
2-1-3-4-5-1-4
2-1-4-1-5-1- 3
3-1-2-1-5-1-4
3-1-2-1-4-1-5
4-1-2-1-3-1-5
attention 2-1-4-1-3 another -1-5 not essentially different routes, it is first routes essentially the same.

Restriction and conventions:
For 10% of the data, $ n, m \ leqslant 5 $.
For 20% of the data, $ n, m \ leqslant 10 $.
For 40% of the data, $ n, m \ leqslant 100 $.
For 60% of the data, $ n, m \ leqslant 1000 $.
For all data, $ 1 \ leqslant n, m \ leqslant {10} ^ 5,1 \ leqslant u, v \ leqslant n $.


answer

All split into two sides, the question becomes to delete two sides, so that the rest is a Eulerian path or FIG Euler circuit.

It is divided into three cases:

  1. Deletion from two rings: Deletion from two rings which have two points of -2 degrees, a Euler be assumed that such a point has $ $ SUM, the program number $ C_ {sum } ^ 2 $.

  2. Deletion of one side and a loopback: loopback puncturing has been described above, consider the edge deletion, deleting an edge point of the two degree of -1, two odd dot becomes, as a Eulerian path, program number $ C_ {sum} ^ 2 \ times (m-sum) $.

  3. Deletion two sides have a common point: the point of -2 degrees, the degree of the other two points are respectively connected to two sides of -1 as a Eulerian path, the program number $ \ sum \ limits_ {i = 1} ^ n C_ {d [i]} ^ 2 $.

The total number of programs is: $ C_ {sum} ^ 2 + C_ {sum} ^ 2 \ times (m-sum) + \ sum \ limits_ {i = 1} ^ n C_ {d [i]} ^ 2 $ .

As for the calculation process, obviously the above calculation method is not convenient, then we consider converting some ideas calculation.

The case 1 and case 2 and up from the first loop can be combined with the remaining sides, the program number m-1, the second embodiment of the self-loop contribution number m-2, according to the difference in the number of medium-high school textbooks Summation formula available number scheme: $ \ frac {(2 \ times m-sum-1) \ times sum} {2} $.

Program Number 3 is the case where: $ \ frac {\ sum \ limits_ {i = 1} ^ nd [i] \ times (d [i] -1)} {2} $, can be obtained while scavenging.

Note that FIG determined as not communication, not a point is not connected, but the edge is not in communication, and the difference can be set using the judgment program number is not 0 Unicom.


Code time

#include<bits/stdc++.h>
using namespace std;
int n,m;
int rd_geq[100001],rd_leq[100001],f[100001];
long long ans,sum;
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
void connect(int x,int y){f[find(x)]=find(y);}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)f[i]=i;
	for(int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		rd_leq[x]++;//所有的度
		rd_leq[y]++;
		if(x==y)sum++;//统计自环
		else
		{
			rd_geq[x]++;//非自环的度
			rd_geq[y]++;
			connect(x,y);
		}
	}
	int flag=0;
	for(int i=1;i<=n;i++)
		if(rd_leq[i])
		{
			flag=i;
			break;
		}
	for(int i=1;i<=n;i++)
	{
		if(rd_leq[i]&&find(i)!=f[flag])
		{
			puts("0");
			return 0;
		}
		ans+=1LL*(rd_geq[i]-1)*rd_geq[i]/2;//第3种情况
	}
	ans+=sum*(m-sum)+sum*(sum-1)/2;//第1种和第2种情况
	printf("%lld",ans);
	return 0;
}

rp++

Guess you like

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