大家希望能选出一个人,所有人都认识他,但同时他不认识镇上除自己以外的其他人(在此,我们默认每个人自己认识自己)。可是小镇里的人太多了,一下子大家谁也说服不了谁...

第一种方法

#include "stdafx.h"

#include<iostream>  
#include<vector>  
#include<string>
using namespace std;

int main()
{
    int num;
    cin >> num;
    //vector<vector<int>> heads;
    while (num!=0)
    {
        num--;
        int n;
        int m;
        cin >> n >> m;
        
        vector<int> inVec(n+1,1);
        vector<int> outVec(n + 1, 1);
        vector<int> head;

        for (int i = 0;i < m;i++)
        {
            
            int a, b;
            cin >> a >> b;
            if (a == b)
            {
                continue;
            }
            else
            {
                outVec[a]++;
                inVec[b]++;
            
            }
            

        }
        

        for (int i = 1;i < n + 1;i++)
        {
            if (inVec[i] == n &&  outVec[i] == 1)
            {
                head.push_back(i);
            }
        }
        if (head.size() == 0)
        {
            cout << 0 << endl << endl;
        }
        else
        {
            cout << head.size() << endl;
            for (int i = 0;i < head.size() - 1;i++)
            {
                cout << head[i] << " ";
            }
            cout << head[head.size() - 1] << endl;
        }
    }
    

    return 0;
}

第二种:
#include "stdafx.h"

#include<iostream>  
#include<vector>  
#include<string>
using namespace std;

//所有的人都认识他
bool getAllpeople(vector<vector<int>> relation, int people)
{
    bool result = true;
    for (int x = 1;x < relation.size();x++)
    {
        if (relation[x][people] != 1)
        {
            result = false;
            break;
        }

    }
    return result;
}
//他不认识所有的人
bool getNopeope(vector<vector<int>> relation, int people)
{
    bool result = true;
    for (int y = 1;y < relation.size();y++)
    {
        if (y == people)
        {
            continue;
        }
        if (relation[people][y] == 1)
        {
            result = false;
            break;
        }

    }
    return result;
}
int main()
{
    int num;
    cin >> num;

    while (num != 0)
    {
        num--;
        int n;
        int m;
        cin >> n >> m;
        vector<vector<int>> relation(n + 1, vector<int>(n + 1, 0));
        vector<int > head;
        for (int i = 0;i < m;i++)
        {
            int a, b;
            cin >> a >> b;
            relation[a][b] = 1;
        }
        for (int i = 1;i < relation.size();i++)
        {
            relation[i][i] = 1;
        }


        for (int people = 1;people <relation.size();people++)
        {
            if (getAllpeople(relation, people) == true && getAllpeople(relation, people) == true)
            {
                head.push_back(people);
            }
        }
        if (head.size() == 0)
        {
            cout << 0 << endl << endl;
        }
        else
        {
            cout << head.size() << endl;
            for (int j = 0;j < head.size() - 1;j++)
            {
                cout << head[j] << " ";
            }
            cout << head[head.size() - 1] << endl;

        }

    }
    

    return 0;
}

评论:第一种方法能够通过,第二种不能通过,因为第二种的占用的空间多

转载于:https://www.cnblogs.com/wdan2016/p/6561575.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值