This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |