#include <bits/stdc++.h>
using namespace std;
vector<int> adj[1000006];
long long ma[1000006];
long long lon[1000006];
vector<long long> pref,suf,ch;
int n ;
void calc(int i,int pr){
long long ch = (ma[i]==1?1:0);
for(auto j:adj[i]){
if(j!=pr){
calc(j,i);
if(ma[i]==1)ch = max(ch,lon[j]+1);
}
}
lon[i] = ch;
}
long long maa = 0;
void dfs(int i,int pr){
maa = max(maa,lon[i]);
for(auto j:adj[i]){
if(j==pr)continue;
dfs(j,i);
}
if(ma[i]==1){
long long firstma = 0 , secondma = 0;
for(auto j:adj[i]){
if(j==pr)continue;
if(firstma<=lon[j]){
secondma = firstma;
firstma = lon[j];
}else if(secondma<=lon[j]){
secondma = lon[j];
}
}
maa = max(maa,firstma+secondma+1);
}
}
bool ss = 0;
void reroor(int i,int pr,int cnt){
if(ma[i]==2){
long long firstma = cnt , secondma = 0;
for(auto j:adj[i]){
if(j==pr)continue;
if(firstma<=lon[j]){
secondma = firstma;
firstma = lon[j];
}else if(secondma<=lon[j]){
secondma = lon[j];
}
}
if(firstma==secondma&&firstma==maa){
ss = 1;
}
}
pref.clear();suf.clear();ch.clear();
long long mm= 0;
for(auto j:adj[i]){
if(j==pr)continue;
mm = max(mm,lon[j]);
pref.push_back(mm);
ch.push_back(j);
}
mm = cnt;suf.push_back(mm);
reverse(ch.begin(),ch.end());
for(auto j:ch){
mm = max(mm,lon[j]);
suf.push_back(mm);
}
reverse(suf.begin(),suf.end());
reverse(ch.begin(),ch.end());
for(int j = 0;j<ch.size();j++){
reroor(ch[j],i,(ma[i]==1?max(suf[j+1],(j==0?0:pref[j-1]))+1:0));
}
}
signed main(){
cin>>n;
for(int i = 0;i<n-1;i++){
int a,b;cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
long long mi = 1e18;
for(int i = 0;i<n;i++){
cin>>ma[i+1];
mi = min(mi,ma[i+1]);
}
if(mi!=1){
cout<<mi<<"/"<<1<<endl;
return 0;
}
calc(1,0);
dfs(1,0);
reroor(1,0,0);
if(ss) cout<<2<<"/"<<2*maa+1<<endl;
else cout<<1<<"/"<<maa<<endl;
}
Compilation message
mag.cpp: In function 'void reroor(int, int, int)':
mag.cpp:73:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int j = 0;j<ch.size();j++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
17 ms |
23752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
23832 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
800 ms |
105096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
1027 ms |
178852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1010 ms |
176472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
904 ms |
70484 KB |
Output is correct |
2 |
Correct |
608 ms |
58180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
856 ms |
82836 KB |
Output is correct |
2 |
Correct |
107 ms |
28492 KB |
Output is correct |
3 |
Correct |
1006 ms |
197704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
28520 KB |
Output is correct |
2 |
Correct |
806 ms |
71272 KB |
Output is correct |
3 |
Correct |
555 ms |
47904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
771 ms |
70976 KB |
Output is correct |
2 |
Correct |
834 ms |
70500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
833 ms |
71264 KB |
Output is correct |
2 |
Correct |
599 ms |
47712 KB |
Output is correct |