#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
vector<int> adj[1000005];
int cac[1000005];
int cac2[10000005];
int a[1000005];
int ans=0, ans2=0;
bool hv=false;
void dfs(int node, int fa)
{
pair<int, int> ok={0, 0};
for(auto i:adj[node])
{
if(i==fa) continue;
dfs(i, node);
if(cac[i]>=cac[ok.first])
{
ok.second=ok.first;
ok.first=i;
}
else if(cac[i]>cac[ok.second])
{
ok.second=i;
}
}
if(a[node]==1)
{
ans=max(ans, cac[ok.first]+cac[ok.second]+1);
cac[node]=cac[ok.first]+1;
for(auto i:adj[node])
{
if(i==fa) continue;
cac2[node]=max(cac2[node], cac2[i]);
if(i==ok.first)
{
ans2=max(ans, cac[ok.second]+cac2[i]+1);
}
else
{
ans2=max(ans2, cac[ok.first]+cac2[i]+1);
}
}
}
else if(a[node]==2)
{
ans2=max(ans2, cac[ok.first]+1);
cac2[node]=cac[ok.first]+1;
}
ans=max(ans, cac[node]);
ans2=max(ans2, cac2[node]);
}
signed main()
{
cin>>n;
for(int i=1; i<n; i++)
{
int u, v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=1; i<=n; i++)
{
cin>>a[i];
if(a[i]==1) hv=true;
}
if(!hv)
{
cout<<*min_element(a+1, a+n+1)<<"/1";
return 0;
}
dfs(1, 1);
if(ans2>2*ans)
{
if(ans2%2==0)
{
cout<<"1/"<<ans2/2;
}
else
{
cout<<"2/"<<ans2;
}
}
else
{
cout<<"1/"<<ans;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Correct |
28 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
23800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1362 ms |
106456 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1669 ms |
169248 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1535 ms |
79684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1596 ms |
82188 KB |
Output is correct |
2 |
Correct |
195 ms |
31480 KB |
Output is correct |
3 |
Incorrect |
1717 ms |
172992 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
188 ms |
31320 KB |
Output is correct |
2 |
Incorrect |
1548 ms |
80616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1464 ms |
76784 KB |
Output is correct |
2 |
Incorrect |
1592 ms |
79188 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1540 ms |
80684 KB |
Output is correct |
2 |
Correct |
934 ms |
55928 KB |
Output is correct |