답안 #130433

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
130433 2019-07-15T08:00:43 Z tjd229 Zagonetka (COI18_zagonetka) C++14
100 / 100
120 ms 520 KB
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
int s[101], l[101];
int p[101];
int mat[101][101];
int pos[101];
int n;
int sign,d;
int tp_sort(queue<int> &q, int s, int *vt, int *ind, vector<int> edge[101], int debug = 0) {
	int vis = 0;
	//int sign = q.top() < 0 ? -1 : 1;
	while (q.size()) {
		int u = q.front(); q.pop();
		if (u < 0) u = -u;
		vt[u] = s++;
		++vis;
		if (debug) printf("v:%d\n", u);
		for (auto v : edge[u]) {
			if (--ind[v] == 0)
				q.push(v);
		}
	}
	return vis;
}

int dfs(int x, int dst, vector<int> edge[101]) {
	if (dst == x) return 1;
	for (auto nxt : edge[x]) {
		if (dfs(nxt, dst, edge)) return 1;
	}
	return 0;
}
void perm(int x,int &num,int *vt) {
	if (vt[x]) return;
	for (int i = 1; i <= n; ++i) {
		if (mat[x][i]) perm(i, num, vt);
	}
	vt[x] = num++;
}
void qry(int x, int y) {//chk pos[x]->pos[y]
	//inv edge ok -> no condition
	//inv edge no -> there is condition 
	//cycle -> there is cond.(inclusive indirect)

	vector<int> edge[101];
	vector<int> loc;
	int ind[101] = { 0 };
	int myp[101] = { 0 };
	for (int i = 1; i <= n; ++i) myp[i] = p[i];
	for (int i = x; i <= y; ++i) loc.push_back(pos[i]);
	for (int i = 0; i < loc.size(); ++i) {
		for (int j = 0; j < loc.size(); ++j) {
			if (mat[loc[i]][loc[j]]) {
				edge[loc[i]].push_back(loc[j]);
				++ind[loc[j]];
			}
		}
	}
	//dfs chk
	if (dfs(pos[x], pos[y], edge)) {
		//printf("%d(%d)->%d(%d) ok\n",pos[x],x,pos[y],y);
		return;
	}

	edge[pos[y]].push_back(pos[x]);//inv edge
	++ind[pos[x]];
	queue<int> q;
	for (auto v : loc) {
		if (ind[v] == 0) q.push(v);
	}
	if (y - x + 1 == tp_sort(q, x, myp, ind, edge)) {
		int i;
		i = 1;
		for (printf("query"), i = 1; i <= n; ++i) printf(" %d",myp[i]);
		printf("\n"); fflush(stdout);
		scanf("%d",&i);
		if (i) return;
	}

	//cycle chk
	//for (int i = 1; i <= n; ++i) printf("%d ", myp[i]);
	//printf("corr?,%d->%d\n", pos[x], pos[y]);
	mat[pos[x]][pos[y]] = 1;
}
int main() {
	int i;
	for (scanf("%d", &n), i = 1; i <= n; ++i) {
		scanf("%d", p + i);
		pos[p[i]] = i;
	}
	for (int range = 1; range < n; ++range) {
		for (i = 1; i + range <= n; ++i) qry(i, i + range);
	}

	for (i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			for (int k = 1; k <= n; ++k)
				mat[j][k] |= mat[j][i] & mat[i][k];
		}
	}
	int src = 1;
	for (i = 1; i <= n; ++i) perm(i, src, l);
	for (i = 1; i <= n; ++i) {
		for (int j = i + 1; j <= n; ++j) {
			mat[i][j] ^= mat[j][i] ^= mat[i][j] ^= mat[j][i];
		}
	}
	src = 1;
	for (i = 1; i <= n; ++i) perm(i, src, s);
	for (i = 1; i <= n; ++i) l[i] = n - l[i] + 1;

	for (printf("end\n"), i = 1; i <= n; ++i) printf("%d ", s[i]);
	for (printf("\n"), i = 1; i <= n; ++i) printf("%d ", l[i]);
	fflush(stdout);

	
	return 0;
}

Compilation message

zagonetka.cpp: In function 'void qry(int, int)':
zagonetka.cpp:53:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < loc.size(); ++i) {
                  ~~^~~~~~~~~~~~
zagonetka.cpp:54:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < loc.size(); ++j) {
                   ~~^~~~~~~~~~~~
zagonetka.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&i);
   ~~~~~^~~~~~~~~
zagonetka.cpp: In function 'int main()':
zagonetka.cpp:89:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (scanf("%d", &n), i = 1; i <= n; ++i) {
       ~~~~~~~~~~~~~~~^~~~~~~
zagonetka.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", p + i);
   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 248 KB Output is correct
3 Correct 2 ms 248 KB Output is correct
4 Correct 2 ms 248 KB Output is correct
5 Correct 2 ms 308 KB Output is correct
6 Correct 2 ms 248 KB Output is correct
7 Correct 2 ms 248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 520 KB Output is correct
2 Correct 26 ms 308 KB Output is correct
3 Correct 51 ms 376 KB Output is correct
4 Correct 41 ms 312 KB Output is correct
5 Correct 14 ms 248 KB Output is correct
6 Correct 44 ms 440 KB Output is correct
7 Correct 10 ms 424 KB Output is correct
8 Correct 10 ms 376 KB Output is correct
9 Correct 42 ms 424 KB Output is correct
10 Correct 21 ms 248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 9 ms 376 KB Output is correct
4 Correct 9 ms 248 KB Output is correct
5 Correct 5 ms 248 KB Output is correct
6 Correct 5 ms 396 KB Output is correct
7 Correct 7 ms 376 KB Output is correct
8 Correct 9 ms 248 KB Output is correct
9 Correct 8 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 9 ms 376 KB Output is correct
12 Correct 9 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 448 KB Output is correct
2 Correct 99 ms 376 KB Output is correct
3 Correct 101 ms 376 KB Output is correct
4 Correct 28 ms 252 KB Output is correct
5 Correct 38 ms 380 KB Output is correct
6 Correct 38 ms 380 KB Output is correct
7 Correct 56 ms 376 KB Output is correct
8 Correct 77 ms 380 KB Output is correct
9 Correct 73 ms 424 KB Output is correct
10 Correct 120 ms 424 KB Output is correct
11 Correct 101 ms 376 KB Output is correct
12 Correct 91 ms 248 KB Output is correct
13 Correct 109 ms 440 KB Output is correct
14 Correct 105 ms 460 KB Output is correct
15 Correct 119 ms 468 KB Output is correct
16 Correct 92 ms 388 KB Output is correct