#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 100005
#define MAXM 500005
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
#define f first
#define s second
ll n, m, t[MAXN], en[MAXN], r[MAXM];
pair<ll, ll> e[MAXM];
vector<pair<ll, ll>> adj[MAXN];
bool v[MAXN];
int main(){
cin >> n >> m;
FOR(i, 1, n) {
cin >> t[i];
en[i] = 0, v[i] = false;
}
FOR(i, 1, m){
cin >> e[i].f >> e[i].s;
adj[e[i].f].push_back({e[i].s, i}), en[e[i].f]++;
adj[e[i].s].push_back({e[i].f, i}), en[e[i].s]++;
r[i] = 1000000001;
}
if(!(m == n - 1 || m == n)) return cout << 0 << endl, 0;
queue<ll> q;
FOR(i, 1, n) if(en[i] == 1) q.push(i);
ll d = n;
while(q.size()){
ll qi = q.front(); q.pop();
if(v[qi]) continue;
v[qi] = true, d--;
if(en[qi] == 0) continue;
for(pair<ll, ll> p : adj[qi]) if(r[p.s] == 1000000001) {
r[p.s] = 2 * t[qi];
t[p.f] -= t[qi], en[p.f]--;
if(!v[p.f] && en[p.f] == 1) q.push(p.f);
}
}
if(d != 0 && d % 2 == 0) return cout << 0 << endl, 0;
ll prvi = -1, stt, ci = -1, fe = 0;
FOR(i, 1, n) if(!v[i]) ci = i;
if(ci == -1) {
FOR(i, 1, m) cout << r[i] << endl;
return 0;
}
stt = ci;
stack<ll> st;
ll alts = 0;
do{
for(pair<ll, ll> p : adj[ci]) if(r[p.s] == 1000000001 && !(e[p.s].f == prvi || e[p.s].s == prvi)) {
prvi = ci, ci = p.f;
if(stt != ci) alts = t[p.f] - alts;
if(fe == 0) fe = p.s;
break;
}
st.push(ci);
}while(stt != ci);
prvi = r[fe] = t[stt] - alts;
if(prvi % 2) return cout << 0 << endl, 0;
ll pi = st.top(); st.pop();
while(!st.empty()){
ll sti = st.top(); st.pop();
for(pair<ll, ll> p : adj[sti]) if(e[p.s].f == pi || e[p.s].s == pi) {
prvi = r[p.s] = 2 * (t[pi] - prvi / 2);
if(prvi % 2) return cout << 0 << endl, 0;
}
pi = sti;
}
FOR(i, 1, m) cout << r[i] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
3 ms |
8784 KB |
Output is correct |
4 |
Correct |
206 ms |
17996 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8528 KB |
Output is correct |
7 |
Correct |
2 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
3 ms |
8784 KB |
Output is correct |
10 |
Correct |
3 ms |
8804 KB |
Output is correct |
11 |
Correct |
4 ms |
8784 KB |
Output is correct |
12 |
Correct |
3 ms |
8784 KB |
Output is correct |
13 |
Correct |
161 ms |
16788 KB |
Output is correct |
14 |
Correct |
186 ms |
17496 KB |
Output is correct |
15 |
Correct |
208 ms |
17924 KB |
Output is correct |
16 |
Correct |
184 ms |
17736 KB |
Output is correct |
17 |
Correct |
200 ms |
18612 KB |
Output is correct |
18 |
Correct |
199 ms |
18000 KB |
Output is correct |
19 |
Correct |
201 ms |
17812 KB |
Output is correct |
20 |
Correct |
2 ms |
8528 KB |
Output is correct |
21 |
Correct |
3 ms |
8804 KB |
Output is correct |
22 |
Correct |
203 ms |
18008 KB |
Output is correct |
23 |
Correct |
159 ms |
16344 KB |
Output is correct |
24 |
Correct |
198 ms |
17996 KB |
Output is correct |
25 |
Correct |
180 ms |
16968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
4 ms |
8784 KB |
Output is correct |
3 |
Correct |
60 ms |
17224 KB |
Output is correct |
4 |
Correct |
67 ms |
17224 KB |
Output is correct |
5 |
Correct |
60 ms |
17932 KB |
Output is correct |
6 |
Correct |
253 ms |
41984 KB |
Output is correct |
7 |
Correct |
2 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
1 ms |
8528 KB |
Output is correct |
10 |
Correct |
1 ms |
8528 KB |
Output is correct |
11 |
Correct |
2 ms |
8528 KB |
Output is correct |
12 |
Correct |
2 ms |
8528 KB |
Output is correct |
13 |
Correct |
2 ms |
8528 KB |
Output is correct |
14 |
Correct |
2 ms |
8528 KB |
Output is correct |
15 |
Correct |
3 ms |
8732 KB |
Output is correct |
16 |
Correct |
4 ms |
8784 KB |
Output is correct |
17 |
Correct |
3 ms |
8784 KB |
Output is correct |
18 |
Correct |
2 ms |
8784 KB |
Output is correct |
19 |
Correct |
2 ms |
8784 KB |
Output is correct |
20 |
Correct |
2 ms |
8740 KB |
Output is correct |
21 |
Correct |
3 ms |
8784 KB |
Output is correct |
22 |
Correct |
4 ms |
8720 KB |
Output is correct |
23 |
Correct |
169 ms |
16712 KB |
Output is correct |
24 |
Correct |
210 ms |
17992 KB |
Output is correct |
25 |
Correct |
59 ms |
16972 KB |
Output is correct |
26 |
Correct |
77 ms |
17268 KB |
Output is correct |
27 |
Correct |
58 ms |
17060 KB |
Output is correct |
28 |
Correct |
67 ms |
18516 KB |
Output is correct |
29 |
Correct |
189 ms |
37924 KB |
Output is correct |
30 |
Correct |
200 ms |
17736 KB |
Output is correct |
31 |
Correct |
209 ms |
19528 KB |
Output is correct |
32 |
Correct |
199 ms |
17736 KB |
Output is correct |
33 |
Correct |
72 ms |
17488 KB |
Output is correct |
34 |
Correct |
63 ms |
18644 KB |
Output is correct |
35 |
Correct |
64 ms |
18760 KB |
Output is correct |
36 |
Correct |
64 ms |
17224 KB |
Output is correct |
37 |
Correct |
247 ms |
42088 KB |
Output is correct |
38 |
Correct |
227 ms |
17992 KB |
Output is correct |
39 |
Correct |
212 ms |
17736 KB |
Output is correct |
40 |
Correct |
206 ms |
17992 KB |
Output is correct |
41 |
Correct |
59 ms |
18760 KB |
Output is correct |
42 |
Correct |
64 ms |
17024 KB |
Output is correct |
43 |
Correct |
62 ms |
18248 KB |
Output is correct |
44 |
Correct |
67 ms |
16800 KB |
Output is correct |
45 |
Correct |
192 ms |
38940 KB |
Output is correct |
46 |
Correct |
211 ms |
17736 KB |
Output is correct |
47 |
Correct |
206 ms |
19020 KB |
Output is correct |
48 |
Correct |
209 ms |
19928 KB |
Output is correct |
49 |
Correct |
59 ms |
16968 KB |
Output is correct |
50 |
Correct |
61 ms |
18260 KB |
Output is correct |
51 |
Correct |
60 ms |
17052 KB |
Output is correct |
52 |
Correct |
58 ms |
16968 KB |
Output is correct |
53 |
Correct |
211 ms |
38404 KB |
Output is correct |
54 |
Correct |
212 ms |
17736 KB |
Output is correct |