hdu5813

来源:互联网 发布:剑网三南风捏脸数据 编辑:程序博客网 时间:2024/05/18 17:44

Elegant Construction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 401    Accepted Submission(s): 202
Special Judge


Problem Description
Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, biology, and even musicology as background. And now in this problem, you are being a city architect!
A city with N towns (numbered 1 through N) is under construction. You, the architect, are being responsible for designing how these towns are connected by one-way roads. Each road connects two towns, and passengers can travel through in one direction.

For business purpose, the connectivity between towns has some requirements. You are given N non-negative integers a1 .. aN. For 1 <= i <= N, passenger start from town i, should be able to reach exactly ai towns (directly or indirectly, not include i itself). To prevent confusion on the trip, every road should be different, and cycles (one can travel through several roads and back to the starting point) should not exist.

Your task is constructing such a city. Now it's your showtime!
 

Input
The first line is an integer T (T <= 10), indicating the number of test case. Each test case begins with an integer N (1 <= N <= 1000), indicating the number of towns. Then N numbers in a line, the ith number ai (0 <= ai < N) has been described above.
 

Output
For each test case, output "Case #X: Y" in a line (without quotes), where X is the case number starting from 1, and Y is "Yes" if you can construct successfully or "No" if it's impossible to reach the requirements.

If Y is "Yes", output an integer M in a line, indicating the number of roads. Then M lines follow, each line contains two integers u and v (1 <= u, v <= N), separated with one single space, indicating a road direct from town u to town v. If there are multiple possible solutions, print any of them.
 

Sample Input
332 1 021 143 1 1 0
 

Sample Output
Case #1: Yes21 22 3Case #2: NoCase #3: Yes41 21 32 43 4
题意:给你n个城市,告诉你每个城市可以到达其他城市的个数,让你输出路径,要求:路径是单向的,并且路径不能有环
将顶点按能到达的点数从小到大排序,排好序之后每个点只能往前面的点连边. 因而如果存在一个排在第i位的点,要求到达的点数大于i-1,
则不可行;否则就可以按照上述方法构造出图. 复杂度O(N^2).构造图时,按照拓扑排序构图
具体见代码
#include <iostream>#include <stack>#include <stdio.h>#include <string.h>#include <algorithm>#include <vector>using namespace std;const int maxn=1e5+5;struct node{    int id,num;    node(){;}    node(int a ,int b)    {        id=a;        num=b;    }}d[maxn];bool cmp(node a,node b){    return a.num<b.num;}vector<node>ans;int main(){    int n;    int T=1,t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%d",&d[i].num);            d[i].id=i+1;        }        printf("Case #%d: ",T++);        ans.clear();         sort(d,d+n,cmp);         int flag=0;         for(int i=0;i<n;i++)         {             if(d[i].num>i)                flag=1;         }         if(flag)         {             printf("No\n");             continue;         }         printf("Yes\n");         for(int i=0;i<n;i++)         {             for(int j=i+1;j<n;j++)             {                 if(d[i].num==0)                 {                     if(d[j].num>0)                     {                         d[j].num--;                         ans.push_back(node(d[j].id,d[i].id));                     }                 }             }         }         printf("%d\n",ans.size());         for(int i=0;i<ans.size();i++){            printf("%d %d\n",ans[i].id,ans[i].num);         }    }    //cout << "Hello world!" << endl;    return 0;}


0 0
原创粉丝点击