--uva247 (calling circles) strong Unicom and Floyd-warshell

Source: Internet
Author: User

Figure Thesis: I started with Tarjan algorithm, wrong answer a lot of times, and then used the Floyd-warshell algorithm, also WA

Finally found a problem, the original final dataset is not the number of groups, is the number of samples, the question is not said at all, let people how to understand ...

Tarjan

#include <stdio.h> #include <iostream> #include <string.h> #include <string> #include <map > #include <algorithm> #define M 100using namespace Std;int qis;int maps[50][50];int dfn[m],low[m],vis[m],    Stacks[m];int belongs[25][50];int cnt, scnt,begin,ans;void init () {memset (dfn,0,sizeof (DFN));    memset (low,0,sizeof (Low));    memset (stacks,0,sizeof (stacks));    memset (vis,0,sizeof (VIS));    memset (belongs,0,sizeof (belongs));    memset (maps,0,sizeof (maps)); Qis = CNT = SCNT = begin = ans = 0;}    void Tarjan (int x) {int V;    DFN[X] = low [x] = ++cnt;    Stacks[++begin] = x;                for (int i = 1;i<=qis;i++) {if (Maps[x][i]) {if (!dfn[i]) {Tarjan (i);            Low[x] = min (low[x],low[i]);            } else if (!vis[i]) {low[x] = min (low[x],dfn[i]);        }}} if (low[x] = = Dfn[x]) {scnt + +;            do{v = stacks[begin--]; Belongs[scnt][++belongs[scnt][0]]= v;//vertex processing vis[v] = 1;        }while (v! = x);    if (belongs[scnt][0]>1) ans + +;    }}int Main () {int flag = 1;    int n,m;    int cases = 1; while (scanf ("%d%d", &n,&m), n| |        m) {string A, B;        Init ();        map<string,int>ms;        map<int,string>_ms;        Ms.clear (); _ms.clear ();           for (int i = 0;i<m;i++) {cin>>a>>b;           if (ms[a] = = 0) Ms[a] = ++qis;           if (ms[b] = = 0) ms[b] = ++qis;           _ms[ms[a]] = A;           _MS[MS[B]] = b;        MAPS[MS[A]][MS[B]] = 1;        } for (int i =1;i<= qis;i++) {if (!dfn[i]) Tarjan (i);        } if (flag = = 1) flag = 0;        Else puts ("");        printf ("Calling circles for data set%d:\n", cases++);            for (int i = 1;i<=scnt;i++) {int J;            for (j = 1;j<belongs[i][0];j++) cout<<_ms[belongs[i][j]]<< ",";        cout<<_ms[belongs[i][j]]<<endl; }    }}

Floyd-warshell:

#include <stdio.h> #include <iostream> #include <string.h> #include <string> #include <map > #include <algorithm> #define M 100using namespace Std;int qis;int maps[50][50];int vis[m];int belongs[25][50];    int scnt, Ans;int cases = 1;int main () {int flag = 1;    int n,m; while (scanf ("%d%d", &n,&m), n| |        m) {Qis =ans = scnt = 0;        String A, B;        map<string,int>ms;        map<int,string>_ms;        Ms.clear (); _ms.clear ();        memset (vis,0,sizeof (VIS));        memset (maps,0,sizeof (maps));        memset (belongs,0,sizeof (belongs));        for (int i = 1;i<=n;i++) maps[i][i] = 1;           for (int i = 0;i<m;i++) {cin>>a>>b;           if (ms[a] = = 0) Ms[a] = ++qis;           if (ms[b] = = 0) ms[b] = ++qis;           _ms[ms[a]] = A;           _MS[MS[B]] = b;        MAPS[MS[A]][MS[B]] = 1; }//floydfor (int k = 1;k <= n;k++) for (int i = 1;i <= n;i++) for (int j = 1;j <= n;j++) {mAPS[I][J] = Maps[i][j] | |        (Maps[i][k] && maps[k][j]); } for (int i = 1;i<=n;i++) {if (vis[i] = = 0) {scnt++;for (int j = 1;j<=n;j++) {if (Maps[i][j]&&maps[j][i]) {Belo Ngs[scnt][++belongs[scnt][0]] = j;vis[j] = 1;}}        if (belongs[scnt][0]>1) ans++;}}        if (flag = = 1) flag = 0;        Else puts ("");        printf ("Calling circles for data set%d:\n", cases++);            for (int i = 1;i<=scnt;i++) {int J;            for (j = 1;j<belongs[i][0];j++) cout<<_ms[belongs[i][j]]<< ",";        cout<<_ms[belongs[i][j]]<<endl; }    }}

  

--uva247 (calling circles) strong Unicom and Floyd-warshell

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.