Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- #include <fstream>
- #include <string>
- #include <set>
- #include <deque>
- #include <queue>
- #include <map>
- #define int long long
- #define endl "\n"
- using namespace std;
- const int INF = 1e18;
- long double eps = 1e-9;
- vector<vector<int>> g;
- vector<int> color;
- int n, x, y, k, res;
- int DFS(int i, int p) {
- color[i] = 1;
- int maxd = -1;
- for (int v : g[i]) {
- if (v == p) {
- continue;
- }
- if (color[v] == 0) {
- int d = DFS(v, i);
- if (maxd != -1) {
- res = max(res, d + maxd + 2);
- }
- maxd = max(maxd, d);
- res = max(res, maxd + 1);
- }
- }
- return maxd + 1;
- }
- signed main() {
- cin >> n;
- int i, j, st = INF;
- bool sup = false;
- vector <int> check(n);
- for (i = 0; i < n; i++) {
- int a;
- cin >> a;
- if (a == 1 && st == INF) {
- st = i;
- }
- check[i] = a;
- }
- g.resize(n);
- color.resize(n);
- for (i = 0; i < n - 1; i++) {
- cin >> x >> y;
- x--;
- y--;
- g[x].push_back(y);
- g[y].push_back(x);
- }
- /*
- for (i = 0; i < n; i++) {
- for (j = 0; j < g[i].size(); j++) {
- cout << g[i][j] << " ";
- }
- cout << endl;
- }
- */
- DFS(0, -1);
- cout << (res + 2 - 1) / 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement