#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <numeric>
#include <cassert>
#define endl '\n'
#define sp ' '
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn = 1e6+5;
const int inf = 1e9;
vector<int> adj[maxn];
int magic[maxn];
int visited[maxn];
int dfs1(int v, int s, int d){
int ret = 1;
if(d==1 && s==-1){
int maxx=0,iki=0;
for(int i : adj[v]){
if(magic[i] == 1 && visited[i]!=s){
visited[i] = s;
int cur = dfs1(i,s,0);
if(cur > maxx){
iki = maxx;
maxx = cur;
}
else if(cur > iki){
iki = cur;
}
}
}
return maxx+iki+1;
}
else{
for(int i : adj[v]){
if(magic[i] == 1 && visited[i]!=s){
visited[i] = s;
ret = max(ret,dfs1(i,s,0)+1);
}
}
}
return ret;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);cerr.tie(nullptr);
int n;cin>>n;
for(int i=0;i<n-1;i++){
int a,b;cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
int minn = inf;
vector<int> iki;
for(int i=1;i<=n;i++){
cin>>magic[i];
minn = min(minn, magic[i]);
if(magic[i] == 2)
iki.pb(i);
}
if(minn != 1){
cout << minn << '/' << 1 << endl;
exit(0);
}
int ikimaxx = 0;
for(int i : iki){
int birmaxx = -1;
int biriki = -1;
int birr = 0;
for(int v : adj[i]){
if(magic[v]==1)
birr++;
}
if(birr<2)
continue;
for(int v : adj[i]){
if(magic[v] == 1){
visited[v] = i;
int cur = dfs1(v,i,0);
if(cur>birmaxx){
biriki = birmaxx;
birmaxx = cur;
}
else if(cur>biriki){
biriki = cur;
}
}
}
cerr << i << sp << biriki << sp << birmaxx << endl;
if(birmaxx != -1 && biriki != -1){
ikimaxx = max(ikimaxx, birmaxx + biriki);
}
}
int maxx = 0;
for(int i=1;i<=n;i++){
if(visited[i]!=-1 && magic[i] == 1){
visited[i] = -1;
maxx = max(maxx, dfs1(i,-1,1));
}
}
if(2*maxx >= ikimaxx+1){
cout << 1 << '/' << maxx << endl;
}
else{
cout << ((ikimaxx+1)%2==0 ? 1 : 2) << '/' << ((ikimaxx+1)%2==0?(ikimaxx+1)/2:ikimaxx+1) << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23916 KB |
Output is correct |
2 |
Correct |
15 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23916 KB |
Output is correct |
2 |
Correct |
17 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
80236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23788 KB |
Output is correct |
2 |
Correct |
2031 ms |
66248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
398 ms |
63252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
399 ms |
62700 KB |
Output is correct |
2 |
Correct |
307 ms |
52588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4041 ms |
68040 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3816 ms |
28312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1701 ms |
62104 KB |
Output is correct |
2 |
Correct |
535 ms |
62520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1160 ms |
63384 KB |
Output is correct |
2 |
Incorrect |
407 ms |
43936 KB |
Output isn't correct |