#include <bits/stdc++.h>
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
*/
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);
using namespace std;
const ll maxn=5e5+123,inf=1e18,mod=1e9+7,N=360360,LOG=20;
vector< pair<int,int> >g[maxn],G[maxn],path;
ll n,m,cnt[maxn],ans[maxn],a[maxn],mm;
bitset<maxn>used;
void dfs(int v,int st){
used[v]=1;
for(int i=0;i<G[v].size();i++){
int to=G[v][i].f;
if(!used[to]){
path.pb(G[v][i]);
dfs(to,st);
}else if(v==st)
path.pb({v,G[v][i].s});
}
}
int main(){
#ifdef LOCAL
freopen ("test.in", "r", stdin);
#endif
IOS
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
a[i]*=2;
}
for(int i=1;i<=m;i++){
int v,u;
cin>>v>>u;
g[v].pb({u,i});
g[u].pb({v,i});
}
queue<int>q;
for(int i=1;i<=n;i++){
cnt[i]=g[i].size();
if(cnt[i]==1)
q.push(i);
}
while(!q.empty()){
int v=q.front();
q.pop();
used[v]=1;
for(auto to:g[v])
if(!used[to.f]){
cnt[to.f]--;
if(cnt[to.f]==1)
q.push(to.f);
ans[to.s]=a[v];
a[to.f]-=a[v];
a[v]-=a[v];
}
}
mm=0;
for(int v=1;v<=n;v++){
if(used[v])
continue;
for(auto to:g[v])
if(!used[to.f]){
mm++;
G[v].pb(to);
}
}
mm/=2;
for(int i=1;i<=n;i++)
if(!used[i] && G[i].size()!=2)
mm=-1;
if( n==used.count()){
bool ok=1;
for(int i=1;i<=n;i++)
if(a[i]!=0)
ok=0;
if(!ok)
cout<<0,exit(0);
for(int i=1;i<=m;i++)
cout<<ans[i]<<endl;
return 0;
}
if( (n-used.count())%2==0 || mm!=n-used.count())
cout<<0,exit(0);
for(int i=1;i<=n;i++)
if(!used[i]){
dfs(i,i);
if( (n-used.count())!=0 )
cout<<0,exit(0);
ll x=0;
for(int i=0;i<path.size();i++){
if(i%2==0)
x+=a[path[i].f];
else
x-=a[path[i].f];
}
x/=2;
a[path[path.size()-1].f]-=x;
a[path[0].f]-=x;
ans[path[0].s]=x;
for(int i=1;i<path.size();i++){
ans[path[i].s]=a[path[i-1].f];
a[path[i].f]-=a[path[i-1].f];
a[path[i-1].f]-=a[path[i-1].f];
}
bool ok=1;
for(int i=1;i<=n;i++)
if(a[i]!=0)
ok=0;
if(!ok)
cout<<0,exit(0);
for(int i=1;i<=m;i++)
cout<<ans[i]<<endl;
break;
}
}
Compilation message
pipes.cpp: In function 'void dfs(int, int)':
pipes.cpp:22:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<G[v].size();i++){
~^~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:82:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if( n==used.count()){
~^~~~~~~~~~~~~~
pipes.cpp:93:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if( (n-used.count())%2==0 || mm!=n-used.count())
~~^~~~~~~~~~~~~~~~
pipes.cpp:101:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<path.size();i++){
~^~~~~~~~~~~~
pipes.cpp:111:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<path.size();i++){
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
23800 KB |
Output is correct |
2 |
Correct |
26 ms |
23908 KB |
Output is correct |
3 |
Correct |
31 ms |
23944 KB |
Output is correct |
4 |
Correct |
326 ms |
30788 KB |
Output is correct |
5 |
Correct |
24 ms |
30788 KB |
Output is correct |
6 |
Correct |
25 ms |
30788 KB |
Output is correct |
7 |
Correct |
23 ms |
30788 KB |
Output is correct |
8 |
Correct |
27 ms |
30788 KB |
Output is correct |
9 |
Correct |
29 ms |
30788 KB |
Output is correct |
10 |
Correct |
31 ms |
30788 KB |
Output is correct |
11 |
Correct |
25 ms |
30788 KB |
Output is correct |
12 |
Correct |
30 ms |
30788 KB |
Output is correct |
13 |
Correct |
232 ms |
30788 KB |
Output is correct |
14 |
Correct |
280 ms |
30788 KB |
Output is correct |
15 |
Correct |
280 ms |
30920 KB |
Output is correct |
16 |
Correct |
223 ms |
30920 KB |
Output is correct |
17 |
Correct |
249 ms |
30920 KB |
Output is correct |
18 |
Correct |
278 ms |
30920 KB |
Output is correct |
19 |
Correct |
341 ms |
30920 KB |
Output is correct |
20 |
Correct |
24 ms |
30920 KB |
Output is correct |
21 |
Correct |
31 ms |
30920 KB |
Output is correct |
22 |
Correct |
300 ms |
30928 KB |
Output is correct |
23 |
Correct |
269 ms |
30928 KB |
Output is correct |
24 |
Correct |
311 ms |
30928 KB |
Output is correct |
25 |
Correct |
249 ms |
30928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
30928 KB |
Output is correct |
2 |
Correct |
26 ms |
30928 KB |
Output is correct |
3 |
Correct |
128 ms |
31036 KB |
Output is correct |
4 |
Correct |
131 ms |
32184 KB |
Output is correct |
5 |
Correct |
124 ms |
32184 KB |
Output is correct |
6 |
Correct |
427 ms |
51036 KB |
Output is correct |
7 |
Correct |
25 ms |
51036 KB |
Output is correct |
8 |
Correct |
24 ms |
51036 KB |
Output is correct |
9 |
Correct |
24 ms |
51036 KB |
Output is correct |
10 |
Correct |
24 ms |
51036 KB |
Output is correct |
11 |
Correct |
26 ms |
51036 KB |
Output is correct |
12 |
Correct |
25 ms |
51036 KB |
Output is correct |
13 |
Correct |
25 ms |
51036 KB |
Output is correct |
14 |
Correct |
23 ms |
51036 KB |
Output is correct |
15 |
Correct |
26 ms |
51036 KB |
Output is correct |
16 |
Correct |
28 ms |
51036 KB |
Output is correct |
17 |
Correct |
28 ms |
51036 KB |
Output is correct |
18 |
Correct |
23 ms |
51036 KB |
Output is correct |
19 |
Correct |
28 ms |
51036 KB |
Output is correct |
20 |
Correct |
26 ms |
51036 KB |
Output is correct |
21 |
Correct |
31 ms |
51036 KB |
Output is correct |
22 |
Correct |
28 ms |
51036 KB |
Output is correct |
23 |
Correct |
266 ms |
51036 KB |
Output is correct |
24 |
Correct |
344 ms |
51036 KB |
Output is correct |
25 |
Correct |
136 ms |
51036 KB |
Output is correct |
26 |
Correct |
152 ms |
51036 KB |
Output is correct |
27 |
Correct |
113 ms |
51036 KB |
Output is correct |
28 |
Correct |
124 ms |
51036 KB |
Output is correct |
29 |
Correct |
439 ms |
51036 KB |
Output is correct |
30 |
Correct |
296 ms |
51036 KB |
Output is correct |
31 |
Correct |
357 ms |
51036 KB |
Output is correct |
32 |
Correct |
306 ms |
51036 KB |
Output is correct |
33 |
Correct |
125 ms |
51036 KB |
Output is correct |
34 |
Correct |
141 ms |
51036 KB |
Output is correct |
35 |
Correct |
111 ms |
51036 KB |
Output is correct |
36 |
Correct |
112 ms |
51036 KB |
Output is correct |
37 |
Correct |
373 ms |
51036 KB |
Output is correct |
38 |
Correct |
287 ms |
51036 KB |
Output is correct |
39 |
Correct |
276 ms |
51036 KB |
Output is correct |
40 |
Correct |
268 ms |
51036 KB |
Output is correct |
41 |
Correct |
110 ms |
51036 KB |
Output is correct |
42 |
Correct |
140 ms |
51036 KB |
Output is correct |
43 |
Correct |
110 ms |
51036 KB |
Output is correct |
44 |
Correct |
108 ms |
51036 KB |
Output is correct |
45 |
Correct |
397 ms |
51036 KB |
Output is correct |
46 |
Correct |
272 ms |
51036 KB |
Output is correct |
47 |
Correct |
300 ms |
51036 KB |
Output is correct |
48 |
Correct |
339 ms |
51036 KB |
Output is correct |
49 |
Correct |
135 ms |
51036 KB |
Output is correct |
50 |
Correct |
117 ms |
51036 KB |
Output is correct |
51 |
Correct |
105 ms |
51036 KB |
Output is correct |
52 |
Correct |
115 ms |
51036 KB |
Output is correct |
53 |
Correct |
350 ms |
51036 KB |
Output is correct |
54 |
Correct |
275 ms |
51036 KB |
Output is correct |