#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=2e5+123,inf=1e18,mod=1e9+7,N=360360,LOG=20;
vector< pair<int,int> >g[maxn],G[maxn],path;
int 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(to==st)
path.pb(G[v][i]);
if(!used[to]){
path.pb(G[v][i]);
dfs(to,st);
}
}
}
int main(){
#ifdef LOCAL
freopen ("test.in", "r", stdin);
#endif
IOS
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
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]*2;
a[to.f]-=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);
}
}
for(int i=1;i<=n;i++)
if(!used[i] && G[i].size()!=2)
m=-1;
if( n==used.count()){
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];
}
a[path[path.size()-1].f]-=x;
a[path[0].f]-=x;
for(int i=1;i<path.size();i++){
a[path[i].f]-=a[path[i-1].f];
a[path[i-1].f]-=a[path[i-1].f];
}
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:79:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if( n==used.count()){
~^~~~~~~~~~~~~~
pipes.cpp:84:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if( (n-used.count())%2==0 || mm!=n-used.count())
~~^~~~~~~~~~~~~~~~
pipes.cpp:92:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<path.size();i++){
~^~~~~~~~~~~~
pipes.cpp:100:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<path.size();i++){
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
11 ms |
9840 KB |
Output is correct |
3 |
Correct |
16 ms |
9948 KB |
Output is correct |
4 |
Correct |
316 ms |
17256 KB |
Output is correct |
5 |
Correct |
13 ms |
17256 KB |
Output is correct |
6 |
Correct |
13 ms |
17256 KB |
Output is correct |
7 |
Correct |
12 ms |
17256 KB |
Output is correct |
8 |
Correct |
12 ms |
17256 KB |
Output is correct |
9 |
Correct |
15 ms |
17256 KB |
Output is correct |
10 |
Correct |
16 ms |
17256 KB |
Output is correct |
11 |
Correct |
14 ms |
17256 KB |
Output is correct |
12 |
Correct |
14 ms |
17256 KB |
Output is correct |
13 |
Correct |
219 ms |
17536 KB |
Output is correct |
14 |
Correct |
273 ms |
19804 KB |
Output is correct |
15 |
Correct |
300 ms |
21780 KB |
Output is correct |
16 |
Correct |
250 ms |
22184 KB |
Output is correct |
17 |
Correct |
278 ms |
24520 KB |
Output is correct |
18 |
Correct |
321 ms |
26128 KB |
Output is correct |
19 |
Correct |
305 ms |
27092 KB |
Output is correct |
20 |
Correct |
11 ms |
27092 KB |
Output is correct |
21 |
Correct |
16 ms |
27092 KB |
Output is correct |
22 |
Correct |
266 ms |
29456 KB |
Output is correct |
23 |
Correct |
236 ms |
29484 KB |
Output is correct |
24 |
Correct |
327 ms |
32228 KB |
Output is correct |
25 |
Correct |
226 ms |
32492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
32492 KB |
Output isn't correct |
2 |
Incorrect |
12 ms |
32492 KB |
Output isn't correct |
3 |
Correct |
111 ms |
35308 KB |
Output is correct |
4 |
Correct |
90 ms |
37880 KB |
Output is correct |
5 |
Correct |
84 ms |
37992 KB |
Output is correct |
6 |
Runtime error |
309 ms |
77948 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Incorrect |
11 ms |
77948 KB |
Output isn't correct |
8 |
Incorrect |
12 ms |
77948 KB |
Output isn't correct |
9 |
Correct |
11 ms |
77948 KB |
Output is correct |
10 |
Correct |
13 ms |
77948 KB |
Output is correct |
11 |
Correct |
11 ms |
77948 KB |
Output is correct |
12 |
Correct |
12 ms |
77948 KB |
Output is correct |
13 |
Correct |
15 ms |
77948 KB |
Output is correct |
14 |
Incorrect |
11 ms |
77948 KB |
Output isn't correct |
15 |
Incorrect |
14 ms |
77948 KB |
Output isn't correct |
16 |
Incorrect |
11 ms |
77948 KB |
Output isn't correct |
17 |
Correct |
14 ms |
77948 KB |
Output is correct |
18 |
Correct |
12 ms |
77948 KB |
Output is correct |
19 |
Correct |
12 ms |
77948 KB |
Output is correct |
20 |
Correct |
12 ms |
77948 KB |
Output is correct |
21 |
Correct |
15 ms |
77948 KB |
Output is correct |
22 |
Incorrect |
14 ms |
77948 KB |
Output isn't correct |
23 |
Incorrect |
96 ms |
77948 KB |
Output isn't correct |
24 |
Incorrect |
120 ms |
77948 KB |
Output isn't correct |
25 |
Correct |
114 ms |
77948 KB |
Output is correct |
26 |
Correct |
103 ms |
77948 KB |
Output is correct |
27 |
Correct |
96 ms |
77988 KB |
Output is correct |
28 |
Correct |
101 ms |
78796 KB |
Output is correct |
29 |
Correct |
307 ms |
99224 KB |
Output is correct |
30 |
Incorrect |
100 ms |
99224 KB |
Output isn't correct |
31 |
Incorrect |
112 ms |
99224 KB |
Output isn't correct |
32 |
Incorrect |
135 ms |
99224 KB |
Output isn't correct |
33 |
Correct |
90 ms |
99224 KB |
Output is correct |
34 |
Correct |
105 ms |
99224 KB |
Output is correct |
35 |
Correct |
116 ms |
99224 KB |
Output is correct |
36 |
Correct |
99 ms |
99224 KB |
Output is correct |
37 |
Runtime error |
364 ms |
109596 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
38 |
Incorrect |
117 ms |
109596 KB |
Output isn't correct |
39 |
Incorrect |
107 ms |
109596 KB |
Output isn't correct |
40 |
Incorrect |
106 ms |
109596 KB |
Output isn't correct |
41 |
Correct |
118 ms |
109596 KB |
Output is correct |
42 |
Correct |
112 ms |
110032 KB |
Output is correct |
43 |
Correct |
118 ms |
111660 KB |
Output is correct |
44 |
Correct |
122 ms |
111660 KB |
Output is correct |
45 |
Runtime error |
338 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
46 |
Runtime error |
121 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
47 |
Runtime error |
124 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
48 |
Runtime error |
115 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
49 |
Runtime error |
86 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
50 |
Runtime error |
115 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
51 |
Runtime error |
93 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
52 |
Runtime error |
114 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
53 |
Runtime error |
364 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
54 |
Runtime error |
94 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |