답안 #147280

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
147280 2019-08-28T17:41:02 Z luciocf Potemkin cycle (CEOI15_indcyc) C++14
50 / 100
1000 ms 102632 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

const int maxn = 1e3+10;
const int maxm = 1e5+10;
const int inf = 1e9+10;

int mat[maxn][maxn];
int dist[maxn], pai[maxn][maxn];

pii edge[maxm];
int minimo[maxm], pivot[maxm];

bool nope[maxm][maxn];

bool mark[maxn];

vector<pii> grafo[maxn];

void bfs(int x)
{
	memset(dist, -1, sizeof dist);

	queue<int> fila;

	dist[x] = 0, pai[x][x] = 0, fila.push(x);

	while (!fila.empty())
	{
		int u = fila.front(); fila.pop();

		for (auto pp: grafo[u])
		{
			int v = pp.first, e = pp.second;

			if (dist[v] == -1)
			{
				dist[v] = dist[u]+1, pai[x][v] = u;
				fila.push(v);
			}
			else if (v != pai[x][u] && !nope[e][pai[x][u]] && !nope[e][pai[x][v]])
			{
				if (dist[u]+dist[v]+1 < minimo[e])
				{
					minimo[e] = dist[u]+dist[v]+1;
					pivot[e] = x;
				}
			}
		}
	}
}

int main(void)
{
	int n, m;
	scanf("%d %d", &n, &m);

	for (int i = 1; i <= m; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);

		mat[u][v] = mat[v][u] = 1;
		edge[i] = {u, v};

		grafo[u].push_back({v, i});
		grafo[v].push_back({u, i});

		minimo[i] = inf;
	}

	for (int u = 1; u <= n; u++)
	{
		for (auto pp: grafo[u])
		{
			int v = pp.first, e = pp.second;

			for (int w = 1; w <= n; w++)
				if (w != u && w != v && mat[w][u] && mat[w][v])
					nope[e][w] = 1;
		}
	}

	for (int i = 1; i <= n; i++)
		bfs(i);

	for (int i = 1; i <= m; i++)
	{
		if (minimo[i] != inf)
		{
			memset(mark, 0, sizeof mark);

			int u = edge[i].first, v = edge[i].second;

			vector<int> ans, aux;

			bool ok = 1;
			mark[pivot[i]] = 1;

			while (u != pivot[i])
			{
				if (mark[u]) ok = 0;
				mark[u] = 1;

				ans.push_back(u);
				u = pai[pivot[i]][u];
			}

			ans.push_back(pivot[i]);

			while (v != pivot[i])
			{	
				if (mark[v]) ok = 0;
				mark[v] = 1;

				aux.push_back(v);
				v = pai[pivot[i]][v];
			}

			if (!ok)
				continue;

			reverse(aux.begin(), aux.end());

			for (auto x: aux)
				ans.push_back(x);

			for (auto x: ans)
				printf("%d ", x);
			printf("\n");

			return 0;
		}
	}

	printf("no\n");
}

Compilation message

indcyc.cpp: In function 'int main()':
indcyc.cpp:59:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
indcyc.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 476 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 2168 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 5752 KB Output is correct
2 Correct 26 ms 5752 KB Output is correct
3 Correct 43 ms 8696 KB Output is correct
4 Correct 44 ms 8612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 29 ms 5856 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1070 ms 55848 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 708 ms 28716 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1060 ms 102632 KB Time limit exceeded
2 Halted 0 ms 0 KB -