Submission #310901

# Submission time Handle Problem Language Result Execution time Memory
310901 2020-10-08T11:53:03 Z HynDuf Potemkin cycle (CEOI15_indcyc) C++11
90 / 100
352 ms 97664 KB
// CEOI 2015 - Potemkin Cycle
// Lúcio Cardoso

// Solution:

// 1. For each edge (u, v) in the graph, create two nodes in a new graph: One node representes
// the edge u->v and the other one, v->u. Now, for each triple (u, v, w) of vertices in the original graph such that
// u is connected to v, and v is connected to w, connect vertices (u->v) with (v->w) iff u is not connected with w.
// This way, a cycle in the new graph is a cycle in the original graph and there are no 3-size cycles.

// 2. Thus, if there is a cycle in the new graph, there is an answer. To find such an answer, run a dfs in the new graph
// and print the cycle formed by the first back edge found. This way, we can assure that the conditions of the statement
// are satisfied.

// Complexity: O(nm)

#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 (!mark[v])
		{
			pai[v] = u;
			dfs(v, u);
		}
	}
	for (auto v: grafo2[u])
	{
		if (!iniCycle && mark[v] == 1)
		{
			iniCycle = v, fimCycle = u;
			break;
		}
    }

	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:114:6: warning: unused variable 'aux' [-Wunused-variable]
  114 |  int aux = 0;
      |      ^~~
indcyc.cpp:63:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   63 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
indcyc.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   68 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5248 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 5760 KB Repeated vertex
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5888 KB Output is correct
2 Correct 7 ms 5888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7040 KB Output is correct
2 Correct 8 ms 7040 KB Output is correct
3 Correct 14 ms 8960 KB Output is correct
4 Correct 16 ms 8960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8192 KB Output is correct
2 Correct 12 ms 8320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 139 ms 29560 KB Output is correct
2 Correct 42 ms 16632 KB Output is correct
3 Correct 151 ms 29564 KB Output is correct
4 Correct 42 ms 16632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 57848 KB Output is correct
2 Correct 183 ms 61944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 10744 KB Output is correct
2 Correct 257 ms 10976 KB Output is correct
3 Correct 325 ms 97664 KB Output is correct
4 Correct 352 ms 97656 KB Output is correct