Advertisement
kcku

00247 - Calling Circles

Aug 4th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <map>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void solve(int now, int &idx, int *use, int *low, int *len, int arr[32][32], int *stk, int &top, int *instk, string *itos)
  7. {
  8.     low[now]=use[now]=++idx;
  9.     stk[top++]=now, instk[now]=1;
  10.  
  11.     for(int i=0; i<len[now]; i++)
  12.     {
  13.         int t=arr[now][i];
  14.  
  15.         if(!use[t])
  16.         {
  17.             solve(t, idx, use, low, len, arr, stk, top, instk, itos);
  18.             low[now]=low[now]<low[t]?low[now]:low[t];
  19.         }
  20.         else if(instk[t])
  21.             low[now]=low[now]<use[t]?low[now]:use[t];
  22.     }
  23.     if(low[now]==use[now])
  24.     {
  25.         printf("%s", itos[stk[--top]].c_str());
  26.         instk[stk[top]]=0;
  27.  
  28.         while(stk[top]!=now)
  29.         {
  30.             printf(", %s", itos[stk[--top]].c_str());
  31.             instk[stk[top]]=0;
  32.         }
  33.         puts("");
  34.     }
  35. }
  36. int main()
  37. {
  38.     for(int n, m, cnt=1; scanf("%d%d", &n, &m) && n; cnt++)
  39.     {
  40.         int idx=0, use[32]={}, low[32], arr[32][32], len[32]={}, stk[32], instk[32]={}, top=0;
  41.         map<string, int> stoi;
  42.         string itos[32];
  43.         int num=0;
  44.  
  45.         for(int i=0; i<m; i++)
  46.         {
  47.             char a[32], b[32];
  48.             scanf("%s%s", a, b);
  49.             int &na=stoi[a], &nb=stoi[b];
  50.             if(!na) na=++num, itos[na]=a;
  51.             if(!nb) nb=++num, itos[nb]=b;
  52.             arr[na][len[na]++]=nb;
  53.         }
  54.         if(cnt>1) puts("");
  55.         printf("Calling circles for data set %d:\n", cnt);
  56.  
  57.         for(int i=1; i<=n; i++)
  58.             if(!use[i])
  59.                 solve(i, idx, use, low, len, arr, stk, top, instk, itos);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement