UVA 247 Calling Circles (Fioyd transitive closures and DFS)

Source: Internet
Author: User

Original title Link:

Https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem &problem=183


Main topic:

Output each person in the phone ring.

Any person in the phone ring can call directly or indirectly to any other person in the circle.


Note the output: ', ' there are spaces after each of the two sets of test data in an empty row.


See the code:

#include <iostream> #include <cstring> #include <string> #include <map> #include <vector>

using namespace Std;
const int N = 25 + 1;
int g[n][n];
BOOL vis[n];//Record has accessed int k,n,m;
	vector<string>name;//the name of the corresponding order map<string, int>peo;//the relationship between the name and order void Init () {memset (g, 0, sizeof (g));
	memset (Vis, 0, sizeof (VIS));
	Name.clear ();
	Peo.clear ();
k = 0;
	} void DFS (int u) {Vis[u] = true; for (int v = 0; v < n; v++) {if (G[u][v]&&g[u][v]==g[v][u]&&!vis[v])//Two people can contact each other and have not accessed {Co
			UT << "," << name[v];
		DFS (v);
	}}} int main () {int kase=1; while (CIN >> n >> m&& (n| |
		m)//n==0&&m==0 terminate loop {init ();
		string u, v;
			for (int i = 0; i < m; i++) {cin >> u >> v;
				if (!peo.count (U)) {Peo[u] = k++;
			Name.push_back (U); 
				} if (!peo.count (v)) {peo[v] = k++;
			Name.push_back (v);
		} G[peo[u]][peo[v]] = 1; } for (int h = 0; h < n; h+ +)//floyd transitive closure processing, so that the indirect contact can be directly linked to {for (int i = 0; i < n; i++) {if (G[i][h]) {(int j = 0; J & Lt
				n;j++) if (G[h][j]) g[i][j] = 1;
		}}} if (kase>1) cout << Endl;
		cout << "Calling circles for data set" << kase++ << ":" << Endl;
				for (int i = 0; i < n; i++) {if (!vis[i]) {cout << name[i];
				DFS (i);
			cout << Endl;
}}} return 0; }


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.