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.
InputThe first line contains a single integer T (T ≤ 1000), indicating there areT cases in total.
Each case begins with two integers N and M (2 ≤ N, M ≤ 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 toN.
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.
<h4< dd="">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.
<h4< dd="">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<h4< dd="">
0.667 0.500 1.429
题目大意:每个样例第一行是公交车的数目与路线的数目,第二行是公交车起点,第三行是终点,从a->b与b->a是同一条路线,计算总路线与车辆数目的比值
#include<stdio.h> typedef struct{int a,b;}line; int main() { int T,N,M,i,j,k,flag;line s[510]; scanf("%d",&T); while(T--) { scanf("%d%d",&N,&M); for(i=0;i<M;i++)scanf("%d",&s[i].a); for(i=0;i<M;i++)scanf("%d",&s[i].b); k=-1;flag=0; for(i=0;i<M-1;i++){ for(j=i+1;j<M;j++)//此双重循环用来甄别一个序列中有多少个相同的元素非常有用 { if(s[i].a==s[j].b&&s[i].b==s[j].a){flag++;break;} else if(s[i].a==s[j].a&&s[i].b==s[j].b){flag++;break;} } } if(flag==0)printf("%.3lf\n",(M*1.0)/N); else printf("%.3lf\n",((M-flag)*1.0)/N); } return 0 ; }