/*
Code by @marlov
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <utility>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <iterator>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
#define maxN 1000005
#define INF 2000000000
int N;
vector<int> adj[maxN];
int magic[maxN];
int dp1[maxN];
int f1[maxN];
int dp2[maxN];
int f2[maxN];
int tmv=INF;
int top=1;
int bottom=1;
void dfsbu(int n,int p){
for(int i:adj[n]) if(i!=p){
dfsbu(i,n);
}
if(magic[n]>2) return;
for(int i:adj[n]) if(i!=p){
if(magic[i]==1){
if(dp1[i]>=dp1[n]){
dp2[n]=dp1[n];
dp1[n]=dp1[i]+1;
f1[n]=i;
}else if(dp2[i]>=dp2[n]){
dp2[n]=dp2[i]+1;
f2[n]=i;
}
}
}
}
void dfsud(int n,int p){
if(magic[n]<3&&n!=0){
if(dp1[p]>=dp1[n]&&f1[p]!=n){
dp2[n]=dp1[n];
dp1[n]=dp1[p]+1;
f1[n]=p;
}else if(dp2[p]>=dp2[n]&&f2[p]!=n){
dp2[n]=dp2[p]+1;
f2[n]=p;
}
}
for(int i:adj[n]) if(i!=p){
dfsud(i,n);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>N;
int u,v;
for(int i=0;i<N-1;i++){
cin>>u>>v;
u--;v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=0;i<N;i++){
cin>>magic[i];
tmv=min(tmv,magic[i]);
f1[i]=-1;
f2[i]=-1;
dp1[i]=0;
dp2[i]=0;
//if(magic[i]==1) dp1[i]=1;
}
if(tmv>1){
cout<<tmv<<"/"<<1<<'\n';
return 0;
}
dfsbu(0,-1);
//dfsud(0,-1);
for(int i=0;i<N;i++){
if(magic[i]>2) continue;
if(top*(dp1[i]+dp2[i]+1)>bottom*magic[i]){
top=magic[i];
bottom=(dp1[i]+dp2[i]+1);
}
}
if(top==2&&bottom%2==0){
cout<<top/2<<"/"<<bottom/2<<'\n';
}else{
cout<<top<<"/"<<bottom<<'\n';
}
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1,n=0?)
* do smth instead of nothing and stay organized
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23936 KB |
Output is correct |
2 |
Correct |
17 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
23936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
349 ms |
95852 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
23808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
439 ms |
149824 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
399 ms |
74360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
382 ms |
75484 KB |
Output is correct |
2 |
Incorrect |
85 ms |
30584 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
28984 KB |
Output is correct |
2 |
Incorrect |
418 ms |
90220 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
355 ms |
71280 KB |
Output is correct |
2 |
Incorrect |
399 ms |
90744 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
400 ms |
75128 KB |
Output is correct |
2 |
Incorrect |
329 ms |
57592 KB |
Output isn't correct |