#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e6+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
int t[N],ans[N],val[N];
vector<pair<int,int> >graf[N];
vi __curr;
bool vis[N];
void wa(){
cout<<0<<"\n";
exit(0);
}
ll dfs(int v){
if(vis[v]) return 0;
vis[v]=1;
ll sum=0;
for(auto u:graf[v]){
ll curr=dfs(u.fi);
sum+=curr,ans[u.se]=(curr<<1);
}
return (ll)t[v]-sum;
}
void dfs2(int v,vi *c,int o,int num){
if(c->size()) return;
val[v]=num;
if(vis[v]){
c->push_back(v);
while(__curr[__curr.size()-1]!=v) c->push_back(__curr[__curr.size()-1]),__curr.pop_back();
return;
}
vis[v]=1;
__curr.push_back(v);
for(auto u:graf[v]) if(o!=u.fi) dfs2(u.fi,c,v,u.se);
if(c->size()) return;
__curr.pop_back();
}
void solve(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>t[i];
for(int a,b,i=1;i<=m;i++){
cin>>a>>b;
graf[a].push_back({b,i}),graf[b].push_back({a,i});
}
if(m>n) wa();
if(m==n-1){
if(dfs(1)) wa();
for(int i=1;i<=m;i++) cout<<ans[i]<<"\n";
return;
}
vi c;
dfs2(1,&c,1,0);
if(!(c.size()&1)) wa();
reverse(c.begin(),c.end());
for(int i=1;i<=n;i++) vis[i]=0;
for(int i=0;i<c.size();i++){
int pop=(i==0?c[c.size()-1]:c[i-1]),nxt=(i==c.size()-1?c[0]:c[i+1]);
vis[pop]=1,vis[nxt]=1;
t[c[i]]=dfs(c[i]);
vis[pop]=0,vis[nxt]=0;
}
ll sum1=0,sum2=0;
for(int i=1;i<c.size();i+=2) sum1+=(ll)t[c[i]];
sum1<<=1LL;
for(int i=0;i<c.size();i++) sum2+=(ll)t[c[i]];
ans[val[c[0]]]=sum2-sum1;
if(ans[val[c[0]]]&1LL) wa();
for(int i=1;i<c.size();i++) ans[val[c[i]]]=(t[c[i-1]]-(ans[val[c[i-1]]]>>1))<<1LL;
if(t[c[c.size()-1]]!=((ans[val[c[c.size()-1]]]+ans[val[c[0]]])>>1)) wa();
for(int i=1;i<=m;i++) cout<<ans[i]<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
while(tt--) solve();
}
Compilation message
pipes.cpp: In function 'void solve()':
pipes.cpp:71:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i=0;i<c.size();i++){
| ~^~~~~~~~~
pipes.cpp:72:51: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | int pop=(i==0?c[c.size()-1]:c[i-1]),nxt=(i==c.size()-1?c[0]:c[i+1]);
| ~^~~~~~~~~~~~
pipes.cpp:78:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i=1;i<c.size();i+=2) sum1+=(ll)t[c[i]];
| ~^~~~~~~~~
pipes.cpp:80:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int i=0;i<c.size();i++) sum2+=(ll)t[c[i]];
| ~^~~~~~~~~
pipes.cpp:83:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int i=1;i<c.size();i++) ans[val[c[i]]]=(t[c[i-1]]-(ans[val[c[i-1]]]>>1))<<1LL;
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23804 KB |
Output is correct |
3 |
Correct |
13 ms |
23804 KB |
Output is correct |
4 |
Correct |
92 ms |
30476 KB |
Output is correct |
5 |
Correct |
13 ms |
23724 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23752 KB |
Output is correct |
8 |
Correct |
12 ms |
23712 KB |
Output is correct |
9 |
Correct |
13 ms |
23828 KB |
Output is correct |
10 |
Correct |
14 ms |
23892 KB |
Output is correct |
11 |
Correct |
13 ms |
23804 KB |
Output is correct |
12 |
Correct |
17 ms |
23892 KB |
Output is correct |
13 |
Correct |
73 ms |
29032 KB |
Output is correct |
14 |
Correct |
67 ms |
30028 KB |
Output is correct |
15 |
Correct |
73 ms |
30384 KB |
Output is correct |
16 |
Correct |
63 ms |
29404 KB |
Output is correct |
17 |
Correct |
81 ms |
30364 KB |
Output is correct |
18 |
Correct |
75 ms |
30388 KB |
Output is correct |
19 |
Correct |
68 ms |
32920 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
14 ms |
23784 KB |
Output is correct |
22 |
Correct |
84 ms |
30356 KB |
Output is correct |
23 |
Correct |
61 ms |
29004 KB |
Output is correct |
24 |
Correct |
69 ms |
30412 KB |
Output is correct |
25 |
Correct |
57 ms |
29288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23820 KB |
Output is correct |
2 |
Correct |
15 ms |
23892 KB |
Output is correct |
3 |
Correct |
63 ms |
32716 KB |
Output is correct |
4 |
Correct |
67 ms |
28936 KB |
Output is correct |
5 |
Correct |
54 ms |
29156 KB |
Output is correct |
6 |
Correct |
188 ms |
44264 KB |
Output is correct |
7 |
Correct |
14 ms |
23820 KB |
Output is correct |
8 |
Correct |
13 ms |
23732 KB |
Output is correct |
9 |
Correct |
13 ms |
23768 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23824 KB |
Output is correct |
13 |
Correct |
12 ms |
23824 KB |
Output is correct |
14 |
Correct |
13 ms |
23816 KB |
Output is correct |
15 |
Correct |
13 ms |
23892 KB |
Output is correct |
16 |
Correct |
14 ms |
23892 KB |
Output is correct |
17 |
Correct |
14 ms |
23892 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
13 ms |
23828 KB |
Output is correct |
20 |
Correct |
17 ms |
23784 KB |
Output is correct |
21 |
Correct |
14 ms |
23892 KB |
Output is correct |
22 |
Correct |
13 ms |
23892 KB |
Output is correct |
23 |
Correct |
76 ms |
33136 KB |
Output is correct |
24 |
Correct |
77 ms |
34312 KB |
Output is correct |
25 |
Correct |
65 ms |
32740 KB |
Output is correct |
26 |
Correct |
54 ms |
29004 KB |
Output is correct |
27 |
Correct |
59 ms |
28924 KB |
Output is correct |
28 |
Correct |
63 ms |
29424 KB |
Output is correct |
29 |
Correct |
158 ms |
40452 KB |
Output is correct |
30 |
Correct |
86 ms |
36344 KB |
Output is correct |
31 |
Correct |
91 ms |
36504 KB |
Output is correct |
32 |
Correct |
80 ms |
32092 KB |
Output is correct |
33 |
Correct |
73 ms |
33604 KB |
Output is correct |
34 |
Correct |
52 ms |
28948 KB |
Output is correct |
35 |
Correct |
68 ms |
28948 KB |
Output is correct |
36 |
Correct |
54 ms |
29320 KB |
Output is correct |
37 |
Correct |
197 ms |
44212 KB |
Output is correct |
38 |
Correct |
83 ms |
33760 KB |
Output is correct |
39 |
Correct |
69 ms |
31444 KB |
Output is correct |
40 |
Correct |
77 ms |
34052 KB |
Output is correct |
41 |
Correct |
68 ms |
35984 KB |
Output is correct |
42 |
Correct |
57 ms |
28856 KB |
Output is correct |
43 |
Correct |
53 ms |
28912 KB |
Output is correct |
44 |
Correct |
53 ms |
29216 KB |
Output is correct |
45 |
Correct |
128 ms |
41624 KB |
Output is correct |
46 |
Correct |
95 ms |
36952 KB |
Output is correct |
47 |
Correct |
86 ms |
34108 KB |
Output is correct |
48 |
Correct |
78 ms |
36320 KB |
Output is correct |
49 |
Correct |
54 ms |
30640 KB |
Output is correct |
50 |
Correct |
53 ms |
29032 KB |
Output is correct |
51 |
Correct |
56 ms |
29192 KB |
Output is correct |
52 |
Correct |
53 ms |
28872 KB |
Output is correct |
53 |
Correct |
154 ms |
41732 KB |
Output is correct |
54 |
Correct |
76 ms |
35140 KB |
Output is correct |