#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 1e6+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
vector<int> adj[MAXN];
ll mg[MAXN];
bool vis[MAXN];
int dep[MAXN];
int dp[MAXN][2];
int vans1 = 1, vans0 = 1;
void dfs(int v){
if(mg[v] == 2){
dp[v][0] = -mod;
dp[v][1] = 1;
}else{
dp[v][0] = 1;
dp[v][1] = -mod;
}
vector<int> ones, twos;
vis[v] = 1;
for(int i: adj[v]){
if(!vis[i]){
dfs(i);
ones.push_back(dp[i][0]);
twos.push_back(dp[i][1]);
if(mg[v] == 2){
dp[v][1] = max(dp[v][1], dp[i][0] + 1);
}else{
dp[v][1] = max(dp[v][1], dp[i][1] + 1);
dp[v][0] = max(dp[v][0], dp[i][0] + 1);
}
}
}
sort(ones.begin(), ones.end(), greater<int>());
sort(twos.begin(), twos.end(), greater<int>());
if(mg[v] == 2){
if(ones.size() >= 2)
vans1 = max(vans1, ones[0] + ones[1] + 1);
}else{
if(ones.size() >= 1 && twos.size() >= 1){
vans1 = max(vans1, ones[0] + twos[0] + 1);
}
if(ones.size() >= 2){
vans0 = max(vans0, ones[0] + ones[1] + 1);
}
}
vans1 = max(vans1, dp[v][1]);
vans0 = max(vans0, dp[v][0]);
}
pii ed[MAXN];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
#endif
cin>>n;
for(int i = 0; i < n - 1; i++){
int a, b;
cin>>a>>b;
ed[i] = {a, b};
}
bool var = 0;
for(int i = 1; i <= n; i++){
cin>>mg[i];
var = (var || mg[i] == 1);
}
if(!var){
sort(mg + 1, mg + n + 1);
cout<<mg[1]<<"/1"<<endl;
return 0;
}
for(int i = 0; i < n - 1; i++){
if(max(mg[ed[i].ff], mg[ed[i].ss]) > 2)
continue;
adj[ed[i].ff].push_back(ed[i].ss);
adj[ed[i].ss].push_back(ed[i].ff);
}
for(int i = 1; i <= n; i++)
if(!vis[i])
dfs(i);
int a, b;
if(vans1 > 2*vans0){
a = 2;
b = vans1;
}else{
a = 1;
b = vans0;
}
int d = __gcd(a, b);
a /= d;
b /= d;
cout<<a<<"/"<<b<<endl;
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23788 KB |
Output is correct |
2 |
Incorrect |
17 ms |
23916 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
23916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
417 ms |
124908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
23788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
543 ms |
215788 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
444 ms |
79296 KB |
Output is correct |
2 |
Incorrect |
396 ms |
65004 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
431 ms |
85104 KB |
Output is correct |
2 |
Correct |
90 ms |
29548 KB |
Output is correct |
3 |
Incorrect |
537 ms |
220584 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
29548 KB |
Output is correct |
2 |
Correct |
450 ms |
80484 KB |
Output is correct |
3 |
Correct |
396 ms |
52332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
411 ms |
78052 KB |
Output is correct |
2 |
Correct |
453 ms |
78776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
456 ms |
80884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |