#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 1e6+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
vector<int> adj[MAXN];
ll mg[MAXN];
bool vis[MAXN];
int dep[MAXN];
int dp[MAXN][2];
int vans1 = 1, vans0 = 1;
void dfs(int v){
if(mg[v] == 2){
dp[v][0] = -mod;
dp[v][1] = 1;
}else{
dp[v][0] = 1;
dp[v][1] = -mod;
}
vector<int> ones, twos;
vis[v] = 1;
int pr = 0;
for(int i: adj[v]){
if(!vis[i]){
dfs(i);
ones.push_back(dp[i][0]);
twos.push_back(dp[i][1]);
if(mg[v] == 2){
dp[v][1] = max(dp[v][1], dp[i][0] + 1);
}else{
dp[v][1] = max(dp[v][1], dp[i][1] + 1);
dp[v][0] = max(dp[v][0], dp[i][0] + 1);
}
}else
pr = i;
}
sort(ones.begin(), ones.end(), greater<int>());
if(mg[v] == 2){
if(ones.size() >= 2)
vans1 = max(vans1, ones[0] + ones[1] + 1);
}else{
for(int i: adj[v]){
if(i != pr){
if(!ones.size() || ! twos.size())
continue;
if(dp[i][0] == ones[0]){
if(ones.size() > 1){
vans1 = max(vans1, dp[i][1] + ones[1] + 1);
}
}else{
vans1 = max(vans1, dp[i][1] + ones[0] + 1);
}
}
}
if(ones.size() >= 2){
vans0 = max(vans0, ones[0] + ones[1] + 1);
}
}
vans1 = max(vans1, dp[v][1]);
vans0 = max(vans0, dp[v][0]);
}
pii ed[MAXN];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
#endif
cin>>n;
for(int i = 0; i < n - 1; i++){
int a, b;
cin>>a>>b;
ed[i] = {a, b};
}
bool var = 0;
for(int i = 1; i <= n; i++){
cin>>mg[i];
var = (var || mg[i] == 1);
}
if(!var){
sort(mg + 1, mg + n + 1);
cout<<mg[1]<<"/1"<<endl;
return 0;
}
for(int i = 0; i < n - 1; i++){
if(max(mg[ed[i].ff], mg[ed[i].ss]) > 2)
continue;
adj[ed[i].ff].push_back(ed[i].ss);
adj[ed[i].ss].push_back(ed[i].ff);
}
for(int i = 1; i <= n; i++)
if(!vis[i])
dfs(i);
int a, b;
if(vans1 > 2*vans0){
a = 2;
b = vans1;
}else{
a = 1;
b = vans0;
}
int d = __gcd(a, b);
a /= d;
b /= d;
cout<<a<<"/"<<b<<endl;
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23916 KB |
Output is correct |
2 |
Correct |
18 ms |
24192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24064 KB |
Output is correct |
2 |
Correct |
17 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
409 ms |
131248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
529 ms |
234348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
530 ms |
231100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
429 ms |
79340 KB |
Output is correct |
2 |
Correct |
331 ms |
65004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
432 ms |
85108 KB |
Output is correct |
2 |
Correct |
83 ms |
29548 KB |
Output is correct |
3 |
Correct |
606 ms |
236396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
29548 KB |
Output is correct |
2 |
Correct |
436 ms |
80620 KB |
Output is correct |
3 |
Correct |
441 ms |
52332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
392 ms |
78308 KB |
Output is correct |
2 |
Correct |
437 ms |
78700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
440 ms |
81004 KB |
Output is correct |
2 |
Correct |
377 ms |
52332 KB |
Output is correct |