UVA 10199

来源:互联网 发布:免费进销存软件排名 编辑:程序博客网 时间:2024/06/06 02:21

 Problem F: Tourist Guide 

The Problem

Rio de Janeiro is a very beautiful city. But there are so many places to visit that sometimes you feel a bit lost. But don't worry, because your friend Bruno promised you to be your tourist guide. The problem is that he is not a very good driver, as he can't see very well (poor Bruno).

Because of his disabilities, Bruno have a lot of fines to pay and he doesn't want to have even more to pay, even though he promised you to be your tourist guide. What he would like to know, however, is where are all the cameras that help the police to fine the bad drivers, so he can drive more carefully when passing by them.

Those cameras are strategically distributed over the city, in locations that a driver must pass through in order to go from one zone of the city to another. In order words, if there are two city locations A and B such that to go from one to another (A to B or B to A) a driver must pass through a location C, then C will have a camera.

For instance, suppose that we have 6 locations (A, B, C, D, E and F) and that we have 7 routes (all of them bidirectonal) B-C, A-B, C-A, D-C, D-E, E-F and F-C. There's a camera on C because to go from A to E, for instance, you must pass through C. In this configuration, there's only one camera (on C).

Your task is to help Bruno (as he wants to be a musician and he doesn't want to get even close of computers) and write a program which will tell him where are all the cameras, given the map of the city, so he can be your tourist guide and avoid further fines.

The Input

The input will consist on an arbitrary number of city maps. Each city map will begin with an integer N (2 < N <= 100) which stands for the total locations of the city. Then will follow N different place names, one per line. Each place name will have at least one and at most 30 characters (all of them will be lowercase latin letters). Then will follow a non-negative integer R which stands for the total routes of the city. Then will follow R lines with the routes. Each route will be represented by the name of both places that the route connects (remember that the routes are bidirectional). Location names in route description will always be valid and there will be no route from one place to itself. You must read until N = 0, and this input should not be processed.

The Output

For each city map you must print the line:

City map #d: c camera(s) found
Where d stands for the city map number (starting from 1) and c stands for the total number of cameras. Then should follow c lines with the location names where are each camera (in alphabetic order). You should print a blank line between output sets.

Sample Input

6sugarloafmaracanacopacabanaipanemacorcovadolapa7ipanema copacabanacopacabana sugarloafipanema sugarloafmaracana lapasugarloaf maracanacorcovado sugarloaflapa corcovado5guanabarabaydowntownbotanicgardencolombosambodromo4guanabarabay sambodromodowntown sambodromosambodromo botanicgardencolombo sambodromo0

Sample Output

City map #1: 1 camera(s) foundsugarloafCity map #2: 1 camera(s) foundsambodromo

© 2001 Universidade do Brasil (UFRJ). Internal Contest 2001.


本题数据规模非常小,不用想,直接模拟枚举每个城市并删除,再枚举其他城市,并以之为起点做一次dfs,看是否有原本可以到达的城市现在变为不可到达即可,至于原本可以到达的城市可以用并查集维护(图是无向图)。


#include<cstdio>#include<cstring>#include<vector>#include<cstdlib>#include<algorithm>#include<map>#include<iostream>#include<string>#define maxn 109using namespace std;vector<int>G[maxn];map<string,int>mp;bool cut[maxn],del[maxn];bool vis[maxn];int p[maxn];int findset(int x){    return x==p[x]?x:p[x]=findset(p[x]);}void unionset(int x,int y){    p[findset(x)]=findset(y);}int n,m;void dfs(int cur){    vis[cur]=1;    for(int i=0;i<G[cur].size();i++)    {        int v=G[cur][i];        if(!vis[v]&&!del[v])            dfs(v);    }}int main(){    int cot=0,f=0;    while(scanf("%d",&n),n)    {        cot++;        for(int i=1;i<=n;i++)G[i].clear();mp.clear();        for(int i=1;i<=n;i++)p[i]=i;        for(int i=1;i<=n;i++)        {            string s;            cin>>s;            mp[s]=i;        }        scanf("%d",&m);        for(int i=0;i<m;i++)        {            string s1,s2;            cin>>s1>>s2;            G[mp[s1]].push_back(mp[s2]);            G[mp[s2]].push_back(mp[s1]);            unionset(mp[s1],mp[s2]);        }        int ans=0;        memset(cut,0,sizeof(cut));        for(int i=1;i<=n;i++)        {            bool ok=1;del[i]=1;            for(int j=1;j<=n;j++)            if(j!=i){            memset(vis,0,sizeof(vis)),dfs(j);            for(int k=1;k<=n;k++)                if(i!=k&&!vis[k]&&findset(k)==findset(j))            {                ok=0;break;            }            if(!ok){cut[i]=1;ans++;break;}           }            del[i]=0;        }        if(f)printf("\n");f=1;        printf("City map #%d: %d camera(s) found\n",cot,ans);        for(map<string,int>::iterator it=mp.begin();it!=mp.end();++it)            if(cut[it->second])            cout<<it->first<<endl;    }}

© 2001 Universidade do Brasil (UFRJ). Internal Contest 2001.
原创粉丝点击