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

    第一种方法

    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;
    }
    
    评论:第一种方法能够通过,第二种不能通过,因为第二种的占用的空间多
  • 相关阅读:
    Set the Welcome Page in SharePoint through Code
    调试工具
    Sublime Text 3编译Sass
    js储存参数的数组arguments
    js 判断客户端浏览器
    手机设计尺寸
    required
    前端工具HBuilder安装Sass插件
    apache极简配置虚拟机
    跳转输入框按键操作
  • 原文地址:https://www.cnblogs.com/wdan2016/p/6561575.html
Copyright © 2011-2022 走看看