#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=5e5+50;
const int mod=1e9+7;
using namespace std;
int n,m,i,ex[nmax],ey[nmax],deg[nmax],vl[nmax],vl1[nmax],rs[nmax],viz[nmax],g[nmax],cur,x,y,st;
vector<int>vc,a[nmax];
queue<int>q;
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>m;
if(m>n)rc(0);
for(i=1;i<=n;i++)cin>>g[i];
for(i=1;i<=m;i++)
{
cin>>ex[i]>>ey[i];
a[ex[i]].pb(i);
a[ey[i]].pb(i);
}
for(i=1;i<=n;i++)
{
deg[i]=(int)a[i].size();
if(deg[i]==1)q.push(i),viz[i]=1;
}
while(!q.empty())
{
x=q.front();
q.pop();
for(i=0;i<(int)a[x].size();i++)
{
y=ex[a[x][i]]^ey[a[x][i]]^x;
if(!deg[y])continue;
rs[a[x][i]]=2*(g[x]-vl[x]);
vl[y]+=g[x]-vl[x];
vl[x]=g[x];
deg[y]--;
deg[x]--;
if(deg[y]==1)
{
viz[y]=1;
q.push(y);
}
}
}
for(i=1;i<=n;i++)if(deg[i])x=i;
st=x;
if(st)
{
while(1)
{
for(i=0;i<(int)a[x].size();i++)
{
y=ex[a[x][i]]^ey[a[x][i]]^x;
if(!deg[y])continue;
vc.pb(a[x][i]);
x=y;
break;
}
if(st==x)break;
}
if((int)vc.size()&1)rc(0);
for(i=1;i<=n;i++)vl1[i]=vl[i];
x=st;
for(i=0;i<(int)vc.size();i++)
{
y=ex[vc[i]]^ey[vc[i]]^x;
vl1[y]+=g[x]-vl1[x];
vl1[x]=g[x];
x=y;
}
cur=(g[x]-vl1[x])/2;
x=st;
for(i=0;i<(int)vc.size();i++)
{
y=ex[vc[i]]^ey[vc[i]]^x;
cur+=g[x]-vl[x];
vl[y]+=cur;
vl[x]+=cur;
rs[vc[i]]=2*cur;
cur=0;
x=y;
}
}
for(i=1;i<=m;i++)cout<<rs[i]<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
12 ms |
12152 KB |
Output is correct |
3 |
Correct |
13 ms |
12152 KB |
Output is correct |
4 |
Correct |
88 ms |
19036 KB |
Output is correct |
5 |
Correct |
13 ms |
12152 KB |
Output is correct |
6 |
Correct |
13 ms |
12152 KB |
Output is correct |
7 |
Correct |
12 ms |
12152 KB |
Output is correct |
8 |
Correct |
12 ms |
12152 KB |
Output is correct |
9 |
Correct |
14 ms |
12152 KB |
Output is correct |
10 |
Correct |
15 ms |
12152 KB |
Output is correct |
11 |
Correct |
14 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
76 ms |
17748 KB |
Output is correct |
14 |
Correct |
81 ms |
18808 KB |
Output is correct |
15 |
Correct |
87 ms |
19160 KB |
Output is correct |
16 |
Correct |
73 ms |
18040 KB |
Output is correct |
17 |
Correct |
88 ms |
19036 KB |
Output is correct |
18 |
Correct |
91 ms |
19164 KB |
Output is correct |
19 |
Correct |
98 ms |
18812 KB |
Output is correct |
20 |
Correct |
12 ms |
12152 KB |
Output is correct |
21 |
Correct |
14 ms |
12152 KB |
Output is correct |
22 |
Correct |
90 ms |
19160 KB |
Output is correct |
23 |
Correct |
68 ms |
17784 KB |
Output is correct |
24 |
Correct |
91 ms |
19160 KB |
Output is correct |
25 |
Correct |
72 ms |
17996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
301 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Runtime error |
295 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Runtime error |
336 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
4 |
Correct |
13 ms |
12024 KB |
Output is correct |
5 |
Correct |
13 ms |
12024 KB |
Output is correct |
6 |
Correct |
13 ms |
12024 KB |
Output is correct |
7 |
Runtime error |
294 ms |
131080 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Runtime error |
315 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
9 |
Runtime error |
295 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
10 |
Correct |
12 ms |
12024 KB |
Output is correct |
11 |
Correct |
13 ms |
12028 KB |
Output is correct |
12 |
Correct |
12 ms |
12024 KB |
Output is correct |
13 |
Correct |
12 ms |
12152 KB |
Output is correct |
14 |
Runtime error |
312 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
15 |
Runtime error |
290 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
16 |
Runtime error |
295 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
17 |
Runtime error |
291 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
18 |
Correct |
12 ms |
12024 KB |
Output is correct |
19 |
Correct |
12 ms |
12024 KB |
Output is correct |
20 |
Correct |
13 ms |
12024 KB |
Output is correct |
21 |
Correct |
12 ms |
12024 KB |
Output is correct |
22 |
Runtime error |
291 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
23 |
Runtime error |
331 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
24 |
Runtime error |
350 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
25 |
Runtime error |
347 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
26 |
Correct |
12 ms |
12024 KB |
Output is correct |
27 |
Correct |
12 ms |
12024 KB |
Output is correct |
28 |
Correct |
13 ms |
12024 KB |
Output is correct |
29 |
Correct |
12 ms |
12024 KB |
Output is correct |
30 |
Runtime error |
346 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
31 |
Runtime error |
337 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
32 |
Runtime error |
351 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
33 |
Runtime error |
342 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
34 |
Correct |
13 ms |
12024 KB |
Output is correct |
35 |
Correct |
12 ms |
12024 KB |
Output is correct |
36 |
Correct |
12 ms |
12028 KB |
Output is correct |
37 |
Correct |
12 ms |
12024 KB |
Output is correct |
38 |
Runtime error |
346 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
39 |
Runtime error |
360 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
40 |
Runtime error |
345 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
41 |
Runtime error |
350 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
42 |
Correct |
12 ms |
12024 KB |
Output is correct |
43 |
Correct |
12 ms |
12024 KB |
Output is correct |
44 |
Correct |
12 ms |
12024 KB |
Output is correct |
45 |
Correct |
12 ms |
12056 KB |
Output is correct |
46 |
Runtime error |
350 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
47 |
Runtime error |
341 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
48 |
Runtime error |
341 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
49 |
Runtime error |
349 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
50 |
Correct |
13 ms |
12184 KB |
Output is correct |
51 |
Correct |
12 ms |
12024 KB |
Output is correct |
52 |
Correct |
12 ms |
12152 KB |
Output is correct |
53 |
Correct |
13 ms |
12024 KB |
Output is correct |
54 |
Runtime error |
353 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |