# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
162906 |
2019-11-10T08:50:42 Z |
_qVp_ |
Mag (COCI16_mag) |
C++14 |
|
577 ms |
168672 KB |
#include <bits/stdc++.h>
using namespace std;
const int md = 1e6 + 10;
typedef pair < long long, int > II;
vector < int > adj[md];
int a[md], u[md], v[md], par[md];
int f[md][5];
queue < int > q;
int n;
long long a1 = 1e9, b1 = 1;
void compare(long long a2, long long b2) {
if (a1 * b2 >= a2 * b1) {
a1 = a2;
b1 = b2;
}
}
void dfs(int u, int root) {
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (par[u] == v) continue;
par[v] = u;
dfs(v, root);
if (a[u] == 1) {
compare(1, f[u][0] + f[v][0]);
compare(2, f[u][0] + f[v][1]);
compare(2, f[u][1] + f[v][0]);
f[u][0] = max(f[u][0], f[v][0] + 1);
f[u][1] = max(f[u][1], f[v][1] + 1);
} else {
compare(2, f[u][1] + f[v][0]);
f[u][1] = max(f[u][1], f[v][0] + 1);
}
}
}
int main() {
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n;
for(int i = 1; i < n; i++)
cin >> u[i] >> v[i];
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i < n; i++) {
//int u, v;
//cin >> u >> v;
if (a[u[i]] + a[v[i]] <= 3) {
adj[u[i]].push_back(v[i]);
adj[v[i]].push_back(u[i]);
}
}
for(int i = 1; i <= n; i++) {
f[i][0] = (a[i] == 1);
f[i][1] = (a[i] == 2);
}
for(int i = 1; i <= n; i++)
if (!par[i]) {
if (a[i] >= 3) {
compare(a[i], 1);
continue;
}
//cout << i << endl;
par[i] = i;
dfs(i, i);
}
//for(int i = 1; i <= n; i++)
// cout << i <<" " << f[i][0] << " " << f[i][1] << endl;
int gcd = __gcd(a1, b1);
cout << a1 / gcd << "/" << b1 / gcd;
return 0;
}
Compilation message
mag.cpp: In function 'void dfs(int, int)':
mag.cpp:23:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++) {
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
23900 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23904 KB |
Output is correct |
2 |
Correct |
26 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
449 ms |
108516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Correct |
480 ms |
87548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
577 ms |
165216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
494 ms |
90072 KB |
Output is correct |
2 |
Correct |
378 ms |
72568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
488 ms |
91024 KB |
Output is correct |
2 |
Correct |
92 ms |
30712 KB |
Output is correct |
3 |
Correct |
571 ms |
168672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
30584 KB |
Output is correct |
2 |
Correct |
497 ms |
90440 KB |
Output is correct |
3 |
Correct |
385 ms |
57848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
456 ms |
85476 KB |
Output is correct |
2 |
Correct |
491 ms |
89132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
503 ms |
90784 KB |
Output is correct |
2 |
Correct |
393 ms |
57720 KB |
Output is correct |