Floyd, delivery closure, and check set (Phone ring, UVA 247)

Source: Internet
Author: User
Tags closure

n Person (n<=25), play M-call, if A to B and B to a, then a, a, a, a, in the same telephone circle. Find all the phone rings and output the same circle owner.


n is very small, building an adjacency matrix, Map[i][j] indicates that the person numbered I called the person numbered J.

Run through the Floyd to find the delivery closure.

Finally, we use and check the set to group.


Purple Book has another method of grouping, that is, a new diagram, the same phone circle of people interconnect an edge, and then output each of the connected components of the owner.


Attached code

#include <stdio.h> #include <string> #include <string.h> #include <iostream> #include <map

> #include <vector> #include <set> #define MAXN using namespace std;
int n,m;
BOOL MAP[MAXN][MAXN];
int cnt;
map<string,int>int;
map<int,string>str;
Vector<set<int> >vec;
int P[MAXN];

int DONE[MAXN];
    void Init () {memset (map,0,sizeof (MAP));
    Int.clear ();
    Str.clear ();
    cnt=0;
    for (int i=0;i<maxn;i++) p[i]=i;
    Vec.clear ();
    memset (done,0,sizeof (done));
    set<int>s;
Vec.push_back (s);
    } void Get () {string s1,s2;
        for (int i=0;i<m;i++) {cin>>s1>>s2; if (!int[s1]) {int[s1]=++cnt;
        STR[CNT]=S1;} if (!int[s2]) {int[s2]=++cnt;
        STR[CNT]=S2;}
    Map[int[s1]][int[s2]]=true; }} void Floyd () {for (int k=1;k<=cnt;k++) for (int. i=1;i<=cnt;i++) for (int j=1;j<=cnt;j+ +) map[i][j]=map[i][j]| | (map[i][k]&&mAP[K][J]);

} int find (int x) {return p[x]==x?x:p[x]=find (p[x]);}
    void Pre () {Floyd ();
            for (int i=1;i<=cnt;i++) for (int j=1;j<=cnt;j++) if (I!=j&&map[i][j]&&map[j][i])
                {int xx=find (i);
                int Yy=find (j);
            if (xx!=yy) p[xx]=yy;
        } for (int i=1;i<=cnt;i++) {int id=find (i);
            if (!done[id]) {done[id]=vec.size ();
            Set<int> s;
            S.insert (i);
        Vec.push_back (s);
    } else Vec[done[id]].insert (i);
        } for (unsigned int i=1;i<vec.size (); i++) {set<int>& s=vec[i];
        Set<int>::iterator It=s.begin ();
            for (; It!=s.end (); it++) {if (It!=s.begin ()) cout<< ",";
        cout<<str[*it];
    } puts ("");

}} int Kase; int main () {while (scanf ("%d%d", &n,&m), n| | m) {if (Kase)Puts ("");
        printf ("Calling circles for data set%d:\n", ++kase);
        Init ();
        Get ();
    Pre ();
 }
}


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.