답안 #230454

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
230454 2020-05-10T07:18:03 Z BamiTorabi Pipes (BOI13_pipes) C++14
100 / 100
505 ms 56416 KB
//Sasayego! Sasayego! Shinzou wo Sasageyo!

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <numeric>
#include <bitset>
#include <ctime>
#define debug(x)  cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;

const int maxN = 1e5 + 5;
const int maxM = 5e5 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

bool mark[maxN];
int c[maxN];
set <int> G[maxN];
vector <int> vec;
vector <pii> E;
map <pii, int> mp;
queue <int> Q;

pii sorted(int u, int v){
	pii pp = {u, v};
	if (pp.first > pp.second)
		swap(pp.first, pp.second);
	return pp;
}

int main(){
	time_t START = clock();
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n, m; scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++){
		scanf("%d", c + i);
		c[i] *= 2;
	}
	for (int i = 0; i < m; i++){
		int u, v; scanf("%d%d", &u, &v);
		tie(u, v) = sorted(u, v);
		G[u].insert(v);
		G[v].insert(u);
		E.push_back({u, v});
	}
	if (m > n)
		return printf("0\n"), 0;
	for (int i = 1; i <= n; i++)
		if ((int)G[i].size() == 1)
			Q.push(i);
	while (!Q.empty()){
		int v = Q.front();
		Q.pop();
		if ((int)G[v].size() != 1)
			continue;
		int u = *G[v].begin();
		G[v].clear();
		mp[sorted(u, v)] = c[v];
		c[u] -= c[v];
		G[u].erase(G[u].lower_bound(v));
		if ((int)G[u].size() == 1)
			Q.push(u);
	}
	int cnt = 0;
	for (int i = 1; i <= n; i++)
		cnt += ((int)G[i].size() == 2);
	if (cnt > 0 && cnt % 2 == 0)
		return printf("0\n"), 0;
	if (cnt)
		for (int i = 1; i <= n; i++)
			if ((int)G[i].size() == 2){
				vec.push_back(i);
				int u = *(G[i].begin());
				G[i].erase(G[i].begin());
				int x = *(G[i].begin());
				G[x].erase(G[x].lower_bound(i));
				ll ans = c[i];
				while (x != i){
					vec.push_back(x);
					ans = c[x] - ans;
					int y = *(G[x].begin());
					G[x].erase(G[x].begin());
					if (y != i)	
						G[y].erase(G[y].lower_bound(x));
					x = y;
				}
				c[u] -= ans / 2;
				c[i] -= ans / 2;
				mp[sorted(i, u)] = ans / 2;
				break;
			}
	for (int i = 1; i < (int)vec.size(); i++){
		mp[sorted(vec[i - 1], vec[i])] = c[vec[i - 1]];
		c[vec[i]] -= c[vec[i - 1]];
	}
	for (pii e : E)
		printf("%d\n", mp[e]);
	time_t FINISH = clock();
	cerr << "Execution time: " << (ld)(FINISH - START) / CLOCKS_PER_SEC * 1000.0 << " milliseconds.\n";
	return 0;
}
 

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:48:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n, m; scanf("%d%d", &n, &m);
            ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", c + i);
   ~~~~~^~~~~~~~~~~~~
pipes.cpp:54:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v; scanf("%d%d", &u, &v);
             ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 8 ms 5248 KB Output is correct
4 Correct 212 ms 16508 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 8 ms 4992 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 8 ms 5248 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 8 ms 5248 KB Output is correct
13 Correct 161 ms 13992 KB Output is correct
14 Correct 190 ms 15720 KB Output is correct
15 Correct 211 ms 16488 KB Output is correct
16 Correct 168 ms 14660 KB Output is correct
17 Correct 219 ms 16620 KB Output is correct
18 Correct 202 ms 16364 KB Output is correct
19 Correct 200 ms 16488 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 8 ms 5248 KB Output is correct
22 Correct 199 ms 16340 KB Output is correct
23 Correct 146 ms 14056 KB Output is correct
24 Correct 210 ms 16620 KB Output is correct
25 Correct 169 ms 14444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 121 ms 15212 KB Output is correct
4 Correct 84 ms 15596 KB Output is correct
5 Correct 84 ms 15348 KB Output is correct
6 Correct 502 ms 56416 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 9 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 8 ms 5120 KB Output is correct
16 Correct 8 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 8 ms 5120 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 8 ms 5376 KB Output is correct
22 Correct 8 ms 5248 KB Output is correct
23 Correct 155 ms 14696 KB Output is correct
24 Correct 202 ms 16824 KB Output is correct
25 Correct 132 ms 15208 KB Output is correct
26 Correct 85 ms 15724 KB Output is correct
27 Correct 81 ms 15520 KB Output is correct
28 Correct 88 ms 16108 KB Output is correct
29 Correct 399 ms 46172 KB Output is correct
30 Correct 173 ms 16492 KB Output is correct
31 Correct 173 ms 17004 KB Output is correct
32 Correct 201 ms 16488 KB Output is correct
33 Correct 127 ms 15980 KB Output is correct
34 Correct 84 ms 15596 KB Output is correct
35 Correct 83 ms 15596 KB Output is correct
36 Correct 92 ms 15724 KB Output is correct
37 Correct 505 ms 56268 KB Output is correct
38 Correct 195 ms 16872 KB Output is correct
39 Correct 203 ms 16360 KB Output is correct
40 Correct 197 ms 16872 KB Output is correct
41 Correct 91 ms 15720 KB Output is correct
42 Correct 82 ms 15596 KB Output is correct
43 Correct 85 ms 15596 KB Output is correct
44 Correct 85 ms 15308 KB Output is correct
45 Correct 469 ms 50432 KB Output is correct
46 Correct 182 ms 16748 KB Output is correct
47 Correct 213 ms 16872 KB Output is correct
48 Correct 189 ms 17128 KB Output is correct
49 Correct 173 ms 15208 KB Output is correct
50 Correct 94 ms 15596 KB Output is correct
51 Correct 84 ms 15596 KB Output is correct
52 Correct 77 ms 15468 KB Output is correct
53 Correct 483 ms 51292 KB Output is correct
54 Correct 196 ms 16748 KB Output is correct