zoukankan      html  css  js  c++  java
  • UVA 10720 Graph Construction 贪心+优先队列

    题目链接:

    题目

    Graph Construction
    Time limit: 3.000 seconds

    问题描述

    Graph is a collection of edges E and vertices V. Graph has a wide variety of applications in computer.
    There are different ways to represent graph in computer. It can be represented by adjacency matrix or
    by adjacency list. There are some other ways to represent graph. One of them is to write the degrees
    (the numbers of edges that a vertex has) of each vertex. If there are n vertices then n integers can
    represent that graph. In this problem we are talking about simple graph which does not have same
    endpoints for more than one edge, and also does not have edges with the same endpoint.
    Any graph can be represented by n number of integers. But the reverse is not always true. If you
    are given n integers, you have to find out whether this n numbers can represent the degrees of n vertices
    of a graph.

    输入

    Each line will start with the number n (≤ 10000). The next n integers will represent the degrees of n
    vertices of the graph. A ‘0’ input for n will indicate end of input which should not be processed.

    输出

    If the n integers can represent a graph then print ‘Possible’. Otherwise print ‘Not possible’. Output
    for each test case should be on separate line.

    样例

    input
    4 3 3 3 3
    6 2 4 5 5 2 1
    5 3 2 3 2 1
    0

    output
    Possible
    Not possible
    Not possible

    题意

    给你每个顶点的度数,问能不能组成无环无平行边的无向图。

    题解

    贪心做:
    从最大度数的那个点(假设度数是x)做开始,在剩下的数中选出度最大的前x个数,如果有哪个节点的度数为0则不可能构成图,否则把这些节点度数减1继续做。 直到所有的点的度数都为0。

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    
    int main(){	
    	int n;
    	while(scanf("%d",&n)==1&&n){
    		priority_queue<int> pq;
    		int x;
    		for(int i=0;i<n;i++){
    			scanf("%d",&x);
    			pq.push(x); 
    		}
    		bool su=1;
    		while(!pq.empty()){
    			int u=pq.top(); pq.pop();
    			if(u==0) break;
    			vector<int> pool;
    			for(int i=0;i<u;i++){
    				if(pq.empty()){
    					su=0; break;
    				}
    				int v=pq.top(); pq.pop();
    				if(v==0){
    					su=0; break;
    				}
    				v--;
    				pool.push_back(v);
    			}
    			if(!su) break;
    			for(int i=0;i<pool.size();i++){
    				pq.push(pool[i]);
    			}
    		}
    		if(su) puts("Possible");
    		else puts("Not possible"); 
    	} 
    	return 0;
    }
  • 相关阅读:
    vs 调试的时候 使用IP地址,局域网的设备可以访问并调试
    jQuery Easing 使用方法及其图解
    win10使用Composer-Setup安装Composer以及使用Composer安装Yii2最新版
    PHP 字符串数组按照拼音排序的问题
    yii2 查询数据库语法
    css禁用鼠标点击事件
    内容显示在HTML页面底端的一些处理方式
    UltraISO制作U盘启动盘
    Swift中使用MPMoviePlayerController实现自定义视频播放器界面
    关于dismissViewControllerAnimated值得注意的一点(deinit)
  • 原文地址:https://www.cnblogs.com/fenice/p/5671564.html
Copyright © 2011-2022 走看看