# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
251617 |
2020-07-22T03:36:49 Z |
abacaba |
Pipes (BOI13_pipes) |
C++14 |
|
302 ms |
55148 KB |
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define emb emplace_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline T range(T l, T r) {
return uniform_int_distribution<T>(l, r)(rng);
}
inline void setin(string s) {
freopen(s.c_str(), "r", stdin);
}
inline void setout(string s) {
freopen(s.c_str(), "w", stdout);
}
template <typename T> void Min(T &a, T b) {
a = min(a, b);
}
template <typename T> void Max(T &a, T b) {
a = max(a, b);
}
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 5e5 + 15;
int n, m, a[N];
vector <int> g[N];
bool used[N];
set <pii> cur;
int deg[N], d[N];
int p[N], sz[N];
pii e[N];
int pp[N];
int ans[N];
set <int> cycles;
vector <int> inc;
int pref[2][N];
vector <int> cycle[N];
int find(int v) {
if(v == p[v])
return v;
return p[v] = find(p[v]);
}
void unio(int a, int b) {
a = find(a);
b = find(b);
if(a != b) {
if(sz[a] < sz[b])
swap(a, b);
p[b] = a;
sz[a] += sz[b];
}
}
inline int gt(int i, int v) {
return e[i].f == v ? e[i].se : e[i].f;
}
void precheck(int v) {
used[v] = true;
for(int ind : g[v]) {
int to = gt(ind, v);
if(to == pp[v])
continue;
if(used[to]) {
if(d[to] > d[v])
continue;
if((d[v] - d[to]) & 1) {
cout << 0 << endl;
exit(0);
}
int u = v;
while(u != to) {
unio(u, pp[u]);
u = pp[u];
}
u = v;
while(u != pp[to]) {
cycle[find(u)].pb(u);
u = pp[u];
}
}
else {
d[to] = d[v] + 1;
pp[to] = v;
precheck(to);
}
}
}
vector <int> now;
inline int get(int k, int l, int r) {
k = (k + 10) & 1;
Max(l, 0LL);
if(l > r)
return 0;
return pref[k][r] - (l == 0 ? 0 : pref[k][l-1]);
}
void solve_for_cycle() {
int sum = 0;
for(int v : now)
sum += a[v];
for(int i = 0; i < now.size(); ++i) {
for(int j = 0; j < 2; ++j)
pref[j][i] = (i == 0 ? 0 : pref[j][i-1]);
pref[i & 1][i] += a[now[i]];
}
for(int i = 0; i + 1 < now.size(); ++i) {
int v = now[i];
int ind = -1;
for(int j = 0; j < g[v].size(); ++j)
if(gt(g[v][j], v) == now[i + 1])
ind = g[v][j];
if(ind == -1) {
cout << "ASdasd" << endl;
exit(0);
}
assert(ind != -1);
int val = get(i + 2, i + 2, now.size() - 1) + get(i - 1, 0, i - 1);
ans[ind] = sum - 2 * val;
}
int v = now.back();
int ind = -1;
for(int j = 0; j < g[v].size(); ++j)
if(gt(g[v][j], v) == now[0])
ind = g[v][j];
if(ind == -1) {
cout << "ASdasd" << endl;
exit(0);
}
assert(ind != -1);
int val = get(1, 1, now.size() - 2);
ans[ind] = sum - 2 * val;
}
main() {
for(int i = 0; i < N; ++i)
p[i] = i, sz[i] = 1;
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
// setin("input.txt");
cin >> n >> m;
for(int i = 1; i <= n; ++i)
cin >> a[i];
for(int i = 1; i <= m; ++i) {
cin >> e[i].f >> e[i].se;
g[e[i].f].pb(i);
g[e[i].se].pb(i);
}
precheck(1);
for(int i = 1; i <= n; ++i)
deg[i] = g[i].size(), cur.insert({g[i].size(), i});
memset(used, 0, sizeof(used));
while(!cur.empty()) {
int v = cur.begin()->se;
cur.erase(cur.begin());
used[v] = true;
for(int ind : g[v]) {
int to = gt(ind, v);
if(used[to] && deg[to] == 1)
a[v] -= ans[ind] / 2;
}
if(deg[v] != 1)
continue;
for(int ind : g[v]) {
int to = gt(ind, v);
if(used[to])
continue;
ans[ind] = 2 * a[v];
cur.erase({deg[to], to});
--deg[to];
cur.insert({deg[to], to});
}
}
set <int> is = {};
for(int i = 1; i <= n; ++i)
if(sz[find(i)] > 1)
is.insert(find(i));
if(is.size() > 1)
return cout << 0 << endl, 0;
memset(used, 0, sizeof(used));
for(int i = 1; i <= n; ++i) {
if(sz[find(i)] > 1 && !used[i]) {
now = cycle[find(i)];
solve_for_cycle();
break;
}
}
for(int i = 1; i <= m; ++i)
cout << ans[i] << endl;
return 0;
}
Compilation message
pipes.cpp: In function 'void solve_for_cycle()':
pipes.cpp:153:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < now.size(); ++i) {
~~^~~~~~~~~~~~
pipes.cpp:158:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i + 1 < now.size(); ++i) {
~~~~~~^~~~~~~~~~~~
pipes.cpp:161:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < g[v].size(); ++j)
~~^~~~~~~~~~~~~
pipes.cpp:174:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < g[v].size(); ++j)
~~^~~~~~~~~~~~~
pipes.cpp: At global scope:
pipes.cpp:186:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
32256 KB |
Output is correct |
2 |
Correct |
20 ms |
32260 KB |
Output is correct |
3 |
Correct |
23 ms |
32384 KB |
Output is correct |
4 |
Correct |
230 ms |
48136 KB |
Output is correct |
5 |
Correct |
18 ms |
32128 KB |
Output is correct |
6 |
Correct |
17 ms |
32128 KB |
Output is correct |
7 |
Correct |
18 ms |
32256 KB |
Output is correct |
8 |
Correct |
17 ms |
32256 KB |
Output is correct |
9 |
Correct |
18 ms |
32384 KB |
Output is correct |
10 |
Correct |
21 ms |
32384 KB |
Output is correct |
11 |
Correct |
18 ms |
32384 KB |
Output is correct |
12 |
Correct |
18 ms |
32384 KB |
Output is correct |
13 |
Correct |
155 ms |
44792 KB |
Output is correct |
14 |
Correct |
183 ms |
47224 KB |
Output is correct |
15 |
Correct |
218 ms |
48120 KB |
Output is correct |
16 |
Correct |
169 ms |
45816 KB |
Output is correct |
17 |
Correct |
197 ms |
47992 KB |
Output is correct |
18 |
Correct |
204 ms |
48120 KB |
Output is correct |
19 |
Correct |
191 ms |
50684 KB |
Output is correct |
20 |
Correct |
18 ms |
32128 KB |
Output is correct |
21 |
Correct |
18 ms |
32384 KB |
Output is correct |
22 |
Correct |
302 ms |
48120 KB |
Output is correct |
23 |
Correct |
160 ms |
44920 KB |
Output is correct |
24 |
Correct |
201 ms |
48120 KB |
Output is correct |
25 |
Correct |
165 ms |
45560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
32256 KB |
Output is correct |
2 |
Correct |
20 ms |
32384 KB |
Output is correct |
3 |
Correct |
101 ms |
41600 KB |
Output is correct |
4 |
Incorrect |
163 ms |
53764 KB |
Output isn't correct |
5 |
Correct |
237 ms |
47352 KB |
Output is correct |
6 |
Correct |
224 ms |
54776 KB |
Output is correct |
7 |
Correct |
22 ms |
32256 KB |
Output is correct |
8 |
Correct |
18 ms |
32256 KB |
Output is correct |
9 |
Correct |
17 ms |
31744 KB |
Output is correct |
10 |
Correct |
19 ms |
32256 KB |
Output is correct |
11 |
Incorrect |
23 ms |
32256 KB |
Output isn't correct |
12 |
Correct |
20 ms |
32256 KB |
Output is correct |
13 |
Correct |
21 ms |
31744 KB |
Output is correct |
14 |
Correct |
17 ms |
32256 KB |
Output is correct |
15 |
Correct |
18 ms |
32384 KB |
Output is correct |
16 |
Correct |
18 ms |
32384 KB |
Output is correct |
17 |
Correct |
17 ms |
31744 KB |
Output is correct |
18 |
Correct |
21 ms |
32384 KB |
Output is correct |
19 |
Incorrect |
21 ms |
32376 KB |
Output isn't correct |
20 |
Correct |
21 ms |
32384 KB |
Output is correct |
21 |
Correct |
18 ms |
31872 KB |
Output is correct |
22 |
Correct |
23 ms |
32376 KB |
Output is correct |
23 |
Correct |
163 ms |
49264 KB |
Output is correct |
24 |
Correct |
205 ms |
52468 KB |
Output is correct |
25 |
Correct |
77 ms |
41592 KB |
Output is correct |
26 |
Correct |
145 ms |
51828 KB |
Output is correct |
27 |
Incorrect |
155 ms |
53964 KB |
Output isn't correct |
28 |
Correct |
265 ms |
48248 KB |
Output is correct |
29 |
Correct |
187 ms |
50296 KB |
Output is correct |
30 |
Correct |
186 ms |
53748 KB |
Output is correct |
31 |
Correct |
155 ms |
55148 KB |
Output is correct |
32 |
Correct |
198 ms |
49396 KB |
Output is correct |
33 |
Correct |
93 ms |
42616 KB |
Output is correct |
34 |
Correct |
139 ms |
50936 KB |
Output is correct |
35 |
Incorrect |
218 ms |
53740 KB |
Output isn't correct |
36 |
Correct |
248 ms |
47864 KB |
Output is correct |
37 |
Correct |
225 ms |
54824 KB |
Output is correct |
38 |
Correct |
241 ms |
53360 KB |
Output is correct |
39 |
Correct |
288 ms |
48928 KB |
Output is correct |
40 |
Correct |
241 ms |
51960 KB |
Output is correct |
41 |
Correct |
110 ms |
44408 KB |
Output is correct |
42 |
Correct |
133 ms |
51696 KB |
Output is correct |
43 |
Incorrect |
183 ms |
54512 KB |
Output isn't correct |
44 |
Correct |
205 ms |
47352 KB |
Output is correct |
45 |
Correct |
197 ms |
51452 KB |
Output is correct |
46 |
Correct |
220 ms |
54512 KB |
Output is correct |
47 |
Correct |
204 ms |
52084 KB |
Output is correct |
48 |
Correct |
249 ms |
54888 KB |
Output is correct |
49 |
Correct |
82 ms |
39800 KB |
Output is correct |
50 |
Correct |
208 ms |
51188 KB |
Output is correct |
51 |
Incorrect |
213 ms |
50420 KB |
Output isn't correct |
52 |
Correct |
198 ms |
49148 KB |
Output is correct |
53 |
Correct |
247 ms |
51576 KB |
Output is correct |
54 |
Correct |
273 ms |
52584 KB |
Output is correct |