#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], leaf[N];
int S = -1, deg[N], sz = 0;
void markcyc(int v, int u){//cyc u --- v(-backedge-)u
if(h[u] < h[v])swap(u,v);
while(u != v){
cyc[edg[par[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;return;
}
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;
}
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);deg[u] ++, deg[v] ++;
ind[u].pb(i), ind[v].pb(i);
}
if(m == n - 1) solvetree();
////////////////////////////////////////////////////////////////////////////////
pre(0);
for(int i = 0; i < n; i ++)mark[i] = 0;
findcyc(0);
set<pii>s;
for(int i= 0; i < n; i ++)s.insert({deg[i], i});
while(!s.empty()){
auto it = s.begin();
int d = it->X, v = it->Y;
s.erase(s.begin());
if(d > 1)break;
for(int i= 0;i < (int)adj[v].size() ; i ++){
int u= adj[v][i], ed = ind[v][i]; pii pr ={deg[u], u};
if(s.find(pr) != s.end()){
s.erase(pr);deg[u] --;
s.insert({deg[u],u});
ans[ed]= a[v]; a[v] = 0;
a[u]-=ans[ed];
break;
}
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
109 ms |
13548 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
5 ms |
5100 KB |
Output is correct |
8 |
Correct |
5 ms |
5228 KB |
Output is correct |
9 |
Correct |
5 ms |
5100 KB |
Output is correct |
10 |
Correct |
6 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5100 KB |
Output is correct |
12 |
Correct |
5 ms |
5228 KB |
Output is correct |
13 |
Correct |
86 ms |
11756 KB |
Output is correct |
14 |
Correct |
112 ms |
13036 KB |
Output is correct |
15 |
Correct |
112 ms |
13548 KB |
Output is correct |
16 |
Correct |
96 ms |
12268 KB |
Output is correct |
17 |
Correct |
118 ms |
13548 KB |
Output is correct |
18 |
Correct |
121 ms |
13676 KB |
Output is correct |
19 |
Correct |
123 ms |
17132 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
112 ms |
13548 KB |
Output is correct |
23 |
Correct |
87 ms |
11756 KB |
Output is correct |
24 |
Correct |
110 ms |
13548 KB |
Output is correct |
25 |
Correct |
92 ms |
12268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
5356 KB |
Output isn't correct |
3 |
Correct |
110 ms |
18284 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Incorrect |
5 ms |
5100 KB |
Output isn't correct |
8 |
Incorrect |
4 ms |
5100 KB |
Output isn't 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 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
15 |
Incorrect |
5 ms |
5356 KB |
Output isn't correct |
16 |
Incorrect |
6 ms |
5228 KB |
Output isn't correct |
17 |
Correct |
5 ms |
5228 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
5 ms |
5100 KB |
Output is correct |
20 |
Correct |
5 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Incorrect |
5 ms |
5356 KB |
Output isn't correct |
23 |
Incorrect |
168 ms |
22944 KB |
Output isn't correct |
24 |
Incorrect |
236 ms |
25576 KB |
Output isn't correct |
25 |
Correct |
116 ms |
18304 KB |
Output is correct |
26 |
Correct |
4 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 |
4 ms |
5100 KB |
Output is correct |
30 |
Incorrect |
210 ms |
28740 KB |
Output isn't correct |
31 |
Incorrect |
192 ms |
29572 KB |
Output isn't correct |
32 |
Incorrect |
287 ms |
21612 KB |
Output isn't correct |
33 |
Correct |
123 ms |
19820 KB |
Output is correct |
34 |
Correct |
4 ms |
5100 KB |
Output is correct |
35 |
Correct |
4 ms |
5100 KB |
Output is correct |
36 |
Correct |
4 ms |
5100 KB |
Output is correct |
37 |
Correct |
4 ms |
5100 KB |
Output is correct |
38 |
Incorrect |
250 ms |
27756 KB |
Output isn't correct |
39 |
Incorrect |
277 ms |
20844 KB |
Output isn't correct |
40 |
Incorrect |
236 ms |
25064 KB |
Output isn't correct |
41 |
Correct |
130 ms |
23276 KB |
Output is correct |
42 |
Correct |
5 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 |
Incorrect |
229 ms |
30192 KB |
Output isn't correct |
47 |
Incorrect |
287 ms |
25320 KB |
Output isn't correct |
48 |
Incorrect |
199 ms |
29164 KB |
Output isn't correct |
49 |
Correct |
101 ms |
14956 KB |
Output is correct |
50 |
Correct |
4 ms |
5100 KB |
Output is correct |
51 |
Correct |
4 ms |
5100 KB |
Output is correct |
52 |
Correct |
4 ms |
5100 KB |
Output is correct |
53 |
Correct |
4 ms |
5100 KB |
Output is correct |
54 |
Incorrect |
281 ms |
26860 KB |
Output isn't correct |