# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
484701 |
2021-11-05T07:33:01 Z |
idas |
Pipes (BOI13_pipes) |
C++17 |
|
0 ms |
0 KB |
#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;
}
}
dfs(l);
assert(all%2==0)
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 getA()':
pipes.cpp:96:5: error: expected ';' before 'x'
96 | x[{in, l}]=x[{l, in}]=all/2-sm;
| ^
pipes.cpp:96:14: error: expected primary-expression before ']' token
96 | x[{in, l}]=x[{l, in}]=all/2-sm;
| ^
pipes.cpp:96:25: error: expected primary-expression before ']' token
96 | x[{in, l}]=x[{l, in}]=all/2-sm;
| ^
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~