ACMer的爱 (拓扑排序)

来源:互联网 发布:js split join 编辑:程序博客网 时间:2024/06/05 04:21

问题 C: ACMer的爱

时间限制: 1 Sec  内存限制: 128 MB
提交: 64  解决: 11
[提交][状态][讨论版]

题目描述

七夕到了,又是一个浪漫的节日,情侣们都释放着对彼此的爱意。此刻,作为ACMer的Jack却甚是烦恼,他喜欢一个叫Lover女孩。但是喜欢Lover的不止他一个,但为了追到Lover,他想向Lover表达自己的爱意。但是其他人又怎么会放过这次机会呢?为了衡量谁的爱意,我们用一个叫爱意值的东西来计算爱意。爱意值最低为0,若A比B(A,B表示人名)更爱Lover表示为A  B,A比B的爱意值至少大1。若A的爱意值大于B,B的爱意值大于C,而C的爱意值又大于A,则这是不可能的,我们称这种情况叫做爱情死循环,那么谁也追不到Lover。Jack为了追到Lover至少得付出多少爱意值呢?作为ACMer的他,准备写一个program来求出自己最少的付出的爱意值。

输入

第一行输入T表示有T组测试数据,然后输入N(N<1000)和M(M<2000),N代表有N个人喜欢Lover,M表示这N个人有M种关系。接下来输入M行,每行有A B(A,B代表人名且长度小于25)表示A的爱意值大于B。

输出

若Jack能追到Lover则输出爱意值,若不能追到则输出-1。每个例子结束之后记得换行。

样例输入

34 3Alice BobSmith JohnAlice Smith5 5a cc dd eb ea d4 5a bb cd ca db a

样例输出

34-1
题解:找出有几层拓扑关系#include <cstdio>#include <cstring>#include <iostream>#include<cstdlib>#include <queue>#include<string>using namespace std;struct arcnode{    int to;    struct arcnode * next;};arcnode * list[1010];//表头int outdegree[1010];//出度string str[1010];int topsort(int n){    queue <int> q;    int count=0;    arcnode * temp;    for(int i=1;i<=n;i++)    {        if(outdegree[i]==0)        {            q.push(i);            count++;        }    }    int sum=0,ans=0;    if(q.empty()==1)//成环就没有出度为零的点        return 0;    for(int i=1,t=0;i<=count;i++)    {        if(q.empty()==1)            return 0;        int k=q.front();        q.pop();        temp=list[k];        while(temp!=NULL)//切断与出度为0有关的点        {            int j=temp->to;            if(--outdegree[j]==0)            {                q.push(j);//出度为0入队列                t++;            }            temp=temp->next;        }        if(i==count)//初始化        {            i=0;            sum+=count;            count=t;            t=0;            ans++;        }        if(sum==n)//每个点都入队列了            return ans;    }    return 0;}int main(){    int t;    cin>>t;    while(t--)    {        int n,m;        arcnode * temp;        cin>>n>>m;        memset(outdegree,0,sizeof(outdegree));        memset(list,0,sizeof(list));        int p=1;        while(m--)        {            string a,b;            cin>>a>>b;            string x1=a,y1=b;            int x,y;            for(int i=1;i<p;i++)//名字处理            {                if(a==str[i])                {                    x=i;                    a="\0";                }                if(b==str[i])                {                    y=i;                    b="\0";                }            }            if(a==x1)            {                str[p++]=x1;                x=p-1;            }            if(b==y1)            {                str[p++]=y1;                y=p-1;            }            outdegree[x]++;            temp=(struct arcnode *)malloc(sizeof(struct arcnode));//邻接表建图            temp->to=x;            temp->next=NULL;            if(list[y]==NULL)                list[y]=temp;            else            {                temp->next=list[y];                list[y]=temp;            }        }        int ans=topsort(n);        if(!ans)            cout<<-1<<endl;        else            printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击