답안 #122964

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
122964 2019-06-29T17:23:13 Z SuperJava Pipes (BOI13_pipes) C++17
90.9259 / 100
314 ms 38384 KB
//fold
#ifndef KHALIL
#include <bits/stdc++.h>
#else
#include "header.h"
#endif
#define endl '\n'
#define mp make_pair
#define tostr(x) static_cast<ostringstream&>((ostringstream()<<dec<<x)).str()
#define rep(i,begin,end) for(auto i = begin;i < end;i++)
#define repr(i,begin,end) for(auto i = begin-1;i >= end;i--)
#define pb push_back
#define sz(a) ((int)(a).size())
#define fi first
#define se second
#define abs(a) ((a) < (0) ? (-1)*(a) : (a))
#define SQ(a) ((a)*(a))
#define eqd(a,b) (abs(a-b)<1e-9)
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename t> t in(t q){cin >> q;return q;}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v){os << "[";for (int i = 0; i < sz(v); ++i) { os << v[i]; if (i != sz(v) - 1) os << ",";}os << "]";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const map<T, S>& v){for (auto it : v)os << "(" << it.first << ":" << it.second << ")";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const pair<T, S>& v){os << "(" << v.first << "," << v.second << ")";return os;}
const long double PI = acosl(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
//endfold
#define  N  (200'005)
#define MOD (1'000'000'007ll)
#define OO (1'050'000'000)
#define OOL (1'100'000'000'000'000'000ll)

//global
set<pair<ll,ll>> s;
set<pair<ll,ll>> adj[N];
ll sum[N];
ll val[N];

vector<ll> res;

void dfs(ll u,ll p){
	res.push_back(u);
	for(auto v:adj[u]){
		if(v.first != p && v.first != res[0]){
			dfs(v.first,u);
			return;
		}
	}
}

int main(){
	//fold
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cout << setprecision(10);
	//endfold
	ll n,m;
	cin >> n >> m;
	if(m > n) return cout << 0,0;
	rep(i,1,n+1){
		cin >> sum[i];
	}
	rep(i,0,m){
		ll u,v;
		cin >> u >> v;
		adj[u].insert({v,i});
		adj[v].insert({u,i});
	}
	rep(i,1,n+1){
		if(sz(adj[i])) s.insert({sz(adj[i]),i});
	}
	
	while(sz(s) && s.begin()->first == 1){
		ll u = s.begin()->second;
		s.erase(s.begin());
		ll v = adj[u].begin()->first;
		ll edgide = adj[u].begin()->second;
		s.erase({sz(adj[v]),v});
		adj[u].erase(adj[u].begin());
		adj[v].erase({u,edgide});
		val[edgide] = sum[u];
		sum[u] = 0;
		sum[v] -= val[edgide];
		if(sz(adj[v])) s.insert({sz(adj[v]),v});
	}
	if(sz(s) && s.begin()->first == 2){
		dfs(s.begin()->second,0);
		ll ans = sum[res[0]];
		rep(i,1,sz(res)){
			if(i&1) ans += sum[res[i]];
			else ans -= sum[res[i]];
		}
		ans /= 2;
		ll u = res[0];
		s.erase({sz(adj[u]),u});
		ll v = res[1];
		ll edgide = (*adj[u].lower_bound({v,0})).second;
		s.erase({sz(adj[v]),v});
		adj[u].erase({v,edgide});
		adj[v].erase({u,edgide});
		val[edgide] = ans;
		sum[u] -= val[edgide];
		sum[v] -= val[edgide];
		if(sz(adj[v])) s.insert({sz(adj[v]),v});
	}
	while(sz(s) && s.begin()->first == 1){
		ll u = s.begin()->second;
		s.erase(s.begin());
		ll v = adj[u].begin()->first;
		ll edgide = adj[u].begin()->second;
		s.erase({sz(adj[v]),v});
		adj[u].erase(adj[u].begin());
		adj[v].erase({u,edgide});
		val[edgide] = sum[u];
		sum[u] = 0;
		sum[v] -= val[edgide];
		if(sz(adj[v])) s.insert({sz(adj[v]),v});
	}
	rep(i,0,m){
		cout << val[i]*2 << endl;
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9720 KB Output is correct
2 Correct 13 ms 9848 KB Output is correct
3 Correct 14 ms 9976 KB Output is correct
4 Correct 235 ms 30968 KB Output is correct
5 Correct 10 ms 9720 KB Output is correct
6 Correct 11 ms 9848 KB Output is correct
7 Correct 10 ms 9720 KB Output is correct
8 Correct 10 ms 9848 KB Output is correct
9 Correct 14 ms 9976 KB Output is correct
10 Correct 14 ms 9976 KB Output is correct
11 Correct 11 ms 9976 KB Output is correct
12 Correct 11 ms 9976 KB Output is correct
13 Correct 179 ms 26852 KB Output is correct
14 Correct 229 ms 29816 KB Output is correct
15 Correct 282 ms 31056 KB Output is correct
16 Correct 204 ms 27916 KB Output is correct
17 Correct 314 ms 31100 KB Output is correct
18 Correct 256 ms 31208 KB Output is correct
19 Correct 214 ms 31076 KB Output is correct
20 Correct 10 ms 9720 KB Output is correct
21 Correct 11 ms 9976 KB Output is correct
22 Correct 223 ms 31024 KB Output is correct
23 Correct 182 ms 26744 KB Output is correct
24 Correct 221 ms 31100 KB Output is correct
25 Correct 264 ms 27640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9720 KB Output is correct
2 Correct 11 ms 10044 KB Output is correct
3 Incorrect 229 ms 34040 KB Output isn't correct
4 Correct 10 ms 9720 KB Output is correct
5 Correct 10 ms 9720 KB Output is correct
6 Correct 10 ms 9720 KB Output is correct
7 Correct 10 ms 9848 KB Output is correct
8 Correct 10 ms 9720 KB Output is correct
9 Incorrect 10 ms 9848 KB Output isn't correct
10 Correct 10 ms 9720 KB Output is correct
11 Correct 10 ms 9692 KB Output is correct
12 Correct 9 ms 9720 KB Output is correct
13 Correct 10 ms 9720 KB Output is correct
14 Correct 10 ms 9720 KB Output is correct
15 Correct 14 ms 10108 KB Output is correct
16 Correct 11 ms 9976 KB Output is correct
17 Incorrect 11 ms 10044 KB Output isn't correct
18 Correct 10 ms 9720 KB Output is correct
19 Correct 9 ms 9720 KB Output is correct
20 Correct 10 ms 9720 KB Output is correct
21 Correct 9 ms 9724 KB Output is correct
22 Correct 11 ms 10080 KB Output is correct
23 Correct 180 ms 31664 KB Output is correct
24 Correct 246 ms 35976 KB Output is correct
25 Incorrect 227 ms 34032 KB Output isn't correct
26 Correct 12 ms 9720 KB Output is correct
27 Correct 10 ms 9720 KB Output is correct
28 Correct 10 ms 9724 KB Output is correct
29 Correct 10 ms 9720 KB Output is correct
30 Correct 222 ms 34936 KB Output is correct
31 Correct 240 ms 38380 KB Output is correct
32 Correct 262 ms 33396 KB Output is correct
33 Incorrect 244 ms 36216 KB Output isn't correct
34 Correct 10 ms 9720 KB Output is correct
35 Correct 10 ms 9720 KB Output is correct
36 Correct 9 ms 9720 KB Output is correct
37 Correct 10 ms 9720 KB Output is correct
38 Correct 224 ms 35580 KB Output is correct
39 Correct 243 ms 32732 KB Output is correct
40 Correct 244 ms 35680 KB Output is correct
41 Incorrect 229 ms 38384 KB Output isn't correct
42 Correct 12 ms 9764 KB Output is correct
43 Correct 12 ms 9720 KB Output is correct
44 Correct 12 ms 9720 KB Output is correct
45 Correct 10 ms 9720 KB Output is correct
46 Correct 205 ms 35060 KB Output is correct
47 Correct 237 ms 35700 KB Output is correct
48 Correct 234 ms 38256 KB Output is correct
49 Incorrect 247 ms 31888 KB Output isn't correct
50 Correct 9 ms 9720 KB Output is correct
51 Correct 10 ms 9720 KB Output is correct
52 Correct 10 ms 9720 KB Output is correct
53 Correct 10 ms 9720 KB Output is correct
54 Correct 233 ms 34628 KB Output is correct