#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e5,M=5e5;
int val[N+10];
vector<pair<int,int>> e[N+10];
bool vis[N+10];
bool on_c[N+10];
vector<int> stck;
int ans[M+10];
int pref[N+10];
int ss=0;
void on_cycle(int x)
{
vis[x]=true;
stck.push_back(x);
for(auto v:e[x])
{
if(!vis[v.fi])
on_cycle(v.fi);
else if(!on_c[x] && stck.size()>=2 && stck[stck.size()-2]!=v.fi)
{
for(size_t i=stck.size()-1;stck[i]!=v.fi;i--)
on_c[stck[i]]=true;
on_c[v.fi]=true;
}
}
stck.pop_back();
}
void solve_trees(int x,int p)
{
for(auto v:e[x])
{
if(on_c[v.fi] || v.fi==p)
continue;
solve_trees(v.fi,x);
ans[v.se]=2*val[v.fi];
val[x]-=val[v.fi];
}
return;
}
void get_pref(int x,int p,int p0)
{
for(auto v:e[x])
{
if(!on_c[v.fi] || v.fi==p || v.fi==p0)
continue;
pref[v.fi]=val[v.fi]-pref[x];
get_pref(v.fi,x,p0);
return;
}
ss=pref[x];
return;
}
void solve_cycle(int x,int p,int p0,bool sgn_x) // true -> +, false -> -
{
for(auto v:e[x])
{
if(!on_c[v.fi] || v.fi==p)
continue;
if(v.fi==p0)
{
ans[v.se]=ss;
return;
}
if(sgn_x)
ans[v.se]=-ss+2*pref[x];
else
ans[v.se]=ss+2*pref[x];
solve_cycle(v.fi,x,p0,!sgn_x);
return;
}
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>val[i];
for(int i=1;i<=m;i++)
{
int a,b;
cin>>a>>b;
e[a].emplace_back(b,i);
e[b].emplace_back(a,i);
}
if(m>n)
{
cout<<"0\n";
return 0;
}
if(m<n)
solve_trees(1,0);
else
{
on_cycle(1);
int cnt_c=0,cc=0;
for(int i=1;i<=n;i++)
{
if(on_c[i])
{
solve_trees(i,0);
cnt_c++;
cc=i;
}
}
if(cnt_c%2==0)
{
cout<<"0\n";
return 0;
}
pref[cc]=val[cc];
get_pref(cc,0,cc);
solve_cycle(cc,0,cc,true);
}
for(int i=1;i<=m;i++)
cout<<ans[i]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
62 ms |
9176 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2684 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
3 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
53 ms |
7820 KB |
Output is correct |
14 |
Correct |
87 ms |
8776 KB |
Output is correct |
15 |
Correct |
72 ms |
9100 KB |
Output is correct |
16 |
Correct |
60 ms |
8140 KB |
Output is correct |
17 |
Correct |
71 ms |
9156 KB |
Output is correct |
18 |
Correct |
62 ms |
9156 KB |
Output is correct |
19 |
Correct |
75 ms |
12412 KB |
Output is correct |
20 |
Correct |
2 ms |
2668 KB |
Output is correct |
21 |
Correct |
3 ms |
2636 KB |
Output is correct |
22 |
Correct |
66 ms |
9144 KB |
Output is correct |
23 |
Correct |
51 ms |
7748 KB |
Output is correct |
24 |
Correct |
64 ms |
9124 KB |
Output is correct |
25 |
Correct |
55 ms |
8080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2764 KB |
Output is correct |
3 |
Correct |
74 ms |
11368 KB |
Output is correct |
4 |
Correct |
47 ms |
7792 KB |
Output is correct |
5 |
Correct |
46 ms |
8060 KB |
Output is correct |
6 |
Correct |
179 ms |
23036 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
3 ms |
2688 KB |
Output is correct |
13 |
Correct |
2 ms |
2680 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2764 KB |
Output is correct |
16 |
Correct |
2 ms |
2764 KB |
Output is correct |
17 |
Correct |
2 ms |
2696 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
3 ms |
2764 KB |
Output is correct |
22 |
Correct |
3 ms |
2764 KB |
Output is correct |
23 |
Correct |
57 ms |
11712 KB |
Output is correct |
24 |
Correct |
72 ms |
13000 KB |
Output is correct |
25 |
Correct |
65 ms |
11348 KB |
Output is correct |
26 |
Correct |
42 ms |
7924 KB |
Output is correct |
27 |
Correct |
50 ms |
7752 KB |
Output is correct |
28 |
Correct |
47 ms |
8260 KB |
Output is correct |
29 |
Correct |
154 ms |
19324 KB |
Output is correct |
30 |
Correct |
77 ms |
14960 KB |
Output is correct |
31 |
Correct |
92 ms |
15092 KB |
Output is correct |
32 |
Correct |
82 ms |
10820 KB |
Output is correct |
33 |
Correct |
66 ms |
12352 KB |
Output is correct |
34 |
Correct |
46 ms |
7844 KB |
Output is correct |
35 |
Correct |
47 ms |
7852 KB |
Output is correct |
36 |
Correct |
47 ms |
8148 KB |
Output is correct |
37 |
Correct |
209 ms |
23092 KB |
Output is correct |
38 |
Correct |
77 ms |
14272 KB |
Output is correct |
39 |
Correct |
65 ms |
10364 KB |
Output is correct |
40 |
Correct |
74 ms |
12816 KB |
Output is correct |
41 |
Correct |
62 ms |
14268 KB |
Output is correct |
42 |
Correct |
46 ms |
7752 KB |
Output is correct |
43 |
Correct |
42 ms |
7676 KB |
Output is correct |
44 |
Correct |
43 ms |
8024 KB |
Output is correct |
45 |
Correct |
155 ms |
20408 KB |
Output is correct |
46 |
Correct |
77 ms |
15676 KB |
Output is correct |
47 |
Correct |
78 ms |
12868 KB |
Output is correct |
48 |
Correct |
83 ms |
14908 KB |
Output is correct |
49 |
Correct |
53 ms |
9540 KB |
Output is correct |
50 |
Correct |
51 ms |
7892 KB |
Output is correct |
51 |
Correct |
47 ms |
8056 KB |
Output is correct |
52 |
Correct |
46 ms |
7768 KB |
Output is correct |
53 |
Correct |
166 ms |
20420 KB |
Output is correct |
54 |
Correct |
78 ms |
13800 KB |
Output is correct |