#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m;
int a[N];
int par[N];
int deg[N];
int res[N];
bool vis[N];
vector< pair<int, int> > G[N];
map< pair<int, int>, int > mp;
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
if (m > n) {
cout << 0; return 0;
}
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= m; ++i) {
int u, v; cin >> u >> v;
G[u].push_back({v, i});
G[v].push_back({u, i});
deg[u]++, deg[v]++;
mp[{u, v}] = mp[{v, u}] = i;
}
queue<int> qu;
for (int i = 1; i <= n; ++i) {
if (deg[i] == 1) qu.push(i);
}
while (qu.size()) {
int u = qu.front(); qu.pop();
for (auto v : G[u]) {
if (par[v.first] == u) continue;
deg[v.first]--, par[u] = v.first;
a[v.first] -= a[u], res[v.second] = a[u];
if (deg[v.first] == 1) qu.push(v.first);
}
}
for (int i = 1; i <= n; ++i) {
if (deg[i] != 2) continue;
int u = i;
vector<int> ver;
while (1) {
bool found = 0;
vis[u] = 1, ver.push_back(u);
for (auto v : G[u]) {
if (deg[v.first] == 2 && !vis[v.first]) {
u = v.first, found = 1; break;
}
}
if (!found) break;
}
int sz = ver.size();
if (sz % 2 == 0) {
cout << 0; return 0;
}
vector<int> edg;
for (int j = 0; j < sz; ++j) {
edg.push_back(mp[{ver[j], ver[(j + 1) % sz]}]);
}
res[edg[0]] = 0;
for (int j = 1; j < sz; ++j) {
res[edg[j]] = a[ver[j]] - res[edg[j - 1]];
}
if ((res[edg[sz - 1]] & 1) != (a[ver[0]] & 1)) {
cout << 0; return 0;
}
res[edg[0]] = (a[ver[0]] - res[edg[sz - 1]]) / 2;
for (int j = 1; j < sz; ++j) {
res[edg[j]] = a[ver[j]] - res[edg[j - 1]];
}
break;
}
for (int i = 1; i <= m; ++i) cout << res[i] * 2 << ' ';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
8 ms |
3092 KB |
Output is correct |
4 |
Correct |
275 ms |
21180 KB |
Output is correct |
5 |
Correct |
5 ms |
21180 KB |
Output is correct |
6 |
Correct |
4 ms |
21180 KB |
Output is correct |
7 |
Correct |
5 ms |
21180 KB |
Output is correct |
8 |
Correct |
4 ms |
21180 KB |
Output is correct |
9 |
Correct |
5 ms |
21180 KB |
Output is correct |
10 |
Correct |
5 ms |
21180 KB |
Output is correct |
11 |
Correct |
5 ms |
21180 KB |
Output is correct |
12 |
Correct |
5 ms |
21180 KB |
Output is correct |
13 |
Correct |
203 ms |
21180 KB |
Output is correct |
14 |
Correct |
256 ms |
21180 KB |
Output is correct |
15 |
Correct |
287 ms |
21552 KB |
Output is correct |
16 |
Correct |
220 ms |
21552 KB |
Output is correct |
17 |
Correct |
300 ms |
21552 KB |
Output is correct |
18 |
Correct |
269 ms |
21552 KB |
Output is correct |
19 |
Correct |
263 ms |
21552 KB |
Output is correct |
20 |
Correct |
5 ms |
21552 KB |
Output is correct |
21 |
Correct |
6 ms |
21552 KB |
Output is correct |
22 |
Correct |
328 ms |
21552 KB |
Output is correct |
23 |
Correct |
211 ms |
21552 KB |
Output is correct |
24 |
Correct |
287 ms |
21552 KB |
Output is correct |
25 |
Correct |
263 ms |
21552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21552 KB |
Output is correct |
2 |
Correct |
5 ms |
21552 KB |
Output is correct |
3 |
Correct |
250 ms |
21552 KB |
Output is correct |
4 |
Correct |
4 ms |
21552 KB |
Output is correct |
5 |
Correct |
5 ms |
21552 KB |
Output is correct |
6 |
Correct |
4 ms |
21552 KB |
Output is correct |
7 |
Correct |
4 ms |
21552 KB |
Output is correct |
8 |
Correct |
4 ms |
21552 KB |
Output is correct |
9 |
Correct |
4 ms |
21552 KB |
Output is correct |
10 |
Correct |
4 ms |
21552 KB |
Output is correct |
11 |
Correct |
4 ms |
21552 KB |
Output is correct |
12 |
Correct |
4 ms |
21552 KB |
Output is correct |
13 |
Correct |
6 ms |
21552 KB |
Output is correct |
14 |
Correct |
4 ms |
21552 KB |
Output is correct |
15 |
Correct |
6 ms |
21552 KB |
Output is correct |
16 |
Correct |
6 ms |
21552 KB |
Output is correct |
17 |
Correct |
13 ms |
21552 KB |
Output is correct |
18 |
Correct |
5 ms |
21552 KB |
Output is correct |
19 |
Correct |
5 ms |
21552 KB |
Output is correct |
20 |
Correct |
17 ms |
21552 KB |
Output is correct |
21 |
Correct |
5 ms |
21552 KB |
Output is correct |
22 |
Correct |
6 ms |
21552 KB |
Output is correct |
23 |
Correct |
250 ms |
21552 KB |
Output is correct |
24 |
Correct |
312 ms |
21552 KB |
Output is correct |
25 |
Correct |
236 ms |
21552 KB |
Output is correct |
26 |
Correct |
5 ms |
21552 KB |
Output is correct |
27 |
Correct |
4 ms |
21552 KB |
Output is correct |
28 |
Correct |
5 ms |
21552 KB |
Output is correct |
29 |
Correct |
5 ms |
21552 KB |
Output is correct |
30 |
Correct |
261 ms |
21552 KB |
Output is correct |
31 |
Correct |
371 ms |
21804 KB |
Output is correct |
32 |
Correct |
271 ms |
21860 KB |
Output is correct |
33 |
Correct |
205 ms |
21860 KB |
Output is correct |
34 |
Correct |
4 ms |
21860 KB |
Output is correct |
35 |
Correct |
5 ms |
21860 KB |
Output is correct |
36 |
Correct |
4 ms |
21860 KB |
Output is correct |
37 |
Correct |
4 ms |
21860 KB |
Output is correct |
38 |
Correct |
319 ms |
21860 KB |
Output is correct |
39 |
Correct |
274 ms |
21860 KB |
Output is correct |
40 |
Correct |
339 ms |
21860 KB |
Output is correct |
41 |
Correct |
226 ms |
21860 KB |
Output is correct |
42 |
Correct |
4 ms |
21860 KB |
Output is correct |
43 |
Correct |
4 ms |
21860 KB |
Output is correct |
44 |
Correct |
4 ms |
21860 KB |
Output is correct |
45 |
Correct |
4 ms |
21860 KB |
Output is correct |
46 |
Correct |
297 ms |
21860 KB |
Output is correct |
47 |
Correct |
283 ms |
21860 KB |
Output is correct |
48 |
Correct |
319 ms |
22076 KB |
Output is correct |
49 |
Correct |
212 ms |
22076 KB |
Output is correct |
50 |
Correct |
4 ms |
22076 KB |
Output is correct |
51 |
Correct |
4 ms |
22076 KB |
Output is correct |
52 |
Correct |
4 ms |
22076 KB |
Output is correct |
53 |
Correct |
4 ms |
22076 KB |
Output is correct |
54 |
Correct |
244 ms |
22076 KB |
Output is correct |