Friends ZOJ
来源:互联网 发布:VPN网络加速器 官网 编辑:程序博客网 时间:2024/06/08 07:30
Friends
ZOJ - 3710Alice 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.
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 < n, ui ≠ vi) indicating there is friendship between person ui and vi.
Note: The edges in test data are generated randomly.
<h4< dd="">For each case, print one line containing the answer.
<h4< dd="">34 4 20 10 21 32 35 5 20 11 22 33 44 05 6 20 11 22 33 44 02 0<h4< dd="">
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;}
- Friends ZOJ
- ZOJ-3710(Friends)
- zoj 3710 Friends
- zoj 3710 Friends
- ZOJ 3710 Friends 暴力
- zoj 3710 Friends
- Friends zoj 3710
- ZOJ 3710 Friends
- zoj 3710 Friends
- ZOJ 3710 Friends
- ZOJ Friends(暴力)
- ZOJ 3710 Friends
- ZOJ 3710Friends(暴力)
- ZOJ 3710 Friends
- ZOJ 3710 Friends
- ZOJ-3710-Friends
- ZOJ 3710 Friends
- Friends ZOJ 3710
- Android消息处理机制(Handler、Looper、MessageQueue与Message)
- 运算符和结合性
- 【weekly-sharing】学习可行性推导记录
- How to execute the script automatically without the authority of /etc
- Redis之AOF重写及其实现原理
- Friends ZOJ
- 刷新框架选项卡切换以及连续下拉问题解决
- 安卓 SQLite数据库
- noip滚粗记
- 记录开通
- 13.1如何集成通用Mapper
- java IO的字节流和字符流及其区别
- HTTPS与HTTP详解
- 【Qt】Qt Plugin:Qt插件创建与使用