#include <bits/stdc++.h>
#define int int64_t
using namespace std;
const int maxn = 1e5 + 10;
bool mark[maxn];
int ans[5*maxn], s[maxn], grau[maxn];
map<pair<int, int>, int> mapa;
int n;
vector<int> grafo[maxn];
bool marca[maxn];
int32_t main() {
int m;
cin >> n >> m;
if(m > n) {
cout << "0\n";
return 0;
}
int total = 0;
for(int i=1;i<=n;i++)
cin >> s[i], s[i] *= 2, total += s[i];
for(int i=1;i<=m;i++) {
int a, b;
cin >> a >> b;
grafo[a].push_back(b);
grafo[b].push_back(a);
mapa[{a, b}] = i;
mapa[{b, a}] = i;
grau[a]++;
grau[b]++;
}
queue<int> fila;
for(int i=1;i<=n;i++) {
if(grau[i] == 1)
fila.push(i);
}
while(!fila.empty()) {
int u = fila.front();
fila.pop();
if(mark[u]) continue;
mark[u] = true;
// cout << u << ": " << s[u] << "\n";
for(auto v : grafo[u]) {
if(mark[v]) continue;
grau[v]--;
ans[mapa[{u, v}]] = s[u], s[v] -= s[u];
if(grau[v] == 1) fila.push(v);
}
}
vector<int> ciclo;
for(int i=1;i<=n;i++)
if(!mark[i]) ciclo.push_back(i);
if(ciclo.size() == 0) {
for(int i=1;i<=m;i++)
cout << ans[i] << "\n";
return 0;
}
// cout << "SHITE\n"
if(ciclo.size() % 2 == 0){
cout << "0\n";
return 0;
}
vector<int> ordem;
int k = 0;
ordem.push_back(ciclo[0]);
while(k < ordem.size()) {
int u = ordem[k++];
if(marca[u]) continue;
marca[u] = true;
for(auto v : grafo[u]) {
if(mark[v] || marca[v]) continue;
ordem.push_back(v);
break;
}
}
int sums = 0;
int val = 1;
for(auto u : ordem) {
sums += (s[u] * val);
val *= -1;
}
sums >>= 1;
ans[mapa[{ordem.back(), ordem.front()}]] = sums;
int l = sums;
for(int i = 0; i < (int)ordem.size() - 1;i++) {
ans[mapa[{ordem[i], ordem[i+1]}]] = s[ordem[i]] - l;
l = s[ordem[i]] - l;
}
for(int i=1;i<=m;i++)
cout << ans[i] << "\n";
return 0;
}
Compilation message
pipes.cpp: In function 'int32_t main()':
pipes.cpp:80:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(k < ordem.size()) {
~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
6 ms |
2936 KB |
Output is correct |
4 |
Correct |
455 ms |
22696 KB |
Output is correct |
5 |
Correct |
5 ms |
2684 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
5 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
6 ms |
2808 KB |
Output is correct |
10 |
Correct |
6 ms |
2936 KB |
Output is correct |
11 |
Correct |
6 ms |
2936 KB |
Output is correct |
12 |
Correct |
6 ms |
2808 KB |
Output is correct |
13 |
Correct |
295 ms |
18672 KB |
Output is correct |
14 |
Correct |
359 ms |
21600 KB |
Output is correct |
15 |
Correct |
391 ms |
22596 KB |
Output is correct |
16 |
Correct |
330 ms |
19780 KB |
Output is correct |
17 |
Correct |
398 ms |
22472 KB |
Output is correct |
18 |
Correct |
382 ms |
22540 KB |
Output is correct |
19 |
Correct |
396 ms |
22012 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
6 ms |
2936 KB |
Output is correct |
22 |
Correct |
397 ms |
22632 KB |
Output is correct |
23 |
Correct |
303 ms |
18728 KB |
Output is correct |
24 |
Correct |
387 ms |
22580 KB |
Output is correct |
25 |
Correct |
325 ms |
19500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2684 KB |
Output is correct |
2 |
Correct |
6 ms |
2936 KB |
Output is correct |
3 |
Correct |
332 ms |
22368 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2756 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
6 ms |
2936 KB |
Output is correct |
16 |
Correct |
6 ms |
2940 KB |
Output is correct |
17 |
Correct |
6 ms |
2936 KB |
Output is correct |
18 |
Correct |
4 ms |
2680 KB |
Output is correct |
19 |
Correct |
4 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
6 ms |
2936 KB |
Output is correct |
23 |
Correct |
311 ms |
20400 KB |
Output is correct |
24 |
Correct |
395 ms |
24240 KB |
Output is correct |
25 |
Correct |
320 ms |
22384 KB |
Output is correct |
26 |
Correct |
4 ms |
2680 KB |
Output is correct |
27 |
Correct |
4 ms |
2680 KB |
Output is correct |
28 |
Correct |
4 ms |
2680 KB |
Output is correct |
29 |
Correct |
4 ms |
2808 KB |
Output is correct |
30 |
Correct |
388 ms |
23452 KB |
Output is correct |
31 |
Correct |
405 ms |
24820 KB |
Output is correct |
32 |
Correct |
401 ms |
24028 KB |
Output is correct |
33 |
Correct |
337 ms |
23284 KB |
Output is correct |
34 |
Correct |
4 ms |
2744 KB |
Output is correct |
35 |
Correct |
4 ms |
2680 KB |
Output is correct |
36 |
Correct |
4 ms |
2680 KB |
Output is correct |
37 |
Correct |
4 ms |
2680 KB |
Output is correct |
38 |
Correct |
386 ms |
23972 KB |
Output is correct |
39 |
Correct |
359 ms |
23792 KB |
Output is correct |
40 |
Correct |
395 ms |
24560 KB |
Output is correct |
41 |
Correct |
311 ms |
23672 KB |
Output is correct |
42 |
Correct |
4 ms |
2680 KB |
Output is correct |
43 |
Correct |
4 ms |
2680 KB |
Output is correct |
44 |
Correct |
4 ms |
2808 KB |
Output is correct |
45 |
Correct |
4 ms |
2680 KB |
Output is correct |
46 |
Correct |
402 ms |
23788 KB |
Output is correct |
47 |
Correct |
395 ms |
24280 KB |
Output is correct |
48 |
Correct |
409 ms |
24832 KB |
Output is correct |
49 |
Correct |
342 ms |
22300 KB |
Output is correct |
50 |
Correct |
4 ms |
2680 KB |
Output is correct |
51 |
Correct |
4 ms |
2680 KB |
Output is correct |
52 |
Correct |
4 ms |
2680 KB |
Output is correct |
53 |
Correct |
4 ms |
2680 KB |
Output is correct |
54 |
Correct |
410 ms |
23776 KB |
Output is correct |