#include <bits/stdc++.h>
#define FOR(i,x,y) for (int i=x;i < y;i++)
using namespace std;const int N=6e5;int n,m;set<pair<int,int>> g[N];int c[N],ans[N],visited[N],S=0;void d(int node,int parity=1) {visited[node]=parity;for (pair<int,int> i : g[node]) if (!visited[i.first]) {d(i.first,3 - parity);if (parity == 1) S += c[i.first];else S -= c[i.first];}}void p() {queue<int> leaves;FOR(i,1,n + 1) if (g[i].size() == 1) leaves.push(i);while (leaves.size()) {int curr=leaves.front();leaves.pop();for (pair<int,int> i : g[curr]) {g[i.first].erase({curr,i.second});ans[i.second]=2 * c[curr];c[i.first] -= c[curr];if (g[i.first].size() == 1) leaves.push(i.first);}g[curr].clear();}}int main() {iostream::sync_with_stdio(false);cin.tie(0);cin >> n >> m;FOR(i,1,n + 1) cin >> c[i];FOR(i,1,m + 1) {int x,y;cin >> x >> y;g[x].insert({y,i});g[y].insert({x,i});}p();int cnt=0;FOR(i,1,n + 1) {if (g[i].size() > 2) return cout << 0,0;if (g[i].size() == 2) cnt++;}if (cnt != 0 && !(cnt & 1)) return cout << 0,0;FOR(i,1,n + 1) if (g[i].size() == 2) {d(i);int x=(*g[i].begin()).first,y=(*g[i].begin()).second;ans[y]=c[i] + S;g[i].erase({x,y});g[x].erase({i,y});c[i] -= ans[y] / 2;c[x] -= ans[y] / 2;break;}p();FOR(i,1,m + 1) cout << ans[i] << '\n';return 0;}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
28536 KB |
Output is correct |
2 |
Correct |
23 ms |
28536 KB |
Output is correct |
3 |
Correct |
24 ms |
28664 KB |
Output is correct |
4 |
Correct |
157 ms |
38904 KB |
Output is correct |
5 |
Correct |
22 ms |
28536 KB |
Output is correct |
6 |
Correct |
23 ms |
28536 KB |
Output is correct |
7 |
Correct |
22 ms |
28536 KB |
Output is correct |
8 |
Correct |
22 ms |
28536 KB |
Output is correct |
9 |
Correct |
23 ms |
28664 KB |
Output is correct |
10 |
Correct |
24 ms |
28664 KB |
Output is correct |
11 |
Correct |
24 ms |
28664 KB |
Output is correct |
12 |
Correct |
23 ms |
28664 KB |
Output is correct |
13 |
Correct |
127 ms |
36728 KB |
Output is correct |
14 |
Correct |
153 ms |
38392 KB |
Output is correct |
15 |
Correct |
148 ms |
38904 KB |
Output is correct |
16 |
Correct |
122 ms |
37368 KB |
Output is correct |
17 |
Correct |
146 ms |
38776 KB |
Output is correct |
18 |
Correct |
152 ms |
38996 KB |
Output is correct |
19 |
Correct |
129 ms |
38776 KB |
Output is correct |
20 |
Correct |
22 ms |
28536 KB |
Output is correct |
21 |
Correct |
23 ms |
28664 KB |
Output is correct |
22 |
Correct |
149 ms |
38904 KB |
Output is correct |
23 |
Correct |
116 ms |
36728 KB |
Output is correct |
24 |
Correct |
147 ms |
38864 KB |
Output is correct |
25 |
Correct |
120 ms |
37112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
28536 KB |
Output is correct |
2 |
Correct |
25 ms |
28792 KB |
Output is correct |
3 |
Correct |
113 ms |
38264 KB |
Output is correct |
4 |
Correct |
104 ms |
38776 KB |
Output is correct |
5 |
Correct |
123 ms |
38648 KB |
Output is correct |
6 |
Correct |
572 ms |
76044 KB |
Output is correct |
7 |
Correct |
22 ms |
28536 KB |
Output is correct |
8 |
Correct |
22 ms |
28536 KB |
Output is correct |
9 |
Correct |
22 ms |
28536 KB |
Output is correct |
10 |
Correct |
23 ms |
28536 KB |
Output is correct |
11 |
Correct |
24 ms |
28540 KB |
Output is correct |
12 |
Correct |
22 ms |
28536 KB |
Output is correct |
13 |
Correct |
23 ms |
28536 KB |
Output is correct |
14 |
Correct |
23 ms |
28536 KB |
Output is correct |
15 |
Correct |
23 ms |
28792 KB |
Output is correct |
16 |
Correct |
23 ms |
28664 KB |
Output is correct |
17 |
Correct |
24 ms |
28664 KB |
Output is correct |
18 |
Correct |
25 ms |
28664 KB |
Output is correct |
19 |
Correct |
23 ms |
28664 KB |
Output is correct |
20 |
Correct |
22 ms |
28664 KB |
Output is correct |
21 |
Correct |
23 ms |
28792 KB |
Output is correct |
22 |
Correct |
23 ms |
28664 KB |
Output is correct |
23 |
Correct |
124 ms |
40824 KB |
Output is correct |
24 |
Correct |
165 ms |
42992 KB |
Output is correct |
25 |
Correct |
115 ms |
38264 KB |
Output is correct |
26 |
Correct |
110 ms |
38752 KB |
Output is correct |
27 |
Correct |
99 ms |
38648 KB |
Output is correct |
28 |
Correct |
130 ms |
39280 KB |
Output is correct |
29 |
Correct |
432 ms |
66936 KB |
Output is correct |
30 |
Correct |
135 ms |
42232 KB |
Output is correct |
31 |
Correct |
147 ms |
45048 KB |
Output is correct |
32 |
Correct |
142 ms |
40776 KB |
Output is correct |
33 |
Correct |
111 ms |
38864 KB |
Output is correct |
34 |
Correct |
105 ms |
38700 KB |
Output is correct |
35 |
Correct |
104 ms |
38792 KB |
Output is correct |
36 |
Correct |
124 ms |
38904 KB |
Output is correct |
37 |
Correct |
551 ms |
75944 KB |
Output is correct |
38 |
Correct |
141 ms |
42488 KB |
Output is correct |
39 |
Correct |
151 ms |
40252 KB |
Output is correct |
40 |
Correct |
148 ms |
42744 KB |
Output is correct |
41 |
Correct |
113 ms |
38708 KB |
Output is correct |
42 |
Correct |
94 ms |
38780 KB |
Output is correct |
43 |
Correct |
96 ms |
38776 KB |
Output is correct |
44 |
Correct |
125 ms |
38648 KB |
Output is correct |
45 |
Correct |
526 ms |
70552 KB |
Output is correct |
46 |
Correct |
140 ms |
42104 KB |
Output is correct |
47 |
Correct |
145 ms |
42744 KB |
Output is correct |
48 |
Correct |
154 ms |
44920 KB |
Output is correct |
49 |
Correct |
125 ms |
38264 KB |
Output is correct |
50 |
Correct |
111 ms |
38776 KB |
Output is correct |
51 |
Correct |
117 ms |
38904 KB |
Output is correct |
52 |
Correct |
104 ms |
38648 KB |
Output is correct |
53 |
Correct |
514 ms |
71288 KB |
Output is correct |
54 |
Correct |
142 ms |
41848 KB |
Output is correct |