/*
Unknown's C++ Template (v3.2)
*/
#include "bits/stdc++.h"
using namespace std;
#define int long long
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vvii = vector<vii>;
template <class T> using maxpq = priority_queue<T>;
template <class T> using minpq = priority_queue<T, vector<T>, greater<T>>;
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mid ((l+r)>>1)
#define fi first
#define se second
#ifdef LOCAL
#define debug(x) cout << #x << " = " << x << "\n";
#else
#define debug(x) ;
#endif
template <class A, class B>
ostream& operator << (ostream& out, pair<A, B> x)
{ out << "(" << x.first << ", " << x.second << ")"; return out; }
template <class T>
ostream& operator << (ostream& out, vector<T> x){
out << "[";
for (int i=0; i<sz(x); i++) { out << (i ? ", " : "") << x[i]; }
out << "]"; return out;
}
template <class T>
istream& operator >> (istream& in, vector<T> &x){
for (auto &i: x) in >> i;
return in;
}
const ld PI = acos(-1.0);
const int allmod[3] = {(int)1e9+7, 998244353, (int)1e9+9};
const int mod = allmod[0];
const int maxn = 2e5 + 64;
const ll inf = 1e18;
const ld eps = 1e-6;
const int multitest = 0;
int n, m, root = -1;
vi deg, change, res;
vi pt_order, edge_order;
vii edges;
vvii adj;
void dfs(int x, int p = -1){
if (x == root && p >= 0) return;
pt_order.pb(x);
for (auto k: adj[x]){
if (k.fi == p) continue;
if (!deg[k.fi]) continue;
edge_order.pb(k.se);
dfs(k.fi, x);
break;
}
}
void main_program(){
cin >> n >> m;
deg.assign(n, 0); change.resize(n); cin >> change;
adj.resize(n);
res.resize(m); edges.resize(m);
for (int i = 0; i < m; i++){
int x, y; cin >> x >> y;
x--; y--;
deg[x]++; deg[y]++;
edges[i] = {x, y};
adj[x].pb({y, i}); adj[y].pb({x, i});
}
queue<int> q;
for (int i = 0; i < n; i++){
if (deg[i] == 1) q.push(i);
}
while (!q.empty()){
int x = q.front(); q.pop();
for (auto k: adj[x]){
if (deg[k.fi]){
res[k.se] += change[x] * 2;
change[k.fi] -= change[x];
deg[k.fi]--;
if (deg[k.fi] == 1) q.push(k.fi);
}
}
change[x] = 0;
if (deg[x]) deg[x]--;
}
if ((n == m) || (*max_element(all(deg)) == 0)){
for (int i = 0; i < n; i++){
assert((deg[i] >= 0));
if (deg[i] > 0) root = i;
}
if (root >= 0){
dfs(root);
int tt = 0;
for (auto i: pt_order) tt += change[i];
for (int i = 1; i < sz(pt_order); i += 2) tt -= change[pt_order[i]] * 2;
res[edge_order.back()] = tt;
for (int i = sz(pt_order) - 1; i >= 1; i--){
res[edge_order[i-1]] = 2 * change[pt_order[i]] - res[edge_order[i]];
}
}
for (int i = 0; i < m; i++) cout << res[i] << "\n";
}
else cout << "0\n";
}
void pre_main(){
}
signed main(){
#ifdef LOCAL
auto stime = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
if (fopen(".inp", "r")){
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
#endif
int t = 1; if (multitest) cin >> t;
pre_main();
while (t--) main_program();
#ifdef LOCAL
auto etime = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(etime-stime).count();
cout << "\n[" << duration << "ms]\n";
#endif
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:148:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
pipes.cpp:149:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
149 | freopen(".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
68 ms |
12476 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
400 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
47 ms |
9988 KB |
Output is correct |
14 |
Correct |
60 ms |
11844 KB |
Output is correct |
15 |
Correct |
61 ms |
12488 KB |
Output is correct |
16 |
Correct |
53 ms |
10632 KB |
Output is correct |
17 |
Correct |
65 ms |
12476 KB |
Output is correct |
18 |
Correct |
62 ms |
12520 KB |
Output is correct |
19 |
Correct |
66 ms |
12228 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
59 ms |
12520 KB |
Output is correct |
23 |
Correct |
42 ms |
9936 KB |
Output is correct |
24 |
Correct |
59 ms |
12600 KB |
Output is correct |
25 |
Correct |
52 ms |
10420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Incorrect |
71 ms |
15308 KB |
Output isn't correct |
4 |
Correct |
42 ms |
11784 KB |
Output is correct |
5 |
Correct |
47 ms |
11712 KB |
Output is correct |
6 |
Correct |
196 ms |
41812 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
51 ms |
14256 KB |
Output is correct |
24 |
Correct |
66 ms |
16556 KB |
Output is correct |
25 |
Incorrect |
57 ms |
15288 KB |
Output isn't correct |
26 |
Correct |
50 ms |
11888 KB |
Output is correct |
27 |
Correct |
47 ms |
11640 KB |
Output is correct |
28 |
Correct |
47 ms |
12364 KB |
Output is correct |
29 |
Correct |
145 ms |
34264 KB |
Output is correct |
30 |
Correct |
62 ms |
15544 KB |
Output is correct |
31 |
Correct |
64 ms |
19220 KB |
Output is correct |
32 |
Correct |
63 ms |
13720 KB |
Output is correct |
33 |
Incorrect |
60 ms |
16824 KB |
Output isn't correct |
34 |
Correct |
47 ms |
11828 KB |
Output is correct |
35 |
Correct |
45 ms |
11896 KB |
Output is correct |
36 |
Correct |
60 ms |
12060 KB |
Output is correct |
37 |
Correct |
175 ms |
41800 KB |
Output is correct |
38 |
Correct |
62 ms |
16012 KB |
Output is correct |
39 |
Correct |
64 ms |
13144 KB |
Output is correct |
40 |
Correct |
62 ms |
16188 KB |
Output is correct |
41 |
Incorrect |
73 ms |
19232 KB |
Output isn't correct |
42 |
Correct |
40 ms |
11720 KB |
Output is correct |
43 |
Correct |
41 ms |
11604 KB |
Output is correct |
44 |
Correct |
47 ms |
11756 KB |
Output is correct |
45 |
Correct |
139 ms |
36488 KB |
Output is correct |
46 |
Correct |
69 ms |
15548 KB |
Output is correct |
47 |
Correct |
71 ms |
16336 KB |
Output is correct |
48 |
Correct |
63 ms |
18888 KB |
Output is correct |
49 |
Incorrect |
55 ms |
12800 KB |
Output isn't correct |
50 |
Correct |
44 ms |
11864 KB |
Output is correct |
51 |
Correct |
64 ms |
11980 KB |
Output is correct |
52 |
Correct |
52 ms |
11676 KB |
Output is correct |
53 |
Correct |
145 ms |
36380 KB |
Output is correct |
54 |
Correct |
63 ms |
15080 KB |
Output is correct |