# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1051120 |
2024-08-09T20:22:21 Z |
ortsac |
Pipes (BOI13_pipes) |
C++17 |
|
90 ms |
39668 KB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<long long, long long>
#define fr first
#define se second
const int MAXN = 1e5 + 10;
const int MAXM = 5e5 + 10;
vector<pii> adj[MAXN];
int bEdge = -1;
vector<pii> edges;
int h[MAXN];
int change[MAXN];
bool vis[MAXN];
int dad[MAXN];
int dp[MAXN];
int peso[MAXM];
void dfs(int node) {
vis[node] = 1;
for (auto [u, z] : adj[node]) {
if (u == dad[node]) continue;
if (vis[u]) bEdge = z;
else {
dad[u] = node;
h[u] = h[node] + 1;
dfs(u);
}
}
}
void calc(int node) {
vis[node] = 1;
for (auto [u, z] : adj[node]) {
if (vis[u]) continue;
calc(u);
dp[node] += (change[u] - dp[u]);
peso[z] += (change[u] - dp[u]);
}
}
vector<int> path;
bool found = 0;
int need;
void cPath(int from, int to) {
path.clear();
while (to != from) {
path.push_back(to);
to = dad[to];
}
path.push_back(from);
reverse(path.begin(), path.end());
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> change[i];
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
edges.push_back({a, b});
adj[a].push_back({b, i});
adj[b].push_back({a, i});
}
if (m > n) {
cout << "0\n";
return 0;
}
dfs(1);
int lca = 0;
if (bEdge != -1) {
auto u = edges[bEdge];
int a = u.fr, b = u.se;
if (h[b] > h[a]) swap(a, b);
lca = b;
int tam = h[a] + h[b] - 2*h[lca] + 1;
if ((tam % 2LL) == 0) {
cout << "0\n";
return 0;
}
}
memset(vis, 0, sizeof vis);
calc(1);
if (dp[1] == change[1]) {
for (int i = 0; i < m; i++) cout << 2*peso[i] << "\n";
return 0;
}
// first case is for tree
// second case is if you can't add 2x to it
if ((bEdge == -1) || ((change[1] - dp[1]) % 2LL)) {
cout << "0\n";
return 0;
}
need = (change[1] - dp[1])/2LL;
cPath(1, lca);
int tam = path.size();
int inv = 1;
for (int i = 0; i < (tam - 1); i++) {
int node = path[i];
int prox = path[i + 1];
for (auto [u, z] : adj[node]) {
if (u == prox) {
peso[z] += 2*need*inv;
inv = -inv;
break;
}
}
}
int a = edges[bEdge].fr, b = edges[bEdge].se;
if (h[b] > h[a]) swap(a, b);
cPath(lca, a);
tam = path.size();
for (int i = 0; i < (tam - 1); i++) {
int node = path[i];
int prox = path[i + 1];
for (auto [u, z] : adj[node]) {
if (u == prox) {
peso[z] += need*inv;
inv = -inv;
break;
}
}
}
if ((h[a] - h[lca]) % 2LL) inv = -inv;
peso[bEdge] += inv*need;
for (int i = 0; i < m; i++) {
cout << 2*peso[i] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5468 KB |
Output is correct |
2 |
Correct |
1 ms |
5468 KB |
Output is correct |
3 |
Correct |
1 ms |
5724 KB |
Output is correct |
4 |
Correct |
33 ms |
17024 KB |
Output is correct |
5 |
Correct |
1 ms |
5464 KB |
Output is correct |
6 |
Correct |
1 ms |
5468 KB |
Output is correct |
7 |
Correct |
1 ms |
5468 KB |
Output is correct |
8 |
Correct |
1 ms |
5468 KB |
Output is correct |
9 |
Correct |
1 ms |
5724 KB |
Output is correct |
10 |
Correct |
1 ms |
5724 KB |
Output is correct |
11 |
Correct |
1 ms |
5724 KB |
Output is correct |
12 |
Correct |
1 ms |
5724 KB |
Output is correct |
13 |
Correct |
26 ms |
13772 KB |
Output is correct |
14 |
Correct |
29 ms |
14996 KB |
Output is correct |
15 |
Correct |
36 ms |
15300 KB |
Output is correct |
16 |
Correct |
27 ms |
14276 KB |
Output is correct |
17 |
Correct |
30 ms |
16832 KB |
Output is correct |
18 |
Correct |
31 ms |
17100 KB |
Output is correct |
19 |
Correct |
35 ms |
20868 KB |
Output is correct |
20 |
Correct |
1 ms |
5468 KB |
Output is correct |
21 |
Correct |
1 ms |
5724 KB |
Output is correct |
22 |
Correct |
35 ms |
17056 KB |
Output is correct |
23 |
Correct |
26 ms |
13772 KB |
Output is correct |
24 |
Correct |
31 ms |
15304 KB |
Output is correct |
25 |
Correct |
30 ms |
15876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5464 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
25 ms |
14740 KB |
Output is correct |
4 |
Correct |
22 ms |
12452 KB |
Output is correct |
5 |
Correct |
21 ms |
11980 KB |
Output is correct |
6 |
Correct |
88 ms |
39668 KB |
Output is correct |
7 |
Correct |
1 ms |
5468 KB |
Output is correct |
8 |
Correct |
1 ms |
5468 KB |
Output is correct |
9 |
Correct |
1 ms |
5464 KB |
Output is correct |
10 |
Correct |
1 ms |
5468 KB |
Output is correct |
11 |
Correct |
1 ms |
5468 KB |
Output is correct |
12 |
Correct |
1 ms |
5468 KB |
Output is correct |
13 |
Correct |
1 ms |
5468 KB |
Output is correct |
14 |
Correct |
1 ms |
5468 KB |
Output is correct |
15 |
Correct |
1 ms |
5724 KB |
Output is correct |
16 |
Correct |
1 ms |
5724 KB |
Output is correct |
17 |
Correct |
1 ms |
5724 KB |
Output is correct |
18 |
Correct |
1 ms |
5724 KB |
Output is correct |
19 |
Correct |
1 ms |
5724 KB |
Output is correct |
20 |
Correct |
1 ms |
5720 KB |
Output is correct |
21 |
Correct |
1 ms |
5724 KB |
Output is correct |
22 |
Correct |
1 ms |
5724 KB |
Output is correct |
23 |
Correct |
33 ms |
18996 KB |
Output is correct |
24 |
Correct |
37 ms |
20424 KB |
Output is correct |
25 |
Correct |
25 ms |
14528 KB |
Output is correct |
26 |
Correct |
22 ms |
12224 KB |
Output is correct |
27 |
Correct |
25 ms |
12236 KB |
Output is correct |
28 |
Correct |
23 ms |
12748 KB |
Output is correct |
29 |
Correct |
71 ms |
34172 KB |
Output is correct |
30 |
Correct |
43 ms |
24508 KB |
Output is correct |
31 |
Correct |
40 ms |
25476 KB |
Output is correct |
32 |
Correct |
32 ms |
16844 KB |
Output is correct |
33 |
Correct |
28 ms |
15816 KB |
Output is correct |
34 |
Correct |
23 ms |
12236 KB |
Output is correct |
35 |
Correct |
22 ms |
12228 KB |
Output is correct |
36 |
Correct |
24 ms |
12280 KB |
Output is correct |
37 |
Correct |
90 ms |
39256 KB |
Output is correct |
38 |
Correct |
37 ms |
21956 KB |
Output is correct |
39 |
Correct |
37 ms |
16316 KB |
Output is correct |
40 |
Correct |
37 ms |
21696 KB |
Output is correct |
41 |
Correct |
32 ms |
17600 KB |
Output is correct |
42 |
Correct |
22 ms |
12236 KB |
Output is correct |
43 |
Correct |
22 ms |
12188 KB |
Output is correct |
44 |
Correct |
28 ms |
12076 KB |
Output is correct |
45 |
Correct |
69 ms |
36528 KB |
Output is correct |
46 |
Correct |
40 ms |
23488 KB |
Output is correct |
47 |
Correct |
46 ms |
20160 KB |
Output is correct |
48 |
Correct |
39 ms |
23488 KB |
Output is correct |
49 |
Correct |
22 ms |
12736 KB |
Output is correct |
50 |
Correct |
21 ms |
12236 KB |
Output is correct |
51 |
Correct |
24 ms |
12236 KB |
Output is correct |
52 |
Correct |
21 ms |
12232 KB |
Output is correct |
53 |
Correct |
80 ms |
35584 KB |
Output is correct |
54 |
Correct |
37 ms |
21200 KB |
Output is correct |