hdu 4039 2011成都赛区网络赛I ***

两层搜索,直接for循环就行了,还要注意不能是自己的朋友

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<queue>
  7 #include<map>
  8 using namespace std;
  9 #define MOD 1000000007
 10 const int INF=0x3f3f3f3f;
 11 const double eps=1e-5;
 12 typedef long long ll;
 13 #define cl(a) memset(a,0,sizeof(a))
 14 #define ts printf("*****\n");
 15 const int MAXN=2050;
 16 int n,m,tt;
 17 char s1[100],s2[100];
 18 int vis[MAXN];
 19 map<string,int> mp;
 20 vector<int> vc[MAXN];
 21 int w;
 22 struct Str
 23 {
 24     char str[20];
 25 }s[MAXN],ss[MAXN];
 26 bool cmp1(Str a,Str b)
 27 {
 28     return strcmp(a.str,b.str)<0;
 29 }
 30 int sum=1,to,id,Max;
 31 bool flag[MAXN];
 32 int bfs(int x)
 33 {
 34     sum=0;
 35     id,Max=0;
 36     for(int i=1;i<=w;i++)    vis[i]=0,flag[i]=0;
 37     flag[x]=1;
 38     for(int i=0;i<vc[x].size();i++)
 39     {
 40         flag[vc[x][i]]=1;
 41     }
 42     for(int j=0;j<vc[x].size();j++)
 43     {
 44         int e=vc[x][j];
 45         for(int i=0;i<vc[e].size();i++)
 46         {
 47             int y=vc[e][i];
 48             if(!flag[y])
 49             {
 50                 vis[y]++;//
 51                 if(Max<vis[y])
 52                 {
 53                     Max=vis[y];
 54                     id=y;
 55                     ss[0]=s[y];
 56                     sum=1;
 57                 }
 58             }
 59         }
 60     }
 61     for(int i=1;i<=w;i++)
 62     {
 63         if(Max==vis[i]&&id!=i)
 64         {
 65             strcpy(ss[sum++].str,s[i].str);
 66         }
 67     }
 68     if(Max==0)  return 0;
 69     else return sum;
 70 }
 71 int main()
 72 {
 73     int i,j,k;
 74     #ifndef ONLINE_JUDGE
 75     freopen("1.in","r",stdin);
 76     #endif
 77     scanf("%d",&tt);
 78     int ca=1;
 79     while(tt--)
 80     {
 81         printf("Case %d:\n",ca++);
 82         scanf("%d%d",&n,&m);
 83         int tot=1;
 84         int u,v;
 85         mp.clear();
 86         for(i=0;i<=2*n;i++) vc[i].clear();
 87         for(i=0;i<n;i++)
 88         {
 89             scanf("%s%s",s1,s2);
 90             if(!mp[s1]) mp[s1]=tot++,u=tot-1,strcpy(s[tot-1].str,s1);
 91             else u=mp[s1];
 92             if(!mp[s2]) mp[s2]=tot++,v=tot-1,strcpy(s[tot-1].str,s2);
 93             else v=mp[s2];
 94             vc[u].push_back(v);
 95             vc[v].push_back(u);
 96         }
 97         w=tot;
 98         while(m--)
 99         {
100             scanf("%s",s1);
101             int q=bfs(mp[s1]);
102             if(q==1)
103             {
104                 cout<<ss[0].str<<endl;
105             }
106             else if(q>=2)
107             {
108                 sort(ss,ss+q,cmp1);
109                 cout<<ss[0].str;
110                 for(i=1;i<q;i++)
111                 {
112                     cout<<" "<<ss[i].str;
113                 }
114                 cout<<endl;
115             }
116             else
117             {
118                 printf("-\n");
119             }
120         }
121     }
122 }

 

posted @ 2015-08-06 17:17  miao_a_miao  阅读(136)  评论(0编辑  收藏  举报