#include <bits/stdc++.h>
using namespace std;
vector<int> adj[1000006];
int ma[1000006];
int lon[1000006];
int n ;
void calc(int i,int pr){
int 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;
}
int 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){
int 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;int mm;
void reroor(int i,int pr,int cnt){
if(ma[i]==2){
int 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;
}
}
vector<int> suf,ch;
for(auto j:adj[i]){
if(j==pr)continue;
mm = max(mm,lon[j]);
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());
mm= 0;
for(int j = 0;j<ch.size();j++){
reroor(ch[j],i,(ma[i]==1?max(suf[j+1],mm)+1:0));
mm = max(mm,lon[ch[j]]);
}
suf.clear();ch.clear();
}
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);
}
int mi = 1e9;
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:71:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int j = 0;j<ch.size();j++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
882 ms |
142692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Runtime error |
1018 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1047 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
910 ms |
62916 KB |
Output is correct |
2 |
Correct |
663 ms |
52972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
973 ms |
68120 KB |
Output is correct |
2 |
Correct |
171 ms |
27848 KB |
Output is correct |
3 |
Runtime error |
1054 ms |
262144 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
27720 KB |
Output is correct |
2 |
Correct |
911 ms |
64868 KB |
Output is correct |
3 |
Correct |
617 ms |
44008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
901 ms |
63992 KB |
Output is correct |
2 |
Correct |
927 ms |
62324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1043 ms |
64128 KB |
Output is correct |
2 |
Correct |
647 ms |
43832 KB |
Output is correct |