#include <bits/stdc++.h>
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
#define pb push_back
using namespace std;
using ll = long long;
using pii = pair <int, int>;
const int maxN = 1e5 + 1;
const int inf = 0x3f3f3f3f;
//const int Mod =
int n;
int a[maxN];
vector <int> adj[maxN];
int dp[maxN][2];
struct TFrac{
int x, y;
bool operator < (const TFrac other) const{
//x / y < other.x / other.y;
//x * other.y < other.x * y;
return x * other.y < other.x * y;
}
};
TFrac ans = {1, 1};
void update(pair <pii, pii> &x, pii y){
if (y > x.fi){
swap(x.fi, x.se);
x.fi = y;
}
else x.se = max(x.se, y);
}
void dfs(int u, int p){
for (int i: adj[u]){
if (i == p) continue;
dfs(i, u);
}
if (a[u] > 2) return;
if (a[u] == 1){
pair <pii, pii> max1 = {{0, 0}, {0, 0}};
pair <pii, pii> max2 = {{0, 0}, {0, 0}};
for (int i: adj[u]){
if (i == p) continue;
update(max1, {dp[i][0], i});
update(max2, {dp[i][1], i});
}
TFrac tem1 = {1, max1.fi.fi + max1.se.fi + 1};
TFrac tem2;
if (max1.fi.se == max2.fi.se){
tem2.y = 2;
tem2.y = 1 + max(max1.fi.fi + max2.se.fi, max1.se.fi + max2.fi.fi);
}
else{
tem2.y = 2;
tem2.y = 1 + max1.fi.fi + max2.fi.fi;
}
tem1 = max(tem1, tem2);
ans = max(ans, tem1);
dp[u][1] = 1 + max2.fi.fi;
dp[u][0] = 1 + max1.fi.fi;
}
else{
pii max1 = {0, 0};
for (int i: adj[u]){
if (i == p) continue;
if (dp[i][0] > max1.fi){
swap(max1.fi, max1.se);
max1.fi = dp[i][0];
}
else max1.se = max(max1.se, dp[i][0]);
}
TFrac tem = {2, max1.fi + max1.se + 1};
ans = min(ans, tem);
dp[u][1] = 1 + max1.fi;
}
}
void Init(){
cin >> n;
for (int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 1; i <= n; ++i){
cin >> a[i];
}
if (*min_element(a + 1, a + n + 1) > 1){
cout << *min_element(a + 1, a + n + 1) << "/" << 1;
return;
}
dfs(1, 0);
int gcd = __gcd(ans.x, ans.y);
ans.x /= gcd;
ans.y /= gcd;
cout << ans.x << "/" << ans.y;
}
#define taskname "test"
signed main(){
faster
if (fopen(taskname ".inp", "r")){
freopen(taskname ".inp", "r", stdin);
freopen(taskname ".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
}
}
Compilation message
mag.cpp: In function 'int main()':
mag.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(taskname ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(taskname ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp: In function 'void dfs(int, int)':
mag.cpp:23:38: warning: 'tem2.TFrac::x' may be used uninitialized in this function [-Wmaybe-uninitialized]
23 | return x * other.y < other.x * y;
| ~~~~~~~~^~~
mag.cpp:50:15: note: 'tem2.TFrac::x' was declared here
50 | TFrac tem2;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
5248 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
36 ms |
13680 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
5204 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
5204 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
64 ms |
7212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
5204 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
40 ms |
13636 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |