UVA 247 - Calling Circles Floyd闭包

链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=183

题意&题解:

紫书P365

代码:

31 int n, m;
32 int d[MAXN][MAXN];
33 map<string, int> mmp;
34 string s[MAXN];
35 int par[MAXN];
36 
37 int find(int x) {
38     return par[x] = par[x] == x ? x : find(par[x]);
39 }
40 
41 void unite(int x, int y) {
42     int a = find(x), b = find(y);
43     if (a != b) par[a] = b;
44 }
45 
46 void init() {
47     mmp.clear();
48     memset(d, 0, sizeof(d));
49 }
50 
51 int main() {
52     ios::sync_with_stdio(false), cin.tie(0);
53     int cas = 1;
54     while (cin >> n >> m, n) {
55         init();
56         int cnt = 1;
57         while (m--) {
58             string a, b;
59             cin >> a >> b;
60             if (!mmp[a]) s[cnt] = a, mmp[a] = cnt++;
61             if (!mmp[b]) s[cnt] = b, mmp[b] = cnt++;
62             d[mmp[a]][mmp[b]] = 1;
63         }
64         rep(k, 1, n + 1) rep(i, 1, n + 1) rep(j, 1, n + 1)
65             if (d[i][k] && d[k][j]) d[i][j] = 1;
66         rep(i, 1, n + 1) par[i] = i;
67         rep(i, 1, n + 1) rep(j, 1, n + 1) if (d[i][j] && d[j][i]) unite(i, j);
68         if (cas != 1) cout << endl;
69         cout << "Calling circles for data set " << cas++ << ":" << endl;
70         rep(i, 1, n + 1) if (par[i] == i) {
71             cout << s[i];
72             rep(j, 1, n + 1) if (par[j] == i && j != i) cout << ", " << s[j];
73             cout << endl;
74         }
75     }
76     return 0;
77 }

 

转载于:https://www.cnblogs.com/baocong/p/7422424.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值