답안 #631459

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
631459 2022-08-18T07:09:42 Z TranGiaHuy1508 Pipes (BOI13_pipes) C++17
100 / 100
192 ms 41932 KB
/*
	Unknown's C++ Template (v3.2)
*/

#include "bits/stdc++.h"
using namespace std;

#define int long long

using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vvii = vector<vii>;
template <class T> using maxpq = priority_queue<T>;
template <class T> using minpq = priority_queue<T, vector<T>, greater<T>>;

#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mid ((l+r)>>1)
#define fi first
#define se second

#ifdef LOCAL
	#define debug(x) cout << #x << " = " << x << "\n";
#else
	#define debug(x) ;
#endif

template <class A, class B>
ostream& operator << (ostream& out, pair<A, B> x)
{ out << "(" << x.first << ", " << x.second << ")"; return out; }

template <class T>
ostream& operator << (ostream& out, vector<T> x){
	out << "[";
	for (int i=0; i<sz(x); i++) { out << (i ? ", " : "") << x[i]; }
	out << "]"; return out;
}

template <class T>
istream& operator >> (istream& in, vector<T> &x){
	for (auto &i: x) in >> i;
	return in;
}

const ld PI = acos(-1.0);
const int allmod[3] = {(int)1e9+7, 998244353, (int)1e9+9};
const int mod = allmod[0];
const int maxn = 2e5 + 64;
const ll inf = 1e18;
const ld eps = 1e-6;
const int multitest = 0;

int n, m, root = -1;
vi deg, change, res;
vi pt_order, edge_order;
vii edges;
vvii adj;

void dfs(int x, int p = -1){
	if (x == root && p >= 0) return;
	pt_order.pb(x);

	for (auto k: adj[x]){
		if (k.fi == p) continue;
		if (!deg[k.fi]) continue;
		edge_order.pb(k.se);
		dfs(k.fi, x);
		break;
	}
}

void main_program(){
	cin >> n >> m;

	deg.assign(n, 0); change.resize(n); cin >> change;
	adj.resize(n);
	res.resize(m); edges.resize(m);

	for (int i = 0; i < m; i++){
		int x, y; cin >> x >> y;
		x--; y--;
		deg[x]++; deg[y]++;
		edges[i] = {x, y};
		adj[x].pb({y, i}); adj[y].pb({x, i});
	}

	queue<int> q;
	for (int i = 0; i < n; i++){
		if (deg[i] == 1) q.push(i);
	}

	int r = 0;

	while (!q.empty()){
		int x = q.front(); q.pop();
		for (auto k: adj[x]){
			if (deg[k.fi]){
				res[k.se] += change[x] * 2;
				change[k.fi] -= change[x];
				deg[k.fi]--;

				if (deg[k.fi] == 1) q.push(k.fi);
			}
		}
		change[x] = 0;
		if (deg[x]) deg[x]--;
		r++;
	}

	if ((n == m && ((n - r) & 1)) || (*max_element(all(deg)) == 0)){
		for (int i = 0; i < n; i++){
			if (deg[i] > 0) root = i;
		}

		if (root >= 0){
			dfs(root);

			for (auto i: pt_order){
				deg[i] = 0;
			}

			int tt = 0;
			for (auto i: pt_order) tt += change[i];

			for (int i = 1; i < sz(pt_order); i += 2) tt -= change[pt_order[i]] * 2;

			res[edge_order.back()] = tt;
			for (int i = sz(pt_order) - 1; i >= 1; i--){
				res[edge_order[i-1]] = 2 * change[pt_order[i]] - res[edge_order[i]];
			}
		}

		for (int i = 0; i < m; i++) cout << res[i] << "\n";
	}
	else cout << "0\n";
}

void pre_main(){

}

signed main(){
	#ifdef LOCAL
		auto stime = chrono::high_resolution_clock::now();
	#endif
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	#ifndef ONLINE_JUDGE
		if (fopen(".inp", "r")){
			freopen(".inp", "r", stdin);
			freopen(".out", "w", stdout);
		}
	#endif
	int t = 1; if (multitest) cin >> t;
	pre_main();
	while (t--) main_program();
	#ifdef LOCAL
		auto etime = chrono::high_resolution_clock::now();
		auto duration = chrono::duration_cast<chrono::milliseconds>(etime-stime).count();
		cout << "\n[" << duration << "ms]\n";
	#endif
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:154:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  154 |    freopen(".inp", "r", stdin);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~
pipes.cpp:155:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  155 |    freopen(".out", "w", stdout);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 60 ms 12476 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 45 ms 9944 KB Output is correct
14 Correct 56 ms 11840 KB Output is correct
15 Correct 58 ms 12560 KB Output is correct
16 Correct 50 ms 10704 KB Output is correct
17 Correct 57 ms 12504 KB Output is correct
18 Correct 58 ms 12580 KB Output is correct
19 Correct 61 ms 12176 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 71 ms 12580 KB Output is correct
23 Correct 48 ms 10008 KB Output is correct
24 Correct 74 ms 12532 KB Output is correct
25 Correct 49 ms 10400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 41 ms 11320 KB Output is correct
4 Correct 54 ms 11892 KB Output is correct
5 Correct 52 ms 11688 KB Output is correct
6 Correct 192 ms 41880 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 51 ms 14204 KB Output is correct
24 Correct 64 ms 16588 KB Output is correct
25 Correct 42 ms 11272 KB Output is correct
26 Correct 42 ms 11820 KB Output is correct
27 Correct 42 ms 11596 KB Output is correct
28 Correct 48 ms 12308 KB Output is correct
29 Correct 160 ms 34220 KB Output is correct
30 Correct 76 ms 15632 KB Output is correct
31 Correct 65 ms 19124 KB Output is correct
32 Correct 60 ms 13788 KB Output is correct
33 Correct 47 ms 11964 KB Output is correct
34 Correct 42 ms 11844 KB Output is correct
35 Correct 47 ms 11788 KB Output is correct
36 Correct 56 ms 12116 KB Output is correct
37 Correct 181 ms 41932 KB Output is correct
38 Correct 63 ms 16100 KB Output is correct
39 Correct 57 ms 13180 KB Output is correct
40 Correct 69 ms 16192 KB Output is correct
41 Correct 43 ms 11812 KB Output is correct
42 Correct 39 ms 11628 KB Output is correct
43 Correct 42 ms 11692 KB Output is correct
44 Correct 49 ms 11724 KB Output is correct
45 Correct 142 ms 36420 KB Output is correct
46 Correct 60 ms 15524 KB Output is correct
47 Correct 60 ms 16352 KB Output is correct
48 Correct 69 ms 18896 KB Output is correct
49 Correct 54 ms 11320 KB Output is correct
50 Correct 47 ms 11920 KB Output is correct
51 Correct 45 ms 11960 KB Output is correct
52 Correct 41 ms 11652 KB Output is correct
53 Correct 152 ms 36484 KB Output is correct
54 Correct 70 ms 15228 KB Output is correct