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!
InputThe 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.OutputFor 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
3 3 2 1 0 2 1 1 4 3 1 1 0
Sample Output
Case #1: Yes 2 1 2 2 3 Case #2: No Case #3: Yes 4 1 2 1 3 2 4 3 4
启发来自于队内大佬余神和楼主
首先判读能否构成这样的有向无环图,对拿到的出度进行排序,然后看比一个点出度小的点数有没有小于等于它本身的出度,如果有大于的则输出no,全部小于等于则输出yes
判断yes后,有一个最傻瓜的思路,就是全先连0,然后连1,然后连下去。因为小的数连完以后再连大的数之后就不用考虑大数所连数,直接+1即可。
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<vector> 5 #include<cstring> 6 #include<string> 7 #include<algorithm> 8 using namespace std; 9 10 11 int b[1005]; 12 13 struct node 14 { 15 int u,v; 16 node(int x,int y){u=x;v=y;} 17 }; 18 19 struct node2 20 { 21 int num,id; 22 }a[1005]; 23 24 bool cmp(node2 m,node2 n) 25 { 26 return m.num<n.num; 27 } 28 int main() 29 { 30 int T; 31 scanf("%d",&T); 32 for(int t=1;t<=T;t++) 33 { 34 int n; 35 scanf("%d",&n); 36 for(int i=1;i<=n;i++) 37 { 38 scanf("%d",&a[i].num); 39 a[i].id=i; 40 } 41 printf("Case #%d: ",t); 42 sort(a+1,a+n+1,cmp); 43 memset(b,0,sizeof(b)); 44 int s=0,tmp=1; 45 for(int i=2;i<=n;i++) 46 { 47 48 if(a[i].num==a[i-1].num) 49 { 50 b[i]=s; 51 tmp++; 52 } 53 else 54 { 55 s+=tmp; 56 b[i]=s; 57 tmp=1; 58 } 59 } 60 bool flag=true; 61 for(int i=1;i<=n;i++) 62 if(a[i].num>b[i]) 63 { 64 flag=false; 65 break; 66 } 67 if(!flag) 68 printf("No "); 69 else 70 { 71 printf("Yes "); 72 queue<node>Q; 73 int num=0; 74 for(int i=1;i<=n;i++) 75 { 76 for(int j=1;j<=a[i].num;j++) 77 { 78 Q.push(node(a[i].id,a[j].id)); 79 num++; 80 } 81 } 82 printf("%d ",num); 83 while(!Q.empty()) 84 { 85 printf("%d %d ",Q.front().u,Q.front().v); 86 Q.pop(); 87 } 88 } 89 } 90 return 0; 91 }