Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- #define ll long long
- int tt, tc;
- const int N = 100005;
- const ll INF = 1e18;
- int to_leaf[N];
- int max_len[N];
- vector <int> adj[N];
- void dfs(int v , int par)
- {
- int mx1 = -1; int mx2 = -1;
- for(int u : adj[v])
- {
- if(u != par)
- {
- dfs(u , v);
- to_leaf[v] = max(to_leaf[u] + 1 , to_leaf[v]);
- if(mx1 <= to_leaf[u]) {
- mx2 = mx1;
- mx1 = to_leaf[u];
- }
- else if(mx2 < to_leaf[u])
- {
- mx2 = to_leaf[u];
- }
- }
- }
- max_len[v] = mx1+mx2+2;
- }
- void solve() {
- int n , m; cin>>n>>m;
- for(int i =0 ; i<m; i++)
- {
- int u , v; cin>>u>>v;
- adj[v].push_back(u);
- adj[u].push_back(v);
- }
- dfs(1 , 0);
- cout<<to_leaf[1]<<"\n";
- cout<<to_leaf[2]<<"\n";
- cout<<to_leaf[4]<<"\n";
- cout<<max_len[1]<<"\n";
- }
- int main() {
- ios::sync_with_stdio(0); cin.tie(0);
- tt = 1, tc = 1; cin >> tt;
- while (tt--) solve(), tc++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement