이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int int64_t
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e5 + 10;
const int maxm = 5e5 + 10;
vector<int> grafo[maxn];
int n;
int s[maxn];
map<pair<int, int>, int> mapa;
int val[maxn], ans[maxn];
int grau[maxn];
namespace tree{
int nivel[maxn], pai[maxn], soma[maxn];
void dfs(int u, int p = 0) {
pai[u] = p;
for(auto v : grafo[u]) {
if(v == p) continue;
nivel[v] = nivel[u] + 1;
dfs(v, u);
}
}
};
vector<int> ordem;
namespace cycle {
int st;
void solve() {
if(ordem.size() % 2 == 0){
cout << "0\n";
return;
}
int x = 0;
int sig = 1;
for(auto& u : ordem){
x += s[u]*sig;
sig *= -1;
}
x/=2;
ans[mapa[{ordem[0], ordem.back()}]] = x;
// cout << ordem[0] << " " << ordem.back() << ": " << ans[mapa[{ordem[0], ordem.back()}]] << "\n";
for(int i = 0; i < (int)ordem.size() - 1; i++) {
ans[mapa[{ordem[i], ordem[i+1]}]] = s[ordem[i]] - x;
// cout << ordem[i] << " " << ordem[i+1] << ": " << s[ordem[i]] - x << "\n";
x = s[ordem[i]] - x;
}
for(int i=1;i<=n;i++){
cout << ans[i] << "\n";
}
}
};
bool mark[maxn], mark2[maxn];
int dfs(int u, int l = 0) {
mark[u] = true;
mark2[u] = true;
for(auto v : grafo[u]){
if(mark2[v] && v != l) {
mark2[u] = false;
ordem.push_back(u);
return v;
}else if(mark[v]) continue;
int x = dfs(v, u);
if(x != 0){
mark2[u] = false;
ordem.push_back(u);
return (u == x ? 0 : x);
}
}
mark2[u] = false;
return 0;
}
bool test[maxn];
int ciclo[maxn];
void dfs2(int u) {
test[u] = true;
for(auto v : grafo[u]) {
if(test[v] || ciclo[v]) continue;
dfs2(v);
s[u] -= s[v];
}
for(auto v : grafo[u])
if(ciclo[v]) s[v] -= s[u];
}
int32_t main() {
int m;
cin >> n >> m;
if(m > n) {
cout << "0\n";
return 0;
}
int total = 0;
for(int i=1;i<=n;i++)
cin >> s[i], s[i] *= 2, total += s[i];
for(int i=1;i<=m;i++) {
int a, b;
cin >> a >> b;
grafo[a].push_back(b);
grafo[b].push_back(a);
mapa[{a, b}] = i;
mapa[{b, a}] = i;
grau[a]++;
grau[b]++;
}
if(n - 1 == m) {
tree::dfs(1);
vector<pii> arr;
for(int i=1;i<=n;i++)
arr.push_back({tree::nivel[i], i});
sort(arr.begin(), arr.end(), greater<pii>());
for(auto e : arr) {
int v = e.second;
int u = tree::pai[v];
tree::soma[u] += (s[v] - tree::soma[v]);
ans[mapa[{u, v}]] = (s[v] - tree::soma[v]);
}
if(tree::soma[1] != s[1]){
cout << "0\n";
return 0;
}
for(int i=1;i<=m;i++)
cout << ans[i] << "\n";
}else { // n == m
dfs(1);
// for(auto& u : ordem){
// cout << u << "\n";
// }
for(auto& o : ordem)
ciclo[o] = true;
for(int u=1;u<=n;u++) {
if(ciclo[u] || test[u]) continue;
dfs2(u);
}
cycle::st = ordem[0];
cycle::solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |