#include<bits/stdc++.h>
using namespace std;
const int N=1e6+2;
const int inf=1e9+7;
vector<int> adj[N];
int val[N],f[N],max1=1;
pair<int,int> nei[N],nei1[N];
void dfs1(int x,int p,int mx){
for(int i:adj[x]){
if(i!=p){
if(val[x]==1){
int val=-1;
if(i!=nei[x].second){
val=nei[x].first+1;
}
else{
val=nei1[x].first+1;
}
dfs1(i,x,max(mx+1,val));
}
else{
dfs1(i,x,0);
}
}
}
// cout<<x<<' '<<nei[x].first<<' '<<nei[x].second<<' '<<mx<<' '<<nei1[x].first<<' '<<nei1[x].second<<endl;
if(val[x]==2&&nei[x].first==max1&&(nei1[x].first==max1||mx==max1)){
cout<<2<<'/'<<2*max1+1;
exit(0);
}
}
void dfs(int x,int p){
int mx=0;
if(val[x]==1){
f[x]=1;
}
for(int i:adj[x]){
if(i!=p){
dfs(i,x);
if(nei[x].first<=f[i]){
nei1[x]=nei[x];
nei[x]={f[i],i};
}
else{
if(nei1[x].first<f[i]){
nei1[x]={f[i],i};
}
}
if(val[i]>1||val[x]>1){
continue;
}
if(f[x]<=f[i]+1){
mx=f[x];
f[x]=f[i]+1;
}
else{
if(f[i]+1>mx){
mx=f[i]+1;
}
}
}
}
max1=max(max1,mx+f[x]-1);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,i,j,k,l,min1=inf;
cin>>n;
for(i=1;i<n;i++){
cin>>j>>k;
adj[j].push_back(k);
adj[k].push_back(j);
}
for(i=1;i<=n;i++){
cin>>val[i];
min1=min(min1,val[i]);
}
if(min1>1){
cout<<min1<<'/'<<1;
return 0;
}
dfs(1,1);
// for(i=1;i<=n;i++){
// cout<<i<<' '<<f[i]<<endl;
// }
dfs1(1,1,0);
cout<<1<<'/'<<max1;
}
/*
5
1 2
2 4
1 3
5 2
2
1
1
1
3
*/
/*
5
1 2
2 3
3 4
4 5
1 1 2 1 1
*/
Compilation message
mag.cpp: In function 'int main()':
mag.cpp:68:14: warning: unused variable 'l' [-Wunused-variable]
int n,i,j,k,l,min1=inf;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
23 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
442 ms |
105168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
594 ms |
171344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
563 ms |
170360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
492 ms |
79276 KB |
Output is correct |
2 |
Correct |
366 ms |
64760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
461 ms |
80560 KB |
Output is correct |
2 |
Correct |
93 ms |
31096 KB |
Output is correct |
3 |
Correct |
607 ms |
173080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
30712 KB |
Output is correct |
2 |
Correct |
496 ms |
80304 KB |
Output is correct |
3 |
Correct |
403 ms |
52440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
488 ms |
76332 KB |
Output is correct |
2 |
Correct |
488 ms |
78456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
500 ms |
80572 KB |
Output is correct |
2 |
Correct |
396 ms |
53224 KB |
Output is correct |