답안 #111620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
111620 2019-05-15T18:18:49 Z rajarshi_basu Pipes (BOI13_pipes) C++14
100 / 100
669 ms 17112 KB
#include <iostream>
#include <vector>
#include <set>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <complex>
#include <stack>
#include <set>

#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long int
#define vi vector<int>
#define ii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pll pair<ll,ll>
#define cd complex<double> 

const int INF = 1e9;
using namespace std;
const int MAXN = 100*1000 +5;

int nodeVal[MAXN];
int edgesVal[MAXN];
bool marked[MAXN];
vector<ii> g[MAXN];

stack<ii> st;
bool vis[MAXN];
vector<ii> cycle;
bool done = 0;
void markCycle(int node,int p,int pe){
	st.push({node,pe});
	
	if(vis[node]){
		if(done)return;
		done = 1;
		bool got = 0;
	
		while(!st.empty()){

			auto e = st.top();st.pop();
			//marked[e.ff] = 1;
			if(e.ff == node){
				if(got)break;
				got = 1;
				
			}
			cycle.pb(e);
		}
	}else{
		vis[node] = 1;
		for(auto e:g[node]){
			if(e.ff != p){
				markCycle(e.ff,node,e.ss);
			}
		}
	}
	if(!st.empty())st.pop();
}

void dfs(int node,int p,int pe){
	for(auto e:g[node]){
		if(e.ff != p and !marked[e.ff]){
			dfs(e.ff,node,e.ss);
			nodeVal[node] -= nodeVal[e.ff];
		}
	}
	if(pe > -1){
		edgesVal[pe] = nodeVal[node];
	}
}

int main(){
	int n,m;
	cin >> n >> m;
	FOR(i,n)cin >> nodeVal[i];
	FOR(i,m){
		int a,b;
		cin >> a >> b;
		a--;b--;
		g[a].pb({b,i});
		g[b].pb({a,i});
	}
	if(m > n){
		cout << "0" << endl;
	}else if(m < n){
		
		dfs(0,-1,-1);
		FOR(i,m){
			cout << 2*edgesVal[i] << endl;
		}
	}else{
		markCycle(0,-1,-1);
		for(auto e:cycle){
		//	cout << e.ff << " ";
			marked[e.ff] = 1;
		}
		//cout << endl;

		int cnt = 0;
		FOR(i,n){
			if(marked[i]){
				dfs(i,-1,-1);
				cnt++;
			}
		}
		if(cnt%2 == 0){
			cout << 0 << endl;
		}else{
			ll sum = 0;int x = 1;
			for(auto e:cycle){
				sum += nodeVal[e.ff]*x;
				x *= -1;
			}
			edgesVal[cycle.back().ss] = sum/2;
			sum /= 2;
			nodeVal[cycle.back().ff] -= sum;
			nodeVal[cycle.front().ff] -= sum;
			FORE(i,0,(int)cycle.size()-2){
				edgesVal[cycle[i].ss] = nodeVal[cycle[i].ff];
				nodeVal[cycle[i+1].ff] -= edgesVal[cycle[i].ss];
			}
			FOR(i,m){
				cout << 2*edgesVal[i] << endl;
			}
		}
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 8 ms 2688 KB Output is correct
4 Correct 262 ms 7672 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
7 Correct 3 ms 2688 KB Output is correct
8 Correct 4 ms 2688 KB Output is correct
9 Correct 7 ms 2688 KB Output is correct
10 Correct 6 ms 2816 KB Output is correct
11 Correct 6 ms 2816 KB Output is correct
12 Correct 6 ms 2816 KB Output is correct
13 Correct 263 ms 6704 KB Output is correct
14 Correct 254 ms 7328 KB Output is correct
15 Correct 260 ms 7676 KB Output is correct
16 Correct 225 ms 7016 KB Output is correct
17 Correct 250 ms 7544 KB Output is correct
18 Correct 266 ms 7756 KB Output is correct
19 Correct 277 ms 11000 KB Output is correct
20 Correct 5 ms 2688 KB Output is correct
21 Correct 6 ms 2816 KB Output is correct
22 Correct 279 ms 7676 KB Output is correct
23 Correct 239 ms 6648 KB Output is correct
24 Correct 281 ms 7688 KB Output is correct
25 Correct 227 ms 6876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2688 KB Output is correct
2 Correct 7 ms 2816 KB Output is correct
3 Correct 149 ms 11168 KB Output is correct
4 Correct 134 ms 6400 KB Output is correct
5 Correct 126 ms 6648 KB Output is correct
6 Correct 538 ms 17064 KB Output is correct
7 Correct 5 ms 2688 KB Output is correct
8 Correct 4 ms 2688 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 4 ms 2688 KB Output is correct
11 Correct 4 ms 2688 KB Output is correct
12 Correct 4 ms 2688 KB Output is correct
13 Correct 5 ms 2816 KB Output is correct
14 Correct 4 ms 2688 KB Output is correct
15 Correct 7 ms 2816 KB Output is correct
16 Correct 6 ms 2760 KB Output is correct
17 Correct 5 ms 2816 KB Output is correct
18 Correct 4 ms 2688 KB Output is correct
19 Correct 4 ms 2688 KB Output is correct
20 Correct 4 ms 2688 KB Output is correct
21 Correct 5 ms 2816 KB Output is correct
22 Correct 6 ms 2944 KB Output is correct
23 Correct 218 ms 11696 KB Output is correct
24 Correct 273 ms 12552 KB Output is correct
25 Correct 171 ms 11156 KB Output is correct
26 Correct 147 ms 6500 KB Output is correct
27 Correct 136 ms 6296 KB Output is correct
28 Correct 158 ms 6860 KB Output is correct
29 Correct 539 ms 14468 KB Output is correct
30 Correct 316 ms 15016 KB Output is correct
31 Correct 298 ms 15524 KB Output is correct
32 Correct 282 ms 9460 KB Output is correct
33 Correct 161 ms 12496 KB Output is correct
34 Correct 131 ms 6436 KB Output is correct
35 Correct 120 ms 6392 KB Output is correct
36 Correct 117 ms 6648 KB Output is correct
37 Correct 669 ms 17112 KB Output is correct
38 Correct 304 ms 13940 KB Output is correct
39 Correct 283 ms 8888 KB Output is correct
40 Correct 328 ms 12108 KB Output is correct
41 Correct 161 ms 15120 KB Output is correct
42 Correct 123 ms 6208 KB Output is correct
43 Correct 126 ms 6240 KB Output is correct
44 Correct 113 ms 6648 KB Output is correct
45 Correct 383 ms 15072 KB Output is correct
46 Correct 318 ms 15752 KB Output is correct
47 Correct 340 ms 12224 KB Output is correct
48 Correct 348 ms 15288 KB Output is correct
49 Correct 145 ms 8728 KB Output is correct
50 Correct 132 ms 6392 KB Output is correct
51 Correct 133 ms 6520 KB Output is correct
52 Correct 131 ms 6232 KB Output is correct
53 Correct 496 ms 15100 KB Output is correct
54 Correct 302 ms 13516 KB Output is correct