#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
#define int long long
const int mod=1e9+7,mxn=5e5+5,lg=30,inf=1e18,minf=-1e18;
int ans[mxn+10],val[mxn+10],up[mxn+10],curval[mxn+10];
vector<pii>adj[mxn+10];
bitset<mxn+10>vis;
bool yes=false;
void solve(int cur,int p){
if(vis[cur])return void(yes=true);
vis[cur]=true;
bool leaf=true;
for(auto i:adj[cur])if(i.f!=p)leaf=false;
if(leaf){
ans[up[cur]]+=val[cur];
curval[p]+=val[cur];
return;
}
for(auto i:adj[cur]){
if(i.f==p)continue;
up[i.f]=i.s;
solve(i.f,cur);
}
if(cur==1)return;
ans[up[cur]]+=(val[cur]-curval[cur]);
curval[p]+=(val[cur]-curval[cur]);
}
vector<ppii>path;
int deg[mxn+10],sum,cnt=0;
int tin[mxn+10];
int start=1,t=0;
void solve2(int cur,int p){
if(vis[cur])return;
cnt++;
tin[cur]=t++;
if(cnt%2)sum+=(val[cur]-curval[cur]);
else sum-=(val[cur]-curval[cur]);
vis[cur]=true;
for(auto i:adj[cur]){
if(i.f==start&&i.f!=p){
path.pb({i.s,{i.f,cur}});
return;
}
if(i.f!=p&&!vis[i.f]){
path.pb({i.s,{i.f,cur}});
solve2(i.f,cur);
}
}
}
int32_t main(){
int n,m;cin>>n>>m;
for(int i=1;i<=n;i++)cin>>val[i];
for(int i=0;i<m;i++){
int a,b;cin>>a>>b;
adj[a].pb({b,i});
adj[b].pb({a,i});
}
if(m>n){
cout<<0;
return 0;
}
if(m==n-1){
solve(1,-1);
for(int i=0;i<m;i++)cout<<2*ans[i]<<'\n';
return 0;
}
queue<int>st;
for(int i=1;i<=n;i++){
if(adj[i].size()==1)st.push(i);
deg[i]=adj[i].size();
}
cnt=0;
int add;
while(!st.empty()){
vis[st.front()]=true;
for(auto i:adj[st.front()]){
if(vis[i.f])continue;
deg[i.f]--;
add=val[st.front()]-curval[st.front()];
ans[i.s]+=add;
curval[i.f]+=add;
curval[st.front()]+=add;
if(deg[i.f]<=1){
st.push(i.f);
vis[i.f]=true;
}
}
st.pop();
cnt++;
}
for(int i=1;i<=n;i++)if(!vis[i])start=i;
for(int i=1;i<=n;i++)if(!vis[i]&&curval[i]!=0)start=i;
if((n-cnt)%2==0){
cout<<0;
return 0;
}
cnt=0;
solve2(start,-1);
sum/=2;
reverse(all(path));
for(int i=0;i<path.size();i++){
if(i==0)add=sum;
else add=val[path[i].s.f]-curval[path[i].s.f];
ans[path[i].f]+=add;
curval[path[i].s.f]+=add;
curval[path[i].s.s]+=add;
}
/*
*/
for(int i=0;i<m;i++)cout<<2*ans[i]<<'\n';
}
Compilation message
pipes.cpp: In function 'int32_t main()':
pipes.cpp:132:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | for(int i=0;i<path.size();i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20916 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
5 ms |
20828 KB |
Output is correct |
4 |
Correct |
79 ms |
26452 KB |
Output is correct |
5 |
Correct |
4 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20824 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20964 KB |
Output is correct |
9 |
Correct |
4 ms |
20828 KB |
Output is correct |
10 |
Correct |
4 ms |
20824 KB |
Output is correct |
11 |
Correct |
4 ms |
20828 KB |
Output is correct |
12 |
Correct |
5 ms |
20828 KB |
Output is correct |
13 |
Correct |
62 ms |
25304 KB |
Output is correct |
14 |
Correct |
76 ms |
25952 KB |
Output is correct |
15 |
Correct |
83 ms |
26448 KB |
Output is correct |
16 |
Correct |
67 ms |
25424 KB |
Output is correct |
17 |
Correct |
79 ms |
26452 KB |
Output is correct |
18 |
Correct |
81 ms |
26456 KB |
Output is correct |
19 |
Correct |
89 ms |
29456 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
4 ms |
20828 KB |
Output is correct |
22 |
Correct |
82 ms |
26456 KB |
Output is correct |
23 |
Correct |
63 ms |
25304 KB |
Output is correct |
24 |
Correct |
79 ms |
26316 KB |
Output is correct |
25 |
Correct |
69 ms |
25556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
21084 KB |
Output is correct |
3 |
Correct |
78 ms |
28260 KB |
Output is correct |
4 |
Correct |
69 ms |
22000 KB |
Output is correct |
5 |
Correct |
66 ms |
21724 KB |
Output is correct |
6 |
Correct |
276 ms |
42772 KB |
Output is correct |
7 |
Correct |
3 ms |
20824 KB |
Output is correct |
8 |
Correct |
4 ms |
20828 KB |
Output is correct |
9 |
Correct |
4 ms |
20828 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
3 ms |
16732 KB |
Output is correct |
14 |
Correct |
4 ms |
20828 KB |
Output is correct |
15 |
Correct |
4 ms |
21176 KB |
Output is correct |
16 |
Correct |
5 ms |
20824 KB |
Output is correct |
17 |
Correct |
4 ms |
20828 KB |
Output is correct |
18 |
Correct |
3 ms |
16732 KB |
Output is correct |
19 |
Correct |
4 ms |
16732 KB |
Output is correct |
20 |
Correct |
3 ms |
16732 KB |
Output is correct |
21 |
Correct |
4 ms |
16988 KB |
Output is correct |
22 |
Correct |
4 ms |
21084 KB |
Output is correct |
23 |
Correct |
91 ms |
36760 KB |
Output is correct |
24 |
Correct |
94 ms |
38148 KB |
Output is correct |
25 |
Correct |
69 ms |
27984 KB |
Output is correct |
26 |
Correct |
68 ms |
22028 KB |
Output is correct |
27 |
Correct |
70 ms |
21844 KB |
Output is correct |
28 |
Correct |
71 ms |
22096 KB |
Output is correct |
29 |
Correct |
216 ms |
37328 KB |
Output is correct |
30 |
Correct |
93 ms |
36828 KB |
Output is correct |
31 |
Correct |
98 ms |
42456 KB |
Output is correct |
32 |
Correct |
85 ms |
33268 KB |
Output is correct |
33 |
Correct |
74 ms |
28240 KB |
Output is correct |
34 |
Correct |
70 ms |
21844 KB |
Output is correct |
35 |
Correct |
69 ms |
21904 KB |
Output is correct |
36 |
Correct |
69 ms |
21844 KB |
Output is correct |
37 |
Correct |
281 ms |
42816 KB |
Output is correct |
38 |
Correct |
95 ms |
37008 KB |
Output is correct |
39 |
Correct |
83 ms |
32340 KB |
Output is correct |
40 |
Correct |
88 ms |
37584 KB |
Output is correct |
41 |
Correct |
72 ms |
28244 KB |
Output is correct |
42 |
Correct |
70 ms |
21928 KB |
Output is correct |
43 |
Correct |
69 ms |
21964 KB |
Output is correct |
44 |
Correct |
66 ms |
21704 KB |
Output is correct |
45 |
Correct |
215 ms |
40504 KB |
Output is correct |
46 |
Correct |
91 ms |
36100 KB |
Output is correct |
47 |
Correct |
90 ms |
37644 KB |
Output is correct |
48 |
Correct |
94 ms |
42444 KB |
Output is correct |
49 |
Correct |
70 ms |
27976 KB |
Output is correct |
50 |
Correct |
70 ms |
21968 KB |
Output is correct |
51 |
Correct |
77 ms |
22096 KB |
Output is correct |
52 |
Correct |
68 ms |
21844 KB |
Output is correct |
53 |
Correct |
223 ms |
39588 KB |
Output is correct |
54 |
Correct |
91 ms |
35844 KB |
Output is correct |