[Blue Bridge Cup] [Algorithm to improve VIP] 3000 meters ranking prediction

Time Limit: 1Sec Memory Limit: 128MB Commits: 12 Resolved: 10

Topic description
During the 3,000-meter long run, the onlookers happily predicted the final ranking. Because they came from different classes, they didn't necessarily know all the athletes, so they made an assessment of the strength of some athletes they knew, that is, made a prediction of the relative ranking of some athletes, and told the poor left-behind class leader. Because they were bored, they formed a group to play Dota. After the game they asked the squad leader about the final ranking, but the squad leader couldn't remember, only which of them was right and who was wrong. They want to know what the ranking of the competition might be. 

Data scale and conventions 
  1< =n< =10, 2< =c< =n, 1< =m< =10, ensure that the data is legal, and the number of possible rankings in the answer does not exceed 20,000. For a ranking sequence, a prediction is correct if and only if the relative order of the predicted rankings is a subsequence of the ranking sequence. A prediction is wrong if and only if the prediction is incorrect. 
enter
The first line contains two integers n, m, where n is the number of athletes, and m is the number of onlookers. Athletes are numbered from 0 to n-1. 
Next m lines, each line predicts the relative ranking of one onlooker party. The first number c in each line represents the number of people he predicted, followed by c different numbers from 0 to n-1, indicating the relative ranking of the athletes he predicted, and there is a number at the end, 0 means the prediction is wrong, 1 Indicates that it is correct. 
output
In the first line, a number k is the number of possible rankings. 
The following k lines, each line is an arrangement of 0~n-1, which is a possible ranking, and the adjacent numbers are separated by spaces. All rankings are output sequentially in lexicographical order. 
sample input
3 2
2 0 1 1
2 1 2 0
Sample output
2
0 2 1
2 0 1
//The ans[][] array here is initially set to ans[maxn][maxn]. I want to hammer myself to death. The title says it is within 20000, so I set it to 20. 
#include<cstdio> #include < set > #include <iostream> #include <algorithm> using namespace std; const int maxn = 20 ; int a[maxn][maxn],b[maxn],ans[ 20001 ][maxn ],cnt; set < int > pre[maxn]; int n,m; bool vis[maxn]; void DFS( int index, int u) { if(index==n) { for(int i=1;i<=m;i++) { int yes=0; if(a[i][a[i][0]+1]==1) continue; int k=1,t=0,c=a[i][0]; while(t!=n) { if(a[i][k]!=b[t]) t++; else k++,t++; if(k==c+1){ yes=1; break; } } if(yes) return ; } cnt++; for(int i=0;i<n;i++) ans[cnt][i]=b[i]; return ; } for(int v=0;v<n;v++) { if(vis[v]==false) { int yes=1; for(int i=0;i<index;i++) { if(pre[b[i]].find(v)!=pre[b[i]].end()) yes=0; } if(yes) { vis[v] = true ; b[index]=v; DFS(index+1,v); vis [v] = false ; } } } } int main(void) { cin>>n>>m; for(int i=1;i<=m;i++) { cin>>a[i][0]; for(int j=1;j<=a[i][0]+1;j++) cin>>a[i][j]; if(a[i][a[i][0]+1]!=0) for(int j=1;j<=a[i][0];j++) { int u=a[i][j],v; for(int k=1;k<j;k++) { v=a[i][k]; pre[u].insert(v); } } } DFS(0,11); cout<<cnt<<endl; for(int i=1;i<=cnt;i++) { for(int j=0;j<n;j++) cout<<ans[i][j]<<" "; cout<<endl; } return 0; }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324866266&siteId=291194637