【BFS】ural 1837

來源:互聯網
上載者:User

貢獻了一次CE!timus竟然不認<string.h>!

#include <vector>#include <list>#include <map>#include <set>#include <queue>#include <string>#include <deque>#include <stack>#include <algorithm>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <limits.h>#include <time.h>#include <string.h>using namespace std;int lowbit(int t){return t&(-t);}int countbit(int t){return (t==0)?0:(1+countbit(t&(t-1)));}int gcd(int a,int b){return (b==0)?a:gcd(b,a%b);}#define LL long long#define PI acos(-1.0)#define N  301#define MAX INT_MAX#define MIN INT_MIN#define eps 1e-8#define FRE freopen("a.txt","r",stdin)struct node{    string str;    int id;    bool vis;}name[N];vector<int> v[N];int cnt;int gao(string a){    int i,j;    for(i=0;i<cnt;i++)    if(name[i].str==a)    return i;    name[cnt++].str=a;    return cnt-1;}bool cmp(node a,node b){    return a.str<b.str;}void bfs(int k){    queue<int> q;    q.push(k);    int i,j;    name[k].id=0;    name[k].vis=1;    while(!q.empty()){        int x=q.front();        q.pop();        for(i=0;i<v[x].size();i++){            int y=v[x][i];            if(name[y].vis==0)            {                q.push(y);                name[y].vis=1;                name[y].id=name[x].id+1;            }        }    }}int main(){    int n;    while(scanf("%d",&n)!=EOF){        int i,j,k;        for(i=0;i<=3*n;i++){            v[i].clear();            name[i].id=-1;            name[i].vis=0;        }        cnt=0;        for(i=1;i<=n;i++){            string a,b,c;            cin>>a>>b>>c;            int id1=gao(a);            int id2=gao(b);            int id3=gao(c);            v[id1].push_back(id2);            v[id1].push_back(id3);            v[id2].push_back(id3);            v[id2].push_back(id1);            v[id3].push_back(id1);            v[id3].push_back(id2);        }        for(i=0;i<cnt;i++)        if(name[i].str=="Isenbaev"){            bfs(i);            break;        }        sort(name,name+cnt,cmp);        for(i=0;i<cnt;i++){            cout<<name[i].str<<" ";            if(name[i].id==-1)puts("undefined");            else            cout<<name[i].id<<endl;        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.