#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n,m;
cin>>n>>m;
if(m>n){
cout<<0;
return 0;
}
map<pair<ll,ll>,ll>mp;
set<ll>s[n+5];
ll c[n+5],p[n+5];
fill(p,p+n+1,0);
for(ll i=1;i<=n;i++){
cin>>c[i];
}
vector<pair<ll,ll>>edge;
for(ll i=1;i<=m;i++){
ll u,v;
cin>>u>>v;
edge.pb({u,v});
s[u].insert(v);
s[v].insert(u);
p[u]++;
p[v]++;
}
if(m==n-1){
queue<ll>q;
for(ll i=1;i<=n;i++){
if(p[i]==1){
q.push(i);
}
}
for(ll i=1;i<n;i++){
ll x=q.front();
q.pop();
if(p[x]==0){
continue;
}
ll y=*s[x].begin();
mp[{x,y}]=2*c[x];
c[y]-=c[x];
p[y]--;
p[x]--;
s[x].erase(y);
s[y].erase(x);
if(p[y]==1){
q.push(y);
}
}
if(c[q.front()]!=0){
cout<<0;
return 0;
}
for(auto [u,v]:edge){
cout<<mp[{u,v}]+mp[{v,u}]<<"\n";
}
}
else{
queue<ll>q;
ll used[n+5];
fill(used,used+n+1,0);
for(ll i=1;i<=n;i++){
if(p[i]==1){
q.push(i);
}
}
ll cnt=n;
while(!q.empty()){
ll x=q.front();
q.pop();
if(used[x]==1){
continue;
}
used[x]=1;
cnt--;
ll y=*s[x].begin();
mp[{x,y}]=2*c[x];
c[y]-=c[x];
p[y]--;
p[x]--;
s[x].erase(y);
s[y].erase(x);
if(p[y]==1){
q.push(y);
}
}
if(cnt%2==0){
cout<<0;
return 0;
}
vector<ll>node;
ll sum=0;
function <void(ll)> dfs=[&](ll x){
used[x]=1;
node.pb(x);
sum+=c[x];
for(auto y:s[x]){
if(used[y]==0){
dfs(y);
}
}
};
for(ll i=1;i<=n;i++){
if(used[i]==0){
dfs(i);
break;
}
}
sum/=2;
ll sz=node.size();
node.insert(node.end(),all(node));
ll a[sz*2+5];
a[0]=c[node[0]];
a[1]=c[node[1]];
for(ll i=2;i<sz*2;i++){
a[i]=a[i-2]+c[node[i]];
}
for(ll i=0;i<sz;i++){
mp[{node[i],node[i+1]}]=sum-(a[i+(sz-1)]-a[i]);
}
for(auto [u,v]:edge){
cout<<(mp[{u,v}]+mp[{v,u}])*2<<"\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
232 ms |
23076 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
164 ms |
18460 KB |
Output is correct |
14 |
Correct |
220 ms |
21880 KB |
Output is correct |
15 |
Correct |
223 ms |
23252 KB |
Output is correct |
16 |
Correct |
157 ms |
19760 KB |
Output is correct |
17 |
Correct |
198 ms |
23144 KB |
Output is correct |
18 |
Correct |
201 ms |
23240 KB |
Output is correct |
19 |
Correct |
198 ms |
23092 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
226 ms |
23228 KB |
Output is correct |
23 |
Correct |
195 ms |
18420 KB |
Output is correct |
24 |
Correct |
207 ms |
23156 KB |
Output is correct |
25 |
Correct |
177 ms |
19512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
3 |
Correct |
83 ms |
18924 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
848 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
584 KB |
Output isn't correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
328 KB |
Output is correct |
22 |
Incorrect |
2 ms |
588 KB |
Output isn't correct |
23 |
Incorrect |
161 ms |
29300 KB |
Output isn't correct |
24 |
Incorrect |
257 ms |
33880 KB |
Output isn't correct |
25 |
Correct |
101 ms |
18976 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
328 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Incorrect |
216 ms |
31820 KB |
Output isn't correct |
31 |
Incorrect |
218 ms |
40060 KB |
Output isn't correct |
32 |
Incorrect |
238 ms |
27256 KB |
Output isn't correct |
33 |
Correct |
91 ms |
20156 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Incorrect |
286 ms |
32644 KB |
Output isn't correct |
39 |
Incorrect |
219 ms |
25892 KB |
Output isn't correct |
40 |
Incorrect |
275 ms |
33068 KB |
Output isn't correct |
41 |
Correct |
79 ms |
20024 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Incorrect |
275 ms |
31404 KB |
Output isn't correct |
47 |
Incorrect |
266 ms |
33380 KB |
Output isn't correct |
48 |
Incorrect |
242 ms |
39388 KB |
Output isn't correct |
49 |
Correct |
108 ms |
19160 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
320 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
1 ms |
336 KB |
Output is correct |
54 |
Incorrect |
322 ms |
30680 KB |
Output isn't correct |