#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
#define X first
#define Y second
#define pb push_back
#define fastio ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#define rndom mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define endl '\n'
//#define int long long
const int N = 100 * 1000 + 20, L = 20, M = 100 * 1000 + 20;////////////////ya5e5
int n, m, c[N], ans[M], a[N], h[N], par[N];
vector<int>adj[N], ind[N];
int edg[N];
void dfst(int v, int p){
int id = -1;
for(int i = 0; i < (int)adj[v].size(); i ++){
int u = adj[v][i], ed = ind[v][i];
if(u == p){id = ed; continue;}
dfst(u, v);
a[v] -= ans[ed];
}
if(id!= -1)ans[id] = a[v], a[v] = 0;
}
void solvetree(){
dfst(0, -1);
for(int i = 0; i < n; i ++)if(a[i] != 0){cout << 0 << endl; exit(0);}
for(int i = 0; i < m; i ++)cout << ans[i] << endl;
exit(0);
}
bool mark[N], dead[M], cyc[M], seen[N];
int S = -1, sz = 0;
void markcyc(int v, int u){//cyc u --- v(-backedge-)u
if(h[u] < h[v])swap(u,v);
seen[v] = 1;
while(u != v){
seen[u] = 1;
cyc[edg[u]] = 1;
u = par[u];
}return;
}
void pre(int v, int id = -1){
mark[v] = 1;
for(int i = 0; i < (int)adj[v].size() ; i ++){
int u = adj[v][i], ed = ind[v][i];
//cout << "ADJ " << u << " VIA " << ed << endl;
if(ed == id)continue;
if(!mark[u]){
par[u] = v;
h[u] = h[v] + 1;
pre(u, ed);
}
}
edg[v] = id;
}
void findcyc(int v, int id = -1){
// cout << "HERE " << v << " FROM " << id << endl;
mark[v] = 1;
for(int i = 0; i < (int)adj[v].size() ; i ++){
int u = adj[v][i], ed = ind[v][i];
//cout << "ADJ " << u << " VIA " << ed << endl;
if(ed == id)continue;
if(mark[u]){
if((h[v] - h[u])%2){
// cout << "h" <<v << " -h" << u << " = " << h[v] -h[u] << endl;
cout << 0 << endl;
exit(0);
}
cyc[ed] = 1;
S=v;sz = abs(h[u]-h[v])+1;
markcyc(v,u);
}else{
par[u] = v;
h[u] = h[v] + 1;
findcyc(u, ed);
}
}
return;
}
void dfs(int v, int id = -1){
mark[v] = 1;
for(int i = 0; i < (int)adj[v].size(); i ++){
int u = adj[v][i], ed = ind[v][i];
if(!mark[u])dfs(u,ed);
a[v] -= ans[ed];
}
if(id != -1 && !seen[v])ans[id] = a[v], a[v] = 0;
return;
}
int32_t main(){
fastio;
///auto t = clock();
cin >> n >> m;
if(m > n) return cout << 0 << endl, 0;
for(int i = 0; i < n; i ++)cin >> c[i], a[i] = c[i]*2;
for(int i = 0; i < m; i ++){
int u, v; cin >> u >> v;
u --; v --;
adj[u].pb(v), adj[v].pb(u);
ind[u].pb(i), ind[v].pb(i);
}
if(m == n - 1) solvetree();
////////////////////////////////////////////////////////////////////////////////
/// \brief findcyc
pre(0);
for(int i = 0; i < n; i ++)mark[i] = 0;
findcyc(0);int root = 0;
for(int i = 0;i < n; i ++){mark[i] = 0; if((int)adj[i].size() != 1)root = i;}
dfs(root);
vector<int>vec;
int cur = S, pr = -1;
for(int t = 0; t < sz; t ++){
for(int i = 0; i < (int)adj[cur].size(); i ++){
int u = adj[cur][i], ed =ind[cur][i];
if(cyc[ed] && u != pr){
vec.pb(ed);
if(pr == -1){
ans[ed] = 0;
}else{
ans[ed] = a[cur];
a[cur]=0;
a[u]-=ans[ed];
}pr= cur; cur = u;goto hell;
}
}
hell:;
}
//cout << "S : " << S << ": " << a[S] << endl;
int df = a[S]/2;
ans[vec[0]] = df;
for(int i = 1; i < (int)vec.size(); i ++){ans[vec[i]] -= df; df*=-1;}
for(int i = 0; i < m; i ++)cout << ans[i] << '\n';
///cout << clock() - t << "ms" << endl;
return 0;
}
/*13 13
3 7 6 8 -2 -4 12 -5 17 -6 1 0 3
1 2
2 3
3 5
5 9
9 8
5 13
13 10
13 7
7 11
11 12
11 6
7 4
4 3*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
5 ms |
5100 KB |
Output is correct |
3 |
Correct |
6 ms |
5228 KB |
Output is correct |
4 |
Correct |
112 ms |
14700 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
5 ms |
5100 KB |
Output is correct |
9 |
Correct |
6 ms |
5104 KB |
Output is correct |
10 |
Correct |
5 ms |
5228 KB |
Output is correct |
11 |
Correct |
6 ms |
5228 KB |
Output is correct |
12 |
Correct |
5 ms |
5228 KB |
Output is correct |
13 |
Correct |
85 ms |
12780 KB |
Output is correct |
14 |
Correct |
105 ms |
14188 KB |
Output is correct |
15 |
Correct |
117 ms |
14864 KB |
Output is correct |
16 |
Correct |
90 ms |
13344 KB |
Output is correct |
17 |
Correct |
109 ms |
14700 KB |
Output is correct |
18 |
Correct |
121 ms |
14828 KB |
Output is correct |
19 |
Correct |
121 ms |
18412 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5228 KB |
Output is correct |
22 |
Correct |
119 ms |
14956 KB |
Output is correct |
23 |
Correct |
81 ms |
12780 KB |
Output is correct |
24 |
Correct |
110 ms |
14700 KB |
Output is correct |
25 |
Correct |
91 ms |
13164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5100 KB |
Output is correct |
2 |
Correct |
5 ms |
5356 KB |
Output is correct |
3 |
Correct |
111 ms |
19308 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
5 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
6 ms |
5368 KB |
Output is correct |
16 |
Correct |
5 ms |
5228 KB |
Output is correct |
17 |
Correct |
5 ms |
5228 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Incorrect |
5 ms |
5228 KB |
Output isn't correct |
23 |
Correct |
137 ms |
20260 KB |
Output is correct |
24 |
Correct |
172 ms |
22148 KB |
Output is correct |
25 |
Correct |
111 ms |
19308 KB |
Output is correct |
26 |
Correct |
5 ms |
5100 KB |
Output is correct |
27 |
Correct |
4 ms |
5100 KB |
Output is correct |
28 |
Correct |
4 ms |
5100 KB |
Output is correct |
29 |
Correct |
5 ms |
5100 KB |
Output is correct |
30 |
Correct |
179 ms |
25576 KB |
Output is correct |
31 |
Correct |
182 ms |
25956 KB |
Output is correct |
32 |
Correct |
155 ms |
18284 KB |
Output is correct |
33 |
Correct |
122 ms |
21100 KB |
Output is correct |
34 |
Correct |
4 ms |
5100 KB |
Output is correct |
35 |
Correct |
4 ms |
5100 KB |
Output is correct |
36 |
Correct |
5 ms |
5100 KB |
Output is correct |
37 |
Correct |
6 ms |
5100 KB |
Output is correct |
38 |
Correct |
184 ms |
24436 KB |
Output is correct |
39 |
Incorrect |
148 ms |
17388 KB |
Output isn't correct |
40 |
Incorrect |
175 ms |
21868 KB |
Output isn't correct |
41 |
Correct |
131 ms |
24556 KB |
Output is correct |
42 |
Correct |
4 ms |
5100 KB |
Output is correct |
43 |
Correct |
4 ms |
5100 KB |
Output is correct |
44 |
Correct |
4 ms |
5100 KB |
Output is correct |
45 |
Correct |
4 ms |
5100 KB |
Output is correct |
46 |
Correct |
175 ms |
26728 KB |
Output is correct |
47 |
Correct |
186 ms |
21872 KB |
Output is correct |
48 |
Correct |
182 ms |
25700 KB |
Output is correct |
49 |
Correct |
105 ms |
16296 KB |
Output is correct |
50 |
Correct |
4 ms |
5100 KB |
Output is correct |
51 |
Correct |
4 ms |
5228 KB |
Output is correct |
52 |
Correct |
4 ms |
5100 KB |
Output is correct |
53 |
Correct |
4 ms |
5100 KB |
Output is correct |
54 |
Correct |
175 ms |
23532 KB |
Output is correct |