247-calling Circles (Floyd for delivery closure)

Source: Internet
Author: User

The problem is just about the Floyd for the transfer closure, because the problem is not a large amount of data, if the big estimate will use other methods. But this is, after all, a very simple and easy-to-write algorithm.

After the delivery closure is found, the set will be merged with the person in a telephone ring, and the final output can be obtained.

See the code for details:

#include <bits/stdc++.h>using namespace Std;int n,m,d[30][30],par[30],kase = 0;string s1,s2;map<string, int    > p;map<int,int> pp;struct Node {string s;    int id; Node (string s= "", int id=0): s (s), ID (ID) {}}a[30];int find (int x) {return par[x] = = x = x:par[x] = find (Par[x]);}                void Floyd () {//For transitive closure for (int k=0;k<n;k++) for (int i=0;i<n;i++) for (int j=0;j<n;j++) D[I][J] = d[i][j]| | (D[i][k]&&d[k][j]);}    int main () {bool Flage = true;        while (~SCANF ("%d%d", &n,&m)) {if (!n &&!m) return 0; P.clear ();        Pp.clear ();        if (!flage) printf ("\ n");        else Flage = false;        Memset (d,0,sizeof (d));        int cnt = 0;            for (int i=0;i<m;i++) {cin>>s1>>s2;                if (!p.count (S1)) {a[cnt] = node (s1,cnt); P[S1] = cnt;            ++cnt;             } if (!p.count (S2)) {a[cnt] = node (s2,cnt);   P[S2] = cnt;            ++cnt;        } D[P[S1]][P[S2]] = 1;        } Floyd ();  vector<int> Res;        Use and look up the merge set for (int i=0;i<cnt;i++) par[i] = i;                    for (int i=0;i<cnt;i++) {for (int j=i+1;j<cnt;j++) {if (D[i][j]&&d[j][i]) {                     int x = Find (i), y = Find (j);                if (x! = y) par[x] = y;        }}} printf ("Calling circles for data set%d:\n", ++kase);            for (int i=0;i<cnt;i++) {//By parent node finds the person in the corresponding collection int x = find (i);            if (!pp.count (x)) {pp[x] = 1; res.push_back (x); }} int len = Res.size ();            Output for (int i=0;i<len;i++) {bool OK = true;            int v = res[i];                for (int j=0;j<cnt;j++) {int x = find (j);                    if (x = = v) {if (OK) printf ("%s", A[j].s.c_str ()), OK = false; else printf (",%s", a[J].s.c_str ());        }} printf ("\ n"); }} return 0;}


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

247-calling Circles (Floyd for 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.