ZOJ--3710--Friends--二维数组模拟建立关系

来源:互联网 发布:安卓版电子狗软件 编辑:程序博客网 时间:2024/06/04 23:22

题目链接https://vjudge.net/problem/ZOJ-3710

题目大意是输入朋友关系对,双方拥有共同朋友达到数量k之后可以变成朋友。求一段时间后有多少对新的朋友对关系建立起来。

要点:

1、建立friends[from][to]二维数组存储关系对。

2、注意新建立的朋友关系可能会连锁反应建立更多的好友关系。


代码如下:

#include<iostream>#include<cstring>using namespace std;const int maxn=110;int friends[maxn][maxn];int ans;int make_friends(int n,int k){    ans=0;    for(int i=0;i<n;i++){        for(int j=0;j<n;j++){            if(i==j) continue;            if(friends[i][j]==0){                int cnt=0;                for(int t=0;t<n;t++){                    if(friends[i][t]==1&&friends[j][t]==1)                        cnt++;                }                if(cnt>=k){                    friends[i][j]=1;                    friends[j][i]=1;                    ans++;                    i=-1;                    break; //此两行的作用就是让i重新循环,计算要点2中的连锁朋友关系                }            }        }    }    return ans;}int main(){    int T,n,m,k,from,to;    cin>>T;    while(T--){        memset(friends,0,sizeof(friends));        cin>>n>>m>>k;        while(m--){            cin>>from>>to;            friends[from][to]=1;            friends[to][from]=1;        }        ans=make_friends(n,k);        cout<<ans<<endl;    }    return 0;}


原创粉丝点击