답안 #29085

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
29085 2017-07-18T08:20:02 Z 윤교준(#1173) Railway Trip (JOI17_railway_trip) C++11
20 / 100
2000 ms 16656 KB
#include <bits/stdc++.h>
#define pb push_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define revv(V) reverse(allv(V))
#define clv(V) (V).clear()
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
#define MAXN (100005)
#define MAXQ (100005)
#define MAXK (100005)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
void fg(vector<int> G[], int a, int b) { G[a].pb(b); G[b].pb(a); }

vector<int> G[MAXN];
vector<int> V[MAXK];
int d[MAXN];
int A[MAXN];
int S[MAXQ], E[MAXQ];
int N, K, Q;

int main() {
	scanf("%d%d%d", &N, &K, &Q);
	for(int i = 1; i <= N; i++) scanf("%d", &A[i]);
	for(int i = 0; i < Q; i++) scanf("%d%d", &S[i], &E[i]);
	for(int i = 1; i <= N; i++) V[A[i]].pb(i);
	{
		set<int> PQ;
		for(int i = 1; i <= N; i++) PQ.insert(i);
		for(int l = 1; l <= K; l++) {
			for(int v : V[l]) {
				auto it = PQ.find(v);
				if(PQ.begin() != it) {
					auto sit = it; sit--;
					fg(G, v, *sit);
				}
				it++; if(PQ.end() != it) fg(G, v, *it);
			}
			for(int v : V[l]) PQ.erase(v);
		}
	}
	for(int i = 0; i < Q; i++) {
		fill(d, d+N+1, 0); d[S[i]] = 1;
		queue<pii> PQ; PQ.push({S[i], 1});
		for(; !PQ.empty(); PQ.pop()) {
			int idx, dst; tie(idx, dst) = PQ.front();
			for(int v : G[idx]) {
				if(d[v]) continue;
				d[v] = dst+1; PQ.push({v, dst+1});
			}
		}
		printf("%d\n", d[E[i]] - 2);
	}
	return 0;
}

Compilation message

railway_trip.cpp: In function 'int main()':
railway_trip.cpp:31:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &N, &K, &Q);
                             ^
railway_trip.cpp:32:48: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 1; i <= N; i++) scanf("%d", &A[i]);
                                                ^
railway_trip.cpp:33:56: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 0; i < Q; i++) scanf("%d%d", &S[i], &E[i]);
                                                        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 8276 KB Output is correct
2 Correct 0 ms 8276 KB Output is correct
3 Correct 0 ms 8276 KB Output is correct
4 Correct 0 ms 8276 KB Output is correct
5 Correct 0 ms 8276 KB Output is correct
6 Correct 0 ms 8276 KB Output is correct
7 Correct 0 ms 8276 KB Output is correct
8 Correct 0 ms 8276 KB Output is correct
9 Correct 0 ms 8276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8408 KB Output is correct
2 Correct 393 ms 15160 KB Output is correct
3 Correct 303 ms 15064 KB Output is correct
4 Correct 439 ms 14752 KB Output is correct
5 Correct 516 ms 14748 KB Output is correct
6 Correct 413 ms 14876 KB Output is correct
7 Correct 479 ms 16464 KB Output is correct
8 Correct 139 ms 16656 KB Output is correct
9 Correct 159 ms 14084 KB Output is correct
10 Correct 146 ms 15128 KB Output is correct
11 Correct 356 ms 15140 KB Output is correct
12 Correct 313 ms 14744 KB Output is correct
13 Correct 423 ms 14084 KB Output is correct
14 Correct 459 ms 15144 KB Output is correct
15 Correct 443 ms 14744 KB Output is correct
16 Correct 476 ms 14088 KB Output is correct
17 Correct 409 ms 14288 KB Output is correct
18 Correct 416 ms 14344 KB Output is correct
19 Correct 309 ms 15936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2000 ms 14940 KB Execution timed out
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2000 ms 16332 KB Execution timed out
2 Halted 0 ms 0 KB -