#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pipii pair<int, pair<int, int>>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
#define out(name) if(fopen(name, "w")) freopen(name, "w", stdout);
#define int long long
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
using namespace std;
long long n, m, c[N], ans[N];
vpi ad[N];
void dfs(int u, int p, int id = 0)
{
long long res = 0;
for (auto [v, pos] : ad[u])
{
if (v == p) continue;
dfs(v, u, pos);
res += ans[pos];
}
ans[id] = c[u] - res;
}
void sub1()
{
dfs(1, 0);
for (int i = 1; i <= m; ++i)
{
cout << 2 * ans[i] << "\n";
}
}
long long del[N], deg[N], ok[N], tmp, x, root, root2, index;
queue<int> q;
void dfs2(int u, int p, int dau)
{
ok[u] = 1;
tmp = tmp + dau * c[u];
for (auto [v, i] : ad[u])
{
if (v == p || del[v])
{
continue;
}
if(ok[v] == 0)
{
dfs2(v, u, dau * (-1));
if (x != 0)
{
return;
}
}
else
{
ans[index] = tmp;
return ;
}
}
}
void dfs3(int u, int p, int pre)
{
ok[u] = 1;
for (auto [v, i] : ad[u])
{
if (v == p || del[v] || ok[v]) continue;
ans[i] = 2 * c[u] - ans[pre];
dfs3(v, u, i);
}
}
void sub2()
{
for (int i = 1; i <= n; ++i)
{
if (deg[i] == 1)
{
q.push(i);
del[i] = 1;
}
}
while (!q.empty())
{
int u = q.front(); q.pop();
del[u] = 1;
for (auto [v, i] : ad[u])
{
if (del[v]) continue;
ans[i] = 2 * c[u];
c[v] -= c[u];
deg[v]--;
if (deg[v] == 1)
{
q.push(v);
}
}
}
long long cnt = 0;
for(int i = 1; i <= n; ++i)
{
if(!del[i])
{
root = i;
cnt++;
}
}
if(cnt % 2 == 0)
{
cout << 0;
return ;
}
for(auto [x, i] : ad[root])
{
if(del[x]) continue ;
root2 = x;
index = i;
break ;
}
ok[root] = 1;
dfs2(root, root2, 1);
memset(ok, 0, sizeof(ok));
dfs3(root, root2, index);
for (int i = 1; i <= m; ++i)
{
cout << ans[i] << "\n";
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
cin >> c[i];
}
for (int i = 1; i <= m; ++i)
{
int u, v;
cin >> u >> v;
ad[u].pb({v, i});
ad[v].pb({u, i});
deg[u]++;
deg[v]++;
}
if (m > n)
{
cout << 0;
return 0;
}
if (m == (n - 1))
{
sub1();
return 0;
}
sub2();
return 0;
}
Compilation message
pipes.cpp:40:55: error: 'long long int index' redeclared as different kind of entity
40 | long long del[N], deg[N], ok[N], tmp, x, root, root2, index;
| ^~~~~
In file included from /usr/include/string.h:432,
from /usr/include/c++/10/cstring:42,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:48,
from pipes.cpp:1:
/usr/include/strings.h:61:1: note: previous declaration 'const char* index(const char*, int)'
61 | index (const char *__s, int __c) __THROW
| ^~~~~
pipes.cpp: In function 'void dfs2(long long int, long long int, long long int)':
pipes.cpp:63:16: error: invalid types 'long long int [1000005][<unresolved overloaded function type>]' for array subscript
63 | ans[index] = tmp;
| ^
pipes.cpp: In function 'void sub2()':
pipes.cpp:128:17: error: overloaded function with no contextual type information
128 | index = i;
| ^
pipes.cpp:135:28: error: cannot resolve overloaded function 'index' based on conversion to type 'long long int'
135 | dfs3(root, root2, index);
| ^