#include<bits/stdc++.h>
using namespace std;
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
#define fi first
#define se second
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
if(sz(NAME)) {
freopen((NAME + ".inp").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
tcT> bool ckmin(T&a, const T&b) {
return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
return b > a ? a=b,1 : 0; }
const int MOD = 1e9 + 7;
bool less_than(const pi&a, const pi&b) {
return 1LL*a.fi*b.se < 1LL*a.se*b.fi;
}
const int MX = 1e6+5;
int N, dpmx1[MX], dpmx2[MX], dpup[MX], mag[MX];
vi adj[MX];
pi ans = {MOD, 1};
void dfs1(int u, int p) {
dpmx1[u] = dpmx2[u] = 0;
each(v, adj[u]) if(v != p) {
dfs1(v, u);
int tmp = mag[v] == 1 ? 1 + dpmx1[v] : 0;
if(tmp > dpmx1[u]) {
dpmx2[u] = dpmx1[u];
dpmx1[u] = tmp;
}
else ckmax(dpmx2[u], tmp);
}
}
void dfs2(int u, int p) {
if(mag[u] == 1) {
if(less_than(pi(mag[u], max(dpmx1[u], dpup[u])+1), ans)) ans = pi(mag[u], max(dpmx1[u], dpup[u])+1);
}
else {
vi vals = {dpup[u], dpmx1[u], dpmx2[u]};
sort(rall(vals));
if(less_than(pi(mag[u], vals[0] + vals[1]+1), ans)) ans = pi(mag[u], vals[0] + vals[1]+1);
}
each(v, adj[u]) if(v != p) {
if(mag[u] != 1) {
dpup[v] = 0;
}
else {
int tmp = mag[v] == 1 ? 1 + dpmx1[v] : 0;
if(tmp == dpmx1[u]) dpup[v] = 1 + max(dpup[u], dpmx2[u]);
else dpup[v] = 1 + max(dpup[u], dpmx1[u]);
}
dfs2(v, u);
}
}
void solve() {
cin>>N;
F0R(i,N-1) {
int a, b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
FOR(i,1,N+1) cin>>mag[i];
dfs1(1, -1);
dfs2(1, -1);
int gc = __gcd(ans.fi, ans.se);
ans.fi /= gc, ans.se /= gc;
cout << ans.fi << "/" << ans.se << nl;
}
int main() {
setIO();
int t=1;
//cin>>t;
while(t-->0) {
solve();
}
return 0;
}
Compilation message
mag.cpp: In function 'void setIO(std::string)':
mag.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen((NAME + ".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((NAME + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23892 KB |
Output is correct |
2 |
Correct |
15 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23820 KB |
Output is correct |
2 |
Correct |
12 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
303 ms |
131500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23736 KB |
Output is correct |
2 |
Correct |
415 ms |
225200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
420 ms |
223960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
328 ms |
85944 KB |
Output is correct |
2 |
Correct |
232 ms |
69584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
348 ms |
88884 KB |
Output is correct |
2 |
Correct |
67 ms |
30068 KB |
Output is correct |
3 |
Correct |
448 ms |
229040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
29852 KB |
Output is correct |
2 |
Correct |
338 ms |
86936 KB |
Output is correct |
3 |
Correct |
290 ms |
55460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
297 ms |
84416 KB |
Output is correct |
2 |
Correct |
309 ms |
86812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
335 ms |
87024 KB |
Output is correct |
2 |
Correct |
279 ms |
55428 KB |
Output is correct |