zoukankan      html  css  js  c++  java
  • vijos p1876 bfs+map

    题意:

    Xiaodao是一位喜欢参加ACM比赛的孩子.
    所谓ACM比赛, 是一种团队比赛.
    每一次比赛, 每队需要由恰好三位选手组成.
    现在, Xiaodao希望组建一支新的队伍, 在这之前, 他需要知道每一位朋友有多少可能成为自己的好队友.

    他计划给每一位朋友做出一个等级标号.
    Xiaodao本人的等级标号为0.
    如果一位朋友曾经和Xiaodao组队参加过比赛, 那么就标号为1.
    如果一位朋友并没有与Xiaodao组队参加过比赛, 但是曾经与一位"与Xiaodao一起参加过比赛的人"组队参加过比赛. 那么就标号为2.
    如果一位朋友无法标号为小于等于 k 的整数, 但是曾经与"标号为k的人"一起参加过比赛, 那么便可以标号为k+1.
    其余的朋友们, 便无法给出编号, 我们记为"undefined".

    现在, 我们给出了 n 组曾经参赛过的队伍, 每一组中给出了三位选手的名字.
    我们希望知道每一位涉及到的选手应该被给予什么样的标号.

    链接:点我

    邻接表和邻接矩阵都没满分

     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("*****
    ");
    15 const int MAXN=10005;
    16 int n,m,tt,u,tot;
    17 int c[MAXN][MAXN],dist[MAXN],vis[MAXN];
    18 string a[MAXN];
    19 struct Node
    20 {
    21     int to,next;
    22 }edge[MAXN*6];
    23 int head[MAXN];
    24 int tol;
    25 void init()
    26 {
    27     tol=0;
    28     memset(head,-1,sizeof(head));
    29 }
    30 void addedge(int a,int b)
    31 {
    32     edge[tol].to=b;
    33     edge[tol].next=head[a];
    34     head[a]=tol++;
    35     edge[tol].to=a;
    36     edge[tol].next=head[b];
    37     head[b]=tol++;
    38 }
    39 void bfs()
    40 {
    41     queue<int> q;
    42     cl(vis);
    43     vis[u]=1;
    44     dist[u]=0;
    45     int now,next;
    46     q.push(u);
    47     while(!q.empty())
    48     {
    49         now=q.front();
    50         q.pop();
    51         for(int i=head[now];i!=-1;i=edge[i].next)
    52         {
    53             next=edge[i].to;
    54             if(!vis[next])
    55             {
    56                 dist[next]=dist[now]+1;
    57                 vis[next]=1;
    58                 q.push(next);
    59             }
    60         }
    61     }
    62 }
    63 map<string,int> mp;
    64 string s="Xiaodao";
    65 int main()
    66 {
    67     int i,j,k;
    68     /*#ifndef ONLINE_JUDGE
    69     freopen("1.in","r",stdin);
    70     #endif*/
    71     scanf("%d",&n);
    72     tot=1;
    73     cl(c);
    74     string s1,s2,s3;
    75     bool flag=0;
    76     init();
    77     int i1,i2,i3;
    78     for(i=0;i<n;i++)
    79     {
    80         cin>>s1,cin>>s2,cin>>s3;
    81         if((s1==s||s2==s||s3==s)&&!flag)   u=tot,flag=1;
    82         i1=mp[s1],i2=mp[s2],i3=mp[s3];
    83         if(!mp[s1])    a[tot]+=s1,i1=mp[s1]=tot++;
    84         if(!mp[s2])    a[tot]+=s2,i2=mp[s2]=tot++;
    85         if(!mp[s3])    a[tot]+=s3,i3=mp[s3]=tot++;
    86         addedge(i1,i2),addedge(i1,i3),addedge(i2,i3);
    87     }
    88     bfs();
    89     sort(a+1,a+tot+1);
    90     for(i=1;i<=tot;i++)
    91     {
    92         int l=a[i].length();
    93         if(l==0)    continue;
    94         for(j=0;j<l;j++)printf("%c",a[i][j]);
    95         int id=mp[a[i]];
    96         if(!vis[id])    printf(" undefined
    ");
    97         else printf(" %d
    ",dist[id]);
    98     }
    99 }
  • 相关阅读:
    定制选择范围的按钮RangeButton
    获取系统屏幕尺寸参数的类WxHxD
    解决UITableView在iOS7中UINavigationController里的顶部留白问题
    [翻译] INSSearchBar
    可以简易设置文字内边距的EdgeInsetsLabel
    [翻译] STAlertView
    keyWindow与delegate中Window的区别
    定制二选一按钮SwitchButton
    【转】Xcode真机调试初体验
    【转】Xcode真机测试could not find developer disk image解决方法
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4515756.html
Copyright © 2011-2022 走看看