#include <bits/stdc++.h>
using namespace std;
/***
N equations and M unknowns - can only have a unique solution if M <= N
Since M >= N - 1 =>
* the graph is a tree, in wich case we can solve the problem
* the graph is a tree with a simple cycle (N edges). Solve the problem for leafs and solve from a cycle
Solve for a cycle:
* even length: multiple solution (for each node do +1 on one edge and -1 on the other)
* odd length: unique solution, do some math stuff and deduce first edge
N = the number of nodes that are left are the process
sum(1) = W(n, 1) + W(1, 2)
sum(2) = W(1, 2) + W(2, 3)
sum(1) = W(n, 1) + sum(2) - W(2, 3)
sum(1) = W(n, 1) + sum(2) - sum(3) + sum(4) - ... - sum(N) + W(n, 1)
2 * W(n, 1) = sum(1) - sum(2) + sum(3) - sum(4) + .... + sum(N)
***/
const int MAXN = 1000 * 100;
int n, m;
set<int> g[MAXN + 1];
pair<int, int> edges[MAXN + 1];
map<pair<int, int>, int> edgeId;
int weight[MAXN + 1], sum[MAXN + 1];
bool calculated[MAXN + 1], visited[MAXN + 1];
vector<int> cycle;
void dfs(int u, int dad = -1) {
visited[u] = true;
cycle.push_back(u);
for (const int &v : g[u]) {
if (v == dad || visited[v]) {
continue;
}
dfs(v, u);
}
}
int main() {
cin >> n >> m;
if (m > n) {
cout << "0\n";
return 0;
}
for (int i = 1; i <= n; i++) {
cin >> sum[i];
}
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
edges[i] = make_pair(u, v);
edgeId[make_pair(u, v)] = edgeId[make_pair(v, u)] = i;
g[u].insert(v);
g[v].insert(u);
}
queue<int> q;
for (int i = 1; i <= n; i++) {
if ((int)g[i].size() == 1) {
q.push(i);
}
}
while (!q.empty()) {
int u = q.front();
q.pop();
int v = *g[u].begin();
int id = edgeId[make_pair(u, v)];
calculated[id] = true;
weight[id] = 2 * sum[u];
sum[v] -= weight[id];
g[u].erase(v);
g[v].erase(u);
if ((int)g[v].size() == 1) {
q.push(v);
}
}
for (int i = 1; i <= n; i++) {
if ((int)g[i].size() > 1) {
dfs(i);
break;
}
}
if ((int)cycle.size() > 0 && (int)cycle.size() % 2 == 0) {
cout << "0\n";
return 0;
}
if ((int)cycle.size() > 0) {
int s = 0;
for (int i = 0; i < (int)cycle.size(); i++) {
if (i % 2 == 0) {
s += sum[cycle[i]];
} else {
s -= sum[cycle[i]];
}
}
int id = edgeId[make_pair(cycle[0], cycle.back())];
calculated[id] = true;
weight[id] = s / 2;
g[cycle[0]].erase(cycle.back());
g[cycle.back()].erase(cycle[0]);
sum[cycle[0]] -= weight[id];
sum[cycle.back()] -= weight[id];
q.push(cycle[0]);
q.push(cycle.back());
while (!q.empty()) {
int u = q.front();
q.pop();
int v = *g[u].begin();
int id = edgeId[make_pair(u, v)];
calculated[id] = true;
weight[id] = 2 * sum[u];
sum[v] -= weight[id];
g[u].erase(v);
g[v].erase(u);
if ((int)g[v].size() == 1) {
q.push(v);
}
}
}
for (int i = 0; i < m; i++) {
cout << weight[i] << " ";
}
cout << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Incorrect |
3 ms |
5024 KB |
Output isn't correct |
3 |
Incorrect |
4 ms |
5280 KB |
Output isn't correct |
4 |
Incorrect |
279 ms |
30692 KB |
Output isn't correct |
5 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
6 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
7 |
Incorrect |
3 ms |
5016 KB |
Output isn't correct |
8 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
9 |
Incorrect |
4 ms |
5276 KB |
Output isn't correct |
10 |
Incorrect |
4 ms |
5204 KB |
Output isn't correct |
11 |
Incorrect |
5 ms |
5280 KB |
Output isn't correct |
12 |
Incorrect |
5 ms |
5204 KB |
Output isn't correct |
13 |
Incorrect |
235 ms |
25428 KB |
Output isn't correct |
14 |
Incorrect |
278 ms |
29276 KB |
Output isn't correct |
15 |
Incorrect |
333 ms |
30748 KB |
Output isn't correct |
16 |
Incorrect |
247 ms |
27056 KB |
Output isn't correct |
17 |
Incorrect |
291 ms |
30704 KB |
Output isn't correct |
18 |
Incorrect |
308 ms |
30844 KB |
Output isn't correct |
19 |
Incorrect |
302 ms |
31200 KB |
Output isn't correct |
20 |
Incorrect |
3 ms |
5016 KB |
Output isn't correct |
21 |
Incorrect |
4 ms |
5152 KB |
Output isn't correct |
22 |
Incorrect |
298 ms |
30804 KB |
Output isn't correct |
23 |
Incorrect |
207 ms |
25356 KB |
Output isn't correct |
24 |
Incorrect |
331 ms |
30752 KB |
Output isn't correct |
25 |
Incorrect |
245 ms |
26448 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Incorrect |
4 ms |
5332 KB |
Output isn't correct |
3 |
Correct |
242 ms |
31884 KB |
Output is correct |
4 |
Correct |
3 ms |
4976 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
5028 KB |
Output is correct |
7 |
Incorrect |
3 ms |
5016 KB |
Output isn't correct |
8 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
9 |
Correct |
4 ms |
5012 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
15 |
Incorrect |
5 ms |
5284 KB |
Output isn't correct |
16 |
Incorrect |
4 ms |
5204 KB |
Output isn't correct |
17 |
Correct |
4 ms |
5204 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5020 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
5020 KB |
Output is correct |
22 |
Incorrect |
4 ms |
5204 KB |
Output isn't correct |
23 |
Incorrect |
302 ms |
29948 KB |
Output isn't correct |
24 |
Incorrect |
292 ms |
34624 KB |
Output isn't correct |
25 |
Correct |
251 ms |
31884 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
29 |
Correct |
3 ms |
4948 KB |
Output is correct |
30 |
Incorrect |
307 ms |
33916 KB |
Output isn't correct |
31 |
Incorrect |
373 ms |
37132 KB |
Output isn't correct |
32 |
Incorrect |
294 ms |
32032 KB |
Output isn't correct |
33 |
Correct |
282 ms |
34044 KB |
Output is correct |
34 |
Correct |
3 ms |
4948 KB |
Output is correct |
35 |
Correct |
3 ms |
4948 KB |
Output is correct |
36 |
Correct |
3 ms |
5028 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Incorrect |
298 ms |
34348 KB |
Output isn't correct |
39 |
Incorrect |
282 ms |
31444 KB |
Output isn't correct |
40 |
Incorrect |
332 ms |
34400 KB |
Output isn't correct |
41 |
Correct |
235 ms |
36176 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5028 KB |
Output is correct |
44 |
Correct |
3 ms |
4948 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Incorrect |
315 ms |
34164 KB |
Output isn't correct |
47 |
Incorrect |
288 ms |
34432 KB |
Output isn't correct |
48 |
Incorrect |
346 ms |
36852 KB |
Output isn't correct |
49 |
Correct |
243 ms |
29780 KB |
Output is correct |
50 |
Correct |
2 ms |
4948 KB |
Output is correct |
51 |
Correct |
3 ms |
4948 KB |
Output is correct |
52 |
Correct |
3 ms |
4948 KB |
Output is correct |
53 |
Correct |
3 ms |
4948 KB |
Output is correct |
54 |
Incorrect |
312 ms |
33472 KB |
Output isn't correct |