UVA 247 graph theory Floyd algorithm + recursive traversal

Source: Internet
Author: User
Tags cas closure

First, the Floyd is used to find the transitive closure, a new graph is constructed, and then the recursive method is used to traverse the graphs to output the interconnected nodes.
#include <iostream> #include <string> #include <algorithm> #include <map> #include <cstring > #include <vector> #include <cstdio>using namespace std; #define N 500int D[n][n];map<string,int>q ; map<int,string>p;string s1,s2;int n,m,cas=0;int vis[n];void dfs (int u) {vis[u]=1;for (int k=0;k<n;k++) {if (!v  Is[k]) {if (D[u][k]&&d[k][u]) {cout<< "," <<p[k];d FS (k);}}} int main () {//freopen ("I.txt", "R", stdin),//freopen ("O.txt", "w", stdout);//ios::sync_with_stdio (false); while (~ scanf ("%d%d", &n,&m)) {memset (vis,0,sizeof (VIS));p. Clear (); Q.clear (); if (m==0&&n==0) break;if (CAS) cout<<endl;printf ("Calling circles for data set%d:\n", ++cas); int Tt=-1;memset (d,0,sizeof (d)); while (m--) {cin >>s1>>s2;if (!q.count (S1)) {q[s1]=++tt;p[tt]=s1;} if (!q.count (S2)) {q[s2]=++tt;p[tt]=s2;} D[q[s1]][q[s2]]=1;} for (int k=0;k<n;k++)//Find transitive closure {for (int i=0;i<n;i++) {if (D[i][k]) {if (int j=0;j<n;j++) {if (D[k][j]) {d[i][j]=1 ;}}}}} for (inti=0;i<n;i++)//{//for (int j=0;j<n;j++)//{//printf ("%d", d[i][j]);//}//puts ("");//}for (int i=0;i<n;i++) {if (!vis[i]) {Cout<<p[i];d fs (i); Cout<<endl;}}} return 0;}
Calling Circles
Time limit:3000ms Memory limit:0kb 64bit IO format:%lld &%llu
Submit

Status

Practice
Description
Download as PDF
If you've seen television commercials for long-distance phone companies lately, you've noticed that many companies has been En spending a lot of money trying to convince people this they provide the best service at 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 business . Libertybell have calling circles, but they figure 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 is contain one or more data sets. Each data set begins with a line containing the integers, N and M. The first integer, n, represents the number of different people who is 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 space. Names is first Names only (unique within a data set), is case sensitive, and consist of the only alphabetic characters; No name is longer than letters.
For example, if Ben called Dolly, it would is represented in the data file as
Ben Dolly
Input is terminated by the values 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 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 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.

UVA 247 graph theory Floyd algorithm + recursive traversal

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.