#include <bits/stdc++.h>
using namespace std ;
const int MAX = 1e6 + 10 ;
int arr[MAX] , dpdown[MAX] , dpup[MAX] ;
int n ;
vector< vector<int> >adj(MAX) ;
int Max = 0 ;
void dfs(int node , int par)
{
if(arr[node] == 1)
dpdown[node] = 1 ;
else
dpdown[node] = 0 ;
for(auto &child : adj[node])
{
if(child == par)
continue ;
dfs(child , node) ;
if(dpdown[node])
dpdown[node] = max(dpdown[node] , dpdown[child]+1) ;
}
Max = max(Max , dpdown[node]) ;
}
bool flag = false ;
void dfs2(int node , int par)
{
int cnt = 0 ;
for(auto &child : adj[node])
{
if(child == par)
continue ;
dfs2(child , node) ;
if(arr[node] == 2)
cnt += (dpdown[child] == Max) ;
}
if(cnt > 1)
flag = true ;
return ;
}
void dfs3(int node , int par , int best)
{
dpup[node] = best + 1 ;
if(arr[node] != 1)
{
dpup[node] = 0 ;
for(auto &child : adj[node])
{
if(child == par)
continue ;
dfs3(child , node , 0) ;
}
if(arr[node] == 2)
{
for(auto &child : adj[node])
{
if(child == par)
continue ;
if(dpdown[child] == Max && best == Max)
flag = true ;
}
}
return ;
}
int mx = dpup[node] , c = node ;
for(auto &child : adj[node])
{
if(child == par)
continue ;
if(dpdown[child]+1 > mx)
mx = dpdown[child]+1 , c = child ;
}
for(auto &child : adj[node])
{
if(child == par)
continue ;
if(c != child)
dfs3(child , node , mx) ;
else
{
int mx2 = dpup[node] ;
for(auto &child2 : adj[node])
{
if(child2 == par || child == child2)
continue ;
if(dpdown[child2]+1 > mx2)
mx2 = dpdown[child2]+1 ;
}
dfs3(child , node , mx2) ;
}
}
return ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 0 ; i < n-1 ; ++i)
{
int x , y ;
cin>>x>>y ;
adj[x].push_back(y) ;
adj[y].push_back(x) ;
}
int cnt = 0 ;
for(int i = 1 ; i <= n ; ++i)
{
cin>>arr[i] ;
cnt += (arr[i] == 1) ;
}
if(!cnt)
return cout<<*min_element(arr+1 , arr+n+1)<<"/"<<1<<"\n" , 0 ;
dfs(1 , -1) ;
int ans = Max ;
dfs2(1 , -1) ;
dfs3(1 , -1 , 0) ;
if(flag == true)
return cout<<2<<"/"<<ans*2+1<<"\n" , 0 ;
else
return cout<<1<<"/"<<ans<<"\n" , 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23928 KB |
Output is correct |
2 |
Correct |
19 ms |
23832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
426 ms |
89592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23800 KB |
Output is correct |
2 |
Correct |
554 ms |
144120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
537 ms |
142328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
466 ms |
66680 KB |
Output is correct |
2 |
Correct |
348 ms |
55540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
439 ms |
67720 KB |
Output is correct |
2 |
Incorrect |
96 ms |
28280 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
88 ms |
28256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
404 ms |
64240 KB |
Output is correct |
2 |
Correct |
457 ms |
66144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
475 ms |
67320 KB |
Output is correct |
2 |
Incorrect |
390 ms |
45944 KB |
Output isn't correct |