#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
const int MXN = 1e5+5;
int n, m;
ll C[MXN];
vector<pii> adj[MXN];
int degree[MXN];
ll E[5*MXN];
bool visited[MXN];
void dfs(int u, vi &comp) {
visited[u] = true;
comp.PB(u);
for(auto p : adj[u]) {
int v = p.F;
if(visited[v] || !degree[v]) continue;
dfs(v, comp);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> C[i];
for(int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
adj[a].PB({b, i});
adj[b].PB({a, i});
}
queue<int> Q;
for(int i = 1; i <= n; i++) {
degree[i] = adj[i].size();
if(degree[i] == 1) Q.push(i);
}
while(!Q.empty()) {
int u = Q.front();
Q.pop();
degree[u] = 0;
for(auto p : adj[u]) {
if(degree[p.F]) {
// there should only be one v that satisfies this condition
E[p.S] = C[u];
degree[p.F]--;
C[p.F] -= C[u];
if(degree[p.F] == 1) Q.push(p.F);
}
}
}
// all remaining vertices should be disjoint cycles of odd length
for(int i = 1; i <= n; i++) {
if(degree[i] != 0 && degree[i] != 2) {
cout << "0\n";
return 0;
}
}
for(int i = 1; i <= n; i++) {
if(degree[i] && !visited[i]) {
vi comp;
dfs(i, comp);
cerr << comp.size() << "\n";
if(comp.size() % 2 == 0) {
cout << "0\n";
return 0;
}
ll sum = 0;
for(int u : comp) sum += C[u];
sum /= 2;
// keep edge cost between 0 and 1
for(int u = 2; u < comp.size(); u += 2) {
sum -= C[comp[u]];
}
ll last = 0;
for(auto p : adj[comp[0]]) {
if(p.F == comp[1]) {
E[p.S] = sum;
last = E[p.S];
}
}
for(int u = 1; u < comp.size(); u++) {
for(auto p : adj[comp[u]]) {
if(p.F == comp[(u+1)%comp.size()]) {
E[p.S] = C[comp[u]]-last;
last = E[p.S];
}
}
}
}
}
for(int i = 0; i < m; i++) {
cout << 2*E[i] << "\n";
}
return 0;
}
/*
3 2
1 2 1
1 2
2 3
3 3
8 10 12
1 2
2 3
1 3
3 7 5
*/
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:81:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int u = 2; u < comp.size(); u += 2) {
| ~~^~~~~~~~~~~~~
pipes.cpp:91:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int u = 1; u < comp.size(); u++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
70 ms |
10020 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2676 KB |
Output is correct |
7 |
Correct |
2 ms |
2624 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2684 KB |
Output is correct |
13 |
Correct |
44 ms |
8736 KB |
Output is correct |
14 |
Correct |
68 ms |
9612 KB |
Output is correct |
15 |
Correct |
56 ms |
9940 KB |
Output is correct |
16 |
Correct |
43 ms |
8964 KB |
Output is correct |
17 |
Correct |
53 ms |
9980 KB |
Output is correct |
18 |
Correct |
63 ms |
9984 KB |
Output is correct |
19 |
Correct |
73 ms |
9248 KB |
Output is correct |
20 |
Correct |
2 ms |
2676 KB |
Output is correct |
21 |
Correct |
3 ms |
2644 KB |
Output is correct |
22 |
Correct |
59 ms |
10032 KB |
Output is correct |
23 |
Correct |
56 ms |
8632 KB |
Output is correct |
24 |
Correct |
52 ms |
10024 KB |
Output is correct |
25 |
Correct |
47 ms |
8928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2684 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
46 ms |
11996 KB |
Output is correct |
4 |
Correct |
51 ms |
8860 KB |
Output is correct |
5 |
Correct |
48 ms |
9208 KB |
Output is correct |
6 |
Correct |
185 ms |
18760 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2676 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2680 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2684 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2816 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
52 ms |
11844 KB |
Output is correct |
24 |
Correct |
61 ms |
13004 KB |
Output is correct |
25 |
Correct |
44 ms |
12040 KB |
Output is correct |
26 |
Correct |
39 ms |
8984 KB |
Output is correct |
27 |
Correct |
38 ms |
8728 KB |
Output is correct |
28 |
Correct |
45 ms |
9444 KB |
Output is correct |
29 |
Correct |
174 ms |
16564 KB |
Output is correct |
30 |
Correct |
79 ms |
12224 KB |
Output is correct |
31 |
Correct |
62 ms |
15008 KB |
Output is correct |
32 |
Correct |
56 ms |
11016 KB |
Output is correct |
33 |
Correct |
52 ms |
12952 KB |
Output is correct |
34 |
Correct |
54 ms |
8856 KB |
Output is correct |
35 |
Correct |
42 ms |
8912 KB |
Output is correct |
36 |
Correct |
44 ms |
9276 KB |
Output is correct |
37 |
Correct |
143 ms |
18724 KB |
Output is correct |
38 |
Correct |
66 ms |
12508 KB |
Output is correct |
39 |
Correct |
60 ms |
10628 KB |
Output is correct |
40 |
Correct |
69 ms |
12844 KB |
Output is correct |
41 |
Correct |
67 ms |
14900 KB |
Output is correct |
42 |
Correct |
36 ms |
8784 KB |
Output is correct |
43 |
Correct |
38 ms |
8696 KB |
Output is correct |
44 |
Correct |
51 ms |
9164 KB |
Output is correct |
45 |
Correct |
150 ms |
16588 KB |
Output is correct |
46 |
Correct |
59 ms |
11892 KB |
Output is correct |
47 |
Correct |
61 ms |
12836 KB |
Output is correct |
48 |
Correct |
67 ms |
14788 KB |
Output is correct |
49 |
Correct |
61 ms |
10112 KB |
Output is correct |
50 |
Correct |
60 ms |
8960 KB |
Output is correct |
51 |
Correct |
45 ms |
9164 KB |
Output is correct |
52 |
Correct |
37 ms |
8780 KB |
Output is correct |
53 |
Correct |
129 ms |
16508 KB |
Output is correct |
54 |
Correct |
60 ms |
11912 KB |
Output is correct |