Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<queue>
- #include <vector>
- using namespace std;
- int way[2500][2500];
- int used[110000];
- int matr[2500][2500];
- int maxs[10000];
- int t = 0;
- void dfs(int v, int n) {
- used[v] = 1;
- int maxx = 0;
- int max1 = 0;
- for (int i = 0; i < n; ++i) {
- if (!used[i] && matr[v][i]) {
- dfs(i, n);
- int pr_max = 0;
- for (int j = 0; j < n; ++j) {
- pr_max = max(pr_max, way[i][j]);
- }
- way[v][i] = pr_max + 1;
- }
- }
- for (int j = 0; j < n; ++j) {
- if (way[v][j] > maxx) {
- max1 = maxx; maxx = way[v][j];
- }
- else {
- if (way[v][j] > max1)
- max1 = way[v][j];
- }
- }
- int s_max = maxx + max1;
- maxs[t] = s_max;
- t++;
- }
- int main() {
- #ifdef SYSTEM
- freopen("In.txt", "r", stdin);
- freopen("Out.txt", "w", stdout);
- #endif
- int n;
- cin >> n;
- for (int i = 0; i < n - 1; ++i) {
- int a, b;
- cin >> a >> b;
- --a;
- --b;
- matr[a][b] = 1;
- matr[b][a] = 1;
- }
- for (int j = 0; j < n; ++j) {
- if (!used[j]) {
- dfs(j, n);
- }
- }
- int sup_max = 0;
- for (int k = 0; k < n; ++k) {
- if (maxs[k] > sup_max) sup_max = maxs[k];
- }
- cout << sup_max + 1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment