This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define mp make_pair
#define f first
#define s second
#define pb push_back
struct DSU{
vi e;
void init(int n){
e = vi(n, -1);
}
int get(int a){
if(e[a] < 0) return a;
e[a] = get(e[a]);
return e[a];
}
bool unite(int a, int b){
a = get(a);
b = get(b);
if(a == b) return 0;
if(-e[a] < -e[b]) swap(a, b);
e[b] = a;
return 1;
}
};
const int mx = 500005;
const int mx1 = 100005;
int N, M;
ll c[mx1];
int u[mx];
int v[mx];
ll x[mx];
bool inTree[mx1];
vpi adj[mx1];
int depth[mx1];
void genTree(int node, int prv = -1, int d = 0){
depth[node] = d;
for(auto u: adj[node]){
if(u.f == prv) continue;
genTree(u.f, node, d+1);
ll val = c[u.f];
x[u.s]+=val;
c[u.f]-=val;
c[node]-=val;
}
}
int main(){
cin >> N >> M;
DSU dsu;
dsu.init(N+5);
for(int i = 1; i <= N; i++){
cin >> c[i];
}
for(int i = 1; i <= M; i++){
cin >> u[i] >> v[i];
if(dsu.unite(u[i], v[i])){
inTree[i] = 1;
adj[u[i]].pb(mp(v[i], i));
adj[v[i]].pb(mp(u[i], i));
}
}
genTree(1);
if(M == N-1){
if(c[1] != 0){
cout << 0 << "\n";
return 0;
}
for(int i = 1; i <= M; i++){
cout << 2*x[i] << "\n";
}
return 0;
}
if(M >= N+1){
cout << 0 << "\n";
return 0;
}
for(int i = 1; i <= M; i++){
if(inTree[i]) continue;
if(depth[u[i]] % 2 != depth[v[i]] % 2){
cout << 0 << "\n";
return 0;
}
}
for(int i = 1; i <= M; i++){
cout << 2*x[i] << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |