#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
const int N = 1e5 + 5;
int n, m, c[N], deg[N], a[N], b[N];
long long x[N];
ii par[N];
vector<ii> graph[N];
bool vis[N], in[N];
vector<int> cycle;
void dfs(int u, int p, int type) {
vis[u] = 1;
for(int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i].fi;
if(v == p) continue;
if(vis[v] && type == 0 && in[v] == 0) {
int z = u;
while(z != v) {
if(z == 0) break;
cycle.pb(z);
in[z] = 1;
z = par[z].fi;
}
in[v] = 1;
cycle.pb(v);
continue;
}
if(vis[v]) continue;
par[v] = {u, graph[u][i].se};
dfs(v, u, type);
}
if(type == 1) {
if(in[u] == 0) {
ii lmao = par[u];
x[lmao.se] = 2 * c[u];
c[lmao.fi] = c[lmao.fi] - c[u];
c[u] = 0;
}
}
}
void bfs(int u, int p) {
for(int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i].fi;
if(v == p) continue;
bfs(v, u);
}
if(u != 1) {
ii lmao = par[u];
x[lmao.se] = 2 * c[u];
c[lmao.fi] = c[lmao.fi] - c[u];
c[u] = 0;
}
}
void bye() {
cout << "0\n";
exit(0);
}
map<ii, int> edge;
queue<int> q;
int main() {
cin.tie(0), ios::sync_with_stdio(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> c[i];
if(n < m)
bye();
for(int i = 1; i <= m; i++) {
cin >> a[i] >> b[i];
edge[{a[i], b[i]}] = i;
edge[{b[i], a[i]}] = i;
++deg[a[i]]; ++deg[b[i]];
graph[a[i]].pb({b[i], i});
graph[b[i]].pb({a[i], i});
}
dfs(1, 1, 0);
memset(vis, 0, sizeof(vis));
if(n == m + 1) {
bfs(1, 1);
if(c[1] != 0)
bye();
for(int i = 1; i <= m; i++)
cout << x[i] << "\n";
exit(0);
}
if(cycle.size() % 2 == 0)
bye();
dfs(cycle[0], cycle[0], 1);
long long total = 0, s = cycle.size() - 1;
for(int i = 0; i < cycle.size(); i++) {
if(i % 2 == 0)
total += c[cycle[i]];
else
total -= c[cycle[i]];
}
x[edge[{cycle[0], cycle[s]}]] = total;
if(total % 2 != 0)
bye();
c[cycle[0]] -= total / 2;
c[cycle[s]] -= total / 2;
for(int i = 0; i < cycle.size() - 1; i++) {
x[edge[{cycle[i], cycle[i + 1]}]] = 2 * c[cycle[i]];
c[cycle[i + 1]] = c[cycle[i + 1]] - c[cycle[i]];
c[cycle[i]] = 0;
}
if(c[cycle[s]] != 0)
bye();
for(int i = 1; i <= m; i++) {
if(x[i] > 1000000000 || x[i] < -1000000000)
bye();
}
for(int i = 1; i <= n; i++) {
if(c[i])
bye();
}
for(int i = 1; i <= m; i++)
cout << x[i] << "\n";
}
Compilation message
pipes.cpp: In function 'void dfs(int, int, int)':
pipes.cpp:27:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < graph[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~
pipes.cpp: In function 'void bfs(int, int)':
pipes.cpp:57:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < graph[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:108:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < cycle.size(); i++) {
~~^~~~~~~~~~~~~~
pipes.cpp:119:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < cycle.size() - 1; i++) {
~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2940 KB |
Output is correct |
3 |
Correct |
5 ms |
3144 KB |
Output is correct |
4 |
Correct |
210 ms |
22616 KB |
Output is correct |
5 |
Correct |
4 ms |
22616 KB |
Output is correct |
6 |
Correct |
4 ms |
22616 KB |
Output is correct |
7 |
Correct |
4 ms |
22616 KB |
Output is correct |
8 |
Correct |
4 ms |
22616 KB |
Output is correct |
9 |
Correct |
5 ms |
22616 KB |
Output is correct |
10 |
Correct |
5 ms |
22616 KB |
Output is correct |
11 |
Correct |
5 ms |
22616 KB |
Output is correct |
12 |
Correct |
5 ms |
22616 KB |
Output is correct |
13 |
Correct |
154 ms |
22616 KB |
Output is correct |
14 |
Correct |
206 ms |
22616 KB |
Output is correct |
15 |
Correct |
265 ms |
22852 KB |
Output is correct |
16 |
Correct |
173 ms |
22852 KB |
Output is correct |
17 |
Correct |
243 ms |
22852 KB |
Output is correct |
18 |
Correct |
225 ms |
22852 KB |
Output is correct |
19 |
Correct |
227 ms |
26184 KB |
Output is correct |
20 |
Correct |
4 ms |
26184 KB |
Output is correct |
21 |
Correct |
5 ms |
26184 KB |
Output is correct |
22 |
Correct |
211 ms |
26184 KB |
Output is correct |
23 |
Correct |
173 ms |
26184 KB |
Output is correct |
24 |
Correct |
220 ms |
26184 KB |
Output is correct |
25 |
Correct |
175 ms |
26184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
26184 KB |
Output is correct |
2 |
Correct |
5 ms |
26184 KB |
Output is correct |
3 |
Correct |
195 ms |
26184 KB |
Output is correct |
4 |
Correct |
13 ms |
26184 KB |
Output is correct |
5 |
Correct |
13 ms |
26184 KB |
Output is correct |
6 |
Correct |
14 ms |
26184 KB |
Output is correct |
7 |
Correct |
4 ms |
26184 KB |
Output is correct |
8 |
Correct |
4 ms |
26184 KB |
Output is correct |
9 |
Correct |
4 ms |
26184 KB |
Output is correct |
10 |
Correct |
4 ms |
26184 KB |
Output is correct |
11 |
Correct |
4 ms |
26184 KB |
Output is correct |
12 |
Correct |
4 ms |
26184 KB |
Output is correct |
13 |
Correct |
4 ms |
26184 KB |
Output is correct |
14 |
Correct |
4 ms |
26184 KB |
Output is correct |
15 |
Correct |
5 ms |
26184 KB |
Output is correct |
16 |
Correct |
5 ms |
26184 KB |
Output is correct |
17 |
Correct |
5 ms |
26184 KB |
Output is correct |
18 |
Correct |
4 ms |
26184 KB |
Output is correct |
19 |
Correct |
4 ms |
26184 KB |
Output is correct |
20 |
Correct |
4 ms |
26184 KB |
Output is correct |
21 |
Correct |
4 ms |
26184 KB |
Output is correct |
22 |
Correct |
5 ms |
26184 KB |
Output is correct |
23 |
Correct |
224 ms |
26184 KB |
Output is correct |
24 |
Correct |
264 ms |
27156 KB |
Output is correct |
25 |
Correct |
202 ms |
27156 KB |
Output is correct |
26 |
Correct |
14 ms |
27156 KB |
Output is correct |
27 |
Correct |
13 ms |
27156 KB |
Output is correct |
28 |
Correct |
13 ms |
27156 KB |
Output is correct |
29 |
Correct |
14 ms |
27156 KB |
Output is correct |
30 |
Correct |
286 ms |
29648 KB |
Output is correct |
31 |
Correct |
315 ms |
29780 KB |
Output is correct |
32 |
Correct |
253 ms |
29780 KB |
Output is correct |
33 |
Correct |
210 ms |
29780 KB |
Output is correct |
34 |
Correct |
13 ms |
29780 KB |
Output is correct |
35 |
Correct |
13 ms |
29780 KB |
Output is correct |
36 |
Correct |
14 ms |
29780 KB |
Output is correct |
37 |
Correct |
14 ms |
29780 KB |
Output is correct |
38 |
Correct |
276 ms |
29780 KB |
Output is correct |
39 |
Correct |
238 ms |
29780 KB |
Output is correct |
40 |
Correct |
263 ms |
29780 KB |
Output is correct |
41 |
Correct |
212 ms |
29780 KB |
Output is correct |
42 |
Correct |
14 ms |
29780 KB |
Output is correct |
43 |
Correct |
14 ms |
29780 KB |
Output is correct |
44 |
Correct |
13 ms |
29780 KB |
Output is correct |
45 |
Correct |
9 ms |
29780 KB |
Output is correct |
46 |
Correct |
274 ms |
30136 KB |
Output is correct |
47 |
Correct |
292 ms |
30136 KB |
Output is correct |
48 |
Correct |
294 ms |
30136 KB |
Output is correct |
49 |
Correct |
180 ms |
30136 KB |
Output is correct |
50 |
Correct |
14 ms |
30136 KB |
Output is correct |
51 |
Correct |
13 ms |
30136 KB |
Output is correct |
52 |
Correct |
13 ms |
30136 KB |
Output is correct |
53 |
Correct |
11 ms |
30136 KB |
Output is correct |
54 |
Correct |
277 ms |
30136 KB |
Output is correct |