#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using cd = complex <double>;
const long long INF = 1e15;
const int N = 3e5 + 2;
const int mod = 1e9 + 7;//998244353;//1e9 + 7;//786433;
const double Pi = acos(-1);
void Fastio()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n, m;
long long a[100005], par[100005], H[100005], ans[100005];
bool Vis[100005];
long long c[2];
vector <pair <int, int> > Edge;
vector <pair <int, int> > Adj[100005];
void DFS(int node, int p = -1)
{
for(auto x : Adj[node])
{
if(x.fi == p)
{
continue;
}
if(H[x.fi] != -1)
{
if((H[node] - H[x.fi]) % 2 == 1)
{
cout << 0;
exit(0);
}
}
else
{
H[x.fi] = H[node] + 1;
DFS(x.fi, node);
}
}
}
void DFS2(int node, int p = -1)
{
Vis[node] = true;
for(auto x : Adj[node])
{
if(Vis[x.fi])
{
continue;
}
DFS2(x.fi, node);
ans[x.se] = a[x.fi];
a[node] -= a[x.fi];
}
}
signed main()
{
Fastio();
cin >> n >> m;
if(n < m)
{
cout << 0;
return 0;
}
for(int i = 1; i <= n; i++)
{
H[i] = -1;
cin >> a[i];
}
for(int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
Adj[u].eb(v, i - 1);
Adj[v].eb(u, i - 1);
Edge.eb(u, v);
}
H[1] = 0;
DFS(1);
for(int i = 1; i <= n; i++)
{
c[H[i] & 1] += a[i];
}
for(int i = 0; i < m; i++)
{
pair <int, int> x = Edge[i];
if(H[x.fi] % 2 == H[x.se] % 2)
{
if(H[x.fi] % 2 == 1)
{
ans[i] = (c[1] - c[0]) / 2;
a[x.fi] -= ans[i];
a[x.se] -= ans[i];
}
else
{
ans[i] = (c[0] - c[1]) / 2;
a[x.fi] -= ans[i];
a[x.se] -= ans[i];
}
}
}
DFS2(1);
for(int i = 0; i < m; i++)
{
cout << ans[i] * 2 << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2816 KB |
Output is correct |
4 |
Correct |
87 ms |
12260 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2816 KB |
Output is correct |
10 |
Correct |
3 ms |
2816 KB |
Output is correct |
11 |
Correct |
3 ms |
2816 KB |
Output is correct |
12 |
Correct |
3 ms |
2816 KB |
Output is correct |
13 |
Correct |
63 ms |
10340 KB |
Output is correct |
14 |
Correct |
82 ms |
11748 KB |
Output is correct |
15 |
Correct |
99 ms |
12264 KB |
Output is correct |
16 |
Correct |
76 ms |
10852 KB |
Output is correct |
17 |
Correct |
89 ms |
12260 KB |
Output is correct |
18 |
Correct |
88 ms |
12260 KB |
Output is correct |
19 |
Correct |
103 ms |
14628 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
3 ms |
2816 KB |
Output is correct |
22 |
Correct |
88 ms |
12260 KB |
Output is correct |
23 |
Correct |
67 ms |
10340 KB |
Output is correct |
24 |
Correct |
92 ms |
12396 KB |
Output is correct |
25 |
Correct |
71 ms |
10728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2816 KB |
Output is correct |
3 |
Correct |
64 ms |
12644 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2720 KB |
Output is correct |
13 |
Correct |
2 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2816 KB |
Output is correct |
16 |
Correct |
3 ms |
2816 KB |
Output is correct |
17 |
Correct |
2 ms |
2816 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 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 |
2688 KB |
Output is correct |
22 |
Correct |
3 ms |
2816 KB |
Output is correct |
23 |
Correct |
78 ms |
13028 KB |
Output is correct |
24 |
Correct |
101 ms |
14820 KB |
Output is correct |
25 |
Correct |
64 ms |
12520 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2688 KB |
Output is correct |
28 |
Correct |
2 ms |
2688 KB |
Output is correct |
29 |
Correct |
2 ms |
2688 KB |
Output is correct |
30 |
Correct |
102 ms |
16228 KB |
Output is correct |
31 |
Correct |
105 ms |
16356 KB |
Output is correct |
32 |
Correct |
105 ms |
13028 KB |
Output is correct |
33 |
Correct |
66 ms |
13540 KB |
Output is correct |
34 |
Correct |
2 ms |
2688 KB |
Output is correct |
35 |
Correct |
2 ms |
2688 KB |
Output is correct |
36 |
Correct |
2 ms |
2688 KB |
Output is correct |
37 |
Correct |
2 ms |
2688 KB |
Output is correct |
38 |
Correct |
103 ms |
15844 KB |
Output is correct |
39 |
Correct |
93 ms |
12644 KB |
Output is correct |
40 |
Correct |
100 ms |
14564 KB |
Output is correct |
41 |
Correct |
79 ms |
15076 KB |
Output is correct |
42 |
Correct |
2 ms |
2688 KB |
Output is correct |
43 |
Correct |
2 ms |
2688 KB |
Output is correct |
44 |
Correct |
2 ms |
2688 KB |
Output is correct |
45 |
Correct |
2 ms |
2688 KB |
Output is correct |
46 |
Correct |
105 ms |
16872 KB |
Output is correct |
47 |
Correct |
97 ms |
14692 KB |
Output is correct |
48 |
Correct |
102 ms |
16228 KB |
Output is correct |
49 |
Correct |
60 ms |
11236 KB |
Output is correct |
50 |
Correct |
2 ms |
2816 KB |
Output is correct |
51 |
Correct |
2 ms |
2688 KB |
Output is correct |
52 |
Correct |
2 ms |
2688 KB |
Output is correct |
53 |
Correct |
2 ms |
2688 KB |
Output is correct |
54 |
Correct |
100 ms |
15332 KB |
Output is correct |