-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUVA_924.cpp
82 lines (78 loc) · 1.82 KB
/
UVA_924.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
using namespace std;
#define sz 3000
#define inf 100000
vector <int> adj[sz];
int dis[sz];
int visited[sz];
void bfs(int s , int vertex) {
queue<int> Q ;
Q.push(s);
while(!Q.empty()) {
int u = Q.front();
Q.pop();
for ( int i = 0 ; i < adj[u].size() ; i++) {
if ( visited[adj[u][i]] == 0) {
dis[adj[u][i]] = dis[u]+1 ;
visited[adj[u][i]] = 1 ;
Q.push(adj[u][i]);
}
}
}
vector < int > r ;
for ( int i = 0 ; i < vertex ; i++) r.push_back(dis[i]);
sort(r.begin(),r.end());
//for ( int i = 0 ; i < r.size() ; i++) cout << r[i] << " " ;
//cout << endl;
int boomsize = -5 ;
int boomday = -5 ;
int cnt = 1 ;
for ( int i = 1 ; i < vertex-1 ; i++) {
if ( r[i] != inf ) {
if ( r[i] == r[i+1]) cnt++ ;
if ( r[i] != r[i+1]) {
if ( cnt > boomsize) {
boomsize = cnt ;
boomday = r[i];
}
cnt = 1 ;
}
}
}
if ( boomsize == - 5 && boomday == -5) printf("0\n");
else {
if ( cnt > boomsize) {
boomsize = cnt ;
boomday = r[vertex-1];
}
printf("%d %d\n",boomsize,boomday);
}
return ;
}
int main() {
int n ;
scanf("%d",&n);
int connections;
for ( int i = 0 ; i < n ; i++) {
scanf("%d",&connections);
int c ;
while(connections--) {
scanf("%d",&c);
adj[i].push_back(c);
}
}
int q ;
scanf("%d",&q);
int x ;
while(q--) {
scanf("%d",&x);
for ( int i = 0 ; i < n; i++) {
dis[i] = inf ;
visited[i] = 0 ;
}
dis[x] = 0 ;
visited[x] = 1 ;
bfs(x,n);
}
return 0 ;
}