Uva 247 WA了n次才发现,后面有个空格......

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<limits.h>
#include<cmath>
#include<map>
#include<set>
using namespace std;

const int peoplemax = 26;
int peoplenum, pairnum;  //peoplenum<=25   no name is longer than 25 letters.
map<string, int> dic;
set<string> names;
string nameindex[peoplemax];
bool graph[peoplemax][peoplemax];
vector<int> newg[peoplemax];
bool mark[peoplemax];
int testecase;

void init();
void dfs(int cur);
void floyd();

int main()
{
    // freopen("247in.txt", "r", stdin);
    int testecase = 0;
    while (cin >> peoplenum >> pairnum)
    {
        if (peoplenum == 0)    break;
        if(testecase)
            cout << endl;
        testecase++;


        init();

        floyd();

        printf("Calling circles for data set %d:\n", testecase);
        memset(mark, 0, sizeof(mark));
        for(int i=0;i<peoplenum;i++)
            if (!mark[i])
            {
                cout<<nameindex[i+1];
                 dfs(i);
            cout<<endl;
            }
    }
    return 0;
}

void floyd()
{
     for(int k=0;k<peoplenum;k++)
         for(int i=0;i<peoplenum;i++)
             for(int j=0;j<peoplenum;j++)
    {
        graph[i][j] = graph[i][j] || (graph[i][k] && graph[k][j]);
    }

     for(int i=0;i<peoplenum;i++)
        newg[i].clear();
}

void dfs(int cur)
{
    mark[cur] = true;
     for(int i=0;i<peoplenum;i++)
        if (!mark[i] && graph[cur][i] && graph[i][cur])
        {
            cout<<", "<<nameindex[i+1];
            dfs(i);
        }
}

void init()
{
    string name1, name2;
    dic.clear();
    names.clear();
    memset(graph, 0, sizeof(graph));
    // memset(nameindex,0,sizeof(nameindex));
    for(int i=0;i<peoplemax;i++)
        nameindex[i].clear();
    int num = 1;          //人的序号从1开始  但是graph等里面都是以0开始的
    while (pairnum--)
    {
        cin >> name1 >> name2;
        int &t1 = dic[name1];
        int &t2 = dic[name2];
        if (!t1)
        {
            dic[name1] = num;
            nameindex[num++] = name1;
        }
        if (!t2)
        {
            dic[name2] = num;
            nameindex[num++] = name2;
        }

        graph[t1 - 1][t2 - 1] = true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值