답안 #125148

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
125148 2019-07-04T18:03:18 Z eriksuenderhauf Potemkin cycle (CEOI15_indcyc) C++11
70 / 100
1000 ms 3192 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e3 + 5;
const int MAXM = 1e5 + 5;
const double eps = 1e-9;
int U[MAXM], V[MAXM], ap[MAXM];
int mrk[MAXN], dist[MAXN];
int lo[MAXN], disc[MAXN], ti = 1, par[MAXN];
vi adj[MAXN];
bool nei[MAXN][MAXN];

void dfs(int u, int p=0) {
	lo[u] = disc[u] = ti++;
	for (int i: adj[u]) {
		int v = U[i]^V[i]^u;
		if (!disc[v]) {
			dfs(v, u);
			lo[u] = min(lo[u], lo[v]);
			if (lo[v] > disc[u])
				ap[i] = 1;
		} else if (v != p)
			lo[u] = min(lo[u], disc[v]);
	}
}

void dfs2(int u, int p) {
	lo[u] = p;
	for (int i: adj[u]) {
		int v = U[i]^V[i]^u;
		if (par[v] == u)
			dfs2(v, p);
	}
}

stack<int> st;

void solve(int u, int root) {
	//cerr << "st: " << u << "\n";
	int nx = -1, d = INF;
	for (int i: adj[u]) {
		int v = U[i]^V[i]^u;
		if (mrk[v] || par[v] == u || v == par[u] || dist[v] == 0 || lo[u] == lo[v]) continue;
		//cerr << "cur: " << u << " candidate: " << v << " par: " << lo[v] << "\n";
		if (dist[v] < d)
			tie(nx, d) = mp(v, dist[v]);
	}
	st.push(u);
	if (nx != -1) {
		vi tmp;
		while (par[nx] != nx) {
			tmp.pb(nx);
			nx = par[nx];
		}
		reverse(tmp.begin(), tmp.end());
		while (!st.empty()) {
			printf("%d ", st.top());
			st.pop();
		}
		for (int i: tmp)
			printf("%d ", i);
		printf("\n");
		exit(0);
	}
	for (int i: adj[u]) {
		int v = U[i]^V[i]^u;
		if (par[v] == u)
			solve(v, root);
	}
	st.pop();
	//cerr << "en: " << u << "\n";
}

int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= m; i++) {
		scanf("%d %d", U+i, V+i);
		adj[U[i]].pb(i);
		adj[V[i]].pb(i);
		nei[U[i]][V[i]] = 1;
		nei[V[i]][U[i]] = 1;
	}
	for (int i = 1; i <= n; i++)
		if (!disc[i])
			dfs(i);
	for (int i = 1; i <= n; i++) {
		//cerr << "check root: " << i << "\n";
		for (int j: adj[i]) {
			int v2 = U[j]^V[j]^i;
			//cerr << "child: " << v2 << "\n";
			for (int k: adj[v2]) {
				int u = U[k]^V[k]^v2;
				if (nei[u][i])
					mrk[u] = 1;
			}
			mem(dist, 0);
			mem(par, 0);
			deque<int> pq; pq.pb(i);
			par[i] = i;
			while (!pq.empty()) {
				int u = pq.front(); pq.pop_front();
				//cerr << u << " " << par[u] << " -\n";
				for (int k: adj[u]) {
					int v = U[k]^V[k]^u;
					if (par[v] || mrk[v]) continue;
					par[v] = u;
					dist[v] = dist[u] + 1;
					pq.pb(v);
				}
			}

			mem(lo, 0);
			for (int j: adj[i]) {
				int v = U[j]^V[j]^i;
				if (!mrk[v])
					dfs2(v, v);
			}

			st.push(i);
			solve(v2, i);
			while (!st.empty()) st.pop();
			for (int k: adj[v2]) {
				int u = U[k]^V[k]^v2;
				if (nei[u][i])
					mrk[u] = 0;
			}
		}
		while (!st.empty())
			st.pop();
		//cerr << "\n";
	}
	printf("no\n");
    return 0;
}

Compilation message

indcyc.cpp: In function 'int main()':
indcyc.cpp:107: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:109:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", U+i, V+i);
   ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 48 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 358 ms 832 KB Output is correct
2 Correct 5 ms 760 KB Output is correct
3 Correct 19 ms 888 KB Output is correct
4 Correct 748 ms 988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 760 KB Output is correct
2 Correct 416 ms 760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 490 ms 2752 KB Output is correct
2 Correct 159 ms 2168 KB Output is correct
3 Execution timed out 1077 ms 2680 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 230 ms 2040 KB Output is correct
2 Execution timed out 1083 ms 1912 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 265 ms 3192 KB Output is correct
2 Execution timed out 1082 ms 3192 KB Time limit exceeded
3 Halted 0 ms 0 KB -