Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <map>
- #include <string>
- using namespace std;
- void solve(int now, int &idx, int *use, int *low, int *len, int arr[32][32], int *stk, int &top, int *instk, string *itos)
- {
- low[now]=use[now]=++idx;
- stk[top++]=now, instk[now]=1;
- for(int i=0; i<len[now]; i++)
- {
- int t=arr[now][i];
- if(!use[t])
- {
- solve(t, idx, use, low, len, arr, stk, top, instk, itos);
- low[now]=low[now]<low[t]?low[now]:low[t];
- }
- else if(instk[t])
- low[now]=low[now]<use[t]?low[now]:use[t];
- }
- if(low[now]==use[now])
- {
- printf("%s", itos[stk[--top]].c_str());
- instk[stk[top]]=0;
- while(stk[top]!=now)
- {
- printf(", %s", itos[stk[--top]].c_str());
- instk[stk[top]]=0;
- }
- puts("");
- }
- }
- int main()
- {
- for(int n, m, cnt=1; scanf("%d%d", &n, &m) && n; cnt++)
- {
- int idx=0, use[32]={}, low[32], arr[32][32], len[32]={}, stk[32], instk[32]={}, top=0;
- map<string, int> stoi;
- string itos[32];
- int num=0;
- for(int i=0; i<m; i++)
- {
- char a[32], b[32];
- scanf("%s%s", a, b);
- int &na=stoi[a], &nb=stoi[b];
- if(!na) na=++num, itos[na]=a;
- if(!nb) nb=++num, itos[nb]=b;
- arr[na][len[na]++]=nb;
- }
- if(cnt>1) puts("");
- printf("Calling circles for data set %d:\n", cnt);
- for(int i=1; i<=n; i++)
- if(!use[i])
- solve(i, idx, use, low, len, arr, stk, top, instk, itos);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement