Submission #147344

# Submission time Handle Problem Language Result Execution time Memory
147344 2019-08-29T02:48:24 Z luciocf Potemkin cycle (CEOI15_indcyc) C++14
100 / 100
362 ms 98272 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e3+10;
const int maxm = 2e5+10;

typedef pair<int, int> pii;

int n, m;
int mat[maxn][maxn];

int pai[maxn], iniCycle, fimCycle;
int mark[maxm];

pii edge[maxm];

vector<pii> grafo[maxn];
vector<int> grafo2[maxm], grafo3[maxn];

void dfs(int u, int p)
{
	mark[u] = 1;

	for (auto v: grafo2[u])
	{
		if (!iniCycle && mark[v] == 1)
		{
			iniCycle = v, fimCycle = u;
			break;
		}

		if (!mark[v]) 
		{
			pai[v] = u;
			dfs(v, u);
		}
	}

	mark[u] = 2;
}

int main(void)
{
	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+m});
	}

	for (int i = 1; i <= m; i++)
	{
		int u = edge[i].first, v = edge[i].second;

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

			if (w != u && w != v && !mat[w][u])
				grafo2[i].push_back(e);
		}

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

			if (w != u && w != v && !mat[w][v])
				grafo2[i+m].push_back(e);
		}
	}

	for (int i = 1; i <= 2*m; i++)
	{
		if (!mark[i])
			dfs(i, 0);

		if (iniCycle) break;
	}

	if (!iniCycle)
	{
		printf("no\n");
		return 0;
	}

	int at = fimCycle;

	int aux = 0;

	vector<int> ciclo;

	while (true)
	{
		if (at > m)
			ciclo.push_back(edge[at-m].second);
		else
			ciclo.push_back(edge[at].first);

		if (at == iniCycle) break;

		at = pai[at];
	}

	for (auto c: ciclo)
		printf("%d ", c);
	printf("\n");
}

Compilation message

indcyc.cpp: In function 'int main()':
indcyc.cpp:96:6: warning: unused variable 'aux' [-Wunused-variable]
  int aux = 0;
      ^~~
indcyc.cpp:45: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:50:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 7 ms 5112 KB Output is correct
4 Correct 8 ms 5112 KB Output is correct
5 Correct 7 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5756 KB Output is correct
2 Correct 8 ms 5884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 7160 KB Output is correct
2 Correct 11 ms 7032 KB Output is correct
3 Correct 18 ms 8952 KB Output is correct
4 Correct 19 ms 9080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 8312 KB Output is correct
2 Correct 16 ms 8312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 143 ms 30144 KB Output is correct
2 Correct 47 ms 16824 KB Output is correct
3 Correct 161 ms 30072 KB Output is correct
4 Correct 45 ms 16760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 57976 KB Output is correct
2 Correct 191 ms 62176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 257 ms 11464 KB Output is correct
2 Correct 261 ms 11724 KB Output is correct
3 Correct 351 ms 98272 KB Output is correct
4 Correct 362 ms 98040 KB Output is correct