#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#endif
typedef long long ll;
typedef pair<int, int> pii;
#define x first
#define y second
const int N = 1e5 + 5;
int n, m;
vector<int> c;
vector<set<int>> adj;
vector<pii> e;
vector<ll> ans;
int adjacentNode(int node, int i) {
assert(i == 0 || i == 1);
int adj_edge = (i == 0 ? *adj[node].begin() : *adj[node].rbegin());
int adj_node = e[adj_edge].x ^ e[adj_edge].y ^ node;
return adj_node;
}
void dfs(int at, int p, int root, vector<pii> &order) {
int nxt;
nxt = adjacentNode(at, 0);
if (nxt != p) order.push_back({at, *adj[at].begin()});
else {
nxt = adjacentNode(at, 1);
if (nxt != p) order.push_back({at, *adj[at].rbegin()});
}
if (nxt != root) dfs(nxt, at, root, order);
}
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
scanf("%d %d", &n, &m);
c.resize(n);
for (int i = 0; i < n; i++) scanf("%d", &c[i]);
adj.resize(n);
e.resize(m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--, v--;
adj[u].insert(i);
adj[v].insert(i);
e[i] = {u, v};
}
ans.resize(m);
queue<int> q;
for (int i = 0; i < n; i++) q.push(i);
while (!q.empty()) {
int curr_node = q.front(); q.pop();
if (adj[curr_node].size() == 1) {
int adj_edge = *adj[curr_node].begin();
int adj_node = adjacentNode(curr_node, 0);
ans[adj_edge] = 2LL * c[curr_node];
c[curr_node] -= ans[adj_edge] / 2;
c[adj_node] -= ans[adj_edge] / 2;
adj[curr_node].erase(adj_edge);
adj[adj_node].erase(adj_edge);
if (adj[adj_node].size() == 1) q.push(adj_node);
}
}
int cnt_node = 0;
for (int i = 0; i < n; i++) {
if (adj[i].size() > 2) {
printf("0\n");
return 0;
}
cnt_node += adj[i].size() == 2;
}
if (cnt_node > 0 && cnt_node % 2 == 0) {
printf("0\n");
return 0;
}
int first_node;
for (first_node = 0; first_node < n; first_node++) {
if (adj[first_node].size() == 2) break;
}
if (first_node < n) {
vector<pii> order;
dfs(first_node, -1, first_node, order);
ll sum = 2LL * c[first_node];
ll diff = 0;
for (int i = 1; i < order.size(); i++) {
diff = 2LL * c[order[i].x] - diff;
}
ans[order[0].y] = (sum - diff) / 2LL;
for (int i = 1; i < order.size(); i++) {
ans[order[i].y] = 2LL * c[order[i].x] - ans[order[i - 1].y];
}
}
for (int i = 0; i < m; i++) printf("%lld\n", ans[i]);
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:111:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < order.size(); i++) {
~~^~~~~~~~~~~~~~
pipes.cpp:117:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < order.size(); i++) {
~~^~~~~~~~~~~~~~
pipes.cpp:49:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:52:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 0; i < n; i++) scanf("%d", &c[i]);
~~~~~^~~~~~~~~~~~~
pipes.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
161 ms |
18504 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
119 ms |
14712 KB |
Output is correct |
14 |
Correct |
149 ms |
17756 KB |
Output is correct |
15 |
Correct |
168 ms |
18720 KB |
Output is correct |
16 |
Correct |
163 ms |
15516 KB |
Output is correct |
17 |
Correct |
153 ms |
18632 KB |
Output is correct |
18 |
Correct |
150 ms |
18760 KB |
Output is correct |
19 |
Correct |
142 ms |
18760 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
165 ms |
18760 KB |
Output is correct |
23 |
Correct |
123 ms |
14968 KB |
Output is correct |
24 |
Correct |
164 ms |
18888 KB |
Output is correct |
25 |
Correct |
179 ms |
15640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
151 ms |
17360 KB |
Output is correct |
4 |
Correct |
114 ms |
18296 KB |
Output is correct |
5 |
Correct |
126 ms |
17912 KB |
Output is correct |
6 |
Correct |
645 ms |
63176 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
360 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
632 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
3 ms |
504 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
508 KB |
Output is correct |
21 |
Correct |
4 ms |
760 KB |
Output is correct |
22 |
Correct |
3 ms |
496 KB |
Output is correct |
23 |
Correct |
122 ms |
18664 KB |
Output is correct |
24 |
Correct |
144 ms |
22052 KB |
Output is correct |
25 |
Correct |
113 ms |
17344 KB |
Output is correct |
26 |
Correct |
112 ms |
18476 KB |
Output is correct |
27 |
Correct |
98 ms |
18184 KB |
Output is correct |
28 |
Correct |
127 ms |
18552 KB |
Output is correct |
29 |
Correct |
494 ms |
52224 KB |
Output is correct |
30 |
Correct |
144 ms |
21308 KB |
Output is correct |
31 |
Correct |
138 ms |
24384 KB |
Output is correct |
32 |
Correct |
160 ms |
19716 KB |
Output is correct |
33 |
Correct |
122 ms |
18300 KB |
Output is correct |
34 |
Correct |
98 ms |
18296 KB |
Output is correct |
35 |
Correct |
99 ms |
18552 KB |
Output is correct |
36 |
Correct |
124 ms |
18652 KB |
Output is correct |
37 |
Correct |
628 ms |
63100 KB |
Output is correct |
38 |
Correct |
145 ms |
21596 KB |
Output is correct |
39 |
Correct |
152 ms |
19240 KB |
Output is correct |
40 |
Correct |
149 ms |
21836 KB |
Output is correct |
41 |
Correct |
101 ms |
18400 KB |
Output is correct |
42 |
Correct |
98 ms |
18424 KB |
Output is correct |
43 |
Correct |
98 ms |
18312 KB |
Output is correct |
44 |
Correct |
136 ms |
17944 KB |
Output is correct |
45 |
Correct |
614 ms |
54116 KB |
Output is correct |
46 |
Correct |
136 ms |
21252 KB |
Output is correct |
47 |
Correct |
152 ms |
21948 KB |
Output is correct |
48 |
Correct |
138 ms |
24428 KB |
Output is correct |
49 |
Correct |
134 ms |
17304 KB |
Output is correct |
50 |
Correct |
137 ms |
18296 KB |
Output is correct |
51 |
Correct |
130 ms |
18384 KB |
Output is correct |
52 |
Correct |
99 ms |
18148 KB |
Output is correct |
53 |
Correct |
564 ms |
55900 KB |
Output is correct |
54 |
Correct |
153 ms |
20896 KB |
Output is correct |