Elegant Construction---hdu5813(构造图)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5813

题意是:有n个点,每个点都能到达num个点,让我们构造任意一个有向图满足条件,即:使得 i 能到达 a[i] 个点;

将顶点按能到达的点数从小到大排序,排好序之后每个点只能往前面的点连边. 所以我们必须要让前面点的个数大于或等于它要连得点的个数;

因而如果存在一个排在第i位的点(前面有i-1个点),i-1>=要求到达的点数(i>要求到达的点数);

按照上述方法构造出图. 复杂度O(N^2).

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
using namespace std;
#define N 1005
#define met(a, b) memset(a, b, sizeof(a))

typedef long long LL;

struct node
{
    int Id, num;
    friend bool operator < (node p, node q)
    {
        return p.num < q.num;
    }
}a[N];

int u[N*N], v[N*N];

int main()
{
    int T, t = 1, n;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);

        for(int i=1; i<=n; i++)
        {
            scanf("%d", &a[i].num);
            a[i].Id = i;
        }
        sort(a+1, a+n+1);

        int k = 0, flag = 0;

        for(int i=n; i>=1; i--)
        {
            if(a[i].num >= i)///前面点的个数必须要大于它要到达的点的个数;
            {
                flag = 1;
                break;
            }
            for(int j=1; j<=a[i].num; j++)///由于只需输出一组符合条件的解即可,所以我们可以用a[i].Id连接num条任意的a[j].Id(j < i);
            {
                u[k] = a[i].Id;
                v[k++] = a[j].Id;
            }
        }
        if(flag)
            printf("Case #%d: No\n", t++);
        else
        {
            printf("Case #%d: Yes\n", t++);

            printf("%d\n", k);
            for(int i=0; i<k; i++)
                printf("%d %d\n", u[i], v[i]);
        }
    }
    return 0;
}
/*
3
3
2 1 0
2
1 1
4
3 1 1 0
*/

时间: 2024-08-10 17:17:20

Elegant Construction---hdu5813(构造图)的相关文章

hdu-5813 Elegant Construction(贪心)

题目链接: Elegant Construction Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, biology, and even

HDU 5813 Elegant Construction(优雅建造)

HDU 5813 Elegant Construction(优雅建造) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description 题目描述 Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, biology, and

HDU 5813 Elegant Construction

构造.从a[i]最小的开始放置,例如放置了a[p],那么还未放置的,还需要建边的那个点 需求量-1,然后把边连起来. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

【CV论文阅读】An elegant solution for subspace learning

Pre: It is MY first time to see quite elegant a solution to seek a subspace for a group of local features. I list two related papers for your reference: "Local Feature Discriminant Projection" and "Binary Set Embedding for Cross-Modal Retri

POJ 3352 Road Construction

It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead

lvl plywood or solid wood is best for cajon construction

Solid Woodvs Lvl plywood If you're here, it's most likelybecause you need to know what kind of wood to use when buildingyour own cajon. There is some debate about whether lvl plywood or solid wood is best for cajon construction and even further so, w

bnu 34985 Elegant String(矩阵快速幂+dp推导公式)

Elegant String Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory      2-SAT     Articulation/Bridge/Biconnected Component      Cy

POJ 3352 Road Construction(图论-tarjan)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8647   Accepted: 4318 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro