社区讨论

求锐评码风

灌水区参与者 9已保存回复 13

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
13 条
当前快照
1 份
快照标识符
@luxnk0kx
此快照首次捕获于
2024/04/13 13:24
2 年前
此快照最后确认于
2024/04/13 15:11
2 年前
查看原帖
RT,很多人吐槽我的码风,求各位大佬的意见。
CPP
#include <bits/stdc++.h>

using namespace std;

const int kMaxN = 2e5 + 1;

struct V {
  int d, f;
  vector<int> e;
} v[kMaxN];

int n, x, y, a, b, c, p;

void S(int f, int x) {
  v[x].f = f;
  (v[x].d = v[f].d + 1) > v[y].d && (y = x);
  for (int i : v[x].e) {
    if (i != f) {
      S(x, i);
    }
  }
}

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    v[x].e.push_back(y), v[y].e.push_back(x);
  }
  S(0, y = 1), S(0, c = a = y);
  for (int i = b = y; i; i = v[i].f) {
    v[v[i].f].d = 1;
    for (int j : v[v[i].f].e) {
      if (j != v[v[i].f].f && j != i) {
        S(v[i].f, y = j);
        v[c].d < v[y].d && (c = y);
      }
    }
  }
  cout << v[b].d + v[c].d - 2 << '\n';
  cout << a << " " << b << " " << (c == a ? v[b].f : c);
  return 0;
}

回复

13 条回复,欢迎继续交流。

正在加载回复...