#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
if (M > N) {
cout << 0 << "\n";
exit(0);
}
vector<int64_t> c(N);
for (auto &v : c) {
cin >> v;
}
vector<vector<pair<int, int>>> G(N);
vector<int> d(N);
for (int i = 0; i < M; i++) {
int u, v;
cin >> u >> v;
u--, v--;
d[u]++, d[v]++;
G[u].push_back({v, i});
G[v].push_back({u, i});
}
vector<int> x(M);
queue<int> q;
for (int i = 0; i < N; i++) {
if (d[i] == 1) {
q.push(i);
}
}
vector<bool> used(N);
while (!q.empty()) {
int u = q.front();
q.pop();
used[u] = true;
for (auto [v, i] : G[u]) {
if (!used[v]) {
x[i] = c[u];
c[v] -= c[u];
d[v]--;
if (d[v] == 1) {
q.push(v);
}
}
}
}
if (count(used.begin(), used.end(), false) % 2 == 0 && N == M) {
cout << "0\n";
exit(0);
}
if (N == M) {
vector<int> cycle, cycle_edges;
for (int i = 0; i < N; i++) {
if (!used[i]) {
int j = i, last = -1;
while (j != i || last == -1) {
cycle.push_back(j);
for (auto [v, i] : G[j]) {
if (v != last && !used[v]) {
cycle_edges.push_back(i);
last = j;
j = v;
break;
}
}
}
break;
}
}
while (cycle.empty());
while (cycle.size() != cycle_edges.size());
int k = (int)cycle.size();
int64_t sum = 0;
for (int i = 0; i < k; i++) {
sum += c[cycle[i]] * (i % 2 ? -1 : 1);
}
sum /= 2;
x[cycle_edges.back()] = sum;
int64_t last = sum;
for (int i = 0; i < k - 1; i++) {
x[cycle_edges[i]] = c[cycle[i]] - last;
last = x[cycle_edges[i]];
}
}
for (int i = 0; i < M; i++) {
cout << x[i] * 2 << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
36 ms |
8544 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
29 ms |
6888 KB |
Output is correct |
14 |
Correct |
36 ms |
8132 KB |
Output is correct |
15 |
Correct |
37 ms |
8596 KB |
Output is correct |
16 |
Correct |
31 ms |
7372 KB |
Output is correct |
17 |
Correct |
37 ms |
8556 KB |
Output is correct |
18 |
Correct |
37 ms |
8532 KB |
Output is correct |
19 |
Correct |
45 ms |
7840 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
38 ms |
8536 KB |
Output is correct |
23 |
Correct |
29 ms |
6828 KB |
Output is correct |
24 |
Correct |
49 ms |
8524 KB |
Output is correct |
25 |
Correct |
30 ms |
7140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
28 ms |
7476 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
30 ms |
6848 KB |
Output is correct |
24 |
Correct |
37 ms |
8252 KB |
Output is correct |
25 |
Correct |
27 ms |
7500 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
36 ms |
7768 KB |
Output is correct |
31 |
Correct |
37 ms |
8392 KB |
Output is correct |
32 |
Correct |
38 ms |
8560 KB |
Output is correct |
33 |
Correct |
33 ms |
7676 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
37 ms |
8108 KB |
Output is correct |
39 |
Correct |
36 ms |
8508 KB |
Output is correct |
40 |
Correct |
37 ms |
8420 KB |
Output is correct |
41 |
Correct |
26 ms |
7400 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
37 ms |
7944 KB |
Output is correct |
47 |
Correct |
38 ms |
8340 KB |
Output is correct |
48 |
Correct |
37 ms |
8396 KB |
Output is correct |
49 |
Correct |
27 ms |
7636 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
38 ms |
8020 KB |
Output is correct |