zoukankan      html  css  js  c++  java
  • ZOJ3710Friends

    Friends

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Alice lives in the country where people like to make friends. The friendship is bidirectional and if any two person have no less than k friends in common, they will become friends in several days. Currently, there are totally n people in the country, and m friendship among them. Assume that any new friendship is made only when they have sufficient friends in common mentioned above, you are to tell how many new friendship are made after a sufficiently long time.

    Input

    There are multiple test cases.

    The first lien of the input contains an integer T (about 100) indicating the number of test cases. Then T cases follow. For each case, the first line contains three integers n, m, k (1 ≤ n ≤ 100, 0 ≤ mn×(n-1)/2, 0 ≤ kn, there will be no duplicated friendship) followed by m lines showing the current friendship. The ith friendship contains two integers ui, vi (0 ≤ ui, vi < n, ui ≠ vi) indicating there is friendship between person ui and vi.

    Note: The edges in test data are generated randomly.

    Output

    For each case, print one line containing the answer.

    Sample Input

    3
    4 4 2
    0 1
    0 2
    1 3
    2 3
    5 5 2
    0 1
    1 2
    2 3
    3 4
    4 0
    5 6 2
    0 1
    1 2
    2 3
    3 4
    4 0
    2 0

    Sample Output

    2
    0
    4
    -----------------------------------------------------------------------------------
    这题意思是有n个人,其中有m对朋友关系,其中不认识的两个人要成为朋友的条件是他们有至少k个共同的朋友,问最后能有多少对新朋友。
    使用矩阵储存两个人之间的关系(1表示朋友关系,0表示不认识),然后遍历整个矩阵,寻找不认识的人的共同朋友,
    并判断他们是否能成为新朋友,若能,则关系改为1,再重新遍历矩阵。
    例如第一组样例可表示为;
    | 0 1 2 3
    -|--------
    0| 0 1 1 0
    1| 1 0 0 1
    2| 1 0 0 1
    3| 0 1 1 0
    ----------------------------------------------------------------------------
    #include<stdio.h>
    int main()
    {
    	int t,n,m,k,a[105][105],x,y,i,j,p,count1,count2;
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%d%d%d",&n,&m,&k);
    		for(i=0;i<n;i++)
    			for(j=0;j<n;j++)
    				a[i][j]=0;
    		for(i=0;i<m;i++)
    		{
    			scanf("%d%d",&x,&y);
    			a[x][y]=a[y][x]=1;
    		}
    		count1=0;
    		for(i=0;i<n;i++)
    		{
    			for(j=0;j<n;j++)
    			{
    				count2=0;
    				if(i!=j&&a[i][j]==0)
    				{
    					for(p=0;p<n;p++)
    					{
    						if(a[i][p]==a[j][p]&&a[i][p]==1)
    							count2++;
    					}
    					if(count2>=k)
    					{
    						a[i][j]=1;
    						a[j][i]=1;
    						count1++;
    						i=-1;
    						break;
    					}
    				}
    			}
    		}
    		printf("%d\n",count1);
    	}
    	return 0;
    }
  • 相关阅读:
    python自省函数getattr的用法
    python-mysqldb安装
    weblogic部署脚本
    netcat使用
    ssh批量互信脚本
    yum安装出错
    centos文件误删除恢复
    lamp php的ssl,ssh支持
    ssh免密码登陆
    python 学习笔记 四 条件, 循环以及其他
  • 原文地址:https://www.cnblogs.com/tengtao93/p/3087712.html
Copyright © 2011-2022 走看看