#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;
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);
}
sort(all(g),greater<int>());
if((node==p) && (val[node]==2) && (sz(g)>=2) && (g[0]==g[1]))two=max(two,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))*(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};
}
}
cout<<ans.fi<<"/"<<ans.se<<"\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 |
16 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
547 ms |
111736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23808 KB |
Output is correct |
2 |
Correct |
496 ms |
78116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
588 ms |
79248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
590 ms |
78432 KB |
Output is correct |
2 |
Correct |
444 ms |
63864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
444 ms |
84856 KB |
Output is correct |
2 |
Correct |
162 ms |
29688 KB |
Output is correct |
3 |
Correct |
601 ms |
78712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
29688 KB |
Output is correct |
2 |
Correct |
578 ms |
79212 KB |
Output is correct |
3 |
Correct |
588 ms |
53496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
513 ms |
75120 KB |
Output is correct |
2 |
Correct |
584 ms |
77684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
596 ms |
79096 KB |
Output is correct |
2 |
Correct |
660 ms |
53576 KB |
Output is correct |