#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define emb emplace_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline T range(T l, T r) {
return uniform_int_distribution<T>(l, r)(rng);
}
inline void setin(string s) {
freopen(s.c_str(), "r", stdin);
}
inline void setout(string s) {
freopen(s.c_str(), "w", stdout);
}
template <typename T> void Min(T &a, T b) {
a = min(a, b);
}
template <typename T> void Max(T &a, T b) {
a = max(a, b);
}
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 5e5 + 15;
int n, m, a[N];
vector <int> g[N];
bool used[N];
set <pii> cur;
int deg[N], d[N];
int p[N], sz[N];
pii e[N];
int pp[N];
int ans[N];
int pref[2][N];
int cnt;
vector <int> now;
bool cycle[N];
int find(int v) {
if(v == p[v])
return v;
return p[v] = find(p[v]);
}
void unio(int a, int b) {
a = find(a);
b = find(b);
if(a != b) {
if(sz[a] < sz[b])
swap(a, b);
p[b] = a;
sz[a] += sz[b];
}
}
inline int gt(int i, int v) {
return e[i].f == v ? e[i].se : e[i].f;
}
void precheck(int v) {
used[v] = true;
for(int ind : g[v]) {
int to = gt(ind, v);
if(to == pp[v])
continue;
if(used[to]) {
if(d[to] > d[v])
continue;
if((d[v] - d[to]) & 1) {
cout << 0 << endl;
exit(0);
}
++cnt;
if(cnt > 1) {
cout << 0 << endl;
exit(0);
}
int u = v;
while(u != to) {
unio(u, pp[u]);
u = pp[u];
}
u = v;
while(u != pp[to]) {
cycle[u] = true;
now.pb(u);
u = pp[u];
}
}
else {
d[to] = d[v] + 1;
pp[to] = v;
precheck(to);
}
}
}
inline int get(int k, int l, int r) {
k = (k + 10) & 1;
Max(l, 0LL);
if(l > r)
return 0;
return pref[k][r] - (l == 0 ? 0 : pref[k][l-1]);
}
void solve_for_cycle() {
int sum = 0;
for(int v : now)
sum += a[v];
for(int i = 0; i < now.size(); ++i) {
for(int j = 0; j < 2; ++j)
pref[j][i] = (i == 0 ? 0 : pref[j][i-1]);
pref[i & 1][i] += a[now[i]];
}
for(int i = 0; i + 1 < now.size(); ++i) {
int v = now[i];
int ind = -1;
for(int j = 0; j < g[v].size(); ++j)
if(gt(g[v][j], v) == now[i + 1])
ind = g[v][j];
// if(ind == -1) {
// cout << "ASdasd" << endl;
// exit(0);
// }
assert(ind != -1);
int val = get(i + 2, i + 2, now.size() - 1) + get(i - 1, 0, i - 1);
ans[ind] = sum - 2 * val;
}
int v = now.back();
int ind = -1;
for(int j = 0; j < g[v].size(); ++j)
if(gt(g[v][j], v) == now[0])
ind = g[v][j];
// if(ind == -1) {
// cout << "ASdasd" << endl;
// exit(0);
// }
assert(ind != -1);
int val = get(1, 1, now.size() - 2);
ans[ind] = sum - 2 * val;
}
int q[N], ql, qr;
main() {
for(int i = 0; i < N; ++i)
p[i] = i, sz[i] = 1;
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
// setin("input.txt");
cin >> n >> m;
for(int i = 1; i <= n; ++i)
cin >> a[i];
for(int i = 1; i <= m; ++i) {
cin >> e[i].f >> e[i].se;
g[e[i].f].pb(i);
g[e[i].se].pb(i);
}
precheck(1);
memset(used, 0, sizeof(used));
ql = 0, qr = -1;
for(int i = 1; i <= n; ++i) {
deg[i] = g[i].size();
if(deg[i] == 1) {
q[++qr] = i;
used[i] = true;
if(cnt == 0)
break;
}
}
while(ql <= qr) {
int v = q[ql++];
if(cycle[v])
break;
for(int ind : g[v]) {
int to = gt(ind, v);
if(used[to] && deg[to] == 1)
a[v] -= ans[ind] / 2;
}
for(int ind : g[v]) {
int to = gt(ind, v);
if(used[to])
continue;
ans[ind] = 2 * a[v];
--deg[to];
if(deg[to] == 1) {
q[++qr] = to;
used[to] = true;
}
}
}
memset(used, 0, sizeof(used));
solve_for_cycle();
for(int i = 1; i <= m; ++i)
cout << ans[i] << endl;
return 0;
}
Compilation message
pipes.cpp: In function 'void solve_for_cycle()':
pipes.cpp:158:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < now.size(); ++i) {
~~^~~~~~~~~~~~
pipes.cpp:163:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i + 1 < now.size(); ++i) {
~~~~~~^~~~~~~~~~~~
pipes.cpp:166:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < g[v].size(); ++j)
~~^~~~~~~~~~~~~
pipes.cpp:179:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < g[v].size(); ++j)
~~^~~~~~~~~~~~~
pipes.cpp: At global scope:
pipes.cpp:193:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
38 ms |
41216 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
33 ms |
41208 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
35 ms |
41336 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
123 ms |
56696 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
35 ms |
41208 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
39 ms |
41208 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
36 ms |
41208 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
36 ms |
41208 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
40 ms |
41344 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Runtime error |
35 ms |
41336 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Runtime error |
35 ms |
41356 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Runtime error |
34 ms |
41464 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
13 |
Runtime error |
86 ms |
53496 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
14 |
Runtime error |
94 ms |
55800 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
15 |
Runtime error |
103 ms |
56696 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
16 |
Runtime error |
84 ms |
54392 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
17 |
Runtime error |
105 ms |
56724 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
18 |
Runtime error |
106 ms |
56696 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
19 |
Runtime error |
160 ms |
61848 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
20 |
Runtime error |
44 ms |
41336 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
21 |
Runtime error |
36 ms |
41336 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
22 |
Runtime error |
99 ms |
56696 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
23 |
Runtime error |
105 ms |
53496 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
24 |
Runtime error |
115 ms |
56696 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
25 |
Runtime error |
110 ms |
54136 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
20480 KB |
Output isn't correct |
2 |
Incorrect |
17 ms |
20608 KB |
Output isn't correct |
3 |
Correct |
101 ms |
29820 KB |
Output is correct |
4 |
Correct |
79 ms |
32760 KB |
Output is correct |
5 |
Correct |
61 ms |
27640 KB |
Output is correct |
6 |
Correct |
248 ms |
43096 KB |
Output is correct |
7 |
Incorrect |
11 ms |
20480 KB |
Output isn't correct |
8 |
Incorrect |
12 ms |
20480 KB |
Output isn't correct |
9 |
Correct |
11 ms |
19968 KB |
Output is correct |
10 |
Correct |
12 ms |
19968 KB |
Output is correct |
11 |
Correct |
11 ms |
19968 KB |
Output is correct |
12 |
Correct |
12 ms |
19968 KB |
Output is correct |
13 |
Correct |
11 ms |
19968 KB |
Output is correct |
14 |
Incorrect |
11 ms |
20480 KB |
Output isn't correct |
15 |
Incorrect |
11 ms |
20608 KB |
Output isn't correct |
16 |
Incorrect |
14 ms |
20608 KB |
Output isn't correct |
17 |
Correct |
11 ms |
20096 KB |
Output is correct |
18 |
Correct |
11 ms |
20096 KB |
Output is correct |
19 |
Correct |
12 ms |
20132 KB |
Output is correct |
20 |
Correct |
11 ms |
20096 KB |
Output is correct |
21 |
Correct |
12 ms |
20096 KB |
Output is correct |
22 |
Incorrect |
12 ms |
20608 KB |
Output isn't correct |
23 |
Incorrect |
90 ms |
33008 KB |
Output isn't correct |
24 |
Incorrect |
107 ms |
35056 KB |
Output isn't correct |
25 |
Correct |
67 ms |
29816 KB |
Output is correct |
26 |
Correct |
80 ms |
31864 KB |
Output is correct |
27 |
Correct |
78 ms |
32120 KB |
Output is correct |
28 |
Correct |
61 ms |
26616 KB |
Output is correct |
29 |
Correct |
189 ms |
38520 KB |
Output is correct |
30 |
Incorrect |
110 ms |
36720 KB |
Output isn't correct |
31 |
Incorrect |
112 ms |
37608 KB |
Output isn't correct |
32 |
Incorrect |
100 ms |
32372 KB |
Output isn't correct |
33 |
Correct |
70 ms |
30840 KB |
Output is correct |
34 |
Correct |
97 ms |
31156 KB |
Output is correct |
35 |
Correct |
113 ms |
32760 KB |
Output is correct |
36 |
Correct |
64 ms |
27640 KB |
Output is correct |
37 |
Correct |
216 ms |
43000 KB |
Output is correct |
38 |
Incorrect |
114 ms |
36080 KB |
Output isn't correct |
39 |
Incorrect |
131 ms |
31992 KB |
Output isn't correct |
40 |
Incorrect |
107 ms |
34804 KB |
Output isn't correct |
41 |
Correct |
77 ms |
32624 KB |
Output is correct |
42 |
Correct |
118 ms |
31604 KB |
Output is correct |
43 |
Correct |
84 ms |
33016 KB |
Output is correct |
44 |
Correct |
58 ms |
27512 KB |
Output is correct |
45 |
Correct |
208 ms |
39672 KB |
Output is correct |
46 |
Incorrect |
135 ms |
37232 KB |
Output isn't correct |
47 |
Incorrect |
124 ms |
34804 KB |
Output isn't correct |
48 |
Incorrect |
132 ms |
37352 KB |
Output isn't correct |
49 |
Correct |
72 ms |
28024 KB |
Output is correct |
50 |
Correct |
106 ms |
31352 KB |
Output is correct |
51 |
Correct |
79 ms |
29816 KB |
Output is correct |
52 |
Correct |
67 ms |
28408 KB |
Output is correct |
53 |
Correct |
189 ms |
39800 KB |
Output is correct |
54 |
Incorrect |
135 ms |
35440 KB |
Output isn't correct |