Submission #49009

# Submission time Handle Problem Language Result Execution time Memory
49009 2018-05-21T06:44:46 Z Namnamseo Pipes (BOI13_pipes) C++17
100 / 100
146 ms 55080 KB
#include <bits/stdc++.h>
using namespace std;
#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define coord_comp(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define v_index(v, x) (lower_bound(all(v),x)-(v).begin())
typedef pair<int,int> pp;
typedef long long ll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T1,typename T2>
void read(pair<T1,T2>& p){ read(p.first); read(p.second); }
template<typename T,typename... Args>
void read(T&a,Args&...b){ read(a); read(b...); }

int n,m;
int net[100010];
int net_orig[100010];
vector<pp> edge[100010];
ll ans[100010];
int deg[100010];

bool validate(){
	int cur[n+1];
	for(int i=1; i<=n; ++i) cur[i]=0;
	for(int i=1; i<=n; ++i){
		for(auto& pi:edge[i]){
			cur[i] += ans[pi.second];
		}
		if(cur[i] != net_orig[i]) return false;
	}
	return true;
}

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

	int vises = 0;
	while(ones.size()){
		int x=ones.front(); ones.pop();
		++vises;
		if(deg[x] != 1) continue;
		--deg[x];
		for(auto& pi:edge[x]){
			int y, ei;
			tie(y, ei)=pi;
			if(deg[y] == 0) continue;
			--deg[y];
			if(deg[y] == 1) ones.push(y);
			ans[ei] = net[x];
			net[y] -= net[x];
		}
	}

	return vises;
}

int nxtA[100001];
int nxtB[100001];

int adjS[100001];
int adjP[100001];

int hist[100001];
int nxti[100001];

int main(){
	read(n,m);
	if(m>n){
		puts("0");
		return 0;
	}
	for(int i=1; i<=n; ++i) read(net[i]), net_orig[i]=net[i];
	for(int i=0; i<m; ++i){
		int a,b; read(a,b);
		edge[a].pb({b,i});
		edge[b].pb({a,i});
		++deg[a]; ++deg[b];
	}

	int one_rem = getOnes();
	if(n == one_rem){
		for(int i=0; i<m; ++i) printf("%lld\n", ans[i]*2);
		return 0;
	}

	int sp = -1;
	for(int i=1; i<=n; ++i) if(deg[i]){
		for(auto& pi:edge[i]){
			int y=pi.first;
			if(deg[y]){
				adjS[i] += y;
				adjP[i] = y;
			}
		}
		sp=i;
	}

	vector<int> hist, hii;
	for(int i=sp, j=adjP[sp];;){
		hist.pb(i);
		for(auto& pi : edge[i]) if(pi.first == j){
			hii.pb(pi.second);
		}
		int tmp = i;
		i = j;
		j = adjS[j] - tmp;
		if(i == sp) break;
	}

	if(hist.size() != hii.size()) for(;;);
	int h = hist.size();

	if((n-one_rem) % 2 == 1){
		nxtA[0]=1; nxtB[0]=0;
		for(int i=1; i<h; ++i){
			nxtA[i] = -nxtA[i-1];
			nxtB[i] = net[hist[i]] - nxtB[i-1];
		}
		ll yco = net[hist[0]] - nxtB[h-1];
		int xco = nxtA[h-1]+1;
		ll x = yco/xco;
		for(int i=0; i<h; ++i){
			ans[hii[i]] = nxtA[i]*x + nxtB[i];
		}
		if(!validate()){
			puts("0"); return 0;
		}
		for(int i=0; i<m; ++i) printf("%lld\n", ans[i]*2);
		return 0;
	} else {
		puts("0");	
	}

	return 0;
}

Compilation message

pipes.cpp: In function 'void read(int&)':
pipes.cpp:10:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(int& x){ scanf("%d",&x); }
                    ~~~~~^~~~~~~~~
pipes.cpp: In function 'void read(ll&)':
pipes.cpp:11:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(ll& x){ scanf("%lld",&x); }
                   ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2680 KB Output is correct
2 Correct 5 ms 2836 KB Output is correct
3 Correct 7 ms 2836 KB Output is correct
4 Correct 115 ms 10728 KB Output is correct
5 Correct 5 ms 10728 KB Output is correct
6 Correct 5 ms 10728 KB Output is correct
7 Correct 5 ms 10728 KB Output is correct
8 Correct 4 ms 10728 KB Output is correct
9 Correct 5 ms 10728 KB Output is correct
10 Correct 5 ms 10728 KB Output is correct
11 Correct 6 ms 10728 KB Output is correct
12 Correct 5 ms 10728 KB Output is correct
13 Correct 86 ms 10916 KB Output is correct
14 Correct 101 ms 13280 KB Output is correct
15 Correct 130 ms 15328 KB Output is correct
16 Correct 111 ms 15732 KB Output is correct
17 Correct 105 ms 18096 KB Output is correct
18 Correct 115 ms 19916 KB Output is correct
19 Correct 93 ms 20692 KB Output is correct
20 Correct 4 ms 20692 KB Output is correct
21 Correct 5 ms 20692 KB Output is correct
22 Correct 103 ms 22968 KB Output is correct
23 Correct 94 ms 22968 KB Output is correct
24 Correct 146 ms 25988 KB Output is correct
25 Correct 146 ms 26000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26000 KB Output is correct
2 Correct 5 ms 26000 KB Output is correct
3 Correct 96 ms 28772 KB Output is correct
4 Correct 4 ms 28772 KB Output is correct
5 Correct 9 ms 28772 KB Output is correct
6 Correct 5 ms 28772 KB Output is correct
7 Correct 6 ms 28772 KB Output is correct
8 Correct 4 ms 28772 KB Output is correct
9 Correct 5 ms 28772 KB Output is correct
10 Correct 4 ms 28772 KB Output is correct
11 Correct 4 ms 28772 KB Output is correct
12 Correct 5 ms 28772 KB Output is correct
13 Correct 5 ms 28772 KB Output is correct
14 Correct 5 ms 28772 KB Output is correct
15 Correct 6 ms 28772 KB Output is correct
16 Correct 5 ms 28772 KB Output is correct
17 Correct 6 ms 28772 KB Output is correct
18 Correct 5 ms 28772 KB Output is correct
19 Correct 4 ms 28772 KB Output is correct
20 Correct 5 ms 28772 KB Output is correct
21 Correct 4 ms 28772 KB Output is correct
22 Correct 7 ms 28772 KB Output is correct
23 Correct 93 ms 30440 KB Output is correct
24 Correct 115 ms 33396 KB Output is correct
25 Correct 96 ms 33464 KB Output is correct
26 Correct 5 ms 33464 KB Output is correct
27 Correct 5 ms 33464 KB Output is correct
28 Correct 4 ms 33464 KB Output is correct
29 Correct 4 ms 33464 KB Output is correct
30 Correct 134 ms 36016 KB Output is correct
31 Correct 121 ms 38428 KB Output is correct
32 Correct 117 ms 39364 KB Output is correct
33 Correct 99 ms 39948 KB Output is correct
34 Correct 5 ms 39948 KB Output is correct
35 Correct 5 ms 39948 KB Output is correct
36 Correct 4 ms 39948 KB Output is correct
37 Correct 4 ms 39948 KB Output is correct
38 Correct 125 ms 42696 KB Output is correct
39 Correct 114 ms 44000 KB Output is correct
40 Correct 136 ms 46004 KB Output is correct
41 Correct 91 ms 46456 KB Output is correct
42 Correct 4 ms 46456 KB Output is correct
43 Correct 5 ms 46456 KB Output is correct
44 Correct 5 ms 46456 KB Output is correct
45 Correct 5 ms 46456 KB Output is correct
46 Correct 121 ms 48860 KB Output is correct
47 Correct 114 ms 50736 KB Output is correct
48 Correct 121 ms 52780 KB Output is correct
49 Correct 90 ms 52780 KB Output is correct
50 Correct 4 ms 52780 KB Output is correct
51 Correct 4 ms 52780 KB Output is correct
52 Correct 5 ms 52780 KB Output is correct
53 Correct 5 ms 52780 KB Output is correct
54 Correct 120 ms 55080 KB Output is correct