Kidnapping
Berland's Police has a serious problem. A foreign ambassador arrived to Berland with an important mission, and his daughter was kidnapped just from the Royal Palace! Inspired by adventures of Erast Fandorin, the Police Chief developed the following ingenious plan.
The ambassador agrees to pay ransom, but only if the kidnappers allow his servant to visit the girl and ensure that she is alive. The kidnappers take the blindfolded servant into a coach and transport him to the secret place, where they keep the ambassador's
daughter. Certainly, the role of the servant is certainly played by a secret agent of the Police. The Police Chief knows that when the coach is moving, the wheels are creaking once on each full rotation. So, by counting the number of creaks and multiplying
it by the length of the rim, one can easily calculate the distance covered by the coach.
In spite of this brilliant idea, the affair turned to be much more difficult than it could be in a detective story. There are n intersections in the city numbered from 1 to n, some pairs of intersections are connected by bidirectional roads. The kidnappers
agreed to take the "servant" to the secret place, and the servant is quite sure that this place is located at one of the intersections. Also the agent has calculated the lengths of roads between each pair of consecutive intersections on the route passed by
the coach. But during the trip the agent was concentrated on counting creaks, so he could not remember in which directions the coach turned at the intersections.
Now the route probably couldn't be restored uniquely! Moreover, the agent has a suspicion that the kidnappers could intentionally pass the same intersection or even the same road more than once to confuse the Police.
Your task is to determine all possible locations of the secret place, given that the trip starts at the intersection number 1.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 200). Each of the next n lines contains n integers each. The i-th number in the j-th line l ij is the length of the road between the i-th and the j-th intersections. If l ij = 0 then the road
doesn't exist.
It is guaranteed that 0 ≤ l ij ≤ 200, l ii = 0 and l ij = l ji. The next line contains one integer k (1 ≤ k ≤ 200) — the number of roads passed by the couch. The following line contains k integers r 1, r 2,..., r k (1 ≤ r i ≤ 200) — the lengths of roads between
each pair of consecutive intersections on the route passed by the coach from the starting point to the secret place.
Output
To the first line of the output write m — the number of all possible locations of the secret place. The second line should contain the numbers of intersections in increasing order separated by spaces.
If there are no possible locations of the secret place, the output must contain the only integer 0.
Example(s)
sample input
4
0 1 2 0
1 0 1 0
2 1 0 2
0 0 2 0
3
1 1 2
sample output
3
1 3 4
题意:起点在1,给出每条路的长度,求出可能的终点。
#include<map> #include<stack> #include<queue> #include<math.h> #include<vector> #include<string> #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #define mem(a,b) memset(a,b,sizeof(a)) #define maxn 205 #define maxm 200005 #define mod 1000000007 #define ll long long #define inf 0x3f3f3f3f using namespace std; int a[maxn][maxn]; int c[maxn][maxn]; int b[maxn]; int ans[maxn]; int n,m,k; void dfs(int x,int y){ if(y==m){ans[k++]=x+1;return;} if(y>=m)return; for(int i=0;i<n;i++){ if(a[x][i]==b[y]&&c[i][y+1]==0){ c[i][y+1]=1; dfs(i,y+1); } } } int main(){ while(~scanf("%d",&n)){ mem(a,0);mem(b,0);mem(ans,0);mem(c,0); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ scanf("%d",&a[i][j]); } } scanf("%d",&m); for(int i=0;i<m;i++){ scanf("%d",&b[i]); } k=0;dfs(0,0); printf("%d ",k); if(k!=0){ sort(ans,ans+k); for(int i=0;i<k;i++){ if(i!=0)printf(" "); printf("%d",ans[i]); }printf(" "); } } }