#include <bits/stdc++.h>
using namespace std;
vector<int> val;
vector<int> parent;
vector<int> dp1, dp2, dp3;
//dp1 is the maximum path that goes down
//dp2 is the maximum path that does not necessarily go down
//dp3 is the maximum path that goes up (straight up)
int get_max (vector<int>& v) {
if (v.size() == 1) {
return v[0];
}
pair<int,int> p = make_pair(-1, -1);
for (int i: v) {
if (i > p.first) {
p = make_pair(i, 1);
} else if (i == p.first) {
p.second++;
}
}
if (p.second >= 2) {
return p.first * 2;
}
int myMax = 0;
for (int i: v) {
if (i == p.first) {
continue;
}
myMax = max(myMax, i);
}
return myMax + p.first;
}
void dfs (vector<vector<int> > &adj, int curNode, int prevNode) {
dp1[curNode] = (val[curNode] == 1);
for (int i: adj[curNode]) {
if (i != prevNode) {
dfs (adj, i, curNode);
dp1[curNode] = max(dp1[curNode], dp1[i] + 1);
}
}
if (val[curNode] != 1) {
dp1[curNode] = 0;
}
}
void dfs2 (vector<vector<int> > &adj, int curNode, int prevNode) {
parent[curNode] = prevNode;
dp2[curNode] = dp1[curNode];
vector<int> v;
for (int i: adj[curNode]) {
if (i != prevNode) {
dfs2(adj, i, curNode);
v.push_back(dp1[i]);
}
}
dp2[curNode] = max(dp2[curNode], get_max(v) + 1);
if (val[curNode] != 1) {
dp2[curNode] = 0;
}
}
void dfs3 (vector<vector<int> >&adj, int curNode, int prevNode) {
if (curNode != prevNode) {
dp3[curNode] = dp3[prevNode] + 1;
} else {
dp3[curNode] = 1;
}
if (val[curNode] != 1) {
dp3[curNode] = 0;
}
for (int i: adj[curNode]) {
if (i != prevNode) {
dfs3(adj, i, curNode);
}
}
}
int main() {
vector<vector<int> > adj;
int n;
cin >> n;
adj.resize(n), dp1.resize(n), dp2.resize(n), dp3.resize(n), parent.resize(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v), adj[v].push_back(u);
}
val.resize(n);
int myMin = INT_MAX;
for (int i = 0; i < n; i++) {
cin >> val[i];
myMin = min(myMin, val[i]);
}
if (myMin >= 2) {
cout << myMin << "/" << 1;
return 0;
}
dfs(adj, 0, 0);
dfs2(adj, 0, 0);
dfs3(adj, 0, 0);
int myMax2 = 0;
int myMax1 = 0;
for (int i = 0; i < n; i++) {
myMax1 = max(myMax1, dp2[i]);
if (val[i] == 2) {
vector<int> v;
for (int j: adj[i]) {
if (j == parent[i]) {
v.push_back(dp3[j]);
} else {
v.push_back(dp1[j]);
}
}
myMax2 = max(myMax2, get_max(v));
}
}
myMax2++;
if (myMax2 > myMax1 * 2) {
if (myMax2 % 2 == 1) {
cout << 2 << "/" << myMax2;
} else {
cout << 1 << "/" << myMax2/2;
}
} else {
cout << 1 << "/" << myMax1;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
794 ms |
109844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1026 ms |
198220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1014 ms |
194988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
864 ms |
74128 KB |
Output is correct |
2 |
Correct |
651 ms |
54856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
961 ms |
78616 KB |
Output is correct |
2 |
Correct |
110 ms |
7756 KB |
Output is correct |
3 |
Correct |
1022 ms |
217464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
7848 KB |
Output is correct |
2 |
Correct |
866 ms |
81620 KB |
Output is correct |
3 |
Correct |
616 ms |
45700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
809 ms |
70704 KB |
Output is correct |
2 |
Correct |
939 ms |
77812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
853 ms |
75596 KB |
Output is correct |
2 |
Correct |
609 ms |
39340 KB |
Output is correct |