-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10000.cpp
executable file
·59 lines (49 loc) · 1.22 KB
/
10000.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
using namespace std ;
void dfs( int node, int n, set<int> neighbor[], int dis[], int depth, int &length, int &finish )
{
if( depth > dis[ node ] )
{
dis[ node ] = depth ;
for( set<int>::const_iterator iter = neighbor[ node ].begin() ;
iter != neighbor[ node ].end() ; ++iter )
{
dfs( *iter, n, neighbor, dis, depth+1, length, finish ) ;
}
}
}
int main()
{
const int MaxN = 100 ;
int n = 0 ;
int runningCase = 0 ;
while( cin >> n && n != 0 )
{
runningCase++ ;
int s = 0 ;
int p = 0 ;
int q = 0 ;
int length = 0 ;
int finish = 0 ;
set<int> neighbor[ MaxN + 1 ] ;
int dis[ MaxN + 1 ] = { 0 } ;
cin >> s ;
while( cin >> p >> q )
{
if( p == 0 && q == 0 )
break ;
neighbor[ p ].insert( q ) ;
}
fill( dis, dis+n+1, -1 ) ;
dfs( s, n, neighbor, dis, 0, length, finish ) ;
finish = max_element( dis+1, dis+n+1 ) - dis ;
length = dis[ finish ] ;
cout << "Case " << runningCase << ": The longest path from " << s << " has length "
<< length << ", finishing at " << finish << "." << endl ;
cout << endl ;
}
return 0 ;
}