#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()
#define F first
#define S second
#define PB push_back
#define MP make_pair
using namespace std;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pff;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;
const int INF=1e9, MOD=1e9+7;
void setIO()
{
FAST_IO;
}
void setIO(string s)
{
FAST_IO;
freopen((s+".in").c_str(), "r", stdin);
freopen((s+".out").c_str(), "w", stdout);
}
const int N=5e5+10;
int n, m;
ll c[N], non[N];
vi ad[N];
bool v[N], vc[N];
map<pii, ll> x;
map<int, pii> con;
queue<int> q;
void bfs()
{
while(!q.empty()){
int u=q.front(); q.pop();
if(v[u]) continue;
v[u]=true;
int cn=0;
for(auto it : ad[u]){
if(!x.count({it, u})) cn++;
}
if(cn>=2){
v[u]=false;
continue;
}
int in=-1;
ll sm=0;
for(auto it : ad[u]){
if(!x.count({it, u})) in=it;
sm+=x[{it, u}];
}
q.push(in);
x[{in, u}]=x[{u, in}]=2LL*c[u]-sm;
}
}
int inn;
ll sm, all;
void dfs(int u)
{
v[u]=true; vc[u]=true;
if(inn&1){
sm+=2*c[u]-non[u];
}
all+=2*c[u]-non[u];
inn++;
for(auto it : ad[u]){
if(v[it]) continue;
dfs(it);
}
}
int in;
void getA()
{
int l=-1;
for(auto it : ad[in]){
if(!v[it]){
if(l==-1) l=it;
else break;
}
}
assert(l!=-1);
dfs(l);
x[{in, l}]=x[{l, in}]=all/2-sm;
}
void build(int u)
{
vc[u]=false;
ll have=0;
for(auto it : ad[u]){
if(x.count({u, it})) have+=x[{u, it}];
}
for(auto it : ad[u]){
if(!x.count({u, it})){
x[{u, it}]=x[{it, u}]=2LL*c[u]-have;
}
}
for(auto it : ad[u]){
if(!vc[it]) continue;
build(it);
}
}
void getNon()
{
FOR(i, 0, n)
{
if(!v[i]){
for(auto it : ad[i]){
if(v[it]){
non[i]+=x[{i, it}];
}
}
}
}
}
int main()
{
setIO();
cin >> n >> m;
FOR(i, 0, n) cin >> c[i];
FOR(i, 0, m)
{
int a, b; cin >> a >> b; a--; b--;
ad[a].PB(b);
ad[b].PB(a);
con[i]={a, b};
}
if(n<m){
cout << 0;
return 0;
}
vi lf;
FOR(i, 0, n) if(ad[i].size()==1) lf.PB(i);
for(auto u : lf){
q.push(u);
}
bfs();
getNon();
FOR(i, 0, n)
{
if(!v[i]){
in=i;
}
}
if(in==-1){
FOR(i, 0, m)
{
cout << x[con[i]] << '\n';
}
return 0;
}
getA();
build(in);
FOR(i, 0, m)
{
cout << x[con[i]] << '\n';
}
}
Compilation message
pipes.cpp: In function 'void setIO(std::string)':
pipes.cpp:29:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen((s+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pipes.cpp:30:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen((s+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
15 ms |
24396 KB |
Execution killed with signal 6 |
2 |
Runtime error |
16 ms |
24296 KB |
Execution killed with signal 6 |
3 |
Runtime error |
16 ms |
24780 KB |
Execution killed with signal 6 |
4 |
Runtime error |
343 ms |
70980 KB |
Execution killed with signal 6 |
5 |
Runtime error |
15 ms |
24268 KB |
Execution killed with signal 6 |
6 |
Runtime error |
16 ms |
24344 KB |
Execution killed with signal 6 |
7 |
Runtime error |
15 ms |
24396 KB |
Execution killed with signal 6 |
8 |
Runtime error |
15 ms |
24396 KB |
Execution killed with signal 6 |
9 |
Runtime error |
16 ms |
24780 KB |
Execution killed with signal 6 |
10 |
Runtime error |
16 ms |
24692 KB |
Execution killed with signal 6 |
11 |
Runtime error |
16 ms |
24808 KB |
Execution killed with signal 6 |
12 |
Runtime error |
16 ms |
24760 KB |
Execution killed with signal 6 |
13 |
Runtime error |
246 ms |
61516 KB |
Execution killed with signal 6 |
14 |
Runtime error |
337 ms |
68532 KB |
Execution killed with signal 6 |
15 |
Runtime error |
361 ms |
71176 KB |
Execution killed with signal 6 |
16 |
Runtime error |
282 ms |
64072 KB |
Execution killed with signal 6 |
17 |
Runtime error |
394 ms |
70976 KB |
Execution killed with signal 6 |
18 |
Runtime error |
367 ms |
71336 KB |
Execution killed with signal 6 |
19 |
Runtime error |
234 ms |
70636 KB |
Execution killed with signal 6 |
20 |
Runtime error |
15 ms |
24396 KB |
Execution killed with signal 6 |
21 |
Runtime error |
19 ms |
24780 KB |
Execution killed with signal 6 |
22 |
Runtime error |
347 ms |
71120 KB |
Execution killed with signal 6 |
23 |
Runtime error |
255 ms |
61416 KB |
Execution killed with signal 6 |
24 |
Runtime error |
334 ms |
71232 KB |
Execution killed with signal 6 |
25 |
Runtime error |
289 ms |
63304 KB |
Execution killed with signal 6 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12108 KB |
Output is correct |
2 |
Correct |
7 ms |
12364 KB |
Output is correct |
3 |
Incorrect |
286 ms |
41156 KB |
Output isn't correct |
4 |
Correct |
59 ms |
22220 KB |
Output is correct |
5 |
Correct |
58 ms |
21952 KB |
Output is correct |
6 |
Correct |
232 ms |
53148 KB |
Output is correct |
7 |
Correct |
6 ms |
12108 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Incorrect |
6 ms |
12108 KB |
Output isn't correct |
10 |
Correct |
7 ms |
12044 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
6 ms |
11980 KB |
Output is correct |
13 |
Correct |
6 ms |
12016 KB |
Output is correct |
14 |
Correct |
6 ms |
12108 KB |
Output is correct |
15 |
Correct |
7 ms |
12440 KB |
Output is correct |
16 |
Correct |
7 ms |
12236 KB |
Output is correct |
17 |
Incorrect |
7 ms |
12364 KB |
Output isn't correct |
18 |
Correct |
6 ms |
12108 KB |
Output is correct |
19 |
Correct |
6 ms |
12080 KB |
Output is correct |
20 |
Correct |
6 ms |
12108 KB |
Output is correct |
21 |
Correct |
7 ms |
12236 KB |
Output is correct |
22 |
Correct |
8 ms |
12272 KB |
Output is correct |
23 |
Correct |
218 ms |
38468 KB |
Output is correct |
24 |
Correct |
293 ms |
42864 KB |
Output is correct |
25 |
Incorrect |
281 ms |
41216 KB |
Output isn't correct |
26 |
Correct |
58 ms |
22212 KB |
Output is correct |
27 |
Correct |
60 ms |
22104 KB |
Output is correct |
28 |
Correct |
62 ms |
22752 KB |
Output is correct |
29 |
Correct |
199 ms |
45224 KB |
Output is correct |
30 |
Correct |
257 ms |
42052 KB |
Output is correct |
31 |
Correct |
263 ms |
48068 KB |
Output is correct |
32 |
Correct |
345 ms |
38860 KB |
Output is correct |
33 |
Incorrect |
290 ms |
43228 KB |
Output isn't correct |
34 |
Correct |
64 ms |
22212 KB |
Output is correct |
35 |
Correct |
64 ms |
22208 KB |
Output is correct |
36 |
Correct |
62 ms |
22340 KB |
Output is correct |
37 |
Correct |
228 ms |
53224 KB |
Output is correct |
38 |
Correct |
265 ms |
41924 KB |
Output is correct |
39 |
Correct |
353 ms |
37764 KB |
Output is correct |
40 |
Correct |
297 ms |
42304 KB |
Output is correct |
41 |
Incorrect |
275 ms |
48048 KB |
Output isn't correct |
42 |
Correct |
58 ms |
22220 KB |
Output is correct |
43 |
Correct |
60 ms |
22136 KB |
Output is correct |
44 |
Correct |
58 ms |
21932 KB |
Output is correct |
45 |
Correct |
194 ms |
48020 KB |
Output is correct |
46 |
Correct |
257 ms |
41412 KB |
Output is correct |
47 |
Correct |
286 ms |
42568 KB |
Output is correct |
48 |
Correct |
276 ms |
47684 KB |
Output is correct |
49 |
Incorrect |
332 ms |
37020 KB |
Output isn't correct |
50 |
Correct |
58 ms |
22212 KB |
Output is correct |
51 |
Correct |
59 ms |
22228 KB |
Output is correct |
52 |
Correct |
60 ms |
21980 KB |
Output is correct |
53 |
Correct |
197 ms |
48888 KB |
Output is correct |
54 |
Correct |
271 ms |
41032 KB |
Output is correct |