题意理解:这道题就是给出了一个图,然后又很多次查询,查询中给出了一些点,如果这些点任意两点之间的最短路径,经过的点都算上。能够凑满整个图那就是yes,否则就是no。
解题思路:我们就是要找最短路,找到任意两点之间的最短路,因为点最多有40个,我们可以用一个 long long 来存看是否经过了所有的点。
Description
Let G = (V,E) be a connected graph without loops and multiple edges, where V and E are the vertex and edge, respectively, sets of G. For any two vertices u, v ∈ V ,the distance between vertices u and v in G is the number of edges in a shortest u-v path. A shortest path between u and v is called a u-v geodesic. Let I(u, v) denote the set of vertices such that a vertex is in I(u, v) if and only if it is in some u-v geodesic of G and, for a set S <= V , I(S) =
∪
u,v∈SI(u, v). A vertex set D in graph G is called a geodetic set if I(D) = V . The geodetic set problem is to verify whether D is a geodetic set or not. We use Figure 3 as an example. In Figure 3, I(2, 5) = {2, 3, 4, 5} since there are two shortest paths between vertices 2 and 5. We can see that vertices 3 and 4 are lying on one of these two shortest paths respectively. However, I(2, 5) is not a geodetic set since I(2, 5) != V . Vertex set {1, 2, 3, 4, 5} is intuitively a geodetic set of G. Vertex set D = {1, 2, 5} is also a geodetic set of G since vertex 3 (respectively,vertex 4) is in the shortest path between vertices 1 and 5 (respectively, vertices 2 and 5). Thus, I(D) = V . Besides, vertex sets {1, 3, 4} and {1, 4, 5} are also geodetic sets.
However, D = {3, 4, 5} is not a geodetic set since vertex 1 is not in I(D).
However, D = {3, 4, 5} is not a geodetic set since vertex 1 is not in I(D).
Input
The input file consists of a given graph and several test cases. The first line contains an integer n indicating the number of vertices in the given graph,where 2 <= n <= 40. The vertices of a graph are labeled from 1 to n. Each vertex has a distinct label. The following n lines represent the adjacent vertices of vertex i, i = 1, 2, ... , n. For example, the second line of the sample input indicates that vertex 1 is adjacent with vertices 2 and 3. Note that any two integers in each line are separated by at least one space. After these n lines, there is a line which contains the
number of test cases. Each test case is shown in one line and represents a given subset D of vertices. You have to determine whether D is a geodetic set or not.
number of test cases. Each test case is shown in one line and represents a given subset D of vertices. You have to determine whether D is a geodetic set or not.
Output
For each test case, output 'yes' in one line if it is a geodetic set or 'no' otherwise.
Sample Input
5 2 3 1 3 4 1 2 5 2 5 3 4 6 1 2 3 4 5 1 2 5 2 4 1 3 4 1 4 5 3 4 5
Sample Output
yes yes no yes yes no
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
#define inf 0x3fffffff
int map_[100][100];
int book2[100];
long long f[100][100];
int main()
{
int n;
scanf("%d",&n);
memset(f,0,sizeof(f));
for(int i=1;i<50;i++)
{
for(int j=1;j<50;j++)
{
if(i==j)
{
map_[i][j]=0;
}
else
{
map_[i][j]=inf;
}
}
}
for(int i=1;i<=n;i++)
{
while(1)
{
int x;
scanf("%d",&x);
map_[i][x]=1;
char c=getchar();
if(c=='\n')
break;
}
}
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(map_[i][j]>map_[i][k]+map_[k][j])
{
map_[i][j]=map_[i][k]+map_[k][j];
}
}
}
}
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(map_[i][j]==map_[i][k]+map_[k][j])
{
f[i][j]|=((long long)1<<(long long)(k-1));
}
}
}
}
int q;
scanf("%d",&q);
for(int i=0;i<q;i++)
{
int a[50];
int k=0;
while(1)
{
scanf("%d",&a[k]);
k++;
char c=getchar();
if(c=='\n')
break;
}
long long ans=0;
for(int p=0;p<k;p++)
{
for(int j=0;j<k;j++)
{
ans|=(long long )f[a[p]][a[j]];
}
}
int cnt=0;
while(ans)
{
if(ans&1)
{
cnt++;
}
ans=ans>>1;
}
if(cnt==n)
{
printf("yes\n");;
}
else
{
printf("no\n");
}
}
}