This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;
struct P{
int mx,mn,B,S;
void add(int X,int N){
if(X>mx){
mn=mx;
mx=X;
S=B;
B=N;
}
else if(X>mn){
mn=X;
S=N;
}
}
P(int a,int b){
mx=a;
mn=b;
B=-1;
S=-1;
}
};
struct Rate{
int pay,payda;
long double get_value()const{
return (long double)pay/(long double)payda;
}
bool operator<(const Rate& X)const{
return get_value()<X.get_value();
}
Rate(int x,int y){
pay=x;
payda=y;
}
};
vector<int>adj[1000005];
vector<int>val(1000005,INF);
vector<P> data(1000005,P(0,0));
void dfs(int x,int pre){
for(int i=0;i<adj[x].size();i++){
int y=adj[x][i];
if(y==pre)continue;
dfs(y,x);
if(val[y]==1)
data[x].add(data[y].mx+1,y);
}
}
void push(int x,int pre){
for(int i=0;i<adj[x].size();i++){
int y=adj[x][i];
if(y==pre)continue;
if(val[x]==1){
if(data[x].B!=y)
data[y].add(data[x].mx+1,0);
else data[y].add(data[x].mn+1,0);
}
push(y,x);
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("q.gir","r",stdin);
// freopen("q.cik","w",stdout);
int n;
cin>>n;
for(int i=0;i<n-1;i++){
int x,y;
cin>>x>>y;
adj[x].pb(y);
adj[y].pb(x);
}
for(int i=1;i<=n;i++)cin>>val[i];
if(*min_element(all(val))>1){
cout<<*min_element(all(val))<<"/"<<1;
return 0;
}
dfs(1,0);
push(1,0);
Rate ans(1,1);
for(int x=1;x<=n;x++){
if(val[x]==1){
ans=min(ans,Rate(1,data[x].mx+data[x].mn+1));
}
if(val[x]==2){
ans=min(ans,Rate(2,data[x].mx+data[x].mn+1));
}
}
cout<<ans.pay/__gcd(ans.pay,ans.payda)<<"/"<<ans.payda/__gcd(ans.pay,ans.payda);
}
Compilation message (stderr)
mag.cpp: In function 'void dfs(long long int, long long int)':
mag.cpp:49:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[x].size();i++){
~^~~~~~~~~~~~~~
mag.cpp: In function 'void push(long long int, long long int)':
mag.cpp:58:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[x].size();i++){
~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |