Submission #631455

# Submission time Handle Problem Language Result Execution time Memory
631455 2022-08-18T06:56:38 Z TranGiaHuy1508 Pipes (BOI13_pipes) C++17
90.9259 / 100
179 ms 42016 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);
	}

	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]--;
	}

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

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

			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:147:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |    freopen(".inp", "r", stdin);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~
pipes.cpp:148:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |    freopen(".out", "w", stdout);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 67 ms 12444 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 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 53 ms 9980 KB Output is correct
14 Correct 60 ms 11860 KB Output is correct
15 Correct 58 ms 12492 KB Output is correct
16 Correct 46 ms 10612 KB Output is correct
17 Correct 54 ms 12504 KB Output is correct
18 Correct 56 ms 12544 KB Output is correct
19 Correct 61 ms 12180 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 58 ms 12520 KB Output is correct
23 Correct 42 ms 9956 KB Output is correct
24 Correct 55 ms 12524 KB Output is correct
25 Correct 44 ms 10536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Incorrect 66 ms 15376 KB Output isn't correct
4 Correct 46 ms 11852 KB Output is correct
5 Correct 46 ms 11760 KB Output is correct
6 Correct 171 ms 41884 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Incorrect 0 ms 212 KB Output isn't correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 456 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Incorrect 1 ms 468 KB Output isn't 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 66 ms 15512 KB Output is correct
24 Correct 61 ms 18096 KB Output is correct
25 Incorrect 68 ms 15228 KB Output isn't correct
26 Correct 59 ms 11936 KB Output is correct
27 Correct 45 ms 11672 KB Output is correct
28 Correct 46 ms 12320 KB Output is correct
29 Correct 141 ms 34252 KB Output is correct
30 Correct 64 ms 17100 KB Output is correct
31 Correct 68 ms 20788 KB Output is correct
32 Correct 70 ms 15360 KB Output is correct
33 Incorrect 61 ms 16816 KB Output isn't correct
34 Correct 41 ms 11880 KB Output is correct
35 Correct 41 ms 11900 KB Output is correct
36 Correct 49 ms 12116 KB Output is correct
37 Correct 179 ms 42016 KB Output is correct
38 Correct 92 ms 17660 KB Output is correct
39 Correct 61 ms 14792 KB Output is correct
40 Correct 63 ms 17788 KB Output is correct
41 Incorrect 63 ms 19208 KB Output isn't correct
42 Correct 40 ms 11632 KB Output is correct
43 Correct 43 ms 11608 KB Output is correct
44 Correct 50 ms 11636 KB Output is correct
45 Correct 134 ms 36468 KB Output is correct
46 Correct 73 ms 17084 KB Output is correct
47 Correct 67 ms 17880 KB Output is correct
48 Correct 66 ms 20380 KB Output is correct
49 Incorrect 67 ms 12748 KB Output isn't correct
50 Correct 46 ms 11852 KB Output is correct
51 Correct 46 ms 11980 KB Output is correct
52 Correct 43 ms 11696 KB Output is correct
53 Correct 158 ms 36456 KB Output is correct
54 Correct 85 ms 16616 KB Output is correct