Uva247(tarjan模板题)
来源:互联网 发布:网络骑士是哪里人 编辑:程序博客网 时间:2024/06/07 05:05
题目:
If you’ve seen television commercials for long-distance phone companies lately, you’ve noticed that many companies have been spending a lot of money trying to convince people that they provide the best service at the lowest cost. One company has “calling circles.” You provide a list of people that you call most frequently. If you call someone in your calling circle (who is also a customer of the same company), you get bigger discounts than if you call outside your circle. Another company points out
that you only get the big discounts for people in your calling circle, and if you change who you call most frequently, it’s up to you to add them to your calling circle.Liberty Bell Phone Co. is a new company that thinks they have the calling plan that can put other companies out of business. LibertyBell has calling circles, but they figure out your calling circle for
you. This is how it works. Liberty Bell keeps track of all phone calls. In addition to yourself, your calling circle consists of all people whom you call and who call you, either directly or indirectly.For example, if Ben calls Alexander, Alexander calls Dolly, and Dolly calls Ben, they are all within
the same circle. If Dolly also calls Benedict and Benedict calls Dolly, then Benedict is in the same calling circle as Dolly, Ben, and Alexander. Finally, if Alexander calls Aaron but Aaron doesn’t call Alexander, Ben, Dolly, or Benedict, then Aaron is not in the circle. You’ve been hired by Liberty Bell to write the program to determine calling circles given a log of phone calls between people.
Input
The input file will contain one or more data sets. Each data set begins with a line containing two integers, n and m. The first integer, n, represents the number of different people who are in the data set. The maximum value for n is 25. The remainder of the data set consists of m lines, each representing
a phone call. Each call is represented by two names, separated by a single space. Names are first names only (unique within a data set), are case sensitive, and consist of only alphabetic characters; no name
is longer than 25 letters.For example, if Ben called Dolly, it would be represented in the data file as Ben Dolly
Input is terminated by values of zero (0) for n and m.
Output
For each input set, print a header line with the data set number, followed by a line for each calling circle in that data set. Each calling circle line contains the names of all the people in any order within the circle, separated by comma-space (a comma followed by a space). Output sets are separated by
blank lines.
Sample Input
5 6
Ben Alexander
Alexander Dolly
Dolly Ben
Dolly Benedict
Benedict Dolly
Alexander Aaron
14 34
John Aaron
Aaron Benedict
Betsy John
Betsy Ringo
Ringo Dolly
Benedict Paul
John Betsy
John Aaron
Benedict George
Dolly Ringo
Paul Martha
George Ben
Alexander George
Betsy Ringo
Alexander Stephen
Martha Stephen
Benedict Alexander
Stephen Paul
Betsy Ringo
Quincy Martha
Ben Patrick
Betsy Ringo
Patrick Stephen
Paul Alexander
Patrick Ben
Stephen Quincy
Ringo Betsy
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Quincy Martha
0 0
Sample Output
Calling circles for data set 1:
Ben, Alexander, Dolly, Benedict
Aaron
Calling circles for data set 2:
John, Betsy, Ringo, Dolly
Aaron
Benedict
Paul, George, Martha, Ben, Alexander, Stephen, Quincy, Patrick
题目大意:例如:A跟B打电话,B跟C打电话,C跟A打电话..D跟E打电话,E跟D不打电话.则A,B,C属于同一个电话圈,D,E分别属于一个电话圈,输出所有的电话圈。
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <map>using namespace std;const int maxn=50;int d[maxn][maxn],DFN[maxn],low[maxn],flag[maxn],stack_[maxn],cnt,sum,top,j;string name[maxn];map <string,int> idx;int name_num(string s){ if(idx.count(s)) return idx[s]; idx[s]=++cnt; name[cnt]=s; return cnt;}void tarjan(int u){ DFN[u]=low[u]=sum++; stack_[++top]=u; flag[u]=1; for(int v=1; v<=cnt; v++) { if(d[u][v]) { if(!DFN[v]) { tarjan(v); if(low[v]<low[u]) low[u]=low[v]; } else { if(DFN[v]<low[u]&&flag[v]) low[u]=DFN[v]; } } } if(DFN[u]==low[u]) { do { j=stack_[top--]; if(j!=u) cout<<name[j]<<", "; else cout<<name[j]<<endl; flag[j]=0; } while(j!=u); }}int main(){ int n,m,from,to,k=0,T=1; string a,b; while(scanf("%d%d",&n,&m)!=EOF&&(n||m)) { if(T++>1) printf("\n"); k++; cnt=0; sum=1; top=-1; idx.clear(); memset(d,0,sizeof(d)); memset(DFN,0,sizeof(DFN)); memset(low,0,sizeof(low)); memset(flag,0,sizeof(flag)); for(int i=0; i<m; i++) { cin>>a>>b; from=name_num(a); to=name_num(b); d[from][to]=1; } printf("Calling circles for data set %d:\n",k); for(int i=1; i<=n; i++) { if(DFN[i]==0) tarjan(i); }//这里特别要注意,一开始,我就tarjan(1),这样就会忽略一些特殊情况,按上面这样写可以防止出现一些只有一个人的电话圈,比如A打给A,没有其他人跟A有任何联系,或者和它联系的那个人,和另外的人没有联系,没输出A这个单独的电话圈。 } return 0;}
- Uva247(tarjan模板题)
- 受欢迎的牛(Tarjan模板题)
- HDU1269迷宫城堡(Tarjan模板题)
- HDU1289 Tarjan-模板题
- hdu1269 tarjan模板题
- 求割边模板(tarjan)
- tarjan(SCC)模板
- tarjan模板(hdu1269)
- uva247
- uva247
- 【模板】LCA Tarjan算法 (模板题:洛谷P3379)
- poj 1986tarjan模板题
- [Uva247][Tarjan求强连通分量][Calling Circles]
- HDU 1269(Tarjan模板)
- 强联通 tarjan (模板)
- HDU 3062 Party(2-sat 模板题 tarjan )
- Network(特殊的输入格式+tarjan求割点模板题)
- poj 2186 tarjan求强连通分量(模板题)
- hadoop学习笔记003(感受上传到dfs和hadoop统计计算)
- java多态,抽象类和接口
- 训练日记-31
- 数组和指针十个问答
- 线性判别分析(LDA), 主成分分析(PCA)
- Uva247(tarjan模板题)
- 第2节:如何成为scala高手
- 按位操作符与背包问题的遍历
- 《Data Mining》学习——K-邻近(KNN,K-NearestNeighbor)
- 虚电路网络与数据报网络
- 归并排序
- Spring5源码解析-Spring框架中的事件和监听器
- bind(),call(),apply()区别
- [BZOJ]4336: BJOI2015 骑士的旅行 树链剖分+STL(multiset)