#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=5e5+5;
int n, m, h[nx], u, v, sz, res[nx], vs[nx], cy[nx], pa[nx], pidx[nx], cnt, lst;
vector<pair<int, int>> d[nx], c;
void dfs(int u, int p, int idx)
{
vs[u]=1; pa[u]=p; pidx[u]=idx;
for (auto [v, id]:d[u])
{
if (v==p||cy[v]) continue;
if (!vs[v]) dfs(v, u, id);
else
{
int tmp=u;
while (tmp!=v) cy[tmp]=1, c.push_back({tmp, pidx[tmp]}), tmp=pa[tmp], cnt++;
cy[v]=1; cnt++; c.push_back({v, id});
}
}
if (idx==-1||m==n) return;
res[idx]=2*h[u];
h[p]-=h[u];
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n; i++) cin>>h[i];
for (int i=1; i<=m; i++) cin>>u>>v, d[u].push_back({v, i}), d[v].push_back({u, i});
if (m>n) {cout<<0; return 0;}
dfs(1, 1, -1);
if (cnt%2==0&&cnt!=0) {cout<<0; return 0;}
if (m<n) for (int i=1; i<n; i++) cout<<res[i]<<'\n';
else
{
m=n-1;
for (int i=1; i<=n; i++) vs[i]=0;
for (auto [x, y]:c) dfs(x, x, -1);
int l=-1e9, r=1e9;
while (l<=r)
{
vector<int> t(c.size());
int md=(l+r+1)/2;
//cout<<"here "<<l<<' '<<r<<' '<<md<<'\n';
lst=md;
t[0]+=md;
t[c.size()-1]+=md;
res[c[c.size()-1].second]=2*md;
for (int i=1; i<c.size(); i++) t[i]+=(h[c[i-1].first]-t[i-1]), res[c[i-1].second]=2*(h[c[i-1].first]-t[i-1]);
if (t[c.size()-1]==h[c[c.size()-1].first]) break;
if (t[c.size()-1]>=h[c[c.size()-1].first]) r=md;
else l=md;
}
//for (int i=1; i<=n; i++) if (cy[i]) cout<<i<<' '<<h[i]<<'\n';
for (int i=1; i<=m+1; i++) cout<<res[i]<<'\n';
}
}
/*
4 4
2 0 2 2
1 2
1 3
2 3
3 4
*/
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:55:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i=1; i<c.size(); i++) t[i]+=(h[c[i-1].first]-t[i-1]), res[c[i-1].second]=2*(h[c[i-1].first]-t[i-1]);
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21084 KB |
Output is correct |
2 |
Correct |
4 ms |
21008 KB |
Output is correct |
3 |
Correct |
5 ms |
21084 KB |
Output is correct |
4 |
Correct |
41 ms |
25252 KB |
Output is correct |
5 |
Correct |
3 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
3 ms |
21084 KB |
Output is correct |
8 |
Correct |
4 ms |
21084 KB |
Output is correct |
9 |
Correct |
4 ms |
21084 KB |
Output is correct |
10 |
Correct |
4 ms |
21080 KB |
Output is correct |
11 |
Correct |
4 ms |
21084 KB |
Output is correct |
12 |
Correct |
4 ms |
21084 KB |
Output is correct |
13 |
Correct |
32 ms |
24480 KB |
Output is correct |
14 |
Correct |
49 ms |
24948 KB |
Output is correct |
15 |
Correct |
40 ms |
25180 KB |
Output is correct |
16 |
Correct |
34 ms |
24484 KB |
Output is correct |
17 |
Correct |
42 ms |
25172 KB |
Output is correct |
18 |
Correct |
44 ms |
25324 KB |
Output is correct |
19 |
Correct |
47 ms |
30804 KB |
Output is correct |
20 |
Correct |
4 ms |
21084 KB |
Output is correct |
21 |
Correct |
4 ms |
21196 KB |
Output is correct |
22 |
Correct |
41 ms |
25320 KB |
Output is correct |
23 |
Correct |
31 ms |
24400 KB |
Output is correct |
24 |
Correct |
41 ms |
25344 KB |
Output is correct |
25 |
Correct |
34 ms |
24404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21084 KB |
Output is correct |
2 |
Correct |
4 ms |
21340 KB |
Output is correct |
3 |
Correct |
41 ms |
29140 KB |
Output is correct |
4 |
Correct |
28 ms |
18256 KB |
Output is correct |
5 |
Correct |
31 ms |
18256 KB |
Output is correct |
6 |
Correct |
113 ms |
28716 KB |
Output is correct |
7 |
Correct |
4 ms |
21080 KB |
Output is correct |
8 |
Correct |
4 ms |
21080 KB |
Output is correct |
9 |
Correct |
3 ms |
19036 KB |
Output is correct |
10 |
Correct |
3 ms |
14936 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Execution timed out |
1096 ms |
21084 KB |
Time limit exceeded |
15 |
Correct |
4 ms |
21336 KB |
Output is correct |
16 |
Correct |
4 ms |
21084 KB |
Output is correct |
17 |
Correct |
4 ms |
19036 KB |
Output is correct |
18 |
Correct |
3 ms |
14936 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
21084 KB |
Output is correct |
23 |
Correct |
47 ms |
31516 KB |
Output is correct |
24 |
Correct |
60 ms |
32468 KB |
Output is correct |
25 |
Correct |
38 ms |
28752 KB |
Output is correct |
26 |
Correct |
29 ms |
18308 KB |
Output is correct |
27 |
Correct |
29 ms |
17984 KB |
Output is correct |
28 |
Correct |
30 ms |
18512 KB |
Output is correct |
29 |
Correct |
90 ms |
26080 KB |
Output is correct |
30 |
Correct |
65 ms |
36368 KB |
Output is correct |
31 |
Correct |
59 ms |
37068 KB |
Output is correct |
32 |
Correct |
65 ms |
27852 KB |
Output is correct |
33 |
Correct |
40 ms |
30424 KB |
Output is correct |
34 |
Correct |
36 ms |
18052 KB |
Output is correct |
35 |
Correct |
38 ms |
18336 KB |
Output is correct |
36 |
Correct |
29 ms |
18396 KB |
Output is correct |
37 |
Correct |
109 ms |
28716 KB |
Output is correct |
38 |
Correct |
57 ms |
35016 KB |
Output is correct |
39 |
Correct |
57 ms |
26996 KB |
Output is correct |
40 |
Correct |
51 ms |
31812 KB |
Output is correct |
41 |
Correct |
50 ms |
34716 KB |
Output is correct |
42 |
Correct |
28 ms |
18248 KB |
Output is correct |
43 |
Correct |
33 ms |
18068 KB |
Output is correct |
44 |
Correct |
29 ms |
18256 KB |
Output is correct |
45 |
Correct |
86 ms |
27140 KB |
Output is correct |
46 |
Correct |
70 ms |
37324 KB |
Output is correct |
47 |
Correct |
58 ms |
32204 KB |
Output is correct |
48 |
Correct |
61 ms |
36536 KB |
Output is correct |
49 |
Correct |
34 ms |
24912 KB |
Output is correct |
50 |
Correct |
32 ms |
18260 KB |
Output is correct |
51 |
Correct |
33 ms |
18268 KB |
Output is correct |
52 |
Correct |
29 ms |
18012 KB |
Output is correct |
53 |
Correct |
90 ms |
26892 KB |
Output is correct |
54 |
Correct |
58 ms |
33872 KB |
Output is correct |