#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
#define sz(x) int(x.size())
const int mx = 100'000;
const int emx = 500'000;
vl c(1+mx);
vector<pii> edge[1+mx];
vi deg(1+mx, 0);
vi visit(1+emx, 0);
vector<ll> ans(1+emx);
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M;
cin >> N >> M;
for(int i = 1; i <= N; i++) cin >> c[i];
for(int j = 1; j <= M; j++)
{
int u, v;
cin >> u >> v;
edge[u].push_back({v, j});
edge[v].push_back({u, j});
deg[u]++;
deg[v]++;
}
if(M > N)
{
cout << "0\n";
return 0;
}
queue<int> tbv;
for(int i = 1; i <= N; i++)
{
if(deg[i] == 1)
{
tbv.push(i);
}
}
int endv = -1;
vi node_visit(1+N, 0);
while(!tbv.empty())
{
int u = tbv.front();
tbv.pop();
node_visit[u] = 1;
// cerr << "u = " << u << '\n';
int elim = 0;
for(auto ed: edge[u])
{
int v = ed.first;
if(visit[ed.second]) continue;
visit[ed.second] = 1;
deg[v]--;
elim = 1;
if(deg[v] <= 1)
{
tbv.push(v);
}
ans[ed.second] = 2*c[u];
// cerr << ed.first << ' ' << ed.second << " : " << 2*c[u] << '\n';
c[v] -= c[u];
c[u] -= c[u];
}
if(!elim)
{
if(M == N-1)
{
if(c[u] != 0)
{
cout << "0\n";
return 0;
}
}
}
}
// for(int u = 1; u <= N; u++)
// {
// cerr << "node = " << u << '\n';
// for(auto ed: edge[u]) cerr << ed.first << ' ' << ed.second << '\n';
// }
if(M == N)
{
// cerr << "done\n";
ll a[1+N], b[1+N];
int u = 1;
while(node_visit[u]) u++;
int src = u;
vi edgelist;
vi nodelist;
while(1)
{
int nv = 0;
for(auto ed: edge[u])
{
if(visit[ed.second]) continue;
visit[ed.second] = 1;
nv = 1;
nodelist.push_back(u);
edgelist.push_back(ed.second);
// cerr << u << " -> " << ed.first << ' ' << ed.second << '\n';
u = ed.first;
break;
}
if(!nv) break;
}
int Z = sz(edgelist);
a[0] = 1;
b[0] = 0;
if(Z % 2 == 0)
{
cout << "0\n";
return 0;
}
for(int z = 0; z+1 < Z; z++)
{
a[z+1] = -a[z];
b[z+1] = c[ nodelist[z+1] ] - b[z];
}
// for(int i = 0; i < Z; i++) cerr << nodelist[i] << ' ' << edgelist[i] << ' ' << a[i] << ' ' << b[i] << '\n';
ll x = 0;
//if(b[0] + b[z-1] != c[ nodelist[] ])
if((b[0] + b[Z-1]) % 2 != c[nodelist[0]] % 2)
{
cout << "0\n";
}
//x + b[0] + x + b[z-1] == c[nodelist[0]]
ll x2 = (c[nodelist[0]] - b[0] - b[Z-1]);
for(int y = 0; y < Z; y++)
ans[ edgelist[y] ] = (a[y] * x2 + 2*b[y]);
}
for(int e = 1; e <= M; e++)
{
cout << ans[e] << '\n';
}
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:127:7: warning: unused variable 'src' [-Wunused-variable]
127 | int src = u;
| ^~~
pipes.cpp:172:6: warning: unused variable 'x' [-Wunused-variable]
172 | ll x = 0;
| ^
pipes.cpp:60:6: warning: unused variable 'endv' [-Wunused-variable]
60 | int endv = -1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
58 ms |
14476 KB |
Output is correct |
5 |
Correct |
5 ms |
9664 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9640 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
45 ms |
13440 KB |
Output is correct |
14 |
Correct |
54 ms |
14196 KB |
Output is correct |
15 |
Correct |
63 ms |
14476 KB |
Output is correct |
16 |
Correct |
63 ms |
13748 KB |
Output is correct |
17 |
Correct |
67 ms |
14488 KB |
Output is correct |
18 |
Correct |
85 ms |
14448 KB |
Output is correct |
19 |
Correct |
65 ms |
13736 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9768 KB |
Output is correct |
22 |
Correct |
61 ms |
14436 KB |
Output is correct |
23 |
Correct |
57 ms |
13416 KB |
Output is correct |
24 |
Correct |
77 ms |
14500 KB |
Output is correct |
25 |
Correct |
69 ms |
13664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
57 ms |
15544 KB |
Output is correct |
4 |
Correct |
50 ms |
14412 KB |
Output is correct |
5 |
Correct |
44 ms |
14652 KB |
Output is correct |
6 |
Correct |
186 ms |
29704 KB |
Output is correct |
7 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
7 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
6 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
6 ms |
9808 KB |
Output is correct |
22 |
Incorrect |
7 ms |
9792 KB |
Output isn't correct |
23 |
Correct |
47 ms |
14632 KB |
Output is correct |
24 |
Incorrect |
58 ms |
15720 KB |
Output isn't correct |
25 |
Correct |
72 ms |
15464 KB |
Output is correct |
26 |
Correct |
50 ms |
14488 KB |
Output is correct |
27 |
Correct |
47 ms |
14384 KB |
Output is correct |
28 |
Correct |
46 ms |
14932 KB |
Output is correct |
29 |
Correct |
151 ms |
25844 KB |
Output is correct |
30 |
Correct |
68 ms |
15284 KB |
Output is correct |
31 |
Correct |
59 ms |
15804 KB |
Output is correct |
32 |
Incorrect |
57 ms |
15980 KB |
Output isn't correct |
33 |
Correct |
58 ms |
15680 KB |
Output is correct |
34 |
Correct |
40 ms |
14412 KB |
Output is correct |
35 |
Correct |
40 ms |
14400 KB |
Output is correct |
36 |
Correct |
43 ms |
14696 KB |
Output is correct |
37 |
Correct |
192 ms |
29628 KB |
Output is correct |
38 |
Correct |
71 ms |
15576 KB |
Output is correct |
39 |
Incorrect |
66 ms |
15964 KB |
Output isn't correct |
40 |
Correct |
59 ms |
15840 KB |
Output is correct |
41 |
Correct |
48 ms |
15740 KB |
Output is correct |
42 |
Correct |
38 ms |
14276 KB |
Output is correct |
43 |
Correct |
44 ms |
14392 KB |
Output is correct |
44 |
Correct |
48 ms |
14648 KB |
Output is correct |
45 |
Correct |
141 ms |
27284 KB |
Output is correct |
46 |
Correct |
75 ms |
15432 KB |
Output is correct |
47 |
Correct |
59 ms |
15768 KB |
Output is correct |
48 |
Correct |
58 ms |
15736 KB |
Output is correct |
49 |
Correct |
48 ms |
15308 KB |
Output is correct |
50 |
Correct |
41 ms |
14548 KB |
Output is correct |
51 |
Correct |
45 ms |
14632 KB |
Output is correct |
52 |
Correct |
45 ms |
14320 KB |
Output is correct |
53 |
Correct |
151 ms |
27168 KB |
Output is correct |
54 |
Incorrect |
58 ms |
15476 KB |
Output isn't correct |