pat-1149

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>v[100000];
int main(){
	int n,m,_1,_2,k,kt;
	cin>>n>>m;
	
	for(int i=0;i<n;i++){
		cin>>_1>>_2;
		v[_1].push_back(_2);
		v[_2].push_back(_1);
	}
	for(int i=0;i<m;i++){
		cin>>k;
		vector<int>vce;
		int flag=0;
		int hash[100000]={0};//判断有无 
		fill(hash,hash+100000,0);
		for(int j=0;j<k;j++){
			cin>>kt;
			vce.push_back(kt);
			hash[kt]=1;
		}
		for(int j=0;j<k;j++){
			for(int i=0;i<v[vce[j]].size();i++){
				if(hash[v[vce[j]][i]]==1) flag=1;
			}
		}
		if(flag==0){
			printf("Yes\n");
		} 
		else{
			printf("No\n");
		}
		
	}
	return 0;
} 

Summary 1. If you can use the hash to determine the presence or absence, use the hash hash, which is much faster than traversing the first to find the last one

2. When the data is very large, for example, v[100000] should be placed outside the main function as a global variable

3. Even if the output format is yes no, don’t type it yourself, just copy it to reduce errors

English

 

problem

Note that the application of map and two-dimensional vector takes up less space map<int, vector<int>>

 

 

Guess you like

Origin blog.csdn.net/m0_45359314/article/details/112987157