10000 - Longest Paths

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

New poster
Posts: 21
Joined: Fri Dec 05, 2003 5:59 pm

10000 time exceed

Post by lky »

i use dfs to search......but time exceed.....is there a way to minimize time without rewriteing?

Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel »

Yes there is.

First consider all the nodes that can be reached in one move.
And then nodes that can be reached in two moves - whose source is definitely nodes reachable in one move.

In this way keep increasing the length and find the answer. :wink:

New poster
Posts: 15
Joined: Tue Jun 10, 2003 7:30 pm

Post by czar »

I was wondering if anyone could tell me what is wrong with my code or if I am even using the right algorithm.

I just topological sort the vertices.


using namespace std;

vector<vector<int> > g;
vector<int> indeg;
int maxl, minv;

vector<int> toposort(int start, int nv) {
int depth = -1;
int mv;
vector<int> ret(2);
queue<int> q;


while(!q.empty()) {
int qs = q.size();
mv = INT_MAX;

for(int i = 0; i < qs; i++) {
int next = q.front(); q.pop();
mv = min(mv, next);
vector<int>::iterator s = g[next].begin(), e = g[next].end();

//cout<<"NEXT: "<<next<<endl;

while(s != e) {
if(!indeg[*s]) {
//cout<<" "<<*s<<endl;
//cout<<"DEPTH: "<<depth<<endl;
ret[0] = depth; ret[1]=mv;
return ret;

int main() {
int nv;
int start;
int i = 1;

while( cin>>nv ) {
if(nv == 0) break;

g = vector<vector<int> >(nv+1, vector<int>());
indeg = vector<int>(nv+1);
vector<int> ret;

int start;
int from, to;

cin>>start; cin.ignore();
cin>>from>>to; cin.ignore();

while(from != 0 || to != 0) {
cin>>from>>to; cin.ignore();

ret = toposort(start, nv);
cout<<"Case "<<i<<":"<<" The longest path from "<<start<<" has length "<<ret[0]<<", finishing at "<<ret[1]<<".\n\n";
return 0;

Learning poster
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.

Using BFS

Post by Sajid »

I solved this problem using DFS long ago.
After the rejudgement, I got TLE.

Now, I used BFS to solve this problem. But its get RTE always. I increased the Queue size a lot. but still RTE. (Using Circuler Queue, I got WA).

What can I do now?
Last edited by Sajid on Sat Dec 13, 2003 12:54 pm, edited 1 time in total.
Sajid Online: www.sajidonline.com

Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Post by Observer »

You don't need a long long queue here. Just... use a BIG boolean array (of size[1..100, 1..100]) to store visited states, thus you may avoid visiting the same state more than once. Think about it.

P.S. By "state" here I mean "visiting node I with distance J".
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

Learning poster
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.

Post by Sajid »

I did so, but now gets WA...What is the problem now? :-?
Sajid Online: www.sajidonline.com

New poster
Posts: 19
Joined: Sat Apr 12, 2003 6:13 pm
Location: China

Post by sunhong »

I got several WA on this problem.
I use BFS to solve it. I don't know why. Please help me, thanks.
[cpp]void solve()
{int u,v,k;
queue.clear(); queue.push_back(s);
memset(visited,0,sizeof(visited)); visited[s]=1;
while (!queue.empty())
{u=queue.front(); queue.pop_front();
for (k=0;k<adj.size();k++)
if (maxd<dist[v] || (maxd==dist[v] && t>v)) {maxd=dist[v]; t=v;}
if (!visited[v]) {visited[v]=1; queue.push_back(v);}
int main()
{int i,j,count;
while (scanf("%d",&n) && n!=0)
for (i=0;i<=n;i++) adj.clear();
scanf("%d %d",&i,&j);
while (!(i==0 && j==0))
scanf("%d %d",&i,&j);
maxd=-1; t=10010;
printf("Case %d: The longest path from %d has length %d, finishing at %

return 1;
} [/cpp]

A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

I did not check your code throughly, but you could try something yourself.
With a little time-expense, the problem can be solved using Warshall's Algorithm. I was also getting WA with BFS, then i used Warshall and got AC. :wink:

You can test the output of the two programs for larger input to find if any difference occurs.

Experienced poster
Posts: 131
Joined: Mon Dec 15, 2003 5:41 am

top sort (WA)

Post by abishek »

Even I use topological sorting. But I always get WA.
any thing wrong with the algo?

Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City

Post by Larry »

I had used naive DFS the first time, but if you just keep track of "best" of each node, and only DFS when it's better, that's good enough for AC for me. (Simple pruning..)

Learning poster
Posts: 77
Joined: Tue Oct 07, 2003 10:07 pm

Post by Dreamer#1 »

hey per, thanks a lot man for such a quick response.

i made a really stupid mistake in my code for cycle checking. :oops:

hope not too many people got misguided in this short time. next time i will consult with 1 or 2 people before making any such conclusions. :o

and per, thanks again for such a quick response. :)

n.b. if any of you can't understand what i'm talking about then please skip my post and may be the per's one (next) too. i made a wrong statement and he pointed it out .
Last edited by Dreamer#1 on Fri Jan 16, 2004 8:48 pm, edited 1 time in total.
Not all of our dreams can be made to come true. But still we live with the hope to turn them into reality someday.

A great helper
Posts: 429
Joined: Fri Nov 29, 2002 11:27 pm
Location: Sweden

Post by Per »

I got AC using topological sort. I think the trouble with the new input is not that the graphs are cyclic, but that there are nodes which are not reachable from the starting node.

It would be very interesting if you could tell me how you solved the problem without assuming that the graph is acyclic: in general, that is an NP-complete problem.

New poster
Posts: 6
Joined: Tue Jan 27, 2004 1:41 pm
Location: Poland/Katowice

Post by tlucz »

Hi! I got a WA on this problem, too. I think that my program works good. Could someone give me input and output sample??? That is my code:
#include <iostream>

using namespace std;

int ile_wezlow;
int poczatek;

int maks;
int przypadek;
int tabela[110][110];
int sumy[110];
int konce[110];
int najm[110];

void inicjalizacja() {
int i,j;

for(i=1;i<=ile_wezlow;i++) {


void wypisz() {

cout << "Case " << przypadek << ": The longest path from " << poczatek << " has length " << maks << ", finishing at " << najm[poczatek] << "." << endl << endl;

int licz(int numer) {
int i,ile,m,m1;

if (sumy[numer]!=-1) {
return sumy[numer];
else {
for(i=1;i<=ile_wezlow;i++) {
if (tabela[numer]==1) {
if (ile>=m) {

if (ile>m) {
else {
if (najm<m1) { m1=najm; }



return m;

void oblicz() {
int i;
for(i=1;i<=ile_wezlow;i++) {
if (konce==1)

int main(void){

int x,y;


while(1) {
cin >> ile_wezlow;
if (ile_wezlow==0) goto koniec;
cin >> poczatek;
while(1) {
cin >> x;
cin >> y;
if ((x==0) && (y==0)) goto oblicz;
if (x!=y) {


return 0;

What's wrong with my code. Thanks a lot.[/cpp]

Learning poster
Posts: 55
Joined: Sat Jan 24, 2004 9:30 pm
Location: Chittagong

Post by alu_mathics »

i thing bfs is sufficient for this problem.
i solved the problem using bfs.
if i visit from u 2 v,
then i considered whether d[v]>d+1.
if yes i just update it.
and finally checked which index contains the maximum distance.
thats all.
Last edited by alu_mathics on Wed Jan 28, 2004 2:03 pm, edited 1 time in total.
cuii e

New poster
Posts: 6
Joined: Tue Jan 27, 2004 1:41 pm
Location: Poland/Katowice

Post by tlucz »

Thanks for the answer. But I would like somebody to test my program with input data and give me data for which my program generates wrong answer. Sorry for my english. Thanks.

Post Reply

Return to “Volume 100 (10000-10099)”