#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 5e5+10;
int n, m, c[maxn], ans[maxn], gr[maxn];
vector<pair<int,int>> g[maxn], g1[maxn];
int sm = 0;
vector<int> vrt;
void dfs(int u, int ant, int cnt) {
sm+= c[u]*cnt;
auto V = g1[u][0];
if(V.fr == ant) V = g1[u][1];
int v = V.fr;
if(v == vrt[0]) return;
dfs(v,u,cnt*-1);
}
void solve() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> c[i];
}
for(int i = 1; i <= m; i++) {
int u,v; cin >> u >> v;
g[u].pb(mp(v,i));
g[v].pb(mp(u,i));
gr[u]++;
gr[v]++;
}
if(m > n) {
cout << 0 << endl;
return;
}
queue<int> q;
for(int i = 1; i <= n; i++) {
if(gr[i] == 1) q.push(i);
}
while(q.size()) {
int u = q.front(); q.pop();
gr[u] = -1;
for(auto V : g[u]) {
int v = V.fr;
int id = V.sc;
if(gr[v] == -1) continue;
ans[id] = 2*c[u];
c[v]-= c[u];
if(--gr[v] == 1) q.push(v);
}
}
if(n == m) {
for(int i = 1; i <= n; i++) {
if(gr[i] != -1) {
vrt.pb(i);
for(auto V : g[i]) {
if(gr[V.fr] != -1) {
g1[i].pb(V);
}
}
}
}
if(vrt.size()%2 == 1) {
dfs(vrt[0],-1,1);
ans[g1[vrt[0]][1].sc] = sm;
c[g1[vrt[0]][1].fr]-= sm/2;
c[vrt[0]]-= sm/2;
gr[g1[vrt[0]][1].fr]--;
gr[vrt[0]]--;
q.push(g1[vrt[0]][1].fr);
q.push(vrt[0]);
}
else {
cout << 0 << endl;
return;
}
while(q.size()) {
int u = q.front(); q.pop();
gr[u] = -1;
for(auto V : g[u]) {
int v = V.fr;
int id = V.sc;
if(gr[v] == -1 || id == g1[vrt[0]][1].sc) continue;
ans[id] = 2*c[u];
c[v]-= c[u];
if(--gr[v] == 1) q.push(v);
}
}
}
for(int i = 1; i <= m; i++) {
cout << ans[i] << endl;
}
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23792 KB |
Output is correct |
2 |
Correct |
12 ms |
23808 KB |
Output is correct |
3 |
Correct |
12 ms |
23892 KB |
Output is correct |
4 |
Correct |
67 ms |
32176 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23756 KB |
Output is correct |
7 |
Correct |
12 ms |
23772 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23892 KB |
Output is correct |
10 |
Correct |
12 ms |
23892 KB |
Output is correct |
11 |
Correct |
11 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23904 KB |
Output is correct |
13 |
Correct |
52 ms |
30360 KB |
Output is correct |
14 |
Correct |
83 ms |
31612 KB |
Output is correct |
15 |
Correct |
73 ms |
32152 KB |
Output is correct |
16 |
Correct |
55 ms |
30768 KB |
Output is correct |
17 |
Correct |
73 ms |
32124 KB |
Output is correct |
18 |
Correct |
69 ms |
32076 KB |
Output is correct |
19 |
Correct |
71 ms |
31760 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23860 KB |
Output is correct |
22 |
Correct |
63 ms |
32044 KB |
Output is correct |
23 |
Correct |
58 ms |
30320 KB |
Output is correct |
24 |
Correct |
66 ms |
32080 KB |
Output is correct |
25 |
Correct |
59 ms |
30704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23744 KB |
Output is correct |
2 |
Correct |
13 ms |
23892 KB |
Output is correct |
3 |
Correct |
69 ms |
33220 KB |
Output is correct |
4 |
Correct |
58 ms |
30544 KB |
Output is correct |
5 |
Correct |
54 ms |
30236 KB |
Output is correct |
6 |
Correct |
184 ms |
51216 KB |
Output is correct |
7 |
Correct |
13 ms |
23840 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23712 KB |
Output is correct |
10 |
Correct |
12 ms |
23756 KB |
Output is correct |
11 |
Correct |
12 ms |
23744 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
13 ms |
23832 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23924 KB |
Output is correct |
16 |
Correct |
12 ms |
23888 KB |
Output is correct |
17 |
Correct |
12 ms |
23892 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23892 KB |
Output is correct |
20 |
Correct |
11 ms |
23892 KB |
Output is correct |
21 |
Correct |
12 ms |
23984 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
70 ms |
33044 KB |
Output is correct |
24 |
Correct |
79 ms |
34624 KB |
Output is correct |
25 |
Correct |
58 ms |
33224 KB |
Output is correct |
26 |
Correct |
48 ms |
30468 KB |
Output is correct |
27 |
Correct |
53 ms |
30360 KB |
Output is correct |
28 |
Correct |
70 ms |
30716 KB |
Output is correct |
29 |
Correct |
147 ms |
45940 KB |
Output is correct |
30 |
Correct |
81 ms |
33908 KB |
Output is correct |
31 |
Correct |
89 ms |
36356 KB |
Output is correct |
32 |
Correct |
73 ms |
32564 KB |
Output is correct |
33 |
Correct |
61 ms |
34312 KB |
Output is correct |
34 |
Correct |
48 ms |
30540 KB |
Output is correct |
35 |
Correct |
49 ms |
30492 KB |
Output is correct |
36 |
Correct |
52 ms |
30648 KB |
Output is correct |
37 |
Correct |
190 ms |
51312 KB |
Output is correct |
38 |
Correct |
106 ms |
34256 KB |
Output is correct |
39 |
Correct |
70 ms |
32180 KB |
Output is correct |
40 |
Correct |
79 ms |
34360 KB |
Output is correct |
41 |
Correct |
66 ms |
35936 KB |
Output is correct |
42 |
Correct |
55 ms |
30440 KB |
Output is correct |
43 |
Correct |
48 ms |
30416 KB |
Output is correct |
44 |
Correct |
46 ms |
30332 KB |
Output is correct |
45 |
Correct |
144 ms |
48324 KB |
Output is correct |
46 |
Correct |
86 ms |
33732 KB |
Output is correct |
47 |
Correct |
82 ms |
34456 KB |
Output is correct |
48 |
Correct |
89 ms |
36256 KB |
Output is correct |
49 |
Correct |
63 ms |
31524 KB |
Output is correct |
50 |
Correct |
55 ms |
30472 KB |
Output is correct |
51 |
Correct |
63 ms |
30464 KB |
Output is correct |
52 |
Correct |
52 ms |
30476 KB |
Output is correct |
53 |
Correct |
142 ms |
47628 KB |
Output is correct |
54 |
Correct |
76 ms |
33536 KB |
Output is correct |