Submission #121562

# Submission time Handle Problem Language Result Execution time Memory
121562 2019-06-26T19:00:18 Z Adhyyan1252 Pipes (BOI13_pipes) C++11
100 / 100
151 ms 20588 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long

struct edge{
	int u, v;
	int val = LONG_LONG_MAX;
};

vector<edge> e;
vector<vector<int> > g;
vector<int> s;
vector<int> st, cycle;
vector<int> cf;
vector<int> c;
int tym = 1;
bool dfs(int cur, int par){
	s[cur] = tym;
	tym++;
	st.push_back(cur);
	for(int u: g[cur]){
		int next = e[u].u + e[u].v - cur;
		if(s[next] == 0){ //child
			if(dfs(next, cur)) return true;
		}else if(next != par){ //back edge
			while(st.back() != next){
				cycle.push_back(st.back());
				st.pop_back();
			}
			cycle.push_back(st.back());
			st.pop_back();
			return true;
		}
	}
	st.pop_back();
	return false;
}

int find(int cur, int parid){
	int sum = 0;
	for(int eid: g[cur]){
		int next = e[eid].u + e[eid].v - cur;
		if(cf[next]) continue;
		if(eid == parid) continue;
		sum += find(next, eid);
	}
	if(parid == -1) return sum;
	e[parid].val = c[cur] - sum;
	return e[parid].val;
}

signed main(){
	ios::sync_with_stdio(false); cin.tie(0);
	int n, m; cin>>n>>m;
	if(m > n){
		cout<<0<<endl;
		return 0;
	}
	c.resize(n);
	for(int i = 0; i < n; i++){
		cin>>c[i]; 
	}
	g.resize(n);
	e.resize(m);
	for(int i = 0; i < m; i++){
		int u, v; cin>>u>>v; u--, v--;
		g[u].push_back(i);
		g[v].push_back(i);
		e[i].u = u;
		e[i].v = v;
	}
	
	s.resize(n);
	cf.resize(n);
	dfs(0, -1);
	//cout<<"REACHED "<<cycle.size()<<endl;
	for(int u: cycle){
		cf[u] = true;
		//cout<<"C: "<<u<<endl;
	}
	if(cycle.size() == 0){
		//its a tree, just dfs from anywhere
		find(0, -1);
	}else if(cycle.size()%2 == 0){
		cout<<0<<endl;
		return 0;
	}else{
		vector<int> off(n);
		int sum = 0;
		int cnt = 1;
		for(int u: cycle){
			off[u] = find(u, -1);
			//cout<<"OFF "<<u<<" "<<off[u]<<" "<<(c[u]-off[u])*cnt<<" "<<cnt<<endl;
			sum += (c[u] - off[u])*(cnt);
			cnt *= -1;
		}
		sum /= 2;
		//cout<<"SUM "<<sum<<endl;
		int id = -1, prev = sum;
		//sum is the value of edge between u and u+1
		for(int i = 0; i < cycle.size(); i++){
			id = -1;
			int u = cycle[i];
			for(int eid: g[u]){
				int next = e[eid].u + e[eid].v - u;
				if(next == cycle[(i+1)%cycle.size()]){
					//found the egde
					id = eid;
					break;
				}
			}
			assert(id != -1);
			e[id].val = c[u] - off[u] - prev;
			prev = e[id].val;
		}
	}
	for(int i = 0; i < e.size(); i++){
		assert(e[i].val != LONG_LONG_MAX);
		cout<<e[i].val*2<<"\n";
	}
	cout.flush();
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:103:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < cycle.size(); i++){
                  ~~^~~~~~~~~~~~~~
pipes.cpp:119:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < e.size(); i++){
                 ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 129 ms 13048 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 388 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 83 ms 10588 KB Output is correct
14 Correct 106 ms 12408 KB Output is correct
15 Correct 110 ms 13176 KB Output is correct
16 Correct 96 ms 11280 KB Output is correct
17 Correct 115 ms 13148 KB Output is correct
18 Correct 112 ms 13176 KB Output is correct
19 Correct 151 ms 16244 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 114 ms 13176 KB Output is correct
23 Correct 78 ms 10536 KB Output is correct
24 Correct 114 ms 13216 KB Output is correct
25 Correct 86 ms 11000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 90 ms 15660 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 3 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 88 ms 14960 KB Output is correct
24 Correct 120 ms 17396 KB Output is correct
25 Correct 90 ms 15720 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 138 ms 19768 KB Output is correct
31 Correct 123 ms 19912 KB Output is correct
32 Correct 97 ms 14836 KB Output is correct
33 Correct 91 ms 17032 KB Output is correct
34 Correct 2 ms 384 KB Output is correct
35 Correct 2 ms 384 KB Output is correct
36 Correct 2 ms 384 KB Output is correct
37 Correct 2 ms 384 KB Output is correct
38 Correct 119 ms 16752 KB Output is correct
39 Correct 105 ms 14328 KB Output is correct
40 Correct 117 ms 17096 KB Output is correct
41 Correct 105 ms 19824 KB Output is correct
42 Correct 2 ms 384 KB Output is correct
43 Correct 2 ms 384 KB Output is correct
44 Correct 2 ms 384 KB Output is correct
45 Correct 2 ms 384 KB Output is correct
46 Correct 149 ms 20588 KB Output is correct
47 Correct 113 ms 17268 KB Output is correct
48 Correct 137 ms 19764 KB Output is correct
49 Correct 73 ms 13192 KB Output is correct
50 Correct 2 ms 384 KB Output is correct
51 Correct 2 ms 384 KB Output is correct
52 Correct 2 ms 384 KB Output is correct
53 Correct 2 ms 384 KB Output is correct
54 Correct 138 ms 18536 KB Output is correct