A playground is equipped with ball pits and tents connected by tunnels. Kids can crawl through the tunnels to meet their friends at a spot with a tent or a ball pit.
Now let's mark each meeting spot (a tent or a ball pit) by a number. Assume that once a kid starts to explore the playground from any meeting spot, he/she will always choose the next destination with the smallest number, and he/she would never want to visit the same spot twice. Your job is to help the kids to find the best starting spot so they can explore as many spots as they can.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (≤100), the total number of spots, and M, the number of tunnels. Then M lines follow, each describes a tunnel by giving the indices of the spots (from 1 to N) at the two ends.
Output Specification:
Print in a line the best starting spot which leads to the maximum number of spots, and the number of spots a kid can explore. If the solution is not unique, output the one with the smallest index. There must be exactly 1 space between the two numbers, and there must be no extra space at the beginning or the end of the line.
Sample Input:
8 10
1 2
3 4
5 8
1 4
6 1
3 7
2 4
5 3
2 8
2 5
Sample Output:
6 7
Hint:
Actually there are two solutions. Kids can either start from 6 and go through 1->2->4->3->5->8, or from 7 to 3->4->1->2->5->8, both can visit 7 spots. Since 6 is a smaller index, we output 6 as the starting spot.
#include<iostream>
#include<cstring>
using namespace std;
const int N = 110;
bool g[N][N];
bool st[N];
int n,m;
int dfs(int u,int cnt){
int res = cnt;
st[u] = 1;
for(int i = 1;i<=n;i++){
if(g[u][i]&&!st[i]){
res = max(res,dfs(i,cnt+1));
}
}
return res;
}
int main(){
cin>>n>>m;
while(m--){
int a,b;cin>>a>>b;
g[a][b] = g[b][a] = 1;
}
int max_cnt = -1,min_id = n+1;
for(int i = 1;i<=n;i++){
memset(st,0,sizeof st);
int cnt = dfs(i,1);
if(cnt>max_cnt){
max_cnt = cnt;
min_id = i;
}
}
cout<<min_id<<" "<<max_cnt<<endl;
}