Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5813
Test instructions is: There are n points, each point can reach num points, let us construct any one to meet the conditions, that is: so I can reach a[i] points;
The vertices are sorted from small to large by the number of points that can be reached, and each point can only be connected to the front point when ordered. So we have to make the number of points above or equal to the number of points it is to be connected;
Thus, if there is a point at the i-1 (preceded by a point), the i-1>= requires the arrival of the points (i> required to reach the points);
The diagram is constructed according to the above method. Complexity O (n^2).
#include <cstring>#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>#include<stack>#include<vector>#include<queue>using namespacestd;#defineN 1005#defineMet (A, b) memset (A, B, sizeof (a))typedefLong LongLL;structnode{intId, num; FriendBOOL operator<(node p, node Q) {returnP.num <Q.num; }}a[n];intU[n*n], v[n*N];intMain () {intT, T =1, N; scanf ("%d", &T); while(t--) {scanf ("%d", &N); for(intI=1; i<=n; i++) {scanf ("%d", &a[i].num); A[i]. Id=i; } sort (A+1, a+n+1); intK =0, flag =0; for(intJ=m; i>=1; i--) { if(A[i].num >= i)///the number of previous points must be greater than the number of points it is to reach;{flag=1; Break; } for(intj=1; j<=a[i].num; J + +)///we can use a[i] because we just output a set of qualified solutions. ID to connect any of the Num bar 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(intI=0; i<k; i++) printf ("%d%d\n", U[i], v[i]); } } return 0;}/*332 1 021 143 1 1 0*/
View Code
Elegant Construction---hdu5813 (tectonic chart)