이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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]+1);
if(i==ok.first)
{
ans2=max(ans2, 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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |