判断是否是一个无向图

版权声明:转载请注明出处即可 https://blog.csdn.net/qq_35170212/article/details/82989840

题:
输入一个图每个节点的边数的矩阵,判断是否可以成为一个无向图
例:

2 
6
1 1 1 4 2 1
4
1 2 2 3  
//输出:Yes No

码:

#include<iostream>
#include<queue>
using namespace std;

typedef priority_queue<int,vector<int>,greater<int>> RPQ;

int main(){
	int n=0;
	cin>>n;
	while(n--){
		int m=0;
		cin>>m;
		RPQ pq;
		int del=0;
		int cur=0;
		for(int i=0;i<m;++i){
			cin>>cur;
			if(cur==1)
				del++;
			else
				pq.push(cur);
		}
		bool flag=false;
		while(pq.size()&&del>0){
			cur=pq.top();
			pq.pop();
			del--;
			cur--;
			if(cur==1&&del==1){
				flag=true;
				break;
			}
			if(cur>1)
				pq.push(cur);
			else
				del++;
		}
		if(flag)
			cout<<"Yes";
		else
			cout<<"No";
		cout<<endl;		
	}
	return 0;
}

待验证

猜你喜欢

转载自blog.csdn.net/qq_35170212/article/details/82989840