#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFOR(i,start,end) for(int i = start; i>end; i--)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define vsort(a) sort(a.begin(), a.end());
#define mp make_pair
#define v vector
#define sf scanf
#define pf printf
typedef long long ll;
typedef pair<int, int > pii;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
bool isbigger(pair<ll, ll> &a, pair<ll, ll> &b) {
ll g1 = __gcd(a.first, a.second);
ll g2 = __gcd(b.first, b.second);
a.first /= g1, a.second /= g1, b.first /= g2, b.second /= g2;
return b.second*a.first > b.first*a.second;
}
const int MAXN = 1000010;
int N;
v<int> adj[MAXN];
ll magic[MAXN];
int dpdown[MAXN], dpup[MAXN], dpone[MAXN];
void dfsone(int x, int p) {
int q = dpup[x], w = 0, e = 0; // >=
for (auto c : adj[x]) if (c != p) {
e = dpdown[c];
if (w < e) swap(w, e);
if (q < w) swap(q, w);
dfsone(c, x);
}
dpone[x] = q + w;
}
void dfsup(int x, int p) {
if (p != -1 && magic[p] == 1) {
dpup[x] = dpup[p];
for (int s : adj[p]) if (s != x)
dpup[x] = max(dpdown[s], dpup[x]);
dpup[x]++;
}
for (int c : adj[x]) if (c != p)
dfsup(c, x);
}
int dfsdown(int x, int p) {
for (int c : adj[x]) if (c != p)
dpdown[x] = max(dpdown[x], dfsdown(c, x));
if (magic[x] != 1) dpdown[x] = 0;
else dpdown[x] += 1;
return dpdown[x];
}
int main() {
// input
cin >> N;
FOR(i, 0, N - 1) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
FORE(i, 1, N)
cin >> magic[i];
// init and compute dp
memset(dpdown, 0, sizeof dpdown);
memset(dpup, 0, sizeof dpup);
dfsdown(1, -1);
dfsup(1, -1);
dfsone(1, -1);
// find ans
pair<ll, ll> ans(magic[1], dpone[1] + 1);
FORE(i, 1, N) {
pair<ll, ll> cmp(magic[i], dpone[i] + 1);
if (isbigger(ans, cmp))
ans = cmp;
}
cout << ans.first << "/" << ans.second << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
31736 KB |
Output is correct |
2 |
Incorrect |
30 ms |
31744 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
31772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1088 ms |
111448 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
111448 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1407 ms |
180852 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1317 ms |
180852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1351 ms |
180852 KB |
Output is correct |
2 |
Incorrect |
186 ms |
180852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
170 ms |
180852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1322 ms |
180852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1304 ms |
180852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |