#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define rando mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define fi first
#define se second
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugii(x) cerr << " - " << #x << ": " << x.fi<<","<<x.se << endl;
#define sep() cerr << "--------------------" << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ld long double
#define ll long long
#define int ll
#define ii pair<int,int>
#define v vector<int>
#define vii vector<ii>
#define vv vector<vector<int> >
#define mp make_pair
#define INF (int)(1e18)
#define pb push_back
#define EPS 1e-9
const int MOD = 1000000007; // 998244353
const int MX = 1000006;
vii vec[MX];
int val[MX],two,one;
int solve(int node,int p){
v g;
int h=0;
for(auto &it:vec[node]){
if(it.fi!=p && (val[it.fi]==1)){
if(it.se==-1)it.se=solve(it.fi,node);
g.pb(it.se);
h++;
}
}
sort(all(g),greater<int>());
if((node==p) && (val[node]==2) && (sz(g)>=2))two=max(one,g[0]+g[1]+1);
if((node==p) && (val[node]==1) && (sz(g)>=1))one=max(one,g[0]+1);
return (sz(g)?g[0]:0)+(val[node]==1);
}
int32_t main(){
boost;
//freopen(".in","r",stdin);
ii ans={INF,1};
int n,a,b;
cin>>n;
for (int i = 0; i < n-1; ++i)
{
cin>>a>>b;
a--,b--;
vec[a].pb({b,-1});
vec[b].pb({a,-1});
}
for (int i = 0; i < n; ++i){
cin>>val[i];
if(val[i]<ans.fi)ans.fi=val[i];
}
for (int i = 0; i < n; ++i)
solve(i,i);
if(one || two){
if(!two)ans={1,one};
else{
if((2*one)>=two)ans={1,one};
else ans={2,two};
}
}
int g=__gcd(ans.fi,ans.se);
cout<<ans.fi/g<<"/"<<ans.se/g<<"\n";
return 0;
}
//do smth instead of nothing and stay organized
//long long
//array bounds
//special cases
//binary search
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23936 KB |
Output is correct |
2 |
Correct |
26 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
23936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
545 ms |
114040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23808 KB |
Output is correct |
2 |
Correct |
531 ms |
80792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
626 ms |
80376 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
591 ms |
80888 KB |
Output is correct |
2 |
Correct |
446 ms |
66800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
448 ms |
85696 KB |
Output is correct |
2 |
Correct |
130 ms |
31352 KB |
Output is correct |
3 |
Incorrect |
603 ms |
80888 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
31096 KB |
Output is correct |
2 |
Correct |
592 ms |
81836 KB |
Output is correct |
3 |
Correct |
618 ms |
56972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
551 ms |
77648 KB |
Output is correct |
2 |
Correct |
597 ms |
78592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
602 ms |
79504 KB |
Output is correct |
2 |
Correct |
602 ms |
53548 KB |
Output is correct |