Uva_247_calling Circles (Floyd delivery closure)

Source: Internet
Author: User
Tags cmath

calling Circles
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

Description

if you ' ve seen television commercials for long-distance phone companies lately, you ' ve No Ticed that many companies has been spending a lot of money trying to convince people this they provide the best service a t the lowest cost. One company has calling circles. " You provide a list of people the most frequently. If you call someone in your calling circle (who's also a customer of the same company), you get bigger discounts than if You call outside your circle. Another company points out this you only get the big discounts for people in your calling circle, and if your change who yo U call the most frequently, it's up to the add them to your calling circle.

libertybell Phone Co. is a new company that thinks they has the calling plan that can put Other companies out of the business. Libertybell has calling circles and they gure out your calling circle for you. How it works. Libertybell 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 IND irectly. For example, if Ben calls Alexander, Alexander calls Dolly, and Dolly calls Ben, they is all within the same circle. If Dolly also calls Benedict and Benedict calls Dolly, then Benedict are in the same calling circle as Dolly, Ben, and Alex Ander. Finally, if Alexander calls Aaron but Aaron doesn ' t call Alexander, Ben, Dolly, or Benedict, then Aaron was not in the CIRC Le. You ' ve been hired by Libertybell to write the program to determine calling circles given a log of phone calls between Peop Le.

input
the  input  file  will  contain  one  or  more  data  sets.   Each  data  set  begins  with  a  line  containing  both Integers,n and M.the first Integer,n, represents the Numbe R of different people who was 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 was represented by and names, separated by a single s pace.  Names is rst Names only (unique within a data set), is case sensitive, and consist of the only alphabetic Charac Ters No name is longer than letters.
For example, if Ben called Dolly, it would being represented in the data file as
Ben Dolly
Input was terminated by V Alues of zero (0) for N and M.

Output
For each of the input set, print a header line with the data set number, followed by a line for each calling circle in that dat  A 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 is 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.

Test instructions: If two people call each other (directly or indirectly), say they're in the same phone ring. For example, a call to B,b call C,c to call D,d A, then the four people in the same circle, if E to F, and F does not call E, you cannot launch E and F in the same phone ring. Enter n (n<=25) personal m-Call to find all the phone rings. A person's name contains only letters, no more than 25 characters, and does not repeat.

Analysis: The problem is to ask for the strong connected component of the graph. can be solved with Tarjan. But notice that the data is relatively small, you can also think of another algorithm--floyd. In a forward graph, sometimes it is not necessary to care about the length of the path, only about whether there are paths between each of the two points, you can use 1 and 0 to indicate "connected" and "disconnected". In this way, it can be solved with Floyd: dp[i][j]=dp[i][j]| | (Dp[i][k]&&dp[k][j]). Such results are called transitive closures (transitive Closure) of the graph.

Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20840

Code Listing:

Floyd algorithm

#include <set> #include <map> #include <cmath> #include <queue> #include <stack> #include <ctime> #include <cctype> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;typedef long Long ll;typedef unsigned int uint;typedef unsigned long long ull;const int maxs = + 5;const int MAXN = + 5;const int MAXV = 400000 0 + 5;int n,m,cases=0;int dp[maxs][maxs];int idx1,idx2,cnt;string name1,name2;string rstr[maxs];map<string,int>    Idx;bool vis[maxs];void init () {cnt=0; Idx.clear ();    Memset (Dp,0,sizeof (DP)); memset (vis,false,sizeof (Vis));}    int Get_idx (string name) {if (Idx.count (name)) return Idx[name]; idx[name]=++cnt;    Rstr[cnt]=name; return CNT;}        void input () {for (int i=1;i<=m;i++) {cin>>name1>>name2;        Idx1=get_idx (NAME1);        Idx2=get_idx (name2);    Dp[idx1][idx2]=1; }}void Floyd () {for (int k=1;k<=n;k++) {for (int. i=1;i<=n;i++) {for (int j=1;j<=n;j++) dp[i][j]=dp[i][j]| |        (Dp[i][k]&&dp[k][j]);    }}}void Solve () {Floyd ();    if (cases) cout<<endl;    printf ("Calling circles for data set%d:\n", ++cases);        for (int i=1;i<=n;i++) {if (vis[i]) continue;        cout<<rstr[i];            for (int j=i+1;j<=n;j++) {if (vis[j]) continue;                if (Dp[i][j]&&dp[j][i]) {vis[j]=true;            cout << "," <<rstr[j];    }}cout<<endl;        }}int Main () {while (scanf ("%d%d", &n,&m)!=eof) {if (n==0&&m==0) break;        Init ();        Input ();    Solve (); }return 0;}

Tarjan algorithm

#include <set> #include <map> #include <cmath> #include <queue> #include <stack> #include <ctime> #include <cctype> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;typedef long Long ll;typedef unsigned int uint;typedef unsigned long long ull;const int maxs = + 5;int n,m,cases=0;int idx1,idx2,cnt;string Name1,na Me2;string rstr[maxs];map<string,int>idx;int sccno,no;int dfn[maxs];int low[maxs];bool InStack[maxs];stack    <int>sta;vector<int>graph[maxs];void init () {cnt=no=0;    Idx.clear ();        for (int i=1;i<=maxs;i++) {dfn[i]=low[i]=0;        Instack[i]=false;    Graph[i].clear (); } while (!sta.empty ()) Sta.pop ();}    int Get_idx (string name) {if (Idx.count (name)) return Idx[name]; idx[name]=++cnt;    Rstr[cnt]=name; return CNT;}     void input () {for (int i=1;i<=m;i++) {cin>>name1>>name2;   Idx1=get_idx (NAME1);        Idx2=get_idx (name2);    Graph[idx1].push_back (IDX2);    }}void Tarjan (int u) {low[u]=dfn[u]=++no; Instack[u]=true;    Sta.push (U);        for (int i=0;i<graph[u].size (); i++) {int v=graph[u][i];            if (!dfn[v]) {Tarjan (v);        Low[u]=min (Low[u],low[v]);        } else if (Instack[v]) {low[u]=min (low[u],dfn[v]);        }} if (Low[u]==dfn[u]) {int vv=-1;            while (!sta.empty () &&vv!=u) {vv=sta.top ();            Sta.pop ();            Instack[vv]=false;            if (vv==u) cout<<rstr[vv]<<endl;        Else cout<<rstr[vv]<< ",";    }}}void Solve () {if (cases) cout<<endl;    printf ("Calling circles for data set%d:\n", ++cases);    for (int i=1;i<=n;i++) {if (!dfn[i]) Tarjan (i);        }}int Main () {while (scanf ("%d%d", &n,&m)!=eof) {if (!n&&!m) break;        Init ();        Input ();    Solve (); }return 0;} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Uva_247_calling Circles (Floyd delivery closure)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.