답안 #30061

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
30061 2017-07-21T23:11:49 Z cdemirer 007 (CEOI14_007) C++14
23 / 100
1000 ms 27752 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> llp;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)

int N, M;
int Z, D, SV1, SV2;
vvi edges;
int Ddists[200000], Zdists[200000];
int Dparents[200000], Zparents[200000];

void dijsktra(int *dists, int s) {
	set<ii> S;
	for (int i = 0; i < N; i++) {
		if (i == s) {
			dists[i] = 0;
			S.insert(mp(dists[i], i));
			continue;
		}
		dists[i] = (int)1e9;
		S.insert(mp(dists[i], i));
	}
	while ( ! S.empty() ) {
		ii x = *S.begin();
		S.erase(S.begin());
		for (int i = 0; i < edges[x.second].size(); i++) {
			if (dists[x.second] + 1 < dists[edges[x.second][i]]) {
				S.erase(mp(dists[edges[x.second][i]], edges[x.second][i]));
				dists[edges[x.second][i]] = dists[x.second] + 1;
				if (s == D) Dparents[edges[x.second][i]] = x.second;
				else Zparents[edges[x.second][i]] = x.second;
				S.insert(mp(dists[edges[x.second][i]], edges[x.second][i]));
			}
		}
	}
}

bool flagD[200000] = {0};
bool flagZ[200000] = {0};

int main(int argc, char **argv) {

	//freopen("input", "r", stdin);
	//freopen("output", "w", stdout);

	scanf("%d%d", &N, &M);
	scanf("%d%d%d%d", &Z, &D, &SV1, &SV2);
	Z--; D--; SV1--; SV2--;
	edges.resize(N);
	for (int i = 0; i < M; i++) {
		int a, b; scanf("%d%d", &a, &b);
		a--, b--;
		edges[a].pb(b);
		edges[b].pb(a);
	}
	Dparents[D] = -1;
	dijsktra(Ddists, D);
	Zparents[Z] = -1;
	dijsktra(Zdists, Z);

	int ans = -1;
	if (Ddists[SV1] == Ddists[SV2]) {
		int svd = min(Ddists[SV1]-Zdists[SV1], Ddists[SV2]-Zdists[SV2]);
		ans = max(ans, svd);
		if (Zdists[SV1] == Zdists[SV2]) {
			int fakeD = 0, fakeZ = 0;
			int x = SV1;
			while (x != -1) {
				flagD[x] = true;
				x = Dparents[x];
			}
			x = SV2;
			while (x != -1) {
				if (!flagD[x]) fakeD++;
				else break;
				x = Dparents[x];
			}
			x = SV1;
			while (x != -1) {
				flagZ[x] = true;
				x = Zparents[x];
			}
			x = SV2;
			while (x != -1) {
				if (!flagZ[x]) fakeZ++;
				else break;
				x = Dparents[x];
			}
			if (fakeD < fakeZ) {
				ans--;
			}
		}
	}
	else {
		if (Ddists[SV1] < Ddists[SV2]) {
			ans = max(ans, Ddists[SV1] - Zdists[SV1]);
		}
		else {
			ans = max(ans, Ddists[SV2] - Zdists[SV2]);
		}
	}
	printf("%d\n", ans);

	return 0;
}

Compilation message

007.cpp: In function 'void dijsktra(int*, int)':
007.cpp:33:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < edges[x.second].size(); i++) {
                     ^
007.cpp: In function 'int main(int, char**)':
007.cpp:53:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
                       ^
007.cpp:54:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d", &Z, &D, &SV1, &SV2);
                                       ^
007.cpp:58:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b; scanf("%d%d", &a, &b);
                                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 5540 KB Output is correct
2 Correct 0 ms 5540 KB Output is correct
3 Partially correct 0 ms 5540 KB Partially correct
4 Correct 0 ms 5540 KB Output is correct
5 Correct 0 ms 5540 KB Output is correct
6 Correct 0 ms 5540 KB Output is correct
7 Partially correct 0 ms 5540 KB Partially correct
8 Correct 0 ms 5540 KB Output is correct
9 Partially correct 0 ms 5540 KB Partially correct
10 Correct 0 ms 5540 KB Output is correct
11 Correct 0 ms 5540 KB Output is correct
12 Correct 0 ms 5540 KB Output is correct
13 Partially correct 0 ms 5672 KB Partially correct
14 Correct 0 ms 5540 KB Output is correct
15 Partially correct 0 ms 5540 KB Partially correct
16 Correct 0 ms 5540 KB Output is correct
17 Correct 0 ms 5672 KB Output is correct
18 Correct 0 ms 5672 KB Output is correct
19 Partially correct 0 ms 5672 KB Partially correct
20 Partially correct 0 ms 5672 KB Partially correct
21 Correct 0 ms 5672 KB Output is correct
22 Partially correct 0 ms 5672 KB Partially correct
23 Correct 0 ms 5672 KB Output is correct
24 Correct 0 ms 5672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 103 ms 9664 KB Partially correct
2 Correct 166 ms 11552 KB Output is correct
3 Partially correct 123 ms 9980 KB Partially correct
4 Correct 196 ms 11736 KB Output is correct
5 Correct 109 ms 9484 KB Output is correct
6 Correct 116 ms 10156 KB Output is correct
7 Partially correct 139 ms 10520 KB Partially correct
8 Partially correct 149 ms 10520 KB Partially correct
9 Correct 139 ms 11072 KB Output is correct
10 Partially correct 313 ms 15952 KB Partially correct
11 Correct 313 ms 14792 KB Output is correct
12 Partially correct 449 ms 17032 KB Partially correct
13 Correct 329 ms 15480 KB Output is correct
14 Partially correct 216 ms 13940 KB Partially correct
15 Partially correct 399 ms 17364 KB Partially correct
16 Correct 473 ms 17908 KB Output is correct
17 Partially correct 333 ms 16680 KB Partially correct
18 Correct 416 ms 16680 KB Output is correct
19 Partially correct 439 ms 17912 KB Partially correct
20 Correct 579 ms 20908 KB Output is correct
21 Correct 573 ms 21852 KB Output is correct
22 Partially correct 566 ms 19616 KB Partially correct
23 Correct 569 ms 21660 KB Output is correct
24 Partially correct 599 ms 21496 KB Partially correct
25 Correct 703 ms 20636 KB Output is correct
26 Correct 569 ms 19800 KB Output is correct
27 Partially correct 683 ms 21836 KB Partially correct
28 Partially correct 763 ms 21840 KB Partially correct
29 Partially correct 633 ms 21088 KB Partially correct
30 Correct 663 ms 22584 KB Output is correct
31 Correct 769 ms 24256 KB Output is correct
32 Partially correct 739 ms 21652 KB Partially correct
33 Correct 663 ms 22196 KB Output is correct
34 Correct 733 ms 23024 KB Output is correct
35 Correct 673 ms 22360 KB Output is correct
36 Correct 646 ms 23036 KB Output is correct
37 Correct 819 ms 25284 KB Output is correct
38 Partially correct 739 ms 24916 KB Partially correct
39 Partially correct 626 ms 24920 KB Partially correct
40 Correct 803 ms 25892 KB Output is correct
41 Execution timed out 1000 ms 27752 KB Execution timed out