# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
677850 |
2023-01-04T12:44:54 Z |
dooompy |
Pipes (BOI13_pipes) |
C++17 |
|
128 ms |
18212 KB |
#include "bits/stdc++.h"
using namespace std;
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
bool is = false;
for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
return o << '}';
}
#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif
using ll = long long;
ll v[500005];
set<int> adj[100005];
ll calc[500005];
int en[500005], out[500005];
vector<pair<int, int>> ord;
void dfs(int cur, int p, int root) {
int idx = *adj[cur].begin();
int nxtidx = (en[idx] == cur ? out[idx] : en[idx]);
if (nxtidx == p) {
idx = *adj[cur].rbegin();
nxtidx = (en[idx] == cur ? out[idx] : en[idx]);
}
ord.push_back({cur, idx});
if (nxtidx != root) dfs(nxtidx, cur, root);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("test.txt", "r", stdin);
// freopen("", "w", stdout);
int n, m; cin >> n >> m;
if (m > n) {
cout << 0 << endl;
return 0;
}
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i <= m; i++) {
int a, b; cin >> a >> b;
en[i] = a, out[i] = b;
adj[a].insert(i);
adj[b].insert(i);
}
queue<int> q;
for (int i = 1; i <= n; i++) {
q.push(i);
}
while (!q.empty()) {
// deg 1
auto cur = q.front(); q.pop();
if (adj[cur].size() == 1) {
int idx = *adj[cur].begin();
int nxtidx = (en[idx] == cur ? out[idx] : en[idx]);
ll weight = v[cur];
v[nxtidx] -= v[cur];
v[cur] -= v[cur];
calc[idx] = 2LL * weight;
adj[cur].erase(idx);
adj[nxtidx].erase(idx);
if (adj[nxtidx].size() == 1) q.push(nxtidx);
}
}
int ct = 0;
for (int i = 1; i <= n; i++) {
if (adj[i].size() > 2) {
cout << 0;
return 0;
}
ct += (adj[i].size() == 2);
}
if (ct % 2 == 0 && ct > 0) {
cout << 0;
return 0;
}
if (m == n - 1) {
for (int i = 1; i <= m; i++) {
cout << calc[i] * 2 << "\n";
}
return 0;
}
int left = 1;
for (; left <= n; left++) {
if (adj[left].size() == 2) break;
}
dfs(left, -1, left);
// assert(ord.size() == left.size());
// for (auto x : ord) cout << x << " ";
ll prev = 0;
for (int i = 1; i < ord.size() ; i++) {
// assum back ->0 is X
prev = 2LL * v[ord[i].first] - prev;
}
ll x = (2LL * v[ord[0].first] - prev) / 2;
// cout << x << endl;
calc[ord[0].second] = x;
for (int i = 1; i < ord.size(); i++) {
calc[ord[i].second] = 2LL * v[ord[i].first] - calc[ord[i-1].second];
}
for (int i = 1; i <= m; i++) {
cout << calc[i] << "\n";
}
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:141:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | for (int i = 1; i < ord.size() ; i++) {
| ~~^~~~~~~~~~~~
pipes.cpp:154:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
154 | for (int i = 1; i < ord.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
5060 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
4 |
Incorrect |
113 ms |
17704 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
6 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
7 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
8 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
9 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
10 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
11 |
Incorrect |
5 ms |
5076 KB |
Output isn't correct |
12 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
13 |
Incorrect |
77 ms |
15032 KB |
Output isn't correct |
14 |
Incorrect |
95 ms |
16916 KB |
Output isn't correct |
15 |
Incorrect |
93 ms |
17620 KB |
Output isn't correct |
16 |
Incorrect |
78 ms |
15692 KB |
Output isn't correct |
17 |
Incorrect |
100 ms |
17612 KB |
Output isn't correct |
18 |
Incorrect |
102 ms |
17632 KB |
Output isn't correct |
19 |
Incorrect |
88 ms |
17664 KB |
Output isn't correct |
20 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
21 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
22 |
Incorrect |
92 ms |
17676 KB |
Output isn't correct |
23 |
Incorrect |
91 ms |
15044 KB |
Output isn't correct |
24 |
Incorrect |
106 ms |
17676 KB |
Output isn't correct |
25 |
Incorrect |
76 ms |
15532 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
75 ms |
16360 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 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 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5188 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
76 ms |
15608 KB |
Output is correct |
24 |
Correct |
97 ms |
18056 KB |
Output is correct |
25 |
Correct |
67 ms |
16468 KB |
Output is correct |
26 |
Correct |
2 ms |
4948 KB |
Output is correct |
27 |
Correct |
2 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
29 |
Correct |
2 ms |
4948 KB |
Output is correct |
30 |
Correct |
82 ms |
17736 KB |
Output is correct |
31 |
Correct |
94 ms |
18212 KB |
Output is correct |
32 |
Correct |
105 ms |
17648 KB |
Output is correct |
33 |
Correct |
73 ms |
17140 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 |
5012 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Correct |
89 ms |
18004 KB |
Output is correct |
39 |
Correct |
107 ms |
17536 KB |
Output is correct |
40 |
Correct |
128 ms |
17992 KB |
Output is correct |
41 |
Correct |
69 ms |
17156 KB |
Output is correct |
42 |
Correct |
2 ms |
4948 KB |
Output is correct |
43 |
Correct |
2 ms |
4948 KB |
Output is correct |
44 |
Correct |
3 ms |
4948 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Correct |
83 ms |
17904 KB |
Output is correct |
47 |
Correct |
108 ms |
18008 KB |
Output is correct |
48 |
Correct |
96 ms |
18208 KB |
Output is correct |
49 |
Correct |
83 ms |
16380 KB |
Output is correct |
50 |
Correct |
3 ms |
4948 KB |
Output is correct |
51 |
Correct |
3 ms |
4948 KB |
Output is correct |
52 |
Correct |
2 ms |
4948 KB |
Output is correct |
53 |
Correct |
2 ms |
4948 KB |
Output is correct |
54 |
Correct |
87 ms |
17672 KB |
Output is correct |