zoukankan      html  css  js  c++  java
  • UVA 247

    链接:

    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 }
  • 相关阅读:
    codefoces 1405 D Tree Tag
    洛谷P3413 萌数
    数位DP模板
    leetcode 统计所有可行路径
    Josephus Transform 加速置换
    牛客IOI周赛18-提高组 排列
    Find a way HDU
    Oil Deposits HDU
    Fire! UVA
    Pots POJ
  • 原文地址:https://www.cnblogs.com/baocong/p/7422424.html
Copyright © 2011-2022 走看看