日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pat 甲级 1034. Head of a Gang (30)

發布時間:2024/7/19 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pat 甲级 1034. Head of a Gang (30) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1034. Head of a Gang (30)

時間限制 100 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard 作者 CHEN, Yue

One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threshold, respectively. Then N lines follow, each in the following format:

Name1 Name2 Time

where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:

For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1: 8 59 AAA BBB 10 BBB AAA 20 AAA CCC 40 DDD EEE 5 EEE DDD 70 FFF GGG 30 GGG HHH 20 HHH FFF 10 Sample Output 1: 2 AAA 3 GGG 3 Sample Input 2: 8 70 AAA BBB 10 BBB AAA 20 AAA CCC 40 DDD EEE 5 EEE DDD 70 FFF GGG 30 GGG HHH 20 HHH FFF 10 Sample Output 2: 0

題意:gang是一個幫派群體,現在給出一張圖,找到這張圖上的所有幫派以及每個幫派的頭目和幫派人數。其中要成為一個幫派,要滿足以下要求:
1:幫派人數大于等于3 2:幫派中人與人的通話總時長要高于一個界限threshhold.
思路:dfs
AC代碼: #define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<set> #include<queue> #include<map> using namespace std; #define INF 0x3f3f3f #define N_MAX 10000+5 typedef long long ll; struct gang{int num;string head;gang() {}gang(int num,string head):num(num),head(head) {}bool operator < (const gang&b) {return head < b.head;} }; vector<gang>res; int n,m, threshold; map<string, int>Map; bool is_connect[N_MAX][N_MAX]; string name[N_MAX]; int weight[N_MAX],vis[N_MAX];int max_time = 0, id,num=0,sum=0; bool flag = 0; void dfs(int x) {sum += weight[x];num++;if (sum/2 > threshold)flag = true;vis[x] = true;if (max_time < weight[x]) {max_time = weight[x];id = x;}for (int i = 1; i < n;i++) {if (!is_connect[x][i]||vis[i])continue;dfs(i);} }int main() {while (cin>>m>>threshold) {n = 1;//n-1為人數for (int i = 0; i < m;i++) {string from, to; int cost;cin >> from >> to >> cost;if (Map[from] == 0) { Map[from] = n; name[n++] = from; }if (Map[to] == 0) { Map[to] = n; name[n++] = to; }is_connect[Map[from]][Map[to]] = 1;is_connect[Map[to]][Map[from]] = 1;weight[Map[from]] += cost; weight[Map[to]] += cost;}for (int i = 1; i < n;i++) {max_time = 0,flag = 0,num=0,sum=0;if (!vis[i]){dfs(i);if (flag&&num>2) {res.push_back(gang(num, name[id]));}}}sort(res.begin(),res.end());printf("%d\n",res.size());for (int i = 0; i < res.size();i++) {printf("%s %d\n",res[i].head.c_str(),res[i].num);}}return 0; }

?

轉載于:https://www.cnblogs.com/ZefengYao/p/8570696.html

總結

以上是生活随笔為你收集整理的pat 甲级 1034. Head of a Gang (30)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。