Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ll long long
- #include <bits/stdc++.h>
- using namespace std;
- const int OO = 1e9;
- const double EPS = 1e-9;
- bool vis[10000];
- vector<vector<int>> graph;
- int n;
- int mx = -1;
- int mx_idx = -1;
- void clr_vis() {
- for(int i = 0; i < n; i++) {
- vis[i] = 0;
- }
- }
- void dfs(int x, int c) {
- vis[x] = 1;
- if(graph[x].size() == 1 && (mx == -1 || mx < c)) {
- mx = c;
- mx_idx = x;
- }
- for(int e : graph[x]) {
- if(!vis[e]) {
- dfs(e,c+1);
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- int t;
- cin >> t;
- while(t--) {
- cin >> n;
- graph.clear();
- graph.resize(n);
- for(int i = 0; i < n; i++) {
- int m;
- cin >> m;
- for(int j = 0; j < m; j++) {
- int x;
- cin >> x;
- graph[i].push_back(x-1);
- }
- }
- if(n == 1) {
- cout << 0 << "\n";
- continue;
- }
- clr_vis();
- mx = -1;
- mx_idx = -1;
- for(int i = 0; i < n; i++) {
- if(graph[i].size() == 1) {
- dfs(i,0);
- break;
- }
- }
- clr_vis();
- mx = -1;
- dfs(mx_idx,0);
- //cout << "mx is " << mx << "\n";
- cout << mx + (n-(mx+1))*2 << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement