#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
const int NMAX = 100010;
vector <pair <int, int>> adia[NMAX + 10];
int tata[NMAX + 10];
int taken[NMAX + 10];
int h[NMAX + 10];
i64 ce_s_vrea[NMAX + 10];
int ans[5 * NMAX + 10];
vector <vector <int>> centers;
void Prost()
{
cout << 0;
exit(0);
}
bool dfs(int nod, bool did)
{
for (auto i : adia[nod]) {
if (i.first == tata[nod])
continue;
if (tata[i.first] != 0) {
if (h[i.first] > h[nod])
continue;
if (did)
Prost();
vector <int> c;
for (int j = nod; j != tata[i.first]; j = tata[j]) {
if (taken[j])
Prost();
taken[j] = 1;
c.push_back(j);
}
centers.push_back(c);
did = 1;
}
else {
tata[i.first] = nod;
h[i.first] = 1 + h[nod];
did |= dfs(i.first, did);
}
}
return did;
}
int solve_arb(int nod, int tata)
{
int ce_tata;
for (auto i : adia[nod]) {
if (i.first != tata) {
ans[i.second] = solve_arb(i.first, nod);
ce_s_vrea[nod] -= ans[i.second];
}
}
return ce_s_vrea[nod];
}
void solve_ciclu(vector <int> ciclu)
{
vector <int> a(ciclu.size());
vector <i64> s_st(ciclu.size() + 1), s_dr(ciclu.size() + 1);
int n = ciclu.size();
if (n % 2 == 0)
Prost();
for (int i = 0; i < n; i++) {
for (auto j : adia[ciclu[i]]) {
if (j.first != ciclu[(i + 1) % n] && j.first != ciclu[(i + n - 1) % n]) {
int x = solve_arb(j.first, ciclu[i]);
ce_s_vrea[ciclu[i]] -= x;
ans[j.second] = x;
}
else if (j.first == ciclu[(i + 1) % n])
a[i] = j.second;
}
}
i64 s_total = 0;
for (auto i : ciclu)
s_total += ce_s_vrea[i];
if ((s_total & 1LL) || (s_total && n == 1))
Prost();
s_st[0] = ce_s_vrea[ciclu[0]];
for (int i = 1; i < n; i++)
s_st[i] = ce_s_vrea[ciclu[i]] - s_st[i - 1];
s_dr[n - 1] = ce_s_vrea[ciclu[n - 1]];
for (int i = n - 2; i >= 0; i--)
s_dr[i] = ce_s_vrea[ciclu[i]] - s_dr[i + 1];
for (int i = 0; i < n; i++)
ans[a[i]] = (s_st[i] + s_dr[i + 1]) / 2;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, a, b;
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> ce_s_vrea[i];
for (int i = 1; i <= m; i++) {
cin >> a >> b;
adia[a].push_back({ b, i });
adia[b].push_back({ a, i });
}
for (int i = 1; i <= n; i++) {
if (!tata[i]) {
tata[i] = -1;
auto x = dfs(i, 0);
if (!x)
centers.push_back({ i });
}
}
for (auto i : centers)
solve_ciclu(i);
for (int i = 1; i <= m; i++)
cout << 2LL * ans[i] << '\n';
return 0;
}
Compilation message
pipes.cpp: In function 'int solve_arb(int, int)':
pipes.cpp:54:9: warning: unused variable 'ce_tata' [-Wunused-variable]
int ce_tata;
^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2848 KB |
Output is correct |
4 |
Correct |
83 ms |
9004 KB |
Output is correct |
5 |
Correct |
4 ms |
9004 KB |
Output is correct |
6 |
Correct |
4 ms |
9004 KB |
Output is correct |
7 |
Correct |
5 ms |
9004 KB |
Output is correct |
8 |
Correct |
4 ms |
9004 KB |
Output is correct |
9 |
Correct |
5 ms |
9004 KB |
Output is correct |
10 |
Correct |
5 ms |
9004 KB |
Output is correct |
11 |
Correct |
5 ms |
9004 KB |
Output is correct |
12 |
Correct |
5 ms |
9004 KB |
Output is correct |
13 |
Correct |
83 ms |
9004 KB |
Output is correct |
14 |
Correct |
114 ms |
9004 KB |
Output is correct |
15 |
Correct |
96 ms |
9396 KB |
Output is correct |
16 |
Correct |
67 ms |
9396 KB |
Output is correct |
17 |
Correct |
89 ms |
9412 KB |
Output is correct |
18 |
Correct |
95 ms |
9412 KB |
Output is correct |
19 |
Correct |
118 ms |
15752 KB |
Output is correct |
20 |
Correct |
6 ms |
15752 KB |
Output is correct |
21 |
Correct |
6 ms |
15752 KB |
Output is correct |
22 |
Correct |
98 ms |
15752 KB |
Output is correct |
23 |
Correct |
62 ms |
15752 KB |
Output is correct |
24 |
Correct |
80 ms |
15752 KB |
Output is correct |
25 |
Correct |
72 ms |
15752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15752 KB |
Output is correct |
2 |
Correct |
5 ms |
15752 KB |
Output is correct |
3 |
Correct |
102 ms |
16108 KB |
Output is correct |
4 |
Correct |
96 ms |
19516 KB |
Output is correct |
5 |
Correct |
62 ms |
19516 KB |
Output is correct |
6 |
Correct |
252 ms |
19516 KB |
Output is correct |
7 |
Correct |
4 ms |
19516 KB |
Output is correct |
8 |
Correct |
4 ms |
19516 KB |
Output is correct |
9 |
Correct |
9 ms |
19516 KB |
Output is correct |
10 |
Correct |
4 ms |
19516 KB |
Output is correct |
11 |
Correct |
4 ms |
19516 KB |
Output is correct |
12 |
Correct |
5 ms |
19516 KB |
Output is correct |
13 |
Correct |
5 ms |
19516 KB |
Output is correct |
14 |
Correct |
5 ms |
19516 KB |
Output is correct |
15 |
Correct |
6 ms |
19516 KB |
Output is correct |
16 |
Correct |
5 ms |
19516 KB |
Output is correct |
17 |
Correct |
4 ms |
19516 KB |
Output is correct |
18 |
Correct |
4 ms |
19516 KB |
Output is correct |
19 |
Correct |
5 ms |
19516 KB |
Output is correct |
20 |
Correct |
4 ms |
19516 KB |
Output is correct |
21 |
Correct |
5 ms |
19516 KB |
Output is correct |
22 |
Correct |
6 ms |
19516 KB |
Output is correct |
23 |
Correct |
80 ms |
19516 KB |
Output is correct |
24 |
Correct |
120 ms |
19516 KB |
Output is correct |
25 |
Correct |
75 ms |
19516 KB |
Output is correct |
26 |
Correct |
142 ms |
19516 KB |
Output is correct |
27 |
Correct |
121 ms |
19516 KB |
Output is correct |
28 |
Correct |
62 ms |
19516 KB |
Output is correct |
29 |
Correct |
233 ms |
19516 KB |
Output is correct |
30 |
Correct |
139 ms |
22384 KB |
Output is correct |
31 |
Correct |
116 ms |
23948 KB |
Output is correct |
32 |
Correct |
94 ms |
23948 KB |
Output is correct |
33 |
Correct |
88 ms |
23948 KB |
Output is correct |
34 |
Correct |
86 ms |
23948 KB |
Output is correct |
35 |
Correct |
127 ms |
23948 KB |
Output is correct |
36 |
Correct |
75 ms |
23948 KB |
Output is correct |
37 |
Correct |
232 ms |
23948 KB |
Output is correct |
38 |
Correct |
109 ms |
23948 KB |
Output is correct |
39 |
Correct |
95 ms |
23948 KB |
Output is correct |
40 |
Correct |
96 ms |
23948 KB |
Output is correct |
41 |
Correct |
110 ms |
23948 KB |
Output is correct |
42 |
Correct |
80 ms |
23948 KB |
Output is correct |
43 |
Correct |
87 ms |
23948 KB |
Output is correct |
44 |
Correct |
55 ms |
23948 KB |
Output is correct |
45 |
Correct |
209 ms |
23948 KB |
Output is correct |
46 |
Correct |
111 ms |
23948 KB |
Output is correct |
47 |
Correct |
86 ms |
23948 KB |
Output is correct |
48 |
Correct |
114 ms |
23948 KB |
Output is correct |
49 |
Correct |
78 ms |
23948 KB |
Output is correct |
50 |
Correct |
95 ms |
23948 KB |
Output is correct |
51 |
Correct |
80 ms |
23948 KB |
Output is correct |
52 |
Correct |
66 ms |
23948 KB |
Output is correct |
53 |
Correct |
213 ms |
23948 KB |
Output is correct |
54 |
Correct |
122 ms |
23948 KB |
Output is correct |