Friends ZOJ

来源:互联网 发布:VPN网络加速器 官网 编辑:程序博客网 时间:2024/06/08 07:30

Friends

 ZOJ - 3710 


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 ≤ m ≤ n×(n-1)/2, 0 ≤ k ≤ n, there will be no duplicated friendship) followed by m lines showing the current friendship. The ithfriendship contains two integers ui, vi (0 ≤ ui, vi < nui ≠ vi) indicating there is friendship between person ui and vi.

Note: The edges in test data are generated randomly.

<h4< dd="">
Output

For each case, print one line containing the answer.

<h4< dd="">
Sample Input

34 4 20 10 21 32 35 5 20 11 22 33 44 05 6 20 11 22 33 44 02 0

<h4< dd="">
Sample Output

204
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;int vis[109][109];vector<int>a[109];int is_NewFriend(int u,int v,int k){    int i,j;    int commond = 0;    for(i = 0; i < a[u].size(); i++){        for(j = 0; j < a[v].size(); j++){            if(a[u][i] == a[v][j]){                commond++;                break;            }        }        if(commond >= k){            return 1;        }    }    if(k == 0)return 1;//注意!!!!!当k=0并且u没有朋友,这个时候应该也返回1    return 0;}int main(){    int t;    cin >> t;    while(t--){        int n,m,k;        cin >> n >> m >> k;        int i,j;        memset(vis,0,sizeof(vis));        for(i = 0; i < 109; i++){            a[i].clear();        }        for(i = 0; i < m; i++){            int u,v;            cin >> u >> v;            vis[u][v] = 1;            vis[v][u] = 1;            a[u].push_back(v);            a[v].push_back(u);        }        int sum = 0;        while(1){            int flag = 0;            for(i = 0; i < n; i++){                for(j = i+1; j < n; j++){                    if(!vis[i][j]){                        if(is_NewFriend(i,j,k)){                            a[i].push_back(j);                            a[j].push_back(i);                            vis[i][j] = 1;                            vis[j][i] = 1;                            sum++;                            flag = 1;                        }                    }                }            }            if(!flag)                break;        }        cout << sum << endl;    }    return 0;}


原创粉丝点击