#include<bits/stdc++.h>
using namespace std;
map<int,vector<int> >rana;
long a,c,stt,ln,i;
long dis[101];
void bfs()
{
queue<pair<int,int> >q;
pair<int,int>p;
q.push(make_pair(c,0));
while(!q.empty())
{
p=q.front();
q.pop();
if(p.second>dis[p.first])
{
dis[p.first]=p.second;
if(p.second>ln)
{
ln=p.second;
stt=p.first;
}
else if(p.second==ln&&p.first<stt)
stt=p.first;
for(i=0; i<rana[p.first].size(); i++)
{
q.push(make_pair(rana[p.first][i],p.second+1));
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
long b=0;
while(cin>>a&&a)
{
rana.clear();
cin>>c;
long d,e;
while(cin>>d>>e)
{
if(d==0&&e==0)
break;
rana[d].push_back(e);
}
memset(dis,-1,sizeof(dis));
stt=c;
ln=0;
bfs();
cout<<"Case "<<++b<<": The longest path from "<<c<<" has length "<<ln<<", finishing at "<<stt<<"."<<endl<<endl;
}
return 0;
}
using namespace std;
map<int,vector<int> >rana;
long a,c,stt,ln,i;
long dis[101];
void bfs()
{
queue<pair<int,int> >q;
pair<int,int>p;
q.push(make_pair(c,0));
while(!q.empty())
{
p=q.front();
q.pop();
if(p.second>dis[p.first])
{
dis[p.first]=p.second;
if(p.second>ln)
{
ln=p.second;
stt=p.first;
}
else if(p.second==ln&&p.first<stt)
stt=p.first;
for(i=0; i<rana[p.first].size(); i++)
{
q.push(make_pair(rana[p.first][i],p.second+1));
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
long b=0;
while(cin>>a&&a)
{
rana.clear();
cin>>c;
long d,e;
while(cin>>d>>e)
{
if(d==0&&e==0)
break;
rana[d].push_back(e);
}
memset(dis,-1,sizeof(dis));
stt=c;
ln=0;
bfs();
cout<<"Case "<<++b<<": The longest path from "<<c<<" has length "<<ln<<", finishing at "<<stt<<"."<<endl<<endl;
}
return 0;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন