Density of Power Network

The vast power system is the most complicated man-made system and the greatest engineering innovation in the 20th century. The following diagram shows a typical 14 bus power system. In real world, the power system may contains hundreds of buses and thousands of transmission lines.

Network topology analysis had long been a hot topic in the research of power system. And network density is one key index representing the robustness of power system. And you are asked to implement a procedure to calculate the network density of power system.

The network density is defined as the ratio between number of transmission lines and the number of buses. Please note that if two or more transmission lines connecting the same pair of buses, only one would be counted in the topology analysis.

Input

The first line contains a single integer T (T ≤ 1000), indicating there are T cases in total.

Each case begins with two integers N and M (2 ≤ NM ≤ 500) in the first line, representing the number of buses and the number of transmission lines in the power system. Each Bus would be numbered from 1 to N.

The second line contains the list of start bus number of the transmission lines, separated by spaces.

The third line contains the list of corresponding end bus number of the transmission lines, separated by spaces. The end bus number of the transmission lines would not be the same as the start bus number.

Output

Output the network density of the power system in a single line, as defined in above. The answer should round to 3 digits after decimal point.

Sample Input

3
3 2
1 2
2 3
2 2
1 2
2 1
14 20
2 5 3 4 5 4 5 7 9 6 11 12 13 8 9 10 14 11 13 13
1 1 2 2 2 3 4 4 4 5 6 6 6 7 7 9 9 10 12 14

Sample Output

0.667
0.500
1.429

这道题是个水题,但是在今天的训练赛中我看到我队友写的代码后发现了一个新大陆,用make_pair来存,之前学的时候这个方法就只是看了一下,没想那么多,但是这道题用make——pair来写代码量是最少的。

#include<bits/stdc++.h>
using namespace std;
set<pair<int,int> >it;
int x[600],y[600];
/************************/
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m;cin>>n>>m;
        it.clear();
        for(int i=0;i<m;i++)scanf("%d",&x[i]);
        for(int i=0;i<m;i++)scanf("%d",&y[i]);
        //int a,b;
        for(int i=0;i<m;i++){
            if(x[i]<y[i]){
                it.insert(make_pair(x[i],y[i]));
            }else{
                it.insert(make_pair(y[i],x[i]));
            }
        }
        //cout<<it.size()<<endl;
        printf("%.3lf\n",1.0*it.size()/n);
    }
}

make_pair简单来说是把两个变量打包在一起,题目中可能有重边存在,我们就把起点终点打包后丢在set里,自动去重,是不是很巧妙。

还有一种方法,就是把起点和终点放到二维数组的行列中,记录存在与否,如:s[a[i]][b[i]]++

也可以达到去重,遍历一遍s数组就可得出连通边的数目

猜你喜欢

转载自blog.csdn.net/qq_40620465/article/details/82143294