/*
ID: varunra2
LANG: C++
TASK: pipes
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
int n, m;
vector<VII> adj;
VI vals;
VI ret;
VI deg;
MPII in;
void init() {
adj.resize(n);
vals.resize(n);
ret.resize(m);
deg.resize(n);
}
void getDeg() {
for (int i = 0; i < n; i++) {
deg[i] = sz(adj[i]);
}
}
void pr() {
for (int i = 0; i < m; i++) {
cout << ret[i] * 2 << '\n';
}
}
void dfs(int u) {
// debug(u);
deg[u] = 0;
for (auto& x : adj[u]) {
if (deg[x.x] > 0) {
vals[x.x] -= vals[u];
deg[x.x]--;
ret[x.y] = vals[u];
vals[u] = 0;
if (deg[x.x] <= 1) dfs(x.x);
}
}
}
void bad() {
cout << "0\n";
exit(0);
}
void solvetree() {
getDeg();
for (int i = 0; i < n; i++) {
// debug(deg[i], "here", i);
if (deg[i] == 1) {
dfs(i);
}
}
// debug(vals);
// debug(deg);
for (int i = 0; i < n; i++) {
if (vals[i] != 0) {
bad();
}
}
pr();
}
void dfscyc(int u, VI& cmp) {
deg[u] = 0;
cmp.PB(u);
in[u] = 1;
for (auto& x : adj[u]) {
if (deg[x.x] > 0) {
dfscyc(x.x, cmp);
break;
}
}
}
int get(int u, int v) {
trav(x, adj[u]) {
if (x.x == v) return x.y;
}
return -INF;
}
void solvecyc() {
getDeg();
for (int i = 0; i < n; i++) {
if (deg[i] == 1) {
dfs(i);
}
}
VI cmp;
for (int i = 0; i < n; i++) {
if (deg[i] > 0) {
dfscyc(i, cmp);
break;
}
}
if (sz(cmp) % 2 == 0) bad();
int sm = 0;
for (int i = 0; i < sz(cmp); i++) {
if (i % 2)
sm -= vals[cmp[i]];
else
sm += vals[cmp[i]];
}
if (sm % 2) bad();
sm /= 2;
ret[get(cmp.back(), cmp[0])] = sm;
for (int i = 0; i < sz(cmp) - 1; i++) {
int ind = get(cmp[i], cmp[i + 1]);
ret[ind] = vals[cmp[i]] - sm;
sm = ret[ind];
}
pr();
}
void solvemultcyc() { bad(); }
int main() {
// #ifndef ONLINE_JUDGE
// freopen("pipes.in", "r", stdin);
// freopen("pipes.out", "w", stdout);
// #endif
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
init();
for (int i = 0; i < n; i++) {
cin >> vals[i];
// vals[i] *= -1;
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].PB(MP(v, i));
adj[v].PB(MP(u, i));
}
if (m == n - 1) {
solvetree();
} else if (m == n) {
solvecyc();
} else {
solvemultcyc();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
72 ms |
9592 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
53 ms |
7672 KB |
Output is correct |
14 |
Correct |
67 ms |
9080 KB |
Output is correct |
15 |
Correct |
74 ms |
9592 KB |
Output is correct |
16 |
Correct |
59 ms |
8288 KB |
Output is correct |
17 |
Correct |
73 ms |
9720 KB |
Output is correct |
18 |
Correct |
79 ms |
9592 KB |
Output is correct |
19 |
Correct |
80 ms |
10232 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
71 ms |
9592 KB |
Output is correct |
23 |
Correct |
54 ms |
7672 KB |
Output is correct |
24 |
Correct |
73 ms |
9592 KB |
Output is correct |
25 |
Correct |
57 ms |
8056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
88 ms |
13432 KB |
Output is correct |
4 |
Correct |
51 ms |
8696 KB |
Output is correct |
5 |
Correct |
47 ms |
8824 KB |
Output is correct |
6 |
Correct |
241 ms |
25464 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
92 ms |
13428 KB |
Output is correct |
24 |
Correct |
110 ms |
15220 KB |
Output is correct |
25 |
Correct |
89 ms |
13556 KB |
Output is correct |
26 |
Correct |
61 ms |
8696 KB |
Output is correct |
27 |
Correct |
54 ms |
8568 KB |
Output is correct |
28 |
Correct |
58 ms |
9208 KB |
Output is correct |
29 |
Correct |
194 ms |
21496 KB |
Output is correct |
30 |
Correct |
104 ms |
13940 KB |
Output is correct |
31 |
Correct |
139 ms |
19056 KB |
Output is correct |
32 |
Correct |
86 ms |
11512 KB |
Output is correct |
33 |
Correct |
92 ms |
15220 KB |
Output is correct |
34 |
Correct |
50 ms |
8696 KB |
Output is correct |
35 |
Correct |
55 ms |
8696 KB |
Output is correct |
36 |
Correct |
52 ms |
8952 KB |
Output is correct |
37 |
Correct |
226 ms |
25520 KB |
Output is correct |
38 |
Correct |
110 ms |
14452 KB |
Output is correct |
39 |
Correct |
82 ms |
10744 KB |
Output is correct |
40 |
Correct |
107 ms |
15004 KB |
Output is correct |
41 |
Correct |
116 ms |
18592 KB |
Output is correct |
42 |
Correct |
52 ms |
8568 KB |
Output is correct |
43 |
Correct |
50 ms |
8568 KB |
Output is correct |
44 |
Correct |
51 ms |
8824 KB |
Output is correct |
45 |
Correct |
170 ms |
21208 KB |
Output is correct |
46 |
Correct |
102 ms |
13684 KB |
Output is correct |
47 |
Correct |
106 ms |
15068 KB |
Output is correct |
48 |
Correct |
137 ms |
18672 KB |
Output is correct |
49 |
Correct |
66 ms |
10232 KB |
Output is correct |
50 |
Correct |
50 ms |
8824 KB |
Output is correct |
51 |
Correct |
57 ms |
8952 KB |
Output is correct |
52 |
Correct |
53 ms |
8568 KB |
Output is correct |
53 |
Correct |
187 ms |
21768 KB |
Output is correct |
54 |
Correct |
95 ms |
13300 KB |
Output is correct |